Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.Service/WxUserService.cs

222 lines
7.7 KiB
C#
Raw Normal View History

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);
}
}