You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

435 lines
16 KiB
C#

12 months ago
using DS.Module.Core;
using DS.Module.Core.Data;
12 months ago
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;
12 months ago
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Sys.Method;
12 months ago
/// <summary>
///
/// </summary>
public class PermissionService : IPermissionService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public PermissionService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
//数据映射
TypeAdapterConfig<SysPermission, SysPermissionTenant>
.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)
;
12 months ago
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<SysPermission>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
List<SysPermission> Data = new List<SysPermission>();
//查询一级菜单
var info = db.Queryable<SysPermission>()
.OrderBy(st => st.SortCode, OrderByType.Asc)
11 months ago
.Where(st => st.ParentId == 0 && st.MenuType == 1).ToList();
12 months ago
//循环下级权限
if (info.Count > 0)
{
foreach (var item in info)
{
var childs = db.Queryable<SysPermission>().Where(x => x.MenuType == 2 && x.ParentId == item.Id)
.ToList();
if (childs.Count > 0)
{
foreach (var child in childs)
{
var buttons = db.Queryable<SysPermission>().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<SysPermission>()
// .Where(a =>
// a.Deleted == false).Where(whereList).ToQueryPage(request.PageCondition);
return DataResult<List<SysPermission>>.Success(Data);
}
/// <summary>
/// 获取权限详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public DataResult<SysPermission> GetPermissionInfo(string id)
{
var data = db.Queryable<SysPermission>()
11 months ago
.Where(a => a.Id == long.Parse(id))
12 months ago
.First();
return DataResult<SysPermission>.Success(data);
}
/// <summary>
/// 编辑权限
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
11 months ago
public DataResult EditPermission(PermissionReq model)
12 months ago
{
11 months ago
if (model.Id == 0)
12 months ago
{
var isExist = db.Queryable<SysPermission>().Where(x => x.PermissionCode == model.PermissionCode).First();
if (isExist != null)
{
return DataResult.Failed("权限已存在,唯一编码重复!");
}
var data = model.Adapt<SysPermission>();
db.Insertable(data).ExecuteCommand();
return DataResult.Successed("添加权限成功!");
}
else
{
var info = db.Queryable<SysPermission>().Where(x => x.Id == model.Id).First();
var data = model.Adapt(info);
db.Updateable(data).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
11 months ago
// db.Updateable(info).IgnoreColumns(ignoreAllNullColumns:true).IgnoreColumns(u => new { u.AddBy,u.CreateTime }).ExecuteCommand();
12 months ago
return DataResult.Successed("更新权限成功!");
}
}
public DataResult<List<PermissionTreeViewModel>> GetPermissionTree()
{
var list = db.Queryable<SysPermission>().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<SysPermission>().Where(x => (x.MenuType == 1 || x.MenuType == 2) && x.ParentId == item.Value)
.Select(a => new PermissionTreeViewModel
12 months ago
{
Title = a.PermissionName,
Value = a.Id,
}).ToList();
foreach (var child in childs)
{
var buttons = db.Queryable<SysPermission>().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;
}
12 months ago
item.Children = childs;
}
return DataResult<List<PermissionTreeViewModel>>.Success("获取数据成功!", list);
}
/// <summary>
/// 获取权限树列表
/// </summary>
/// <returns></returns>
public DataResult<List<PermissionTreeList>> GetPermissionTreeList()
{
var list = db.Queryable<SysPermission>().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<SysPermission>().Where(x =>(x.MenuType == 1 || x.MenuType == 2) && x.ParentId == item.Key)
12 months ago
.OrderBy(x => x.SortCode)
.Select(a => new PermissionTreeList
{
Title = a.PermissionName,
Key = a.Id,
Icon = a.Icon
}).ToList();
12 months ago
foreach (var child in childs)
{
var buttons = db.Queryable<SysPermission>().Where(x => x.MenuType == 2 && x.ParentId == child.Key)
12 months ago
.OrderBy(x => x.SortCode)
.Select(a => new PermissionTreeList
{
Title = a.PermissionName,
Key = a.Id,
Icon = a.Icon
}).ToList();
child.Children = buttons;
}
item.Children = childs;
12 months ago
}
return DataResult<List<PermissionTreeList>>.Success("获取数据成功!", list);
}
/// <summary>
/// 更新租户权限信息
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult UpdateTenantPermissionInfo(IdModel req)
{
var perId = long.Parse(req.Id);
var info = db.Queryable<SysPermission>().Where(x => x.Id == perId).First();
var tenantPers = db.Queryable<SysPermissionTenant>().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("没有可更新的权限信息!");
}
}
12 months ago
/// <summary>
///
/// </summary>
/// <returns></returns>
public DataResult<List<SysPermission>> GetPermissionList()
{
List<SysPermission> Data = new List<SysPermission>();
//查询一级菜单
var info = db.Queryable<SysPermission>()
.OrderBy(st => st.SortCode, OrderByType.Asc)
11 months ago
.Where(st => st.ParentId == 0 && st.MenuType == 1).ToList();
12 months ago
//循环下级权限
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<List<SysPermission>>.Success(Data);
}
/// <summary>
///
/// </summary>
/// <param name="ParentId"></param>
/// <param name="db"></param>
/// <returns></returns>
11 months ago
public static List<SysPermission> GetPermissionByParent(long ParentId, ISqlSugarClient db)
12 months ago
{
List<SysPermission> Data = new List<SysPermission>();
var parent = db.Queryable<SysPermission>().InSingle(ParentId);
//查询下级权限信息
var childs = db.Queryable<SysPermission>().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();
}
11 months ago
public static List<SysPermission> GetButtonsByParent(long ParentId, ISqlSugarClient db)
12 months ago
{
List<SysPermission> Data = new List<SysPermission>();
var parent = db.Queryable<SysPermission>().InSingle(ParentId);
//查询下级权限信息
var childs = db.Queryable<SysPermission>().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();
}
/// <summary>
/// 更新角色权限
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DataResult UpdateRolePermission(RolePermissionInput model)
{
var existPermissions = db.Queryable<SysRolePermission>().Where(x => x.RoleId == model.RoleId && x.IsPermission == 1).Select(n => n.PermissionId).ToArray();
11 months ago
IEnumerable<long?> delPermissions = existPermissions.AsQueryable().Except(model.PermissionIds);
12 months ago
if (delPermissions.Count() > 0)
{
db.Updateable<SysRolePermission>()
.SetColumns(it => it.IsPermission == 0)//SetColumns是可以叠加的 写2个就2个字段赋值
.Where(it => it.RoleId == model.RoleId && delPermissions.Contains(it.PermissionId))
.ExecuteCommand();
}
11 months ago
IEnumerable<long?> addPermissions = model.PermissionIds.AsQueryable().Except(existPermissions);
12 months ago
if (addPermissions.Count() > 0)
{
var existNoPermissions = db.Queryable<SysRolePermission>().Where(x => x.RoleId == model.RoleId && x.IsPermission == 0).Select(n => n.PermissionId).ToArray();
11 months ago
IEnumerable<long?> updatePermissions = existNoPermissions.AsQueryable().Except(addPermissions);
12 months ago
if (updatePermissions.Count() > 0)
{
db.Updateable<SysRolePermission>()
.SetColumns(it => it.IsPermission == 1)
.Where(it => it.RoleId == model.RoleId && updatePermissions.Contains(it.PermissionId))
.ExecuteCommand();
}
11 months ago
IEnumerable<long?> addRolePermissions = addPermissions.AsQueryable().Except(existNoPermissions);
12 months ago
foreach (var item in addRolePermissions)
{
var permission = new SysRolePermission
{
RoleId = model.RoleId,
PermissionId = item,
IsPermission = 1,
};
db.Insertable(permission).ExecuteCommand();
}
}
return DataResult.Successed("更新成功!");
}
11 months ago
12 months ago
}