|
|
using Common.Const;
|
|
|
using Common.Enum;
|
|
|
using Entity.Base;
|
|
|
using Entity.DbModel.System;
|
|
|
using Entity.Dto.Req;
|
|
|
using Entity.Dto.Resp;
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
using Mapster;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Repository.System;
|
|
|
using Service.Mgr;
|
|
|
using SqlSugar;
|
|
|
|
|
|
namespace Service.System.Roles
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 角色
|
|
|
/// </summary>
|
|
|
[Scope("SingleInstance")]
|
|
|
public class SysRoleService : BaseServices<SysRole>
|
|
|
{
|
|
|
private readonly SysMenuRepository _sysMenuRep;
|
|
|
private readonly SysRoleRepository _sysRoleRep;
|
|
|
private readonly SysRoleMenuMgr _sysRoleMenuMgr;
|
|
|
private readonly SysUserRoleMgr _sysUserRoleMgr;
|
|
|
|
|
|
public SysRoleService(
|
|
|
SysMenuRepository sysMenuRepository,
|
|
|
SysRoleRepository sysRoleRepository,
|
|
|
SysRoleMenuMgr sysRoleMenuService,
|
|
|
SysUserRoleMgr sysUserRoleService)
|
|
|
{
|
|
|
_sysMenuRep = sysMenuRepository;
|
|
|
_sysRoleRep = sysRoleRepository;
|
|
|
_sysRoleMenuMgr = sysRoleMenuService;
|
|
|
_sysUserRoleMgr = sysUserRoleService;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取角色分页列表 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<SqlSugarPagedList<SysRole>> Page(PageRoleReq input)
|
|
|
{
|
|
|
RefAsync<int> total = 0;
|
|
|
var items = await _sysRoleRep.QueryPageAsync(
|
|
|
entity => true, false, entity => true,
|
|
|
!string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name),
|
|
|
!string.IsNullOrEmpty(input.Code), u => u.Code.Contains(input.Code),
|
|
|
u => u.OrderNo, input.Page, input.PageSize, total
|
|
|
);
|
|
|
return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取角色列表 🔖
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public async Task<List<RoleResp>> GetList()
|
|
|
{
|
|
|
// 当前用户已拥有的角色集合
|
|
|
var roleIdList = UserManager.SuperAdmin ? new List<long>() : await _sysUserRoleMgr.GetUserRoleIdList(UserManager.UserId);
|
|
|
|
|
|
return await _sysRoleRep.QueryListByClauseAsync<RoleResp>(
|
|
|
!UserManager.SuperAdmin && !UserManager.SysAdmin,
|
|
|
u => u.CreateUserId == UserManager.UserId || roleIdList.Contains(u.Id),
|
|
|
u => u.OrderNo);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 增加角色 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<string> AddRole(AddRoleReq input)
|
|
|
{
|
|
|
string result = "";
|
|
|
if (await _sysRoleRep.QueryByClauseAsync(u => u.Name == input.Name && u.Code == input.Code) != null)
|
|
|
return result = "数据已存在";
|
|
|
await _sysRoleRep.InsertAsync(input.Adapt<SysRole>());
|
|
|
SysRole newRole = await _sysRoleRep.QueryByClauseAsync(u => u.Name == input.Name);
|
|
|
//var newRole = await _sysRoleRep.InsertReturnEntityAsync(input.Adapt<SysRole>());
|
|
|
input.Id = newRole.Id;
|
|
|
await UpdateRoleMenu(input);
|
|
|
SysRole sysRole = await _sysRoleRep.QueryByClauseAsync(u => u.Name == input.Name && u.Code == input.Code);
|
|
|
if (sysRole != null)
|
|
|
return result = "增加角色成功";
|
|
|
return result = "增加角色失败";
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 更新角色菜单权限
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
private async Task UpdateRoleMenu(AddRoleReq input)
|
|
|
{
|
|
|
if (input.MenuIdList == null || input.MenuIdList.Count < 1)
|
|
|
return;
|
|
|
|
|
|
// 将父节点为0的菜单排除,防止前端全选异常
|
|
|
var pMenuIds = await _sysMenuRep.QueryListByClauseAsync(u => input.MenuIdList.Contains(u.Id) && u.Pid == 0, u => u.Id);
|
|
|
var menuIds = input.MenuIdList.Except(pMenuIds); // 差集
|
|
|
await GrantMenu(new RoleMenuReq()
|
|
|
{
|
|
|
Id = input.Id,
|
|
|
MenuIdList = menuIds.ToList()
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 更新角色 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<string> UpdateRole(AddRoleReq input)
|
|
|
{
|
|
|
string result = "";
|
|
|
if ((await _sysRoleRep.QueryByClauseAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id)) != null)
|
|
|
return result = "数据已存在";
|
|
|
|
|
|
bool uodateResult = await _sysRoleRep.UpdateAsync(input.Adapt<SysRole>());
|
|
|
if (!uodateResult)
|
|
|
return result = "数据更新失败";
|
|
|
|
|
|
await _sysRoleRep.UpdateAsync(input.Adapt<SysRole>(), true, u => new { u.DataScope });
|
|
|
|
|
|
await UpdateRoleMenu(input);
|
|
|
return result = "数据更新成功";
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除角色 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<string> DeleteRole(DeleteRoleReq input)
|
|
|
{
|
|
|
string result = "";
|
|
|
// 禁止删除系统管理员角色
|
|
|
var sysRole = await _sysRoleRep.QueryByClauseAsync(u => u.Id == input.Id);
|
|
|
if (sysRole == null)
|
|
|
return result = "记录不存在";
|
|
|
if (sysRole != null)
|
|
|
{
|
|
|
if (sysRole.Code == CommonConst.SysAdminRole)
|
|
|
return result = "禁止删除系统管理员角色";
|
|
|
}
|
|
|
|
|
|
// 若角色有用户则禁止删除
|
|
|
var userIds = await _sysUserRoleMgr.GetUserIdList(input.Id);
|
|
|
if (userIds != null && userIds.Count > 0)
|
|
|
return result = "此角色下面存在账号禁止删除";
|
|
|
|
|
|
await _sysRoleRep.DeleteAsync(sysRole);
|
|
|
|
|
|
|
|
|
await _sysRoleRep.DeleteAsync(sysRole);
|
|
|
|
|
|
// 级联删除用户角色数据
|
|
|
await _sysUserRoleMgr.DeleteUserRoleByRoleId(sysRole.Id);
|
|
|
|
|
|
// 级联删除角色菜单数据
|
|
|
await _sysRoleMenuMgr.DeleteRoleMenuByRoleId(sysRole.Id);
|
|
|
return result = "删除角色成功";
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 授权角色菜单 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task GrantMenu(RoleMenuReq input)
|
|
|
{
|
|
|
await _sysRoleMenuMgr.GrantRoleMenu(input);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据角色Id获取菜单Id集合 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<List<long>> GetOwnMenuList([FromQuery] RoleReq input)
|
|
|
{
|
|
|
return await _sysRoleMenuMgr.GetRoleMenuIdList(new List<long> { input.Id });
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 设置角色状态 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<string> SetStatus(RoleReq input)
|
|
|
{
|
|
|
string result = "";
|
|
|
if (!Enum.IsDefined(typeof(StatusEnum), input.Status))
|
|
|
result = "字典状态错误";
|
|
|
int updateResult = await _sysRoleRep.UpdateAsync(
|
|
|
u => u.Status == input.Status,
|
|
|
u => u.Id == input.Id);
|
|
|
if (updateResult != 0)
|
|
|
result = "设置角色状态成功";
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
}
|