using Furion; using Furion.DataEncryption; using Furion.Logging; using Furion.TaskScheduler; using Myshipping.Core.Const; using Myshipping.Core.Entity; using Myshipping.Core.Service; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TinyPinyin; namespace Myshipping.Core.Job { /// /// 和川用户同步 /// public class UserSyncWorker : ISpareTimeWorker { [SpareTime(60000, "UserSyncWorker", Description = "和川同步用户", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void SyncUser(SpareTimer timer, long count) { Log.Information($"UserSyncWorker {DateTime.Now}"); var tenantId = 349708986646597L; var roleId = 355651062833221L;//普通用户 var _sugerClient = App.GetService(); _sugerClient.QueryFilter.Clear(); var _repPingTaiUser = App.GetService>(); var _repPingTaiUserBase = App.GetService>(); var _repUser = App.GetService>(); var _repEmp = App.GetService>(); var _repOrg = App.GetService>(); var _repPos = App.GetService>(); var _repEmpPos = App.GetService>(); var _repUserRole = App.GetService>(); var _sysDataUserMenu = App.GetService>(); var _sysCacheService = App.GetService(); var users = _repPingTaiUser.Where(u => u.CompId == "5913ab4b-ff5e-4d31-8b6c-09911bdfc0d4").ToList(); var sysUsers = _repUser.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenantId).ToList(); Log.Information($"准备同步用户数据,共{users.Count}条"); var keyDES = App.GetOptions().DES; var org = _repOrg.FirstOrDefault(x => x.TenantId == tenantId); if (org == null) { Log.Error($"用户数据同步失败:未找到任何组织"); return; } var pos = _repPos.FirstOrDefault(x => x.TenantId == tenantId); if (pos == null) { Log.Error($"用户数据同步失败:未找到任何职位"); return; } _sugerClient.Ado.BeginTran(System.Data.IsolationLevel.ReadCommitted); try { // 是否发生过新增或更新操作 bool isModify = false; foreach (var user in users) { var existUser = sysUsers.Count(x => x.DjyUserId == user.GID) > 0; SysUser sysUser = null; if (!existUser) { sysUser = new SysUser(); } else { sysUser = sysUsers.First(x => x.DjyUserId == user.GID); } var ub = _repPingTaiUserBase.FirstOrDefault(x => x.USERID == user.GID); sysUser.TenantId = tenantId; sysUser.AdminType = AdminType.None; sysUser.DjyUserId = user.GID; sysUser.IsDeleted = user.IsLeave; if (!existUser) { sysUser.Account = user.CODENAME; sysUser.Phone = ub.MOBILE; sysUser.Email = ub.EMAIL1; sysUser.Password = DESCEncryption.Encrypt(user.PASSWORD, keyDES);//只在新增时同步密码 sysUser.Name = user.SHOWNAME;//只在新增时同步姓名,防止覆盖 sysUser.NickName = user.SHOWNAME;//只在新增时同步昵称,防止覆盖 sysUser.UserCode = PinyinHelper.GetPinyinInitials(sysUser.Name); var newUser = _repUser.Insert(sysUser); var emp = new SysEmp(); emp.Id = sysUser.Id; emp.OrgId = org.Id; emp.OrgName = org.Name; _repEmp.Insert(emp); var empPos = new SysEmpPos(); empPos.SysEmpId = emp.Id; empPos.SysPosId = pos.Id; _repEmpPos.Insert(empPos); // 绑定角色:普通员工 var userrole = new SysUserRole(); userrole.SysRoleId = roleId; userrole.SysUserId = sysUser.Id; _repUserRole.Insert(userrole); // 授权"订舱台账"菜单的数据范围 _sysDataUserMenu.Insert(new SysDataUserMenu { SysUserId = sysUser.Id, SysMenuId = MenuConst.MenuBookingOrder, DataScopeType = DataScopeType.DEPT_WITH_CHILD, IsEdit = true }); Log.Information($"新用户,插入:{sysUser.Name}"); isModify = true; } else { if (string.IsNullOrEmpty(sysUser.UserCode)) { sysUser.UserCode = PinyinHelper.GetPinyinInitials(sysUser.Name); } _repUser.Update(sysUser); Log.Information($"老用户,更新:{sysUser.Name}"); isModify = true; } } _sugerClient.Ado.CommitTran(); if (isModify) { //SysUserRoleService里GrantRole()的逻辑:清除缓存 _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_MENU); _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_PERMISSION); //SysDataUserMenuService里GrantData()的逻辑:清除缓存 _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_USERSDATASCOPE); //SysUserService里AddUser()的逻辑:重新缓存用户数据权限 var userdatalist = _sysDataUserMenu.AsQueryable().ToList(); _sysCacheService.Set(CommonConst.CACHE_KEY_USERDATASCOPE, userdatalist); } Log.Information($"用户数据同步完成"); } catch (Exception ex) { _sugerClient.Ado.RollbackTran(); Log.Error($"用户数据同步失败:{ex.Message}"); Log.Error($"{ex.StackTrace}"); } } } }