using DS.Module.Core; using DS.Module.Core.Extensions; using DS.WMS.Core.System.Dtos; using DS.WMS.Core.System.Entity; using DS.WMS.Core.System.Interface; using Mapster; using Microsoft.Extensions.DependencyInjection; using SqlSugar; namespace DS.WMS.Core.System.Method; /// /// /// public class PermissionService : IPermissionService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; /// /// /// /// public PermissionService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); } /// /// /// /// /// public DataResult> GetListByPage(PageRequest request) { //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); List Data = new List(); //查询一级菜单 var info = db.Queryable() .OrderBy(st => st.SortCode, OrderByType.Asc) .Where(st =>st.ParentId == "0" && st.MenuType == 1).ToList(); //循环下级权限 if (info.Count > 0) { foreach (var item in info) { var childs =db.Queryable().Where(x => x.MenuType == 2 && x.ParentId == item.Id) .ToList(); if (childs.Count >0) { foreach (var child in childs) { var buttons =db.Queryable().Where(x => x.MenuType == 3 && x.ParentId == child.Id) .ToList(); if (buttons.Count>0) child.children = buttons; } item.children = childs; } } // foreach (var item in info) // { // var childMember = GetPermissionByParent(item.Id, db); // if (childMember.Count > 0) // { // foreach (var child in childMember) // { // // } // item.children = childMember; // } // // Data.Add(item); // } } Data.AddRange(info); // Data = Data.Where().ToList(); // var data = db.Queryable() // .Where(a => // a.Deleted == false).Where(whereList).ToQueryPage(request.PageCondition); return DataResult>.Success(Data); } /// /// 获取权限详情 /// /// /// /// public DataResult GetPermissionInfo(string id) { var data = db.Queryable() .Where(a => a.Id == id) .First(); return DataResult.Success(data); } /// /// 编辑权限 /// /// /// /// public DataResult EditPermission(PermissionInput model) { if (model.Id.IsNullOrEmpty()) { var isExist = db.Queryable().Where(x => x.PermissionCode == model.PermissionCode).First(); if (isExist!=null) { return DataResult.Failed("权限已存在,唯一编码重复!"); } var data = model.Adapt(); db.Insertable(data).ExecuteCommand(); return DataResult.Successed("添加权限成功!"); } else { var info =db.Queryable().Where(x => x.Id == model.Id).First(); var data = model.Adapt(info); db.Updateable(data).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand(); // db.Updateable(info).IgnoreColumns(ignoreAllNullColumns:true).IgnoreColumns(u => new { u.AddBy,u.AddTime }).ExecuteCommand(); return DataResult.Successed("更新权限成功!"); } } public DataResult> GetPermissionTree() { var list = db.Queryable().Where(x => x.MenuType == 1) .Select(a => new PermissionTreeViewModel { Title = a.PermissionName, Value = a.Id, }) .ToList(); foreach (var item in list) { var childs =db.Queryable().Where(x => x.MenuType == 2 && x.ParentId == item.Value) .Select(a => new PermissionTree { Title = a.PermissionName, Value = a.Id, }).ToList(); item.Children = childs; } return DataResult>.Success("获取数据成功!", list); } /// /// 获取权限树列表 /// /// public DataResult> GetPermissionTreeList() { var list = db.Queryable().Where(x => x.MenuType == 1) .OrderBy(x=>x.SortCode) .Select(a => new PermissionTreeList { Title = a.PermissionName, Key = a.Id, Icon = a.Icon }) .ToList(); foreach (var item in list) { var childs =db.Queryable().Where(x => x.MenuType == 2 && x.ParentId == item.Key) .OrderBy(x=>x.SortCode) .Select(a => new PermissionTreeList { Title = a.PermissionName, Key = a.Id, Icon = a.Icon }).ToList(); item.Children = childs; foreach (var child in childs) { var buttons =db.Queryable().Where(x => x.MenuType == 3 && x.ParentId == child.Key) .OrderBy(x=>x.SortCode) .Select(a => new PermissionTreeList { Title = a.PermissionName, Key = a.Id, Icon = a.Icon }).ToList(); child.Children = buttons; } } return DataResult>.Success("获取数据成功!", list); } /// /// /// /// public DataResult> GetPermissionList() { List Data = new List(); //查询一级菜单 var info = db.Queryable() .OrderBy(st => st.SortCode, OrderByType.Asc) .Where(st => st.ParentId == "0" && st.MenuType == 1).ToList(); //循环下级权限 if (info.Count > 0) { foreach (var item in info) { var childMember = GetPermissionByParent(item.Id, db); if (childMember.Count > 0) { item.children = childMember; } Data.Add(item); } } return DataResult>.Success(Data); } /// /// /// /// /// /// public static List GetPermissionByParent(string ParentId, ISqlSugarClient db) { List Data = new List(); var parent = db.Queryable().InSingle(ParentId); //查询下级权限信息 var childs = db.Queryable().Where(x => x.ParentId == ParentId && x.MenuType == 2).ToList(); if (childs.Count > 0) { foreach (var child in childs) { var childMember = GetPermissionByParent(child.Id, db); // child.Parent = parent; if (childMember.Count > 0) { child.children = childMember; } Data.Add(child); } } return Data.OrderBy(m => m.SortCode).ToList(); } public static List GetButtonsByParent(string ParentId, ISqlSugarClient db) { List Data = new List(); var parent = db.Queryable().InSingle(ParentId); //查询下级权限信息 var childs = db.Queryable().Where(x => x.ParentId == ParentId && x.MenuType == 3).ToList(); if (childs.Count > 0) { foreach (var child in childs) { var childMember = GetButtonsByParent(child.Id, db); // child.Parent = parent; if (childMember.Count > 0) { child.children = childMember; } Data.Add(child); } } return Data.OrderBy(m => m.SortCode).ToList(); } /// /// 更新角色权限 /// /// /// public DataResult UpdateRolePermission(RolePermissionInput model) { var existPermissions = db.Queryable().Where(x => x.RoleId == model.RoleId && x.IsPermission == 1).Select(n=>n.PermissionId).ToArray(); IEnumerable delPermissions = existPermissions.AsQueryable().Except(model.PermissionIds); if (delPermissions.Count()>0) { db.Updateable() .SetColumns(it => it.IsPermission == 0)//SetColumns是可以叠加的 写2个就2个字段赋值 .Where(it => it.RoleId == model.RoleId && delPermissions.Contains(it.PermissionId)) .ExecuteCommand(); } IEnumerable addPermissions = model.PermissionIds.AsQueryable().Except(existPermissions); if (addPermissions.Count()>0) { var existNoPermissions = db.Queryable().Where(x => x.RoleId == model.RoleId && x.IsPermission == 0).Select(n=>n.PermissionId).ToArray(); IEnumerable updatePermissions = existNoPermissions.AsQueryable().Except(addPermissions); if (updatePermissions.Count()>0) { db.Updateable() .SetColumns(it => it.IsPermission == 1) .Where(it => it.RoleId == model.RoleId && updatePermissions.Contains(it.PermissionId)) .ExecuteCommand(); } IEnumerable addRolePermissions = addPermissions.AsQueryable().Except(existNoPermissions); foreach (var item in addRolePermissions) { var permission = new SysRolePermission { RoleId = model.RoleId, PermissionId = item, IsPermission = 1, }; db.Insertable(permission).ExecuteCommand(); } } return DataResult.Successed("更新成功!"); } }