refactor: 重构用户与商品模块,统一业务模型与服务
1. 新增用户状态枚举UserStatusEnum,统一用户状态定义 2. 重构用户体系:合并WxUser与User实体为Users实体,统一用户管理 3. 新增微信小程序认证相关服务与控制器,实现一键登录功能 4. 新增商品管理完整服务与控制器,修复商品表字段映射问题 5. 删除冗余的WxUser相关服务与控制器代码 6. 新增Newtonsoft.Json依赖用于微信接口响应解析 7. 清理无用的文件夹引用配置
This commit is contained in:
256
QYZH.InteractiveMagazine.Service/ProductService.cs
Normal file
256
QYZH.InteractiveMagazine.Service/ProductService.cs
Normal file
@ -0,0 +1,256 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using QYZH.InteractiveMagazine.Repository;
|
||||
using SqlSugar;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 商品服务实现
|
||||
/// </summary>
|
||||
public class ProductService(BaseRepository<Product> productRepository, ILogger<ProductService> logger) : BaseRepository<Product>, IProductService
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 创建商品
|
||||
/// </summary>
|
||||
public async Task<ProductOutput> CreateAsync(ProductInput input)
|
||||
{
|
||||
logger.LogInformation("正在创建商品,商品名称: {Name}", input.Name);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
{
|
||||
throw new BusinessException("商品名称不能为空", 400);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Type))
|
||||
{
|
||||
throw new BusinessException("商品类型不能为空", 400);
|
||||
}
|
||||
|
||||
if (input.Price < 0)
|
||||
{
|
||||
throw new BusinessException("商品价格不能为负数", 400);
|
||||
}
|
||||
|
||||
var product = new Product
|
||||
{
|
||||
Name = input.Name.Trim(),
|
||||
Description = input.Description,
|
||||
ImageUrl = input.ImageUrl,
|
||||
Price = input.Price,
|
||||
Type = input.Type,
|
||||
SaleStatus = input.SaleStatus,
|
||||
MetaData = input.MetaData,
|
||||
IsActive = input.IsActive,
|
||||
Stock = input.Stock,
|
||||
CreatedBy = "System",
|
||||
UpdatedBy = "System",
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedAt = DateTime.Now,
|
||||
IsDeleted = false
|
||||
};
|
||||
|
||||
var result = await productRepository.InsertAsync(product);
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("商品创建失败,商品名称: {Name}", input.Name);
|
||||
throw new BusinessException("创建商品失败", 500);
|
||||
}
|
||||
|
||||
logger.LogInformation("商品创建成功,商品名称: {Name}, ID: {Id}", input.Name, product.Id);
|
||||
|
||||
return new ProductOutput
|
||||
{
|
||||
Id = product.Id,
|
||||
Name = product.Name,
|
||||
Description = product.Description,
|
||||
ImageUrl = product.ImageUrl,
|
||||
Price = product.Price,
|
||||
Type = product.Type,
|
||||
SaleStatus = product.SaleStatus,
|
||||
MetaData = product.MetaData,
|
||||
IsActive = product.IsActive,
|
||||
Stock = product.Stock,
|
||||
CreatedBy = product.CreatedBy,
|
||||
CreatedAt = product.CreatedAt,
|
||||
UpdatedBy = product.UpdatedBy,
|
||||
UpdatedAt = product.UpdatedAt
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新商品
|
||||
/// </summary>
|
||||
public async Task<ProductOutput> UpdateAsync(long id, ProductInput input)
|
||||
{
|
||||
logger.LogInformation("正在更新商品,ID: {Id}", id);
|
||||
|
||||
var product = await productRepository.GetByIdAsync(id);
|
||||
if (product == null)
|
||||
{
|
||||
logger.LogWarning("未找到要更新的商品,ID: {Id}", id);
|
||||
throw new BusinessException("商品不存在", 404);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
{
|
||||
throw new BusinessException("商品名称不能为空", 400);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Type))
|
||||
{
|
||||
throw new BusinessException("商品类型不能为空", 400);
|
||||
}
|
||||
|
||||
if (input.Price < 0)
|
||||
{
|
||||
throw new BusinessException("商品价格不能为负数", 400);
|
||||
}
|
||||
|
||||
product.Name = input.Name.Trim();
|
||||
product.Description = input.Description;
|
||||
product.ImageUrl = input.ImageUrl;
|
||||
product.Price = input.Price;
|
||||
product.Type = input.Type;
|
||||
product.SaleStatus = input.SaleStatus;
|
||||
product.MetaData = input.MetaData;
|
||||
product.IsActive = input.IsActive;
|
||||
product.Stock = input.Stock;
|
||||
product.UpdatedBy = "System";
|
||||
product.UpdatedAt = DateTime.Now;
|
||||
|
||||
var result = await productRepository.UpdateAsync(product);
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("商品更新失败,ID: {Id}", id);
|
||||
throw new BusinessException("更新商品失败", 500);
|
||||
}
|
||||
|
||||
logger.LogInformation("商品更新成功,ID: {Id}", id);
|
||||
|
||||
return new ProductOutput
|
||||
{
|
||||
Id = product.Id,
|
||||
Name = product.Name,
|
||||
Description = product.Description,
|
||||
ImageUrl = product.ImageUrl,
|
||||
Price = product.Price,
|
||||
Type = product.Type,
|
||||
SaleStatus = product.SaleStatus,
|
||||
MetaData = product.MetaData,
|
||||
IsActive = product.IsActive,
|
||||
Stock = product.Stock,
|
||||
CreatedBy = product.CreatedBy,
|
||||
CreatedAt = product.CreatedAt,
|
||||
UpdatedBy = product.UpdatedBy,
|
||||
UpdatedAt = product.UpdatedAt
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除商品(软删除)
|
||||
/// </summary>
|
||||
public async Task DeleteAsync(long id)
|
||||
{
|
||||
logger.LogInformation("正在删除商品,ID: {Id}", id);
|
||||
|
||||
var product = await productRepository.GetByIdAsync(id);
|
||||
if (product == null)
|
||||
{
|
||||
logger.LogWarning("未找到要删除的商品,ID: {Id}", id);
|
||||
throw new BusinessException("商品不存在", 404);
|
||||
}
|
||||
|
||||
var result = await productRepository.DeleteByIdAsync(id);
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("商品删除失败,ID: {Id}", id);
|
||||
throw new BusinessException("删除商品失败", 500);
|
||||
}
|
||||
|
||||
logger.LogInformation("商品删除成功,ID: {Id}", id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取商品
|
||||
/// </summary>
|
||||
public async Task<ProductOutput> GetByIdAsync(long id)
|
||||
{
|
||||
logger.LogInformation("正在获取商品信息,ID: {Id}", id);
|
||||
|
||||
var product = await productRepository.GetByIdAsync(id);
|
||||
if (product == null)
|
||||
{
|
||||
logger.LogWarning("未找到商品,ID: {Id}", id);
|
||||
throw new BusinessException("商品不存在", 404);
|
||||
}
|
||||
|
||||
return new ProductOutput
|
||||
{
|
||||
Id = product.Id,
|
||||
Name = product.Name,
|
||||
Description = product.Description,
|
||||
ImageUrl = product.ImageUrl,
|
||||
Price = product.Price,
|
||||
Type = product.Type,
|
||||
SaleStatus = product.SaleStatus,
|
||||
MetaData = product.MetaData,
|
||||
IsActive = product.IsActive,
|
||||
Stock = product.Stock,
|
||||
CreatedBy = product.CreatedBy,
|
||||
CreatedAt = product.CreatedAt,
|
||||
UpdatedBy = product.UpdatedBy,
|
||||
UpdatedAt = product.UpdatedAt
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询商品列表
|
||||
/// </summary>
|
||||
public async Task<PageListModel<ProductOutput>> GetListAsync(ProductQueryInput 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 productRepository.Queryable()
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), p => p.Name.Contains(input.Name))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Type), p => p.Type == input.Type)
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.SaleStatus), p => p.SaleStatus == input.SaleStatus)
|
||||
.WhereIF(input.IsActive.HasValue, p => p.IsActive == input.IsActive.Value)
|
||||
.OrderByDescending(p => p.CreatedAt)
|
||||
.Select(p => new ProductOutput
|
||||
{
|
||||
Id = p.Id,
|
||||
Name = p.Name,
|
||||
Description = p.Description,
|
||||
ImageUrl = p.ImageUrl,
|
||||
Price = p.Price,
|
||||
Type = p.Type,
|
||||
SaleStatus = p.SaleStatus,
|
||||
MetaData = p.MetaData,
|
||||
IsActive = p.IsActive,
|
||||
Stock = p.Stock,
|
||||
CreatedBy = p.CreatedBy,
|
||||
CreatedAt = p.CreatedAt,
|
||||
UpdatedBy = p.UpdatedBy,
|
||||
UpdatedAt = p.UpdatedAt
|
||||
}, true)
|
||||
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
||||
|
||||
return new PageListModel<ProductOutput>(pageResult, input.PageIndex, input.PageSize, totalNumber);
|
||||
}
|
||||
}
|
||||
59
QYZH.InteractiveMagazine.Service/UsersService.cs
Normal file
59
QYZH.InteractiveMagazine.Service/UsersService.cs
Normal file
@ -0,0 +1,59 @@
|
||||
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;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Service;
|
||||
|
||||
public class UsersService(BaseRepository<Users> usersRepository, ILogger<UsersService> _logger) : BaseRepository<Users>, IUsersService
|
||||
{
|
||||
/// <summary>
|
||||
/// 分页查询用户列表
|
||||
/// </summary>
|
||||
public async Task<BaseResponse<PageListModel<UsersOutput>>> GetListAsync(UsersQueryInput input)
|
||||
{
|
||||
var page = Queryable()
|
||||
.WhereIF(!string.IsNullOrEmpty(input.WxUserId), u => u.OpenId == input.WxUserId)
|
||||
.OrderBy(u => u.Id, OrderByType.Desc)
|
||||
.ToPage<Users, UsersOutput>(input);
|
||||
|
||||
return BaseResponse<PageListModel<UsersOutput>>.Success(page);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户详情
|
||||
/// </summary>
|
||||
public async Task<BaseResponse<UsersOutput>> GetDetailAsync(long id)
|
||||
{
|
||||
var user = await GetByIdAsync<UsersOutput>(u => u.Id == id);
|
||||
if (user == null)
|
||||
{
|
||||
return BaseResponse<UsersOutput>.Fail("用户不存在");
|
||||
}
|
||||
return BaseResponse<UsersOutput>.Success(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新用户状态
|
||||
/// </summary>
|
||||
public async Task<BaseResponse> UpdateStatusAsync(long id, UpdateUserStatusInput input)
|
||||
{
|
||||
var exists = await Queryable().AnyAsync(u => u.Id == id);
|
||||
if (!exists)
|
||||
{
|
||||
return BaseResponse.Fail("用户不存在");
|
||||
}
|
||||
|
||||
var statusValue = input.Status == 1 ? "Active" : "Disabled";
|
||||
var result = await UpdateAsync(
|
||||
u => new Users { Status = statusValue },
|
||||
u => u.Id == id
|
||||
);
|
||||
|
||||
return result ? BaseResponse.Success() : BaseResponse.Fail("更新失败");
|
||||
}
|
||||
}
|
||||
160
QYZH.InteractiveMagazine.Service/WeChatAuthService.cs
Normal file
160
QYZH.InteractiveMagazine.Service/WeChatAuthService.cs
Normal file
@ -0,0 +1,160 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QYZH.InteractiveMagazine.Common.Helpers;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.Auth;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.Cache;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.IService.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using QYZH.InteractiveMagazine.Models.Settings;
|
||||
using QYZH.InteractiveMagazine.Repository;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序认证服务实现
|
||||
/// </summary>
|
||||
public class WeChatAuthService(BaseRepository<Users> usersRepository, IConfiguration configuration, ILogger<WeChatAuthService> logger) : BaseRepository<Users>, IWeChatAuthService
|
||||
{
|
||||
private const string TokenKeyPrefix = "InteractiveMagazine:WeChatAuth:Token";
|
||||
private const string Code2SessionUrl = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code";
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序一键登录
|
||||
/// </summary>
|
||||
/// <param name="input">登录输入(含微信 code)</param>
|
||||
/// <returns>登录结果(含 Token 和用户信息)</returns>
|
||||
public async Task<WeChatLoginOutput> LoginAsync(WeChatLoginInput input)
|
||||
{
|
||||
logger.LogInformation("微信小程序登录尝试");
|
||||
|
||||
// 参数校验
|
||||
if (string.IsNullOrWhiteSpace(input.Code))
|
||||
{
|
||||
throw new BusinessException("微信登录凭证 code 不能为空", 400);
|
||||
}
|
||||
|
||||
// 获取微信配置
|
||||
var weChatSettings = GetWeChatSettings();
|
||||
|
||||
// 调用微信 code2session 接口
|
||||
var wxResponse = await CallCode2SessionAsync(weChatSettings, input.Code);
|
||||
if (wxResponse == null || wxResponse.ErrCode != 0 || string.IsNullOrWhiteSpace(wxResponse.OpenId))
|
||||
{
|
||||
var errMsg = wxResponse?.ErrMsg ?? "未知错误";
|
||||
logger.LogWarning("微信 code2session 接口调用失败,errcode: {ErrCode}, errmsg: {ErrMsg}", wxResponse?.ErrCode, errMsg);
|
||||
throw new BusinessException($"微信登录失败:{errMsg}", 400);
|
||||
}
|
||||
|
||||
logger.LogInformation("微信 code2session 成功,OpenId: {OpenId}", wxResponse.OpenId);
|
||||
|
||||
// 查询用户是否已存在
|
||||
var user = await usersRepository.GetFirstAsync(u => u.OpenId == wxResponse.OpenId);
|
||||
var isNewUser = user == null;
|
||||
|
||||
if (isNewUser)
|
||||
{
|
||||
// 首次登录,创建新用户
|
||||
user = new Users
|
||||
{
|
||||
Name = $"wx_{wxResponse.OpenId[^8..]}",
|
||||
OpenId = wxResponse.OpenId,
|
||||
UnionId = wxResponse.UnionId,
|
||||
Type = "Normal",
|
||||
Status = "Active",
|
||||
GrowthPoints = 0,
|
||||
Points = 0
|
||||
};
|
||||
|
||||
var insertResult = await usersRepository.Insertable(user).ExecuteReturnIdentityAsync();
|
||||
if (insertResult <= 0)
|
||||
{
|
||||
logger.LogError("创建微信用户失败,OpenId: {OpenId}", wxResponse.OpenId);
|
||||
throw new BusinessException("创建用户失败,请稍后重试", 500);
|
||||
}
|
||||
|
||||
user.Id = insertResult;
|
||||
logger.LogInformation("微信新用户创建成功,UserId: {UserId}, OpenId: {OpenId}", user.Id, wxResponse.OpenId);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 已有用户,校验状态
|
||||
if (user.Status == "Disabled")
|
||||
{
|
||||
logger.LogWarning("微信登录失败,用户已被禁用,UserId: {UserId}, OpenId: {OpenId}", user.Id, wxResponse.OpenId);
|
||||
throw new BusinessException("账号已被禁用,请联系客服", 403);
|
||||
}
|
||||
|
||||
logger.LogInformation("微信老用户登录,UserId: {UserId}, OpenId: {OpenId}", user.Id, wxResponse.OpenId);
|
||||
}
|
||||
|
||||
// 生成 JWT Token
|
||||
var jwtSettings = GetJwtSettings();
|
||||
var token = JwtHelper.GenerateToken((long)user.Id, user.Name, jwtSettings);
|
||||
|
||||
// 缓存 Token 到 Redis
|
||||
await RedisHelper.StringSetAsync($"{TokenKeyPrefix}:{user.Id}", token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes));
|
||||
|
||||
return new WeChatLoginOutput
|
||||
{
|
||||
Token = token,
|
||||
UserId = (long)user.Id,
|
||||
UserName = user.Name,
|
||||
OpenId = wxResponse.OpenId
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调用微信 code2session 接口
|
||||
/// </summary>
|
||||
private async Task<WxCode2SessionResponse?> CallCode2SessionAsync(WeChatSettings settings, string code)
|
||||
{
|
||||
var url = string.Format(Code2SessionUrl, settings.AppId, settings.AppSecret, code);
|
||||
try
|
||||
{
|
||||
return await HttpHelper.GetAsync<WxCode2SessionResponse>(url);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "调用微信 code2session 接口异常,URL: {Url}", url);
|
||||
throw new BusinessException("微信服务请求失败,请稍后重试", 500);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信配置
|
||||
/// </summary>
|
||||
private WeChatSettings GetWeChatSettings()
|
||||
{
|
||||
var settings = configuration.GetSection("WeChatSettings").Get<WeChatSettings>();
|
||||
if (settings == null || string.IsNullOrWhiteSpace(settings.AppId) || string.IsNullOrWhiteSpace(settings.AppSecret))
|
||||
{
|
||||
logger.LogError("微信配置不完整,请检查 appsettings.json 中的 WeChatSettings 节点");
|
||||
throw new BusinessException("微信配置不完整,请联系系统管理员", 500);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 JWT 配置
|
||||
/// </summary>
|
||||
private JwtSettings GetJwtSettings()
|
||||
{
|
||||
var jwtSettings = configuration.GetSection("JwtSettings").Get<JwtSettings>()
|
||||
?? new JwtSettings
|
||||
{
|
||||
Issuer = "QYZH.InteractiveMagazine",
|
||||
Audience = "QYZH.InteractiveMagazine",
|
||||
SecretKey = "your-256-bit-secret-key-here-change-in-production",
|
||||
ExpiryMinutes = 120
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(jwtSettings.SecretKey))
|
||||
{
|
||||
throw new BusinessException("JWT 配置不完整", 500);
|
||||
}
|
||||
|
||||
return jwtSettings;
|
||||
}
|
||||
}
|
||||
@ -1,221 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user