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
{
///
/// 公司、用户同步订阅器
///
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 cache = 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,
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();
//用户信息
var dbUsers = await repoUser.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenant.Id && x.IsDeleted == false).ToListAsync();
var keyDES = App.GetOptions().DES;
// 标记用户信息是否发生改变
bool isUserChange = false;
foreach (var usr in dto.Users)
{
var user = dbUsers.FirstOrDefault(u => u.DjyUserId == usr.GID);
if (user == null)
{
isUserChange = true;
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);
}
}
// 清空该租户下所有用户的“用户权限Id集合缓存(usersdatascope_用户Id)”
if (isUserChange)
{
foreach (var item in dbUsers)
{
await cache.DelByPatternAsync(CommonConst.CACHE_KEY_USERSDATASCOPE + item.Id);
}
}
//客户端用户信息同步给运营端往来单位
if (App.Configuration["RunType"] is CommonConst.RUN_TYPE_CUST)
{
var evtPub = App.GetService();
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(context.Source.Payload.ToString());
var repoTenant = App.GetService>();
var repoUser = App.GetService>();
var cache = 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);
await cache.DelByPatternAsync(CommonConst.CACHE_KEY_USERSDATASCOPE + user.Id);
//客户端用户信息同步给运营端往来单位
if (App.Configuration["RunType"] is CommonConst.RUN_TYPE_CUST)
{
var evtPub = App.GetService();
await evtPub.PublishAsync(new ChannelEventSource($"DjySync:TenantUserToCustomer", tenant.Id));
}
}
}
}
}
///
/// 公司及用户信息的同步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; }
}
}