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.
BookingHeChuan/Myshipping.Core/EventSubscriber/CompanyUserSyncSubscriber.cs

478 lines
18 KiB
C#

using Furion;
using Furion.DataEncryption;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.RemoteRequest.Extensions;
using Microsoft.Extensions.Logging;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.OpenXmlFormats.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Core
{
/// <summary>
/// 公司、用户同步订阅器
/// </summary>
public class CompanyUserSyncSubscriber : IEventSubscriber
{
private readonly ILogger<CompanyUserSyncSubscriber> _logger;
public CompanyUserSyncSubscriber(IServiceProvider services, ILogger<CompanyUserSyncSubscriber> logger)
{
Services = services;
_logger = logger;
}
public IServiceProvider Services { get; }
//公司及用户信息的同步
[EventSubscribe("CompanyUserSync:CompanyUser")]
public async Task CompanyUserSyncCompanyUser(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到公司及用户信息的同步订阅消息:{context.Source.Payload}");
var dto = JsonConvert.DeserializeObject<MyshippingCompanyUserSyncDto>(context.Source.Payload.ToString());
var repoMenu = App.GetService<SqlSugarRepository<SysMenu>>();
var repoTenant = App.GetService<SqlSugarRepository<SysTenant>>();
var repoOrg = App.GetService<SqlSugarRepository<SysOrg>>();
var repoPos = App.GetService<SqlSugarRepository<SysPos>>();
var repoRole = App.GetService<SqlSugarRepository<SysRole>>();
var repoRoleMenu = App.GetService<SqlSugarRepository<SysRoleMenu>>();
var repoUser = App.GetService<SqlSugarRepository<SysUser>>();
var repoEmp = App.GetService<SqlSugarRepository<SysEmp>>();
var repoEmpPos = App.GetService<SqlSugarRepository<SysEmpPos>>();
var repoUserRole = App.GetService<SqlSugarRepository<SysUserRole>>();
var cache = App.GetService<ISysCacheService>();
//租户信息
var tenant = await repoTenant.AsQueryable().Filter(null, true).FirstAsync(x => x.CompId == dto.Company.CompId && x.IsDeleted == false);
if (tenant == null)
{
tenant = new SysTenant();
tenant.Id = YitIdHelper.NextId();
tenant.Name = dto.Company.CompName;
tenant.AdminName = dto.Company.AdminShowName;
tenant.Email = dto.Company.AdminUserEmail;
tenant.Phone = dto.Company.AdminUserMobile;
tenant.CompId = dto.Company.CompId;
tenant.CreatedUserId = 142307070910551L;
tenant.CreatedUserName = "超级管理员";
await repoTenant.InsertAsync(tenant);
}
else
{
tenant.Name = dto.Company.CompName;
tenant.AdminName = dto.Company.AdminShowName;
tenant.Email = dto.Company.AdminUserEmail;
tenant.Phone = dto.Company.AdminUserMobile;
tenant.UpdatedUserId = 142307070910551L;
tenant.UpdatedUserName = "超级管理员";
await repoTenant.UpdateAsync(tenant);
}
//组织机构
var org = await repoOrg.AsQueryable().Filter(null, true).FirstAsync(x => x.Pid == 0 && x.TenantId == tenant.Id && x.IsDeleted == false);
if (org == null)
{
org = new SysOrg();
org.Id = YitIdHelper.NextId();
org.Name = tenant.Name;
org.Pid = 0;
org.Pids = string.Empty;
org.Code = string.Empty;
org.Contacts = dto.Company.AdminShowName;
org.Tel = dto.Company.AdminUserMobile;
org.Sort = 0;
org.TenantId = tenant.Id;
org.CreatedUserId = 142307070910551L;
org.CreatedUserName = "超级管理员";
await repoOrg.InsertAsync(org);
}
//职位
var pos = await repoPos.AsQueryable().Filter(null, true).FirstAsync(x => x.Code == "SystemPos" && x.TenantId == tenant.Id && x.IsDeleted == false);
if (pos == null)
{
pos = new SysPos();
pos.Id = YitIdHelper.NextId();
pos.Code = "SystemPos";
pos.Name = "系统职位";
pos.Sort = 0;
pos.TenantId = tenant.Id;
pos.CreatedUserId = 142307070910551L;
pos.CreatedUserName = "超级管理员";
await repoPos.InsertAsync(pos);
}
//系统角色
var roleSystem = await repoRole.AsQueryable().Filter(null, true).FirstAsync(x => x.Code == "SystemRole" && x.TenantId == tenant.Id && x.IsDeleted == false);
if (roleSystem == null)
{
roleSystem = new SysRole();
roleSystem.Id = YitIdHelper.NextId();
roleSystem.Code = "SystemRole";
roleSystem.Name = "系统角色";
roleSystem.RoleType = RoleType.NormalRole;
roleSystem.DataScopeType = DataScopeType.SELF;
roleSystem.Sort = 1;
roleSystem.TenantId = tenant.Id;
roleSystem.CreatedUserId = 142307070910551L;
roleSystem.CreatedUserName = "超级管理员";
await repoRole.InsertAsync(roleSystem);
//角色菜单
var initMenu = new long[] {
347225881825349,
351064299098181,
351175233155141,
353548710699077,
410157538861125,
430004145692741
};
foreach (var menuId in initMenu)
{
var roleMenu = new SysRoleMenu();
roleMenu.SysRoleId = roleSystem.Id;
roleMenu.SysMenuId = menuId;
await repoRoleMenu.InsertAsync(roleMenu);
}
}
//管理员角色
var roleAdmin = await repoRole.AsQueryable().Filter(null, true).FirstAsync(x => x.Code == "AdminRole" && x.TenantId == tenant.Id && x.IsDeleted == false);
if (roleAdmin == null)
{
roleAdmin = new SysRole();
roleAdmin.Id = YitIdHelper.NextId();
roleAdmin.Code = "AdminRole";
roleAdmin.Name = "管理员角色";
roleAdmin.RoleType = RoleType.AdminRole;
roleAdmin.DataScopeType = DataScopeType.ALL;
roleAdmin.Sort = 0;
roleAdmin.TenantId = tenant.Id;
roleAdmin.CreatedUserId = 142307070910551L;
roleAdmin.CreatedUserName = "超级管理员";
await repoRole.InsertAsync(roleAdmin);
//角色菜单
var initMenu = new long[] {
347225881825349,
351064299098181,
351175233155141,
353548710699077,
410157538861125,
430004145692741,
142307070910564,
142307070910581,
142307070910589,
142307070914651,
360699813744709,
374126362341445,
142307070910563,
142307000914633,
533697384435781 //复制项配置
};
foreach (var menuId in initMenu)
{
var roleMenu = new SysRoleMenu();
roleMenu.SysRoleId = roleAdmin.Id;
roleMenu.SysMenuId = menuId;
await repoRoleMenu.InsertAsync(roleMenu);
}
//管理员的部分菜单有子项,需要加入权限
var parentMenuList = new long[] {
142307070910563,
142307070910564,
142307000914633,
142307070910581,
142307070910589,
142307070914651
};
var subMenuList = repoMenu.AsQueryable().Filter(null, true).Where(x => parentMenuList.Contains(x.Pid)).Select(x => x.Id).ToList();
foreach (var menuId in subMenuList)
{
if (repoRoleMenu.AsQueryable().Filter(null, true).Count(x => x.SysRoleId == roleAdmin.Id && x.SysMenuId == menuId) == 0)
{
var roleMenu = new SysRoleMenu();
roleMenu.SysRoleId = roleAdmin.Id;
roleMenu.SysMenuId = menuId;
await repoRoleMenu.InsertAsync(roleMenu);
}
}
}
var listSyncToDjy = new List<DjyCustomerFromCustomerUserDto>();
//用户信息
var dbUsers = await repoUser.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenant.Id && x.IsDeleted == false).ToListAsync();
var keyDES = App.GetOptions<EncryptKeyOptions>().DES;
foreach (var usr in dto.Users)
{
var user = dbUsers.FirstOrDefault(u => u.DjyUserId == usr.GID);
if (user == null)
{
user = new SysUser();
user.Id = YitIdHelper.NextId();
user.Name = usr.SHOWNAME;
user.NickName = usr.SHOWNAME;
user.Account = usr.CODENAME;
user.Password = DESCEncryption.Encrypt(usr.PASSWORD, keyDES);
user.Email = usr.EMAIL1;
user.Phone = usr.MOBILE;
user.AdminType = AdminType.None;
user.DjyUserId = usr.GID;
user.TenantId = tenant.Id;
await repoUser.InsertAsync(user);
//SysEmp
var emp = new SysEmp();
emp.Id = user.Id;
emp.OrgId = org.Id;
emp.OrgName = org.Name;
await repoEmp.InsertAsync(emp);
//SysEmpPos
var empPos = new SysEmpPos();
empPos.SysEmpId = emp.Id;
empPos.SysPosId = pos.Id;
await repoEmpPos.InsertAsync(empPos);
//SysUserRole
var userRole = new SysUserRole();
userRole.SysUserId = user.Id;
userRole.SysRoleId = roleSystem.Id;
await repoUserRole.InsertAsync(userRole);
}
listSyncToDjy.Add(new DjyCustomerFromCustomerUserDto()
{
Id = user.Id,
Name = user.Name,
DjyGid = user.DjyUserId,
Email = user.Email,
Phone = user.Phone,
});
//管理员账号
if (usr.GID == dto.Company.AdminUser && dbUsers.Count(u => u.AdminType == AdminType.Admin) == 0)
{
var adminUser = new SysUser();
adminUser.Id = YitIdHelper.NextId();
adminUser.Name = "系统管理员";
adminUser.NickName = adminUser.Name;
adminUser.Account = usr.EMAIL1;
adminUser.Password = DESCEncryption.Encrypt(usr.PASSWORD, keyDES);
adminUser.Email = string.Empty;
adminUser.Phone = string.Empty;
adminUser.AdminType = AdminType.Admin;
adminUser.TenantId = tenant.Id;
await repoUser.InsertAsync(adminUser);
//SysEmp
var emp = new SysEmp();
emp.Id = adminUser.Id;
emp.OrgId = org.Id;
emp.OrgName = org.Name;
await repoEmp.InsertAsync(emp);
//SysEmpPos
var empPos = new SysEmpPos();
empPos.SysEmpId = emp.Id;
empPos.SysPosId = pos.Id;
await repoEmpPos.InsertAsync(empPos);
//SysUserRole
var userRole = new SysUserRole();
userRole.SysUserId = adminUser.Id;
userRole.SysRoleId = roleAdmin.Id;
await repoUserRole.InsertAsync(userRole);
}
}
//客户端用户信息同步给运营端往来单位
if (App.Configuration["RunType"] is CommonConst.RUN_TYPE_CUST)
{
var evtPub = App.GetService<IEventPublisher>();
await evtPub.PublishAsync(new ChannelEventSource($"DjySync:TenantUserToCustomer", tenant.Id));
}
}
//用户离职信息的同步
[EventSubscribe("CompanyUserSync:UserLeave")]
public async Task CompanyUserSyncUserLeave(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到用户离职信息的同步订阅消息:{context.Source.Payload}");
var dto = JsonConvert.DeserializeObject<MyshippingUserLeaveSyncDto>(context.Source.Payload.ToString());
var repoTenant = App.GetService<SqlSugarRepository<SysTenant>>();
var repoUser = App.GetService<SqlSugarRepository<SysUser>>();
//租户信息
var tenant = await repoTenant.AsQueryable().Filter(null, true).FirstAsync(x => x.CompId == dto.CompId && x.IsDeleted == false);
if (tenant != null)
{
var user = await repoUser.AsQueryable().Filter(null, true).FirstAsync(u => u.DjyUserId == dto.UserId && u.TenantId == tenant.Id);
if (user != null)
{
_logger.LogInformation($"处理 {tenant.Name} 的 {user.Name} 离职");
var keyDES = App.GetOptions<EncryptKeyOptions>().DES;
user.Account = YitIdHelper.NextId().ToString();
user.Phone = string.Empty;
user.Email = string.Empty;
user.Name += "(离职)";
user.Password = DESCEncryption.Encrypt("123456", keyDES);
await repoUser.UpdateAsync(user);
8 months ago
//客户端用户信息同步给运营端往来单位
if (App.Configuration["RunType"] is CommonConst.RUN_TYPE_CUST)
{
var evtPub = App.GetService<IEventPublisher>();
await evtPub.PublishAsync(new ChannelEventSource($"DjySync:TenantUserToCustomer", tenant.Id));
}
}
}
}
}
/// <summary>
/// 公司及用户信息的同步dto
/// </summary>
public class MyshippingCompanyUserSyncDto
{
/// <summary>
/// 类型
/// </summary>
public string Type { get; set; }
/// <summary>
/// 公司
/// </summary>
public MyshippingCompanySyncDto Company { get; set; }
/// <summary>
/// 员工信息
/// </summary>
public List<MyshippingUserSyncDto> Users { get; set; }
}
/// <summary>
/// 用户离职信息的同步dto
/// </summary>
public class MyshippingUserLeaveSyncDto
{
/// <summary>
/// 类型
/// </summary>
public string Type { get; set; }
/// <summary>
/// 公司ID
/// </summary>
public string CompId { get; set; }
/// <summary>
/// 用户ID
/// </summary>
public string UserId { get; set; }
}
/// <summary>
/// 公司信息的同步dto
/// </summary>
public class MyshippingCompanySyncDto
{
/// <summary>
/// 公司ID
/// </summary>
public string CompId { get; set; }
/// <summary>
/// 公司名称
/// </summary>
public string CompName { get; set; }
/// <summary>
/// 地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 税号
/// </summary>
public string TaxCode { get; set; }
/// <summary>
/// 海关登记号
/// </summary>
public string CustomsCode { get; set; }
/// <summary>
/// 开户行
/// </summary>
public string BankName { get; set; }
/// <summary>
/// 开户行账号
/// </summary>
public string BankAccount { get; set; }
/// <summary>
/// 管理员ID
/// </summary>
public string AdminUser { get; set; }
/// <summary>
/// 管理员姓名
/// </summary>
public string AdminShowName { get; set; }
/// <summary>
/// 管理员邮箱
/// </summary>
public string AdminUserEmail { get; set; }
/// <summary>
/// 管理员手机
/// </summary>
public string AdminUserMobile { get; set; }
}
/// <summary>
/// 员工信息的同步dto
/// </summary>
public class MyshippingUserSyncDto
{
/// <summary>
/// 员工ID
/// </summary>
public string GID { get; set; }
/// <summary>
/// 登录名
/// </summary>
public string CODENAME { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string SHOWNAME { get; set; }
/// <summary>
/// 密码
/// </summary>
public string PASSWORD { get; set; }
/// <summary>
/// 手机
/// </summary>
public string MOBILE { get; set; }
/// <summary>
/// 邮箱
/// </summary>
public string EMAIL1 { get; set; }
}
}