using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Sys.Method;
///
///
///
public class PermissionService : IPermissionService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
///
///
///
///
public PermissionService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
//数据映射
TypeAdapterConfig
.NewConfig()
.Map(dto => dto.ParentId, poco => poco.ParentId)
.Map(dto => dto.PermissionName, poco => poco.PermissionName)
.Map(dto => dto.PermissionEnName, poco => poco.PermissionEnName)
.Map(dto => dto.PermissionFrName, poco => poco.PermissionFrName)
.Map(dto => dto.PermissionRuName, poco => poco.PermissionRuName)
.Map(dto => dto.PermissionArName, poco => poco.PermissionArName)
.Map(dto => dto.PermissionEsName, poco => poco.PermissionEsName)
.Map(dto => dto.PermissionCode, poco => poco.PermissionCode)
.Map(dto => dto.Title, poco => poco.Title)
.Map(dto => dto.Icon, poco => poco.Icon)
.Map(dto => dto.Url, poco => poco.Url)
.Map(dto => dto.Redirect, poco => poco.Redirect)
.Map(dto => dto.Component, poco => poco.Component)
.Map(dto => dto.ComponentName, poco => poco.ComponentName)
.Map(dto => dto.MenuType, poco => poco.MenuType)
.Map(dto => dto.PermissionType, poco => poco.PermissionType)
.Map(dto => dto.OpenType, poco => poco.OpenType)
.Map(dto => dto.SortCode, poco => poco.SortCode)
.Map(dto => dto.IsPublic, poco => poco.IsPublic)
.Map(dto => dto.KeepAlive, poco => poco.KeepAlive)
.Map(dto => dto.IsHidden, poco => poco.IsHidden)
.Map(dto => dto.IsHideTab, poco => poco.IsHideTab)
.Map(dto => dto.IsAlwaysShow, poco => poco.IsAlwaysShow)
.Map(dto => dto.IsRoute, poco => poco.IsRoute)
.Map(dto => dto.IsCache, poco => poco.IsCache)
.Map(dto => dto.PermissionEntity, poco => poco.PermissionEntity)
.Map(dto => dto.ColumnView, poco => poco.ColumnView)
;
}
///
///
///
///
///
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 == long.Parse(id))
.First();
return DataResult.Success(data);
}
///
/// 编辑权限
///
///
///
///
public DataResult EditPermission(PermissionReq model)
{
if (model.Id == 0)
{
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.CreateTime }).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 == 1 || x.MenuType == 2) && x.ParentId == item.Value)
.Select(a => new PermissionTreeViewModel
{
Title = a.PermissionName,
Value = a.Id,
}).ToList();
foreach (var child in childs)
{
var buttons = db.Queryable().Where(x => x.MenuType == 2 && x.ParentId == child.Value)
.OrderBy(x => x.SortCode)
.Select(a => new PermissionTreeViewModel
{
Title = a.PermissionName,
Value = a.Id,
}).ToList();
child.Children = buttons;
}
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 == 1 || 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();
foreach (var child in childs)
{
var buttons = db.Queryable().Where(x => x.MenuType == 2 && 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;
}
item.Children = childs;
}
return DataResult>.Success("获取数据成功!", list);
}
///
/// 更新租户权限信息
///
///
///
public DataResult UpdateTenantPermissionInfo(IdModel req)
{
var perId = long.Parse(req.Id);
var info = db.Queryable().Where(x => x.Id == perId).First();
var tenantPers = db.Queryable().Filter(null, true).Where(x => x.PermissionId == perId).ToList();
if (tenantPers.Count>0)
{
foreach (var per in tenantPers)
{
per.ParentId = info.ParentId;
per.PermissionName = info.PermissionName;
per.PermissionEnName = info.PermissionEnName;
per.PermissionFrName = info.PermissionFrName;
per.PermissionRuName = info.PermissionRuName;
per.PermissionArName = info.PermissionArName;
per.PermissionEsName = info.PermissionEsName;
per.PermissionCode = info.PermissionCode;
per.Title = info.Title;
per.Icon = info.Icon;
per.Url = info.Url;
per.Redirect = info.Redirect;
per.Component = info.Component;
per.ComponentName = info.ComponentName;
per.MenuType = info.MenuType;
per.PermissionType = info.PermissionType;
per.OpenType = info.OpenType;
per.SortCode = info.SortCode;
per.IsPublic = info.IsPublic;
per.KeepAlive = info.KeepAlive;
per.IsHidden = info.IsHidden;
per.IsHideTab = info.IsHideTab;
per.IsAlwaysShow = info.IsAlwaysShow;
per.IsRoute = info.IsRoute;
per.IsCache = info.IsCache;
per.PermissionEntity = info.PermissionEntity;
per.ColumnView = info.ColumnView;
per.Note = info.Note;
}
db.Updateable(tenantPers).IgnoreColumns(ignoreAllNullColumns: true).IgnoreColumns(it => new
{
it.PermissionId,
it.CreateBy,
it.CreateTime,
it.TenantId
}).ExecuteCommand();
return DataResult.Successed("更新权限成功!");
}
else {
return DataResult.Failed("没有可更新的权限信息!");
}
}
///
///
///
///
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(long 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(long 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("更新成功!");
}
}