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.

191 lines
7.2 KiB
C#

2 years ago
using DS.Module.Core;
using DS.Module.Core.Extensions;
2 years ago
using DS.Module.UserModule;
2 years ago
using DS.WMS.Common.Enum;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
2 years ago
using Mapster;
2 years ago
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.System.Method;
/// <summary>
///
/// </summary>
public class SysRoleService:ISysRoleService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public SysRoleService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<SysRole>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysRole>()
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DataResult EditRole(RoleInput model)
{
if (model.Id.IsNullOrEmpty())
{
var isExist = db.Queryable<SysRole>().Where(x => x.RoleCode == model.RoleCode).First();
if (isExist!=null)
{
return DataResult.Failed("角色唯一编码已存在!");
}
2 years ago
var data = model.Adapt<SysRole>();
2 years ago
db.Insertable(data).ExecuteCommand();
return DataResult.Successed("添加成功!");
}
else
{
var info =db.Queryable<SysRole>().Where(x => x.Id == model.Id).First();
2 years ago
info = model.Adapt(info);
2 years ago
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand();
return DataResult.Successed("更新成功!");
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<SysRole> GetRoleInfo(string id)
{
var data = db.Queryable<SysRole>()
.Where(a => a.Id == id)
.First();
return DataResult<SysRole>.Success(data);
}
/// <summary>
/// 获取角色权限树
/// </summary>
/// <returns></returns>
public DataResult<RolePermissionTreeViewModel> GetRolePermissionTree()
{
var data = new RolePermissionTreeViewModel();
//查询一级菜单
var info = db.Queryable<SysPermission>().Filter(null, true)
.OrderBy(st => st.SortCode, OrderByType.Asc)
.Where(st =>st.ParentId == "0" && st.MenuType == 1 )//&& (st.PermissionType == (int)PermissionType.Public || st.PermissionType == user.PermissionIdentity )
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
Key = a.Id,
}).ToList();
//循环下级权限
if (info.Count > 0)
{
foreach (var item in info)
{
var childs =db.Queryable<SysPermission>().Filter(null, true).Where(x => x.MenuType == 2 && x.ParentId == item.Key )
//&& (x.PermissionType == (int)PermissionType.Public || x.PermissionType == user.PermissionIdentity )
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
Key = a.Id,
})
.ToList();
if (childs.Count >0)
{
foreach (var child in childs)
{
var buttons =db.Queryable<SysPermission>().Filter(null, true).Where(x => x.MenuType == 3 && x.ParentId == child.Key)
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
Key = a.Id,
})
.ToList();
if (buttons.Count>0)
child.Children = buttons;
}
item.Children = childs;
}
}
}
data.TreeData = info;
data.Ids = db.Queryable<SysPermission>().Select(x => x.Id).ToArray();
return DataResult<RolePermissionTreeViewModel>.Success("获取数据成功!", data);
}
/// <summary>
/// 获取角色权限
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<string[]> GetRolePermission(string id)
{
var data = db.Queryable<SysRolePermission>().Where(x => x.RoleId == id && x.IsPermission == 1).Select(x => x.PermissionId).ToArray();
return DataResult<string[]>.Success("获取数据成功!", data);
}
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("更新成功!");
}
}