Files
WCS/Cowain.Base/Services/UserRoleService.cs
2026-03-02 09:08:20 +08:00

134 lines
4.8 KiB
C#

using Cowain.Base.DBContext;
using Cowain.Base.IServices;
using Cowain.Base.Models;
using Cowain.Base.Models.Admins;
using Cowain.Base.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cowain.Base.Services;
public class UserRoleService : BaseService, IUserRoleService
{
public UserRoleService(IDbContextFactory<SqlDbContext> dbContextFactory) : base(dbContextFactory)
{
}
public async Task<ResultModel> AddUserRoleAsync(UserRoleViewModel? userRole)
{
if (userRole == null)
{
return ResultModel.Error("UserRole cannot be null");
}
if (string.IsNullOrEmpty(userRole.RoleName))
{
return ResultModel.Error("UserRoleName cannot be null");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<UserRoleDto>();
var existingRole = await DbSet.FirstOrDefaultAsync(x => x.RoleName == userRole.RoleName);
if (existingRole != null)
{
return ResultModel.Error("UserRoleName already exists");
}
var entity = new UserRoleDto
{
Id = userRole.RoleId,
RoleName = userRole.RoleName,
IsValid = userRole.IsValid
};
await DbSet.AddAsync(entity);
await dbContext.SaveChangesAsync();
return ResultModel.Success("UserRole added successfully");
}
public async Task<ResultModel> EditUserRoleAsync(UserRoleViewModel? userRole)
{
if (userRole == null)
{
return ResultModel.Error("UserRole cannot be null");
}
if (string.IsNullOrEmpty(userRole.RoleName))
{
return ResultModel.Error("UserRoleName cannot be null");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<UserRoleDto>();
var existingRole = await DbSet.FirstOrDefaultAsync(x => x.Id == userRole.RoleId);
if (existingRole == null)
{
return ResultModel.Error("UserRoleName no exists");
}
var duplicateRole = await DbSet.FirstOrDefaultAsync(x => x.RoleName == userRole.RoleName && x.Id != userRole.RoleId);
if (duplicateRole != null)
{
return ResultModel.Error("UserRoleName already exists");
}
existingRole.IsValid = userRole.IsValid;
existingRole.RoleName = userRole.RoleName;
existingRole.UpdateTime = DateTime.Now;
await dbContext.SaveChangesAsync();
return ResultModel.Success("UserRole updated successfully");
}
public async Task<ResultModel> DelUserRoleAsync(UserRoleViewModel? userRole)
{
if (userRole == null)
{
return ResultModel.Error("UserRole cannot be null");
}
if (string.IsNullOrEmpty(userRole.RoleName))
{
return ResultModel.Error("UserRoleName cannot be null");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<UserRoleDto>();
var entity = await DbSet.FirstOrDefaultAsync(x => x.Id == userRole.RoleId);
if (entity == null)
{
return ResultModel.Error("UserRole not found");
}
// 删除UserRole还要将UserRoleMenu表中RoleId相同的项也同时删除
var userRoleMenuDbSet = dbContext.GetDbSet<UserRoleMenuDto>();
var userRoleMenus = await userRoleMenuDbSet.Where(x => x.RoleId == userRole.RoleId).ToListAsync();
userRoleMenuDbSet.RemoveRange(userRoleMenus);
DbSet.Remove(entity);
await dbContext.SaveChangesAsync();
return ResultModel.Success("UserRole and related UserRoleMenus deleted successfully");
}
public async Task<List<UserRoleViewModel>> GetAllAsync()
{
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<UserRoleDto>();
var data = await DbSet.ToListAsync();
return new List<UserRoleViewModel>(data.Select(x => new UserRoleViewModel
{
RoleId = x.Id,
RoleName = x.RoleName,
IsValid = x.IsValid
}));
}
public async Task<(List<UserRoleViewModel>, int totals)> GetAllAsync(int pageIndex, int pageSize)
{
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<UserRoleDto>();
var data = await DbSet.ToListAsync();
var list = data.Skip((pageIndex - 1) * pageSize).Take(pageSize);
return (new List<UserRoleViewModel>(list.Select(x => new UserRoleViewModel
{
RoleId = x.Id,
RoleName = x.RoleName,
IsValid = x.IsValid
})), data.Count());
}
}