Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.Service/WxUserService.cs
glz 0a32754740 refactor: 完成系统基础架构重构与业务逻辑优化
本次提交包含多项核心变更:
1.  **用户体系重构**:将管理员用户状态从字符串改为整型枚举,移除微信用户冗余字段Points和GrowthPoints,新增通用基础实体主键配置
2.  **代码清理**:删除HealthController、WxUserMedal、WxUserBag等废弃文件,移除WeChatDto中冗余查询字段
3.  **响应格式统一**:重构BaseResponse与ResultCode枚举,标准化全局响应格式
4.  **权限与验证优化**:添加JWT认证与开发环境免认证逻辑,新增模型验证过滤器,统一控制器认证配置
5.  **工具与配置更新**:新增dotnet-tools.json配置EF工具,调整Swagger与压缩中间件配置,优化SqlSugar默认值处理逻辑
6.  **业务逻辑简化**:移除AutoMapper映射,改为手动映射DTO以提升性能,修复异常处理与响应返回逻辑
2026-06-02 17:58:10 +08:00

222 lines
7.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.IService.Dto;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Entity;
using QYZH.InteractiveMagazine.Repository;
using SqlSugar;
using System.Linq.Expressions;
namespace QYZH.InteractiveMagazine.Service;
public class WxUserService(BaseRepository<WxUser> wxUserRepository, ILogger<WxUserService> _logger) : BaseRepository<WxUser>, IWxUserService
{
public async Task<WxUserOutput> CreateAsync(WxUserInput input)
{
_logger.LogInformation("正在创建微信用户OpenId: {OpenId}", input.OpenId);
if (string.IsNullOrWhiteSpace(input.OpenId))
{
throw new BusinessException("OpenId不能为空", 400);
}
var existingUser = await wxUserRepository.GetFirstAsync(a => a.OpenId == input.OpenId);
if (existingUser != null)
{
_logger.LogWarning("创建微信用户失败OpenId已存在: {OpenId}", input.OpenId);
throw new BusinessException("OpenId已存在", 400);
}
var wxUser = new WxUser
{
OpenId = input.OpenId.Trim(),
UnionId = input.UnionId,
NickName = input.NickName,
AvatarUrl = input.AvatarUrl,
Phone = input.Phone,
Type = input.Type,
Status = input.Status,
Pwd = input.Pwd ?? string.Empty,
CreatedBy = "System",
UpdatedBy = "System",
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsDeleted = false
};
var result = await wxUserRepository.InsertAsync(wxUser);
if (!result)
{
_logger.LogError("微信用户创建失败OpenId: {OpenId}", input.OpenId);
throw new BusinessException("创建微信用户失败", 500);
}
_logger.LogInformation("微信用户创建成功OpenId: {OpenId}, ID: {Id}", input.OpenId, wxUser.Id);
return new WxUserOutput
{
Id = wxUser.Id,
OpenId = wxUser.OpenId,
UnionId = wxUser.UnionId,
NickName = wxUser.NickName,
AvatarUrl = wxUser.AvatarUrl,
Phone = wxUser.Phone,
Type = wxUser.Type,
Status = wxUser.Status,
CreatedBy = wxUser.CreatedBy,
CreatedAt = wxUser.CreatedAt,
UpdatedBy = wxUser.UpdatedBy,
UpdatedAt = wxUser.UpdatedAt
};
}
public async Task<WxUserOutput> UpdateAsync(long id, WxUserInput input)
{
_logger.LogInformation("正在更新微信用户ID: {Id}", id);
var wxUser = await wxUserRepository.GetByIdAsync(id);
if (wxUser == null)
{
_logger.LogWarning("未找到要更新的微信用户ID: {Id}", id);
throw new BusinessException("微信用户不存在", 404);
}
if (!string.IsNullOrWhiteSpace(input.OpenId) && input.OpenId != wxUser.OpenId)
{
var existingUser = await wxUserRepository.GetFirstAsync(a => a.OpenId == input.OpenId.Trim());
if (existingUser != null && existingUser.Id != id)
{
_logger.LogWarning("更新微信用户失败OpenId已存在: {OpenId}", input.OpenId);
throw new BusinessException("OpenId已存在", 400);
}
wxUser.OpenId = input.OpenId.Trim();
}
wxUser.UnionId = input.UnionId ?? wxUser.UnionId;
wxUser.NickName = input.NickName ?? wxUser.NickName;
wxUser.AvatarUrl = input.AvatarUrl ?? wxUser.AvatarUrl;
wxUser.Phone = input.Phone ?? wxUser.Phone;
wxUser.Type = input.Type ?? wxUser.Type;
wxUser.Status = input.Status ?? wxUser.Status;
wxUser.Pwd = input.Pwd ?? wxUser.Pwd;
wxUser.UpdatedBy = "System";
wxUser.UpdatedAt = DateTime.Now;
var result = await wxUserRepository.UpdateAsync(wxUser);
if (!result)
{
_logger.LogError("微信用户更新失败ID: {Id}", id);
throw new BusinessException("更新微信用户失败", 500);
}
_logger.LogInformation("微信用户更新成功ID: {Id}", id);
return new WxUserOutput
{
Id = wxUser.Id,
OpenId = wxUser.OpenId,
UnionId = wxUser.UnionId,
NickName = wxUser.NickName,
AvatarUrl = wxUser.AvatarUrl,
Phone = wxUser.Phone,
Type = wxUser.Type,
Status = wxUser.Status,
CreatedBy = wxUser.CreatedBy,
CreatedAt = wxUser.CreatedAt,
UpdatedBy = wxUser.UpdatedBy,
UpdatedAt = wxUser.UpdatedAt
};
}
public async Task DeleteAsync(long id)
{
_logger.LogInformation("正在删除微信用户ID: {Id}", id);
var wxUser = await wxUserRepository.GetByIdAsync(id);
if (wxUser == null)
{
_logger.LogWarning("未找到要删除的微信用户ID: {Id}", id);
throw new BusinessException("微信用户不存在", 404);
}
var result = await wxUserRepository.DeleteByIdAsync(id);
if (!result)
{
_logger.LogError("微信用户删除失败ID: {Id}", id);
throw new BusinessException("删除微信用户失败", 500);
}
_logger.LogInformation("微信用户删除成功ID: {Id}", id);
}
public async Task<WxUserOutput> GetByIdAsync(long id)
{
var wxUser = await wxUserRepository.GetByIdAsync(id);
if (wxUser == null)
{
throw new BusinessException("微信用户不存在", 404);
}
return new WxUserOutput
{
Id = wxUser.Id,
OpenId = wxUser.OpenId,
UnionId = wxUser.UnionId,
NickName = wxUser.NickName,
AvatarUrl = wxUser.AvatarUrl,
Phone = wxUser.Phone,
Type = wxUser.Type,
Status = wxUser.Status,
CreatedBy = wxUser.CreatedBy,
CreatedAt = wxUser.CreatedAt,
UpdatedBy = wxUser.UpdatedBy,
UpdatedAt = wxUser.UpdatedAt
};
}
public async Task<PageListModel<WxUserOutput>> GetListAsync(WxUserQueryInput input)
{
if (input.PageIndex <= 0)
{
throw new BusinessException("页码必须大于0", 400);
}
if (input.PageSize <= 0 || input.PageSize > 100)
{
throw new BusinessException("每页条数必须在1-100之间", 400);
}
RefAsync<int> totalNumber = 0;
var pageResult = await wxUserRepository.Queryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.NickName), a => a.NickName == input.NickName)
.OrderByDescending(a => a.CreatedAt)
.Select(wxUser => new WxUserOutput
{
Id = wxUser.Id,
OpenId = wxUser.OpenId,
UnionId = wxUser.UnionId,
NickName = wxUser.NickName,
AvatarUrl = wxUser.AvatarUrl,
Phone = wxUser.Phone,
Type = wxUser.Type,
Status = wxUser.Status,
CreatedBy = wxUser.CreatedBy,
CreatedAt = wxUser.CreatedAt,
UpdatedBy = wxUser.UpdatedBy,
UpdatedAt = wxUser.UpdatedAt
}, true)
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
return new PageListModel<WxUserOutput>(pageResult, input.PageIndex, input.PageSize, totalNumber);
}
}