|  |  | 
 |  |  | using WIDESEA_Model.Models; | 
 |  |  | using WIDESEA_Model.Models.System; | 
 |  |  | using WIDESEA_SystemRepository; | 
 |  |  | using WIDESEA_Core.Helper; | 
 |  |  | using WIDESEA_Core.HostedService; | 
 |  |  |  | 
 |  |  | namespace WIDESEA_SystemService | 
 |  |  | { | 
 |  |  | 
 |  |  |             _RoleAuthRepository = roleAuthRepository; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public override WebResponseContent AddData(SaveModel saveModel) | 
 |  |  |         { | 
 |  |  |             string authorityScope = saveModel.MainData["authorityScope"].ToString() ?? ""; | 
 |  |  |             Sys_Role role = saveModel.MainData.DicToModel<Sys_Role>(); | 
 |  |  |             if (BaseDal.QueryFirst(x => x.RoleName == role.RoleName) != null) | 
 |  |  |             { | 
 |  |  |                 return WebResponseContent.Instance.Error($"è§è²åéå¤"); | 
 |  |  |             } | 
 |  |  |             List<Dt_Warehouse> warehouses = BaseDal.Db.Queryable<Dt_Warehouse>().Where(x => true).ToList(); | 
 |  |  |             List<Sys_RoleDataPermission> roleDataPermissions = new List<Sys_RoleDataPermission>(); | 
 |  |  |             if (!string.IsNullOrEmpty(authorityScope)) | 
 |  |  |             { | 
 |  |  |                 string[] scopes = authorityScope.Split(','); | 
 |  |  |                 foreach (string scope in scopes) | 
 |  |  |                 { | 
 |  |  |                     Dt_Warehouse? warehouse = warehouses.FirstOrDefault(x => x.WarehouseId == Convert.ToInt32(scope)); | 
 |  |  |                     if (warehouse != null) | 
 |  |  |                     { | 
 |  |  |                         roleDataPermissions.Add(new Sys_RoleDataPermission | 
 |  |  |                         { | 
 |  |  |                             WarehouseId = warehouse.WarehouseId, | 
 |  |  |                             WarehouseName = warehouse.WarehouseName, | 
 |  |  |                             RoleName = role.RoleName | 
 |  |  |                         }); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             int roleId = BaseDal.AddData(role); | 
 |  |  |             if (roleDataPermissions.Count > 0) | 
 |  |  |             { | 
 |  |  |                 roleDataPermissions.ForEach(x => { x.RoleId = roleId; }); | 
 |  |  |                 BaseDal.Db.Insertable(roleDataPermissions).ExecuteCommand(); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             PermissionDataHostService.UserRoles = PermissionDataHostService.GetUserRoles(Db); | 
 |  |  |  | 
 |  |  |             return WebResponseContent.Instance.OK(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public override WebResponseContent UpdateData(SaveModel saveModel) | 
 |  |  |         { | 
 |  |  |             string authorityScope = saveModel.MainData["authorityScope"].ToString() ?? ""; | 
 |  |  |             Sys_Role role = saveModel.MainData.DicToModel<Sys_Role>(); | 
 |  |  |             if (BaseDal.QueryFirst(x => x.RoleId == role.RoleId) == null) | 
 |  |  |             { | 
 |  |  |                 return WebResponseContent.Instance.Error($"æªæ¾å°è¯¥æ°æ®"); | 
 |  |  |             } | 
 |  |  |             List<Dt_Warehouse> warehouses = BaseDal.Db.Queryable<Dt_Warehouse>().Where(x => true).ToList(); | 
 |  |  |  | 
 |  |  |             List<Sys_RoleDataPermission> oldDatas = BaseDal.Db.Queryable<Sys_RoleDataPermission>().Where(x => x.RoleId == role.RoleId).ToList(); | 
 |  |  |             List<Sys_RoleDataPermission> roleDataPermissions = new List<Sys_RoleDataPermission>(); | 
 |  |  |             if (!string.IsNullOrEmpty(authorityScope)) | 
 |  |  |             { | 
 |  |  |                 string[] scopes = authorityScope.Split(','); | 
 |  |  |                 foreach (string scope in scopes) | 
 |  |  |                 { | 
 |  |  |                     Sys_RoleDataPermission? oldData = oldDatas.FirstOrDefault(x => x.WarehouseId == Convert.ToInt32(scope)); | 
 |  |  |                     if (oldData == null) | 
 |  |  |                     { | 
 |  |  |                         Dt_Warehouse? warehouse = warehouses.FirstOrDefault(x => x.WarehouseId == Convert.ToInt32(scope)); | 
 |  |  |                         if (warehouse != null) | 
 |  |  |                         { | 
 |  |  |                             roleDataPermissions.Add(new Sys_RoleDataPermission | 
 |  |  |                             { | 
 |  |  |                                 WarehouseId = warehouse.WarehouseId, | 
 |  |  |                                 WarehouseName = warehouse.WarehouseName, | 
 |  |  |                                 RoleName = role.RoleName | 
 |  |  |                             }); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                     else | 
 |  |  |                     { | 
 |  |  |                         oldDatas.Remove(oldData); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             roleDataPermissions.ForEach(x => { x.RoleId = role.RoleId; }); | 
 |  |  |             BaseDal.Db.Insertable(roleDataPermissions).ExecuteCommand(); | 
 |  |  |             BaseDal.Db.Deleteable(oldDatas).ExecuteCommand(); | 
 |  |  |  | 
 |  |  |             PermissionDataHostService.UserRoles = PermissionDataHostService.GetUserRoles(Db); | 
 |  |  |  | 
 |  |  |             return WebResponseContent.Instance.OK(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public List<int> GetAllChildrenRoleId(int roleId) | 
 |  |  |         { | 
 |  |  |             return GetAllChildren(roleId).Select(x => x.Id).ToList(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public List<RoleNodes> GetAllChildren(int roleId) | 
 |  |  |         { | 
 |  |  |             if (roleId <= 0) return new List<RoleNodes>() { }; | 
 |  |  | 
 |  |  |             return list; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public  List<RoleNodes> GetAllRoleId() | 
 |  |  |         public List<RoleNodes> GetAllRoleId() | 
 |  |  |         { | 
 |  |  |             List<RoleNodes> roles = BaseDal.QueryData().Select(s => new RoleNodes() { Id = s.RoleId, ParentId = s.ParentId, RoleName = s.RoleName }).ToList(); | 
 |  |  |  | 
 |  |  | 
 |  |  |             //è·åç¨æ·æé | 
 |  |  |             List<Permissions> permissions = _MenuRepository.GetPermissions(roleId); | 
 |  |  |             //æéç¨æ·æéæ¥è¯¢ææçèåä¿¡æ¯ | 
 |  |  |             List<MenuDTO> menus =  _MenuService.GetUserMenuList(roleId); | 
 |  |  |             List<MenuDTO> menus = _MenuService.GetUserMenuList(roleId); | 
 |  |  |             //è·åå½åç¨æ·æéå¦:(Add,Search)对åºçæ¾ç¤ºææ¬ä¿¡æ¯å¦:Addï¼æ·»å ï¼Search:æ¥è¯¢ | 
 |  |  |             var data = menus.Select(x => new | 
 |  |  |             { | 
 |  |  | 
 |  |  |             List<MenuDTO> menus = _MenuService.GetUserMenuListPDA(roleId); | 
 |  |  |  | 
 |  |  |             //è·åå½åç¨æ·æéå¦:(Add,Search)对åºçæ¾ç¤ºææ¬ä¿¡æ¯å¦:Addï¼æ·»å ï¼Search:æ¥è¯¢ | 
 |  |  |             var data = menus.Where(x => x.MenuType==1).Select(x => new | 
 |  |  |             var data = menus.Where(x => x.MenuType == 1).Select(x => new | 
 |  |  |             { | 
 |  |  |                 Id = x.MenuId, | 
 |  |  |                 Pid = x.ParentId, | 
 |  |  | 
 |  |  |                 //å½åç¨æ·çæé | 
 |  |  |                 List<Permissions> permissions = _MenuRepository.GetPermissions(App.User.RoleId); | 
 |  |  |  | 
 |  |  |                 List<int> menuIds = _MenuRepository.QueryData(x => x.MenuId, x => x.MenuType == 1); | 
 |  |  |  | 
 |  |  |                 List<int> originalMeunIds = new List<int>(); | 
 |  |  |                 //被åé
è§è²çæé | 
 |  |  |                 List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId); | 
 |  |  |                 List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId && menuIds.Contains(x.MenuId)); | 
 |  |  |                 List<Sys_RoleAuth> updateAuths = new List<Sys_RoleAuth>(); | 
 |  |  |                 foreach (UserPermissionDTO x in userPermissions) | 
 |  |  |                 { | 
 |  |  | 
 |  |  |                     x.AuthValue = ""; | 
 |  |  |                 }); | 
 |  |  |                 //å°åæ¶çæé设置为"" | 
 |  |  |                 _RoleAuthRepository.UpdateData(delAuths); | 
 |  |  |                 _RoleAuthRepository.DeleteData(delAuths); | 
 |  |  |  | 
 |  |  |                 int addCount = updateAuths.Where(x => x.AuthId <= 0).Count(); | 
 |  |  |                 int updateCount = updateAuths.Where(x => x.AuthId > 0).Count(); | 
 |  |  |                  | 
 |  |  |  | 
 |  |  |                 string _version = DateTime.Now.ToString("yyyyMMddHHMMssfff"); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                 content.OK($"ä¿åæåï¼æ°å¢å é
èåæé{addCount}æ¡,æ´æ°èå{updateCount}æ¡,å é¤æé{delAuths.Count}æ¡"); | 
 |  |  |             } | 
 |  |  |             catch (Exception ex) | 
 |  |  |             { | 
 |  |  |                 message = "å¼å¸¸ä¿¡æ¯ï¼" + ex.Message + ex.StackTrace + ","; | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             return content; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         /// <summary> | 
 |  |  |         /// ä¿åè§è²æé | 
 |  |  |         /// </summary> | 
 |  |  |         /// <param name="userPermissions"></param> | 
 |  |  |         /// <param name="roleId"></param> | 
 |  |  |         /// <returns></returns> | 
 |  |  |         public WebResponseContent SavePermissionPDA(List<UserPermissionDTO> userPermissions, int roleId) | 
 |  |  |         { | 
 |  |  |             WebResponseContent content = new WebResponseContent(); | 
 |  |  |             string message = ""; | 
 |  |  |             try | 
 |  |  |             { | 
 |  |  |                 if (!GetAllChildren(App.User.RoleId).Exists(x => x.Id == roleId)) | 
 |  |  |                     return WebResponseContent.Instance.Error("没ææéä¿®æ¹æ¤è§è²çæéä¿¡æ¯"); | 
 |  |  |                 //å½åç¨æ·çæé | 
 |  |  |                 List<Permissions> permissions = _MenuRepository.GetPermissions(App.User.RoleId); | 
 |  |  |  | 
 |  |  |                 List<int> originalMeunIds = new List<int>(); | 
 |  |  |  | 
 |  |  |                 List<int> menuIds = _MenuRepository.QueryData(x => x.MenuId, x => x.MenuType == 1); | 
 |  |  |  | 
 |  |  |                 //被åé
è§è²çæé | 
 |  |  |                 List<Sys_RoleAuth> roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId && menuIds.Contains(x.MenuId)); | 
 |  |  |                 List<Sys_RoleAuth> updateAuths = new List<Sys_RoleAuth>(); | 
 |  |  |                 foreach (UserPermissionDTO x in userPermissions) | 
 |  |  |                 { | 
 |  |  |                     Permissions per = permissions.FirstOrDefault(p => p.MenuId == x.Id); | 
 |  |  |                     //ä¸è½åé
è¶
è¿å½åç¨æ·çæé | 
 |  |  |                     if (per == null) continue; | 
 |  |  |                     //per.UserAuthArr.Contains(a.Value)æ ¡éªæéèå´ | 
 |  |  |                     string[] arr = x.Actions == null || x.Actions.Count == 0 | 
 |  |  |                       ? new string[0] | 
 |  |  |                       : x.Actions.Where(a => per.UserAuthArr.Contains(a.Value)) | 
 |  |  |                       .Select(s => s.Value).ToArray(); | 
 |  |  |  | 
 |  |  |                     //妿å½åæé没æåé
è¿ï¼è®¾ç½®Auth_Idé»è®¤ä¸º0ï¼è¡¨ç¤ºæ°å¢çæé | 
 |  |  |                     var auth = roleAuths.Where(r => r.MenuId == x.Id).Select(s => new { s.AuthId, s.AuthValue, s.MenuId }).FirstOrDefault(); | 
 |  |  |                     string newAuthValue = string.Join(",", arr); | 
 |  |  |                     //æé没æåçåååä¸å¤ç | 
 |  |  |                     if (auth == null || auth.AuthValue != newAuthValue) | 
 |  |  |                     { | 
 |  |  |                         updateAuths.Add(new Sys_RoleAuth() | 
 |  |  |                         { | 
 |  |  |                             RoleId = roleId, | 
 |  |  |                             MenuId = x.Id, | 
 |  |  |                             AuthValue = string.Join(",", arr), | 
 |  |  |                             AuthId = auth == null ? 0 : auth.AuthId, | 
 |  |  |                             ModifyDate = DateTime.Now, | 
 |  |  |                             Modifier = App.User.UserName, | 
 |  |  |                             CreateDate = DateTime.Now, | 
 |  |  |                             Creater = App.User.UserName | 
 |  |  |                         }); | 
 |  |  |                     } | 
 |  |  |                     else | 
 |  |  |                     { | 
 |  |  |                         originalMeunIds.Add(auth.MenuId); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                 } | 
 |  |  |                 //æ´æ°æé | 
 |  |  |                 _RoleAuthRepository.UpdateData(updateAuths); | 
 |  |  |                 //æ°å¢çæé | 
 |  |  |                 _RoleAuthRepository.AddData(updateAuths); | 
 |  |  |  | 
 |  |  |                 //è·åæéåæ¶çæé | 
 |  |  |                 int[] authIds = roleAuths.Where(x => userPermissions.Select(u => u.Id) | 
 |  |  |                  .ToList().Contains(x.MenuId) || originalMeunIds.Contains(x.MenuId)) | 
 |  |  |                 .Select(s => s.AuthId) | 
 |  |  |                 .ToArray(); | 
 |  |  |                 List<Sys_RoleAuth> delAuths = roleAuths.Where(x => x.AuthValue != "" && !authIds.Contains(x.AuthId)).ToList(); | 
 |  |  |                 delAuths.ForEach(x => | 
 |  |  |                 { | 
 |  |  |                     x.AuthValue = ""; | 
 |  |  |                 }); | 
 |  |  |                 //å°åæ¶çæé设置为"" | 
 |  |  |                 _RoleAuthRepository.DeleteData(delAuths); | 
 |  |  |  | 
 |  |  |                 int addCount = updateAuths.Where(x => x.AuthId <= 0).Count(); | 
 |  |  |                 int updateCount = updateAuths.Where(x => x.AuthId > 0).Count(); | 
 |  |  |  | 
 |  |  |                 string _version = DateTime.Now.ToString("yyyyMMddHHMMssfff"); | 
 |  |  |  | 
 |  |  |  |