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 wxUserRepository, ILogger _logger) : BaseRepository, IWxUserService { public async Task 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 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 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> 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 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(pageResult, input.PageIndex, input.PageSize, totalNumber); } }