From 07360f08a50454229af8bfd8415b609a36cd5ef5 Mon Sep 17 00:00:00 2001 From: wanghaomei Date: Mon, 27 Nov 2023 15:40:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8C=E6=AD=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CompanyUserSyncSubscriber.cs | 444 ++++++++++++++++++ Myshipping.Core/Myshipping.Core.xml | 152 ++++++ .../Service/User/SysUserService.cs | 68 ++- Myshipping.Web.Core/Startup.cs | 2 + Myshipping.Web.Core/dbsettings.json | 2 +- 5 files changed, 664 insertions(+), 4 deletions(-) create mode 100644 Myshipping.Core/EventSubscriber/CompanyUserSyncSubscriber.cs diff --git a/Myshipping.Core/EventSubscriber/CompanyUserSyncSubscriber.cs b/Myshipping.Core/EventSubscriber/CompanyUserSyncSubscriber.cs new file mode 100644 index 00000000..1e94b40c --- /dev/null +++ b/Myshipping.Core/EventSubscriber/CompanyUserSyncSubscriber.cs @@ -0,0 +1,444 @@ +using Furion; +using Furion.DataEncryption; +using Furion.EventBus; +using Furion.RemoteRequest.Extensions; +using Microsoft.Extensions.Logging; +using Myshipping.Core.Entity; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yitter.IdGenerator; + +namespace Myshipping.Core +{ + /// + /// 公司、用户同步订阅器 + /// + public class CompanyUserSyncSubscriber : IEventSubscriber + { + private readonly ILogger _logger; + public CompanyUserSyncSubscriber(IServiceProvider services, ILogger 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(context.Source.Payload.ToString()); + + var repoMenu = App.GetService>(); + var repoTenant = App.GetService>(); + var repoOrg = App.GetService>(); + var repoPos = App.GetService>(); + var repoRole = App.GetService>(); + var repoRoleMenu = App.GetService>(); + var repoUser = App.GetService>(); + var repoEmp = App.GetService>(); + var repoEmpPos = App.GetService>(); + var repoUserRole = App.GetService>(); + + //租户信息 + 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, + }; + + 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 dbUsers = await repoUser.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenant.Id && x.IsDeleted == false).ToListAsync(); + var keyDES = App.GetOptions().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); + } + + //管理员账号 + 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); + } + } + } + + //用户离职信息的同步 + [EventSubscribe("CompanyUserSync:UserLeave")] + public async Task CompanyUserSyncUserLeave(EventHandlerExecutingContext context) + { + _logger.LogInformation($"收到用户离职信息的同步订阅消息:{context.Source.Payload}"); + + var dto = JsonConvert.DeserializeObject(context.Source.Payload.ToString()); + + var repoTenant = App.GetService>(); + var repoUser = App.GetService>(); + + //租户信息 + 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().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); + } + } + } + } + + /// + /// 公司及用户信息的同步dto + /// + public class MyshippingCompanyUserSyncDto + { + /// + /// 类型 + /// + public string Type { get; set; } + + /// + /// 公司 + /// + public MyshippingCompanySyncDto Company { get; set; } + + /// + /// 员工信息 + /// + public List Users { get; set; } + } + + /// + /// 用户离职信息的同步dto + /// + public class MyshippingUserLeaveSyncDto + { + /// + /// 类型 + /// + public string Type { get; set; } + + /// + /// 公司ID + /// + public string CompId { get; set; } + + /// + /// 用户ID + /// + public string UserId { get; set; } + } + + /// + /// 公司信息的同步dto + /// + public class MyshippingCompanySyncDto + { + /// + /// 公司ID + /// + public string CompId { get; set; } + /// + /// 公司名称 + /// + public string CompName { get; set; } + /// + /// 地址 + /// + public string Address { get; set; } + /// + /// 税号 + /// + public string TaxCode { get; set; } + /// + /// 海关登记号 + /// + public string CustomsCode { get; set; } + /// + /// 开户行 + /// + public string BankName { get; set; } + /// + /// 开户行账号 + /// + public string BankAccount { get; set; } + /// + /// 管理员ID + /// + public string AdminUser { get; set; } + /// + /// 管理员姓名 + /// + public string AdminShowName { get; set; } + /// + /// 管理员邮箱 + /// + public string AdminUserEmail { get; set; } + /// + /// 管理员手机 + /// + public string AdminUserMobile { get; set; } + } + + /// + /// 员工信息的同步dto + /// + public class MyshippingUserSyncDto + { + /// + /// 员工ID + /// + public string GID { get; set; } + /// + /// 登录名 + /// + public string CODENAME { get; set; } + /// + /// 姓名 + /// + public string SHOWNAME { get; set; } + /// + /// 密码 + /// + public string PASSWORD { get; set; } + /// + /// 手机 + /// + public string MOBILE { get; set; } + /// + /// 邮箱 + /// + public string EMAIL1 { get; set; } + } +} diff --git a/Myshipping.Core/Myshipping.Core.xml b/Myshipping.Core/Myshipping.Core.xml index 17dd639f..ca505341 100644 --- a/Myshipping.Core/Myshipping.Core.xml +++ b/Myshipping.Core/Myshipping.Core.xml @@ -5282,6 +5282,146 @@ 否 + + + 公司、用户同步订阅器 + + + + + 公司及用户信息的同步dto + + + + + 类型 + + + + + 公司 + + + + + 员工信息 + + + + + 用户离职信息的同步dto + + + + + 类型 + + + + + 公司ID + + + + + 用户ID + + + + + 公司信息的同步dto + + + + + 公司ID + + + + + 公司名称 + + + + + 地址 + + + + + 税号 + + + + + 海关登记号 + + + + + 开户行 + + + + + 开户行账号 + + + + + 管理员ID + + + + + 管理员姓名 + + + + + 管理员邮箱 + + + + + 管理员手机 + + + + + 员工信息的同步dto + + + + + 员工ID + + + + + 登录名 + + + + + 姓名 + + + + + 密码 + + + + + 手机 + + + + + 邮箱 + + B格 @@ -16081,6 +16221,18 @@ 职位代码 PCDD-调度 返回用户详情 + + + 公司用户信息同步 + + + + + + 用户离职信息同步 + + + EDI参数设置输出参数 diff --git a/Myshipping.Core/Service/User/SysUserService.cs b/Myshipping.Core/Service/User/SysUserService.cs index 9b05501c..c615960b 100644 --- a/Myshipping.Core/Service/User/SysUserService.cs +++ b/Myshipping.Core/Service/User/SysUserService.cs @@ -20,8 +20,8 @@ using NPOI.HSSF.UserModel; using Myshipping.Core.Helper; using System.Web; using System.Text; - - +using Furion.EventBus; +using Microsoft.AspNetCore.Authorization; namespace Myshipping.Core.Service; @@ -38,6 +38,7 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient private readonly ISysUserDataScopeService _sysUserDataScopeService; private readonly ISysUserRoleService _sysUserRoleService; private readonly ISysEmpPosService _sysEmpPosService; + private readonly IEventPublisher _publisher; public SysUserService(SqlSugarRepository sysUserRep, ISysCacheService sysCacheService, @@ -45,7 +46,8 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient ISysUserDataScopeService sysUserDataScopeService, ISysUserRoleService sysUserRoleService, ISysConfigService sysConfigService, - ISysEmpPosService sysEmpPosService) + ISysEmpPosService sysEmpPosService, + IEventPublisher publisher) { _sysUserRep = sysUserRep; _sysCacheService = sysCacheService; @@ -54,6 +56,7 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient _sysUserRoleService = sysUserRoleService; _sysConfigService = sysConfigService; _sysEmpPosService = sysEmpPosService; + _publisher = publisher; } /// @@ -620,4 +623,63 @@ public class SysUserService : ISysUserService, IDynamicApiController, ITransient return allList; } + + /// + /// 公司用户信息同步 + /// + /// + [HttpPost("/sysUser/CompanyUserSync"), AllowAnonymous, ApiUser(ApiCode = "CompanyUserSync")] + public async Task CompanyUserSync(MyshippingCompanyUserSyncDto dto) + { + if (dto.Type != "CompanyUserSync") + { + throw Oops.Bah($"类型有误:{dto.Type}"); + } + + if (string.IsNullOrEmpty(dto.Company.CompId) + || string.IsNullOrEmpty(dto.Company.CompName) + || string.IsNullOrEmpty(dto.Company.AdminUser) + || string.IsNullOrEmpty(dto.Company.AdminShowName) + || string.IsNullOrEmpty(dto.Company.AdminUserEmail) + || string.IsNullOrEmpty(dto.Company.AdminUserMobile) + || string.IsNullOrEmpty(dto.Company.CompId)) + { + throw Oops.Bah($"公司信息不全。公司ID、名称、管理员ID、名称、手机、邮箱都不能为空"); + } + + var cc = dto.Users.Where(x => string.IsNullOrEmpty(x.GID) + || string.IsNullOrEmpty(x.CODENAME) + || string.IsNullOrEmpty(x.SHOWNAME) + || string.IsNullOrEmpty(x.EMAIL1) + || string.IsNullOrEmpty(x.MOBILE)) + .Count(); + if (cc > 0) + { + throw Oops.Bah($"用户信息不全。用户ID、姓名、登录名、密码、手机、邮箱都不能为空"); + } + + await _publisher.PublishAsync(new ChannelEventSource("CompanyUserSync:CompanyUser", dto.ToJsonString())); + } + + /// + /// 用户离职信息同步 + /// + /// + [HttpPost("/sysUser/UserLeave"), AllowAnonymous, ApiUser(ApiCode = "CompanyUserSync")] + public async Task UserLeave(MyshippingUserLeaveSyncDto dto) + { + if (dto.Type != "UserLeave") + { + throw Oops.Bah($"类型有误:{dto.Type}"); + } + + if (string.IsNullOrEmpty(dto.CompId) + || string.IsNullOrEmpty(dto.UserId)) + { + throw Oops.Bah($"信息不全。公司ID、用户ID都不能为空"); + } + + + await _publisher.PublishAsync(new ChannelEventSource("CompanyUserSync:UserLeave", dto.ToJsonString())); + } } diff --git a/Myshipping.Web.Core/Startup.cs b/Myshipping.Web.Core/Startup.cs index c9537a05..22fd5a4a 100644 --- a/Myshipping.Web.Core/Startup.cs +++ b/Myshipping.Web.Core/Startup.cs @@ -117,6 +117,8 @@ public class Startup : AppStartup builder.AddSubscriber(); //日志压缩清理 builder.AddSubscriber(); + //公司及用户信息的同步 + builder.AddSubscriber(); }); } diff --git a/Myshipping.Web.Core/dbsettings.json b/Myshipping.Web.Core/dbsettings.json index c6071452..27ffc63f 100644 --- a/Myshipping.Web.Core/dbsettings.json +++ b/Myshipping.Web.Core/dbsettings.json @@ -17,7 +17,7 @@ { "DbNumber": "pingtai", "DbType": "SqlServer", - "DbString": "Server=60.209.125.238,28000;Database=DevDsPingTai;User=dev;Password=dev123;MultipleActiveResultSets=True;" + "DbString": "Server=60.209.125.238,32009;Database=DevDsPingTai;User=dev;Password=dev123;MultipleActiveResultSets=True;" }, { "DbNumber": "taskmanage",