- 替换原有自定义生命周期接口为通用基础服务体系 - 将所有实体基类替换为带雪花ID的SqlSugarBaseEntity - 迁移DTO到Models项目并统一管理 - 移除冗余的Repository层实现,改用通用基础服务 - 添加Yitter.IdGenerator雪花ID生成支持 - 重构SqlSugar数据库上下文与依赖注入配置 - 新增微信用户、用户勋章、背包等实体与配套服务 - 整理分页查询与结果封装类 - 优化WebApi控制器结构
192 lines
6.8 KiB
C#
192 lines
6.8 KiB
C#
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,
|
||
Points = input.Points,
|
||
Type = input.Type,
|
||
Status = input.Status,
|
||
Pwd = input.Pwd ?? string.Empty,
|
||
GrowthPoints = input.GrowthPoints,
|
||
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 MapToOutput(wxUser);
|
||
}
|
||
|
||
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.Points = input.Points;
|
||
wxUser.Type = input.Type ?? wxUser.Type;
|
||
wxUser.Status = input.Status ?? wxUser.Status;
|
||
wxUser.Pwd = input.Pwd ?? wxUser.Pwd;
|
||
wxUser.GrowthPoints = input.GrowthPoints;
|
||
|
||
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 MapToOutput(wxUser);
|
||
}
|
||
|
||
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)
|
||
{
|
||
_logger.LogInformation("正在获取微信用户信息,ID: {Id}", id);
|
||
|
||
var wxUser = await wxUserRepository.GetByIdAsync(id);
|
||
if (wxUser == null)
|
||
{
|
||
_logger.LogWarning("未找到微信用户,ID: {Id}", id);
|
||
throw new BusinessException("微信用户不存在", 404);
|
||
}
|
||
|
||
return MapToOutput(wxUser);
|
||
}
|
||
|
||
public async Task<PageListModel<WxUserOutput>> GetListAsync(WxUserQueryInput input)
|
||
{
|
||
_logger.LogInformation("正在查询微信用户列表,页码: {PageIndex}, 每页条数: {PageSize}", input.PageIndex, input.PageSize);
|
||
|
||
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(a => MapToOutput(a), true)
|
||
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
||
|
||
return new PageListModel<WxUserOutput>(pageResult, input.PageIndex, input.PageSize, totalNumber);
|
||
}
|
||
|
||
private static WxUserOutput MapToOutput(WxUser wxUser)
|
||
{
|
||
return new WxUserOutput
|
||
{
|
||
Id = wxUser.Id,
|
||
OpenId = wxUser.OpenId,
|
||
UnionId = wxUser.UnionId,
|
||
NickName = wxUser.NickName,
|
||
AvatarUrl = wxUser.AvatarUrl,
|
||
Phone = wxUser.Phone,
|
||
Points = wxUser.Points,
|
||
Type = wxUser.Type,
|
||
Status = wxUser.Status,
|
||
GrowthPoints = wxUser.GrowthPoints,
|
||
CreatedBy = wxUser.CreatedBy,
|
||
CreatedAt = wxUser.CreatedAt,
|
||
UpdatedBy = wxUser.UpdatedBy,
|
||
UpdatedAt = wxUser.UpdatedAt
|
||
};
|
||
}
|
||
}
|