using Common.Const;
using Common.Enum;
using Entity.Base;
using Entity.DbModel.System.SysBaseObject;
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
{
///
/// 角色
///
[Scope("SingleInstance")]
public class SysRoleService : BaseServices
{
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;
}
///
/// 获取角色分页列表 🔖
///
///
///
public async Task> Page(PageRoleReq input)
{
RefAsync 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);
}
///
/// 获取角色列表 🔖
///
///
public async Task> GetList()
{
// 当前用户已拥有的角色集合
var roleIdList = UserManager.SuperAdmin ? new List() : await _sysUserRoleMgr.GetUserRoleIdList(UserManager.UserId);
return await _sysRoleRep.QueryListByClauseAsync(
!UserManager.SuperAdmin && !UserManager.SysAdmin,
u => u.CreateUserId == UserManager.UserId || roleIdList.Contains(u.Id),
u => u.OrderNo);
}
///
/// 增加角色 🔖
///
///
///
public async Task 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 newRole = await _sysRoleRep.QueryByClauseAsync(u => u.Name == input.Name);
//var newRole = await _sysRoleRep.InsertReturnEntityAsync(input.Adapt());
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 = "增加角色失败";
}
///
/// 更新角色菜单权限
///
///
///
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()
});
}
///
/// 更新角色 🔖
///
///
///
public async Task 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());
if (!uodateResult)
return result = "数据更新失败";
await _sysRoleRep.UpdateAsync(input.Adapt(), true, u => new { u.DataScope });
await UpdateRoleMenu(input);
return result = "数据更新成功";
}
///
/// 删除角色 🔖
///
///
///
public async Task 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 = "删除角色成功";
}
///
/// 授权角色菜单 🔖
///
///
///
public async Task GrantMenu(RoleMenuReq input)
{
await _sysRoleMenuMgr.GrantRoleMenu(input);
}
///
/// 根据角色Id获取菜单Id集合 🔖
///
///
///
public async Task> GetOwnMenuList([FromQuery] RoleReq input)
{
return await _sysRoleMenuMgr.GetRoleMenuIdList(new List { input.Id });
}
///
/// 设置角色状态 🔖
///
///
///
public async Task 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;
}
}
}