2026-06-01 17:59:23 +08:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
using BCrypt.Net;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.IService;
|
2026-06-05 17:57:24 +08:00
|
|
|
|
|
2026-06-01 17:59:23 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.Models.Common;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Entity;
|
2026-06-08 16:57:44 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.Models.Enum;
|
2026-06-01 17:59:23 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.Repository;
|
2026-06-02 14:10:43 +08:00
|
|
|
|
using SqlSugar;
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
namespace QYZH.InteractiveMagazine.Service;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 管理员用户服务实现
|
|
|
|
|
|
/// </summary>
|
2026-06-02 14:10:43 +08:00
|
|
|
|
public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILogger<AdminUserService> logger) : BaseRepository<AdminUser>, IAdminUserService
|
2026-06-01 17:59:23 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建管理员
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<AdminUserOutput> CreateAsync(AdminUserInput input)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("正在创建管理员,用户名: {UserName}", input.UserName);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.UserName))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BusinessException("用户名不能为空", 400);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.Password))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BusinessException("密码不能为空", 400);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查用户名是否已存在
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var existingUser = await adminUserRepository.GetFirstAsync(a => a.UserName == input.UserName);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (existingUser != null)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogWarning("创建管理员失败,用户名已存在: {UserName}", input.UserName);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("用户名已存在", 400);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var adminUser = new AdminUser
|
|
|
|
|
|
{
|
|
|
|
|
|
UserName = input.UserName.Trim(),
|
|
|
|
|
|
PasswordHash = BCrypt.Net.BCrypt.HashPassword(input.Password),
|
|
|
|
|
|
Type = input.Type,
|
|
|
|
|
|
Status = input.Status,
|
|
|
|
|
|
CreatedBy = "System",
|
|
|
|
|
|
UpdatedBy = "System",
|
|
|
|
|
|
CreatedAt = DateTime.Now,
|
|
|
|
|
|
UpdatedAt = DateTime.Now,
|
|
|
|
|
|
IsDeleted = false
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var result = await adminUserRepository.InsertAsync(adminUser);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogError("管理员创建失败,用户名: {UserName}", input.UserName);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("创建管理员失败", 500);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("管理员创建成功,用户名: {UserName}, ID: {Id}", input.UserName, adminUser.Id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
2026-06-08 16:57:44 +08:00
|
|
|
|
return new AdminUserOutput { Id = adminUser.Id, UserName = adminUser.UserName, Type = adminUser.Type.ToString(), Status = adminUser.Status, CreatedBy = adminUser.CreatedBy, CreatedAt = adminUser.CreatedAt, UpdatedBy = adminUser.UpdatedBy, UpdatedAt = adminUser.UpdatedAt };
|
2026-06-01 17:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新管理员
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<AdminUserOutput> UpdateAsync(long id, AdminUserInput input)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("正在更新管理员,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var adminUser = await adminUserRepository.GetByIdAsync(id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (adminUser == null)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogWarning("未找到要更新的管理员,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("管理员不存在", 404);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果用户名有变更,检查是否与其他用户重复
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(input.UserName) && input.UserName != adminUser.UserName)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var existingUser = await adminUserRepository.GetFirstAsync(a => a.UserName == input.UserName.Trim());
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (existingUser != null && existingUser.Id != id)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogWarning("更新管理员失败,用户名已存在: {UserName}", input.UserName);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("用户名已存在", 400);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
adminUser.UserName = input.UserName.Trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果提供了密码,则更新密码
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(input.Password))
|
|
|
|
|
|
{
|
|
|
|
|
|
adminUser.PasswordHash = BCrypt.Net.BCrypt.HashPassword(input.Password);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 16:57:44 +08:00
|
|
|
|
adminUser.Type = input.Type;
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
adminUser.UpdatedBy = "System";
|
|
|
|
|
|
adminUser.UpdatedAt = DateTime.Now;
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var result = await adminUserRepository.UpdateAsync(adminUser);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogError("管理员更新失败,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("更新管理员失败", 500);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("管理员更新成功,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
2026-06-08 16:57:44 +08:00
|
|
|
|
return new AdminUserOutput { Id = adminUser.Id, UserName = adminUser.UserName, Type = adminUser.Type.ToString(), Status = adminUser.Status, CreatedBy = adminUser.CreatedBy, CreatedAt = adminUser.CreatedAt, UpdatedBy = adminUser.UpdatedBy, UpdatedAt = adminUser.UpdatedAt };
|
2026-06-01 17:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除管理员(软删除)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task DeleteAsync(long id)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("正在删除管理员,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var adminUser = await adminUserRepository.GetByIdAsync(id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (adminUser == null)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogWarning("未找到要删除的管理员,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("管理员不存在", 404);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var result = await adminUserRepository.DeleteByIdAsync(id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogError("管理员删除失败,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("删除管理员失败", 500);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("管理员删除成功,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID获取管理员
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<AdminUserOutput> GetByIdAsync(long id)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("正在获取管理员信息,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
var adminUser = await adminUserRepository.GetByIdAsync(id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
if (adminUser == null)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogWarning("未找到管理员,ID: {Id}", id);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
throw new BusinessException("管理员不存在", 404);
|
|
|
|
|
|
}
|
2026-06-02 17:58:10 +08:00
|
|
|
|
return new AdminUserOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = adminUser.Id,
|
|
|
|
|
|
UserName = adminUser.UserName,
|
2026-06-08 16:57:44 +08:00
|
|
|
|
Type = adminUser.Type.ToString(),
|
2026-06-02 17:58:10 +08:00
|
|
|
|
Status = adminUser.Status,
|
|
|
|
|
|
CreatedBy = adminUser.CreatedBy,
|
|
|
|
|
|
CreatedAt = adminUser.CreatedAt,
|
|
|
|
|
|
UpdatedBy = adminUser.UpdatedBy,
|
|
|
|
|
|
UpdatedAt = adminUser.UpdatedAt
|
|
|
|
|
|
};
|
2026-06-01 17:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分页查询管理员列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<PageListModel<AdminUserOutput>> GetListAsync(AdminUserQueryInput input)
|
|
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
logger.LogInformation("正在查询管理员列表,页码: {PageIndex}, 每页条数: {PageSize}", input.PageIndex, input.PageSize);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (input.PageIndex <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BusinessException("页码必须大于0", 400);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (input.PageSize <= 0 || input.PageSize > 100)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BusinessException("每页条数必须在1-100之间", 400);
|
|
|
|
|
|
}
|
2026-06-02 14:10:43 +08:00
|
|
|
|
RefAsync<int> totalNumber = 0;
|
|
|
|
|
|
var pageResult = await adminUserRepository.Queryable()
|
|
|
|
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.UserName), a => a.UserName == input.UserName)
|
|
|
|
|
|
.OrderByDescending(a => a.CreatedAt)
|
2026-06-02 17:58:10 +08:00
|
|
|
|
.Select(a => new AdminUserOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = a.Id,
|
|
|
|
|
|
UserName = a.UserName,
|
2026-06-08 16:57:44 +08:00
|
|
|
|
Type = a.Type.ToString(),
|
2026-06-02 17:58:10 +08:00
|
|
|
|
Status = a.Status,
|
|
|
|
|
|
CreatedBy = a.CreatedBy,
|
|
|
|
|
|
CreatedAt = a.CreatedAt,
|
|
|
|
|
|
UpdatedBy = a.UpdatedBy,
|
|
|
|
|
|
UpdatedAt = a.UpdatedAt
|
|
|
|
|
|
}, true)
|
2026-06-02 14:10:43 +08:00
|
|
|
|
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
|
|
|
|
|
return new PageListModel<AdminUserOutput>(pageResult, input.PageIndex, input.PageSize, totalNumber);
|
2026-06-01 17:59:23 +08:00
|
|
|
|
}
|
2026-06-09 11:41:28 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 切换管理员状态(激活/冻结)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task ToggleStatusAsync(long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在切换管理员状态,ID: {Id}", id);
|
|
|
|
|
|
|
|
|
|
|
|
var adminUser = await adminUserRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (adminUser == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("未找到要更新状态的管理员,ID: {Id}", id);
|
|
|
|
|
|
throw new BusinessException("管理员不存在", 404);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
adminUser.Status = adminUser.Status==(int)AdminUserStatusEnum.Active?(int)AdminUserStatusEnum.Inactive:(int)AdminUserStatusEnum.Active;
|
|
|
|
|
|
adminUser.UpdatedBy = "System";
|
|
|
|
|
|
adminUser.UpdatedAt = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
|
|
var result = await adminUserRepository.UpdateAsync(adminUser);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("管理员状态更新失败,ID: {Id}", id);
|
|
|
|
|
|
throw new BusinessException("更新管理员状态失败", 500);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("管理员状态更新成功,ID: {Id}", id);
|
|
|
|
|
|
}
|
2026-06-01 17:59:23 +08:00
|
|
|
|
}
|