| using SqlSugar; | 
| using System; | 
| using System.Collections.Concurrent; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Core; | 
| using WIDESEAWCS_Core.BaseRepository; | 
| using WIDESEAWCS_Core.Helper; | 
| using WIDESEAWCS_ISystemRepository; | 
| using WIDESEAWCS_Model; | 
| using WIDESEAWCS_Model.Models; | 
|   | 
| namespace WIDESEAWCS_SystemRepository | 
| { | 
|     public class Sys_MenuRepository : RepositoryBase<Sys_Menu>, ISys_MenuRepository | 
|     { | 
|         public Sys_MenuRepository(IUnitOfWorkManage unitOfWorkManage) : base(unitOfWorkManage) | 
|         { | 
|         } | 
|   | 
|         public List<Sys_Menu> GetAllMenu() | 
|         { | 
|             List<Sys_Menu> _menus = base.QueryData(x => x.Enable == 1 || x.Enable == 2).OrderByDescending(a => a.OrderNo).ThenByDescending(q => q.ParentId).ToList(); | 
|   | 
|             _menus.ForEach(x => | 
|             { | 
|                 x.MenuType ??= 0; | 
|                 if (!string.IsNullOrEmpty(x.Auth) && x.Auth.Length > 10) | 
|                 { | 
|                     try | 
|                     { | 
|                         x.Actions = x.Auth.DeserializeObject<List<Sys_Actions>>(); | 
|                     } | 
|                     catch { } | 
|                 } | 
|                 x.Actions ??= new List<Sys_Actions>(); | 
|             }); | 
|             string test = _menus.Serialize(); | 
|             return _menus; | 
|         } | 
|   | 
|         public object GetSuperAdminMenu() | 
|         { | 
|             return GetAllMenu().Select(x => | 
|                   new | 
|                   { | 
|                       id = x.MenuId, | 
|                       name = x.MenuName, | 
|                       url = x.Url, | 
|                       parentId = x.ParentId, | 
|                       icon = x.Icon, | 
|                       x.Enable, | 
|                       x.TableName, // 2022.03.26增移动端加菜单类型 | 
|                       permission = x.Actions.Select(s => s.Value).ToArray() | 
|                   }).ToList(); | 
|         } | 
|   | 
|         public object GetMenuByRoleId(int roleId) | 
|         { | 
|             var menu = from a in GetPermissions(roleId) | 
|                        join b in GetAllMenu() | 
|                        on a.MenuId equals b.MenuId | 
|                        orderby b.OrderNo descending | 
|                        select new | 
|                        { | 
|                            id = a.MenuId, | 
|                            name = b.MenuName, | 
|                            url = b.Url, | 
|                            parentId = b.ParentId, | 
|                            icon = b.Icon, | 
|                            b.Enable, | 
|                            b.TableName, | 
|                            permission = a.UserAuthArr | 
|                        }; | 
|             return menu.ToList(); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 获取角色权限时通过安全字典锁定的角色id | 
|         /// </summary> | 
|         private static ConcurrentDictionary<string, object> objKeyValue = new ConcurrentDictionary<string, object>(); | 
|   | 
|         public List<Permissions> GetPermissions(int roleId) | 
|         { | 
|             if (App.User.IsRoleIdSuperAdmin(roleId)) | 
|             { | 
|                 //2020.12.27增加菜单界面上不显示,但可以分配权限 | 
|                 var permissions = QueryData(x => x.Enable == 1 || x.Enable == 2) | 
|                     .Select(a => new Permissions | 
|                     { | 
|                         MenuId = a.MenuId, | 
|                         ParentId = a.ParentId, | 
|                         TableName = (a.TableName ?? "").ToLower(), | 
|                         UserAuth = a.Auth, | 
|                         MenuType = a.MenuType ?? 0 | 
|                     }).ToList(); | 
|                 return MenuActionToArray(permissions); | 
|             } | 
|   | 
|             //锁定每个角色,通过安全字典减少锁粒度,否则多个同时角色获取缓存会导致阻塞 | 
|             object objId = objKeyValue.GetOrAdd(roleId.ToString(), new object()); | 
|             //锁定每个角色 | 
|             lock (objId) | 
|             { | 
|                 //没有redis/memory缓存角色的版本号或与当前服务器的角色版本号不同时,刷新缓存 | 
|   | 
|                 List<Permissions> _permissions = QueryTabs<Sys_Menu, Sys_RoleAuth, Permissions>((a, b) => new object[] { JoinType.Inner, a.MenuId == b.MenuId }, (a, b) => new Permissions { MenuId = a.MenuId, ParentId = a.ParentId, TableName = (a.TableName ?? "").ToLower(), MenuAuth = a.Auth, UserAuth = b.AuthValue ?? "", MenuType = a.MenuType ?? 0 }, (a, b) => b.RoleId == roleId, x => true); | 
|   | 
|                 ActionToArray(_permissions); | 
|   | 
|                 return _permissions; | 
|             } | 
|         } | 
|   | 
|         private List<Permissions> MenuActionToArray(List<Permissions> permissions) | 
|         { | 
|             permissions.ForEach(x => | 
|             { | 
|                 try | 
|                 { | 
|                     x.UserAuthArr = string.IsNullOrEmpty(x.UserAuth) | 
|                     ? new string[0] | 
|                     : x.UserAuth.DeserializeObject<List<Sys_Actions>>().Select(s => s.Value).ToArray(); | 
|                 } | 
|                 catch { } | 
|                 finally | 
|                 { | 
|                     if (x.UserAuthArr == null) | 
|                     { | 
|                         x.UserAuthArr = new string[0]; | 
|                     } | 
|                 } | 
|             }); | 
|             return permissions; | 
|         } | 
|   | 
|         private List<Permissions> ActionToArray(List<Permissions> permissions) | 
|         { | 
|             permissions.ForEach(x => | 
|             { | 
|                 try | 
|                 { | 
|                     var menuAuthArr = x.MenuAuth.DeserializeObject<List<Sys_Actions>>(); | 
|                     x.UserAuthArr = string.IsNullOrEmpty(x.UserAuth) | 
|                     ? new string[0] | 
|                     : x.UserAuth.Split(",").Where(c => menuAuthArr.Any(m => m.Value == c)).ToArray(); | 
|   | 
|                 } | 
|                 catch { } | 
|                 finally | 
|                 { | 
|                     if (x.UserAuthArr == null) | 
|                     { | 
|                         x.UserAuthArr = new string[0]; | 
|                     } | 
|                 } | 
|             }); | 
|             return permissions; | 
|         } | 
|   | 
|         public object GetMenu(List<int> menuIds) | 
|         { | 
|             return QueryData(x => menuIds.Contains(x.MenuId)).Select(a => | 
|              new | 
|              { | 
|                  id = a.MenuId, | 
|                  parentId = a.ParentId, | 
|                  name = a.MenuName, | 
|                  a.MenuType, | 
|                  a.OrderNo | 
|              }).OrderByDescending(a => a.OrderNo) | 
|                 .ThenByDescending(q => q.parentId).ToList(); | 
|         } | 
|   | 
|         public object GetTreeItem(int menuId) | 
|         { | 
|             var sysMenu = base.QueryData(x => x.MenuId == menuId) | 
|                 .Select( | 
|                 p => new | 
|                 { | 
|                     p.MenuId, | 
|                     p.ParentId, | 
|                     p.MenuName, | 
|                     p.Url, | 
|                     p.Auth, | 
|                     p.OrderNo, | 
|                     p.Icon, | 
|                     p.Enable, | 
|                     // 2022.03.26增移动端加菜单类型 | 
|                     MenuType = p.MenuType ?? 0, | 
|                     p.CreateDate, | 
|                     p.Creater, | 
|                     p.TableName, | 
|                     p.ModifyDate | 
|                 }).FirstOrDefault(); | 
|             return sysMenu; | 
|         } | 
|     } | 
| } |