You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
15 KiB
C#

2 years ago
using Furion;
using Furion.DataEncryption;
2 years ago
using Furion.RemoteRequest.Extensions;
2 years ago
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
2 years ago
using System.Net.Http;
2 years ago
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Core.MQ
{
/// <summary>
/// 接收公司、员工变动消息推送
/// </summary>
public class RecCompanyUserChangeService : BackgroundService
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly IServiceScope _serviceScope;
2 years ago
private readonly ISysUserService _serviceUser;
private readonly ILogger<RecCompanyUserChangeService> _logger;
private readonly ISysTenantService _tenantService;
private readonly ISysUserService _userService;
private readonly SqlSugarRepository<SysUser> _sysUserRep;
private readonly SqlSugarRepository<SysTenant> _sysTenantRep;
2 years ago
private readonly SqlSugarRepository<SysOrg> _sysOrgRep;
private readonly SqlSugarRepository<SysPos> _sysPosRep;
private readonly SqlSugarRepository<SysRole> _sysRoleRep;
private readonly SqlSugarRepository<SysMenu> _sysMenuRep;
private readonly ISysRoleMenuService _sysRoleMenuService;
2 years ago
private readonly IAuthService _IAuthService;
private IConnection mqConn;
private IModel model;
2 years ago
public RecCompanyUserChangeService(IServiceScopeFactory scopeFactory, ISysUserService serviceUser, ISysRoleMenuService sysRoleMenuService,IAuthService IAuthService)
{
_scopeFactory = scopeFactory;
//通过这个注入DBContext
_serviceScope = _scopeFactory.CreateScope();
2 years ago
_serviceUser = serviceUser;
2 years ago
_IAuthService = IAuthService;
2 years ago
_sysRoleMenuService = sysRoleMenuService;
_logger = _serviceScope.ServiceProvider.GetService<ILogger<RecCompanyUserChangeService>>();
_tenantService = _serviceScope.ServiceProvider.GetService<ISysTenantService>();
_userService = _serviceScope.ServiceProvider.GetService<ISysUserService>();
_sysUserRep = _serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysUser>>();
_sysTenantRep = _serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysTenant>>();
2 years ago
_sysOrgRep = _serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysOrg>>();
_sysPosRep = _serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysPos>>();
_sysRoleRep = _serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysRole>>();
_sysMenuRep = _serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysMenu>>();
}
public override void Dispose()
{
base.Dispose();
_serviceScope.Dispose();
if (mqConn != null && mqConn.IsOpen)
mqConn.Close();
_logger.LogInformation("RecCompanyUserChangeService Dispose");
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("RecCompanyUserChangeService ExecuteAsync");
return Task.Run(() =>
{
_logger.LogInformation("RecCompanyUserChangeService ExecuteAsync RunTask");
string ExchangeName = "djy.comp&user.change";
string QueueName = $"djy.comp&user.change.{DateTime.Now.Ticks}";
ConnectionFactory factory = new ConnectionFactory();
var repoSysCfg = _serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysConfig>>();
var mqUrl = repoSysCfg.FirstOrDefault(x => x.Code == "CompanyUserChangeMqUrl").Value;
factory.Uri = new Uri(mqUrl);
mqConn = factory.CreateConnection("客户订舱平台");
model = mqConn.CreateModel();
model.ExchangeDeclare(ExchangeName, ExchangeType.Fanout);
model.QueueDeclare(QueueName, false, false, true, null);
model.QueueBind(QueueName, ExchangeName, "", null);
var consumer = new EventingBasicConsumer(model);
consumer.Received += Consumer_Received;
model.BasicConsume(QueueName, true, consumer);
});
}
2 years ago
private async void Consumer_Received(object ch, BasicDeliverEventArgs ea)
{
2 years ago
2 years ago
//获取授权---------- (用超级管理员的权限)
LoginInput login = new LoginInput();
login.Account = "superAdmin";
login.Password = "123456";
var rtn = "http://localhost:5000/login".SetHttpMethod(HttpMethod.Post).SetContentType("application/json").SetBody(login.ToJsonString()).SetRetryPolicy(3, 5000).PostAsStringAsync();
var token = JObject.Parse(rtn.Result).GetStringValue("data");
var headers = new Dictionary<string, object>();
headers.Add("Authorization", $"Bearer {token}");
2 years ago
//-----
var body = ea.Body;
var strBody = Encoding.UTF8.GetString(body.ToArray());
var jobj = JObject.Parse(strBody);
var type = jobj.GetStringValue("type");
if (type == "CompanyAudit") //新公司审核通过
{
var comp = jobj.GetJObjectValue("company");
var compid = comp.GetStringValue("CompId");
var compname = comp.GetStringValue("CompName");
var adminShowName = comp.GetStringValue("AdminShowName");
var findTenant = _sysTenantRep.FirstOrDefault(x => x.CompId == compid); //先根据关联ID查找
if (findTenant == null)
{
findTenant = _sysTenantRep.FirstOrDefault(x => x.Name == compname); //再根据公司全称查找
}
2 years ago
if (findTenant == null) //找不到,新建
{
var tenant = new SysTenant();
tenant.Id = YitIdHelper.NextId();
tenant.Name = compname;
tenant.AdminName = adminShowName;
tenant.Email = $"{tenant.Id}";
tenant.TenantType = TenantTypeEnum.COMMON;
2 years ago
tenant.CompId = compid;
findTenant = _sysTenantRep.InsertReturnEntity(tenant);
await _tenantService.InitNewTenant(findTenant);
2 years ago
//新增的系统租户新增权限
GrantRoleMenuInput tenantmenuInput = new GrantRoleMenuInput();
tenantmenuInput.Id = findTenant.Id;
List<long> tenantGrantMenuIdList = new List<long>();
tenantGrantMenuIdList = _sysMenuRep.AsQueryable().Where(x => x.Pids.Contains("142307070910563")).Select(x => x.Id).ToList();//暂时写死默认新增权限管理
tenantmenuInput.GrantMenuIdList = tenantGrantMenuIdList;
await _sysRoleMenuService.GrantMenu(tenantmenuInput);
}
2 years ago
2 years ago
}
else if (type == "UserJoin") //用户加入公司
{
2 years ago
var compid = jobj.GetStringValue("compId");
var findTenant = _sysTenantRep.FirstOrDefault(x => x.CompId == compid); //先根据关联ID查找
//同步用户
var user = jobj.GetJArrayValue("users");
//租户id
var tenantid = findTenant.Id;
var keyDES = App.GetOptions<EncryptKeyOptions>().DES;
foreach (JObject item in user)
{
var GID = item.GetStringValue("GID");
var Accont = item.GetStringValue("CODENAME");
var Name = item.GetStringValue("SHOWNAME");
var Pass = DESCEncryption.Encrypt(item.GetStringValue("PASSWORD"), keyDES);
var Email = item.GetStringValue("EMAIL1");
var Phone = item.GetStringValue("MOBILE");
var PassWord = item.GetStringValue("PASSWORD");
var Org = await _sysOrgRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.IsDeleted == false && x.Pid == 0).FirstAsync();
//------ 拼接对象调用新增用户接口
AddUserInput adduser = new AddUserInput();
adduser.Account = Accont;
adduser.Password = PassWord;
adduser.Confirm = PassWord;
adduser.Sex = 1;
adduser.Phone = Phone;
adduser.TenantId = tenantid;
EmpOutput2 empOutput2 = new EmpOutput2();
empOutput2.OrgId = Org.Id.ToString();
empOutput2.OrgName = Org.Name;
List<long> PosIdList = new List<long>();
var pos = _sysPosRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.IsDeleted == false).FirstAsync();
PosIdList.Add(pos.Id);
empOutput2.PosIdList = PosIdList;
adduser.SysEmpParam = empOutput2;
2 years ago
await "http://localhost:5000/sysUser/add".SetHeaders(headers).SetHttpMethod(HttpMethod.Post).SetBody(adduser).SetRetryPolicy(3, 5000).PostAsStringAsync();
// await _serviceUser.AddUser(adduser, true);
2 years ago
}
//-----授权角色id普通用户
var role = _sysRoleRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.Name == "普通用户").FirstAsync();
GrantRoleMenuInput menuInput = new GrantRoleMenuInput();
menuInput.Id = Convert.ToInt64(role.Id);
List<long> GrantMenuIdList = new List<long>();
GrantMenuIdList.Add(351064299098181);//暂时写死 ,后续从字典获取 新增订舱台账
menuInput.GrantMenuIdList = GrantMenuIdList;
await _sysRoleMenuService.GrantMenu(menuInput);
2 years ago
}
else if (type == "UserLeave") //用户离职
{
2 years ago
var userId = jobj.GetJObjectValue("userId");
var compId = jobj.GetStringValue("compId");
//删除用户
_sysUserRep.Delete(x => x.DjyUserId == userId.ToString());
}
else if (type == "CompanyUserSync") //公司及员工数据同步
{
2 years ago
//----------同步租户
2 years ago
var comp = jobj.GetJObjectValue("company");
var compid = comp.GetStringValue("CompId");
var compname = comp.GetStringValue("CompName");
var adminShowName = comp.GetStringValue("AdminShowName");
var findTenant = _sysTenantRep.FirstOrDefault(x => x.CompId == compid); //先根据关联ID查找
if (findTenant == null)
{
findTenant = _sysTenantRep.FirstOrDefault(x => x.Name == compname); //再根据公司全称查找
}
if (findTenant == null) //找不到,新建
{
var tenant = new SysTenant();
tenant.Id = YitIdHelper.NextId();
tenant.Name = compname;
tenant.AdminName = adminShowName;
tenant.Email = $"{tenant.Id}";
tenant.TenantType = TenantTypeEnum.COMMON;
2 years ago
tenant.CompId = compid;
2 years ago
findTenant = _sysTenantRep.InsertReturnEntity(tenant);
2 years ago
await _tenantService.InitNewTenant(findTenant);
//新增的系统租户新增权限
GrantRoleMenuInput tenantmenuInput = new GrantRoleMenuInput();
tenantmenuInput.Id = findTenant.Id;
List<long> tenantGrantMenuIdList = new List<long>();
tenantGrantMenuIdList = _sysMenuRep.AsQueryable().Where(x => x.Pids.Contains("142307070910563")).Select(x => x.Id).ToList();//暂时写死默认新增权限管理
tenantmenuInput.GrantMenuIdList = tenantGrantMenuIdList;
await _sysRoleMenuService.GrantMenu(tenantmenuInput);
2 years ago
}
2 years ago
//----------
//同步用户
var user = jobj.GetJArrayValue("users");
//租户id
2 years ago
var tenantid = findTenant.Id;
2 years ago
var keyDES = App.GetOptions<EncryptKeyOptions>().DES;
foreach (JObject item in user)
2 years ago
{
2 years ago
var GID = item.GetStringValue("GID");
var Accont = item.GetStringValue("CODENAME");
var Name = item.GetStringValue("SHOWNAME");
var Pass = DESCEncryption.Encrypt(item.GetStringValue("PASSWORD"), keyDES);
var Email = item.GetStringValue("EMAIL1");
var Phone = item.GetStringValue("MOBILE");
var PassWord = item.GetStringValue("PASSWORD");
var Org = await _sysOrgRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.IsDeleted == false && x.Pid == 0).FirstAsync();
//------ 拼接对象调用新增用户接口
AddUserInput adduser = new AddUserInput();
adduser.Account = Accont;
adduser.Password = PassWord;
adduser.Confirm = PassWord;
adduser.Sex = 1;
adduser.Phone = Phone;
adduser.TenantId = tenantid;
EmpOutput2 empOutput2 = new EmpOutput2();
empOutput2.OrgId = Org.Id.ToString();
empOutput2.OrgName = Org.Name;
List<long> PosIdList = new List<long>();
var pos = _sysPosRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.IsDeleted == false).FirstAsync();
PosIdList.Add(pos.Id);
empOutput2.PosIdList = PosIdList;
adduser.SysEmpParam = empOutput2;
2 years ago
//await _serviceUser.AddUser(adduser, true);
await "http://localhost:5000/sysUser/add".SetHeaders(headers).SetHttpMethod(HttpMethod.Post).SetBody(adduser).SetRetryPolicy(3, 5000).PostAsStringAsync();
2 years ago
}
2 years ago
//-----授权角色id普通用户
var role = _sysRoleRep.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantid && x.Name == "普通用户").FirstAsync();
GrantRoleMenuInput menuInput = new GrantRoleMenuInput();
menuInput.Id = Convert.ToInt64(role.Id);
List<long> GrantMenuIdList = new List<long>();
GrantMenuIdList.Add(351064299098181);//暂时写死 ,后续从字典获取 新增订舱台账
menuInput.GrantMenuIdList = GrantMenuIdList;
await _sysRoleMenuService.GrantMenu(menuInput);
}
_logger.LogInformation($"收到消息:{strBody}");
}
}
}