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.

327 lines
12 KiB
C#

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;
/// <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>();
}
/// <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)
.Where(st =>st.ParentId == "0" && st.MenuType == 1).ToList();
//循环下级权限
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>()
.Where(a => a.Id == id)
.First();
return DataResult<SysPermission>.Success(data);
}
/// <summary>
/// 编辑权限
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public DataResult EditPermission(PermissionInput model)
{
if (model.Id.IsNullOrEmpty())
{
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();
// db.Updateable(info).IgnoreColumns(ignoreAllNullColumns:true).IgnoreColumns(u => new { u.AddBy,u.AddTime }).ExecuteCommand();
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 == 2 && x.ParentId == item.Value)
.Select(a => new PermissionTree
{
Title = a.PermissionName,
Value = a.Id,
}).ToList();
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 == 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<SysPermission>().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<List<PermissionTreeList>>.Success("获取数据成功!", list);
}
/// <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)
.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<List<SysPermission>>.Success(Data);
}
/// <summary>
///
/// </summary>
/// <param name="ParentId"></param>
/// <param name="db"></param>
/// <returns></returns>
public static List<SysPermission> GetPermissionByParent(string ParentId, ISqlSugarClient db)
{
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();
}
public static List<SysPermission> GetButtonsByParent(string ParentId, ISqlSugarClient db)
{
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();
IEnumerable<string> delPermissions = existPermissions.AsQueryable().Except(model.PermissionIds);
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();
}
IEnumerable<string> addPermissions = model.PermissionIds.AsQueryable().Except(existPermissions);
if (addPermissions.Count()>0)
{
var existNoPermissions = db.Queryable<SysRolePermission>().Where(x => x.RoleId == model.RoleId && x.IsPermission == 0).Select(n=>n.PermissionId).ToArray();
IEnumerable<string> updatePermissions = existNoPermissions.AsQueryable().Except(addPermissions);
if (updatePermissions.Count()>0)
{
db.Updateable<SysRolePermission>()
.SetColumns(it => it.IsPermission == 1)
.Where(it => it.RoleId == model.RoleId && updatePermissions.Contains(it.PermissionId))
.ExecuteCommand();
}
IEnumerable<string> 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("更新成功!");
}
}