297 lines
12 KiB
C#
297 lines
12 KiB
C#
using AutoMapper;
|
||
using LMS.Common.Extensions;
|
||
using LMS.DAO;
|
||
using LMS.Repository.DTO;
|
||
using LMS.Repository.DTO.UserDto;
|
||
using LMS.Repository.Models.DB;
|
||
using LMS.Repository.Role;
|
||
using Microsoft.AspNetCore.Identity;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||
|
||
namespace LMS.service.Service.RoleService
|
||
{
|
||
public class RoleService(RoleManager<Role> roleManager, ApplicationDbContext dbContext, UserManager<User> userManager, IMapper mapper)
|
||
{
|
||
private readonly RoleManager<Role> _roleManager = roleManager;
|
||
private readonly ApplicationDbContext _context = dbContext;
|
||
private readonly UserManager<User> _userManager = userManager;
|
||
private readonly IMapper _mapper = mapper;
|
||
|
||
/// <summary>
|
||
/// 创建角色数据
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
public async Task<ActionResult<APIResponseModel<string>>> AddRole(RoleModel model, long userId)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(model.Name))
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "角色名不能为空");
|
||
}
|
||
|
||
if (await _roleManager.RoleExistsAsync(model.Name))
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.RoleNameExist);
|
||
}
|
||
|
||
await _roleManager.CreateAsync(new Role
|
||
{
|
||
Name = model.Name,
|
||
CreatedUserId = userId,
|
||
UpdatedUserId = userId,
|
||
CreatedTime = BeijingTimeExtension.GetBeijingTime(),
|
||
UpdatedTime = BeijingTimeExtension.GetBeijingTime(),
|
||
Remark = model.Remark
|
||
});
|
||
return APIResponseModel<string>.CreateSuccessResponseModel("添加角色成功");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 修改角色数据
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <param name="model"></param>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
internal async Task<ActionResult<APIResponseModel<string>>> UpdateRole(long id, RoleModel model, long userId)
|
||
{
|
||
try
|
||
{
|
||
Role? role = _roleManager.FindByIdAsync(id.ToString()).Result;
|
||
if (role == null)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindRoleByIdFail);
|
||
}
|
||
|
||
// 判断code是不是存在,不报错自己
|
||
if (await _roleManager.Roles.FirstOrDefaultAsync(x => x.Name == model.Name && x.Id != id) != null)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.RoleNameExist);
|
||
}
|
||
|
||
// 修改
|
||
role.Name = model.Name;
|
||
role.Remark = model.Remark;
|
||
role.UpdatedUserId = userId;
|
||
role.UpdatedTime = BeijingTimeExtension.GetBeijingTime();
|
||
await _roleManager.UpdateAsync(role);
|
||
return APIResponseModel<string>.CreateSuccessResponseModel("修改角色成功");
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除角色数据
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
internal async Task<ActionResult<APIResponseModel<string>>> DeleteRole(long id)
|
||
{
|
||
try
|
||
{
|
||
Role? role = await _roleManager.FindByIdAsync(id.ToString());
|
||
if (role == null)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindRoleByIdFail);
|
||
}
|
||
if (await _context.UserRoles.AnyAsync(x => x.RoleId == id))
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.RoleHasUser);
|
||
}
|
||
|
||
await _roleManager.DeleteAsync(role);
|
||
return APIResponseModel<string>.CreateSuccessResponseModel("删除角色成功");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 查询角色数据,返回集合
|
||
/// </summary>
|
||
/// <param name="page"></param>
|
||
/// <param name="pageSize"></param>
|
||
/// <param name="roleName"></param>
|
||
/// <param name="roleId"></param>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
internal async Task<ActionResult<APIResponseModel<CollectionResponse<RoleDto>>>> QueryRoleCollection(int page, int pageSize, string? roleName, long? roleId, long requestUserId)
|
||
{
|
||
roleName = roleName == "null" ? null : roleName;
|
||
roleId = roleId == null ? null : roleId;
|
||
try
|
||
{
|
||
// 判断当前用户的角色
|
||
User? user = await _userManager.FindByIdAsync(requestUserId.ToString());
|
||
if (user == null)
|
||
{
|
||
return APIResponseModel<CollectionResponse<RoleDto>>.CreateErrorResponseModel(ResponseCode.FindUserByIdFail);
|
||
}
|
||
|
||
// TODO
|
||
//if (!await _userManager.IsInRoleAsync(user, "Super Admin"))
|
||
//{
|
||
// return APIResponseModel<CollectionResponse<RoleDto>>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||
//}
|
||
|
||
// 查询数据,判断是不是传入了roleName和roleId,如果传入了就查询,如果没有就查询全部
|
||
IQueryable<Role> rolesQueryable = _roleManager.Roles;
|
||
if (!string.IsNullOrWhiteSpace(roleName))
|
||
{
|
||
rolesQueryable = rolesQueryable.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.Contains(roleName));
|
||
}
|
||
|
||
if (roleId != null)
|
||
{
|
||
rolesQueryable = rolesQueryable.Where(x => x.Id == roleId);
|
||
}
|
||
// 将插叙出来的数据通过ID降序排序
|
||
rolesQueryable = rolesQueryable.OrderByDescending(x => x.Id);
|
||
|
||
// 查询总数
|
||
int total = await rolesQueryable.CountAsync();
|
||
|
||
// 分页
|
||
rolesQueryable = rolesQueryable.Skip((page - 1) * pageSize).Take(pageSize);
|
||
|
||
|
||
List<Role> roles = await rolesQueryable.ToListAsync();
|
||
|
||
List<RoleDto> roleDtos = roles.Select(x => new RoleDto
|
||
{
|
||
Id = x.Id,
|
||
Name = x.Name ?? string.Empty,
|
||
CreatedTime = x.CreatedTime,
|
||
UpdatedTime = x.UpdatedTime,
|
||
Remark = x.Remark ?? string.Empty,
|
||
}).ToList();
|
||
|
||
for (int i = 0; i < roleDtos.Count; i++)
|
||
{
|
||
long createdUserId = roles[i].CreatedUserId;
|
||
long updatedUserId = roles[i].UpdatedUserId;
|
||
User? createdUser = await _userManager.FindByIdAsync(createdUserId.ToString());
|
||
User? updatedUser = await _userManager.FindByIdAsync(updatedUserId.ToString());
|
||
roleDtos[i].CreatedUser = _mapper.Map<UserBaseDto>(createdUser);
|
||
roleDtos[i].UpdeatedUser = _mapper.Map<UserBaseDto>(updatedUser);
|
||
}
|
||
|
||
CollectionResponse<RoleDto> collectionResponse = new()
|
||
{
|
||
Total = total,
|
||
Collection = roleDtos,
|
||
Current = page,
|
||
};
|
||
|
||
return APIResponseModel<CollectionResponse<RoleDto>>.CreateSuccessResponseModel(collectionResponse);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return APIResponseModel<CollectionResponse<RoleDto>>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 查询角色数据,通过角色ID
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
public async Task<ActionResult<APIResponseModel<RoleDto>>> QueryRoleById(long id, long userId)
|
||
{
|
||
try
|
||
{
|
||
User? user = await _userManager.FindByIdAsync(userId.ToString());
|
||
if (user == null)
|
||
{
|
||
return APIResponseModel<RoleDto>.CreateErrorResponseModel(ResponseCode.FindUserByIdFail);
|
||
}
|
||
|
||
// 判断权限
|
||
|
||
// 开始查询
|
||
Role? role = await _roleManager.FindByIdAsync(id.ToString());
|
||
if (role == null)
|
||
{
|
||
return APIResponseModel<RoleDto>.CreateErrorResponseModel(ResponseCode.FindRoleByIdFail);
|
||
}
|
||
RoleDto roleDto = new()
|
||
{
|
||
Id = role.Id,
|
||
Name = role.Name ?? string.Empty,
|
||
CreatedTime = role.CreatedTime,
|
||
UpdatedTime = role.UpdatedTime,
|
||
Remark = role.Remark ?? string.Empty
|
||
};
|
||
|
||
long createdUserId = role.CreatedUserId;
|
||
long updatedUserId = role.UpdatedUserId;
|
||
User? createdUser = await _userManager.FindByIdAsync(createdUserId.ToString());
|
||
User? updatedUser = await _userManager.FindByIdAsync(updatedUserId.ToString());
|
||
roleDto.CreatedUser = _mapper.Map<UserDto>(createdUser);
|
||
roleDto.UpdeatedUser = _mapper.Map<UserDto>(updatedUser);
|
||
|
||
// 判断是不是超级管理员
|
||
if (roleDto.UpdeatedUser != null && roleDto.CreatedUser != null && !await _userManager.IsInRoleAsync(user, "Super Admin"))
|
||
{
|
||
roleDto.UpdeatedUser.PhoneNumber = "***********";
|
||
roleDto.CreatedUser.PhoneNumber = "***********";
|
||
roleDto.UpdeatedUser.Email = "***********";
|
||
roleDto.CreatedUser.Email = "***********";
|
||
}
|
||
return APIResponseModel<RoleDto>.CreateSuccessResponseModel(roleDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return APIResponseModel<RoleDto>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询角色数据,返回下拉框数据,只要Name
|
||
/// </summary>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public async Task<ActionResult<APIResponseModel<List<string>>>> QueryRoleOption(long userId)
|
||
{
|
||
try
|
||
{
|
||
User? user = await _userManager.FindByIdAsync(userId.ToString());
|
||
if (user == null)
|
||
{
|
||
return APIResponseModel<List<string>>.CreateErrorResponseModel(ResponseCode.FindUserByIdFail);
|
||
}
|
||
|
||
// 开始查询
|
||
List<string> roles = await _roleManager.Roles.Where(x => !string.IsNullOrWhiteSpace(x.Name)).Select(x => x.Name ?? string.Empty).ToListAsync();
|
||
return APIResponseModel<List<string>>.CreateSuccessResponseModel(roles);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return APIResponseModel<List<string>>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|