Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.WebApi/Controllers/ProductController.cs
glz 1d86580db7 refactor: 重构状态更新接口,新增商品管理与勋章规则配置
1. 重构用户、宠物模板、勋章、管理员状态更新接口,移除冗余参数改为自动切换状态
2. 重命名宠物背景枚举为宠物皮肤,调整商品状态字段为枚举类型
3. 新增商品上下架状态管理接口与对应逻辑
4. 新增勋章规则配置系统,支持动态加载可用表和字段
5. 优化商品查询与展示逻辑,适配新的状态字段
2026-06-09 11:41:28 +08:00

177 lines
5.9 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.AspNetCore.Mvc;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Enum;
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
/// <summary>
/// 商品管理控制器
/// </summary>
[Route("api/[controller]")]
[ApiController]
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
public class ProductController : BaseController
{
private readonly IProductService _productService;
private readonly ILogger<ProductController> _logger;
public ProductController(IProductService productService, ILogger<ProductController> logger)
{
_productService = productService;
_logger = logger;
}
/// <summary>
/// 创建商品
/// </summary>
/// <param name="input">商品信息</param>
/// <returns>创建的商品信息</returns>
[HttpPost]
public async Task<BaseResponse<ProductOutput>> CreateAsync([FromBody] ProductInput input)
{
try
{
var result = await _productService.CreateAsync(input);
return Success(result, "创建商品成功");
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "创建商品业务异常: {Message}", ex.Message);
return BaseResponse<ProductOutput>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "创建商品系统异常,参数:{Input}", input);
return BaseResponse<ProductOutput>.Fail("创建商品失败,请稍后重试");
}
}
/// <summary>
/// 更新商品
/// </summary>
/// <param name="id">商品ID</param>
/// <param name="input">商品信息</param>
/// <returns>更新后的商品信息</returns>
[HttpPut("{id}")]
public async Task<BaseResponse<ProductOutput>> UpdateAsync(long id, [FromBody] ProductInput input)
{
try
{
var result = await _productService.UpdateAsync(id, input);
return Success(result, "更新商品成功");
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "更新商品业务异常: {Message}", ex.Message);
return BaseResponse<ProductOutput>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "更新商品系统异常ID{Id},参数:{Input}", id, input);
return BaseResponse<ProductOutput>.Fail("更新商品失败,请稍后重试");
}
}
/// <summary>
/// 删除商品
/// </summary>
/// <param name="id">商品ID</param>
/// <returns>操作结果</returns>
[HttpDelete("{id}")]
public async Task<BaseResponse<object>> DeleteAsync(long id)
{
try
{
await _productService.DeleteAsync(id);
return Success(new object(), "删除商品成功");
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "删除商品业务异常: {Message}", ex.Message);
return BaseResponse<object>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "删除商品系统异常ID{Id}", id);
return BaseResponse<object>.Fail("删除商品失败,请稍后重试");
}
}
/// <summary>
/// 根据ID获取商品
/// </summary>
/// <param name="id">商品ID</param>
/// <returns>商品信息</returns>
[HttpGet("{id}")]
public async Task<BaseResponse<ProductOutput>> GetByIdAsync(long id)
{
try
{
var result = await _productService.GetByIdAsync(id);
return Success(result);
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "获取商品业务异常: {Message}", ex.Message);
return BaseResponse<ProductOutput>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "获取商品系统异常ID{Id}", id);
return BaseResponse<ProductOutput>.Fail("获取商品信息失败,请稍后重试");
}
}
/// <summary>
/// 分页查询商品列表
/// </summary>
/// <param name="input">查询条件</param>
/// <returns>分页结果</returns>
[HttpPost("list")]
public async Task<BaseResponse<PageListModel<ProductOutput>>> GetListAsync([FromBody] ProductQueryInput input)
{
try
{
var result = await _productService.GetListAsync(input);
return Success(result);
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "查询商品列表业务异常: {Message}", ex.Message);
return BaseResponse<PageListModel<ProductOutput>>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "查询商品列表系统异常,参数:{Input}", input);
return BaseResponse<PageListModel<ProductOutput>>.Fail("查询商品列表失败,请稍后重试");
}
}
/// <summary>
/// 更新商品上架/下架状态
/// </summary>
/// <param name="id">商品ID</param>
/// <returns>操作结果</returns>
[HttpPut("{id}/status")]
public async Task<BaseResponse<object>> UpdateSaleStatusAsync(long id)
{
try
{
await _productService.UpdateSaleStatusAsync(id);
return Success(new object(), "更新商品状态成功");
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "更新商品状态业务异常: {Message}", ex.Message);
return BaseResponse<object>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "更新商品状态系统异常ID{Id}", id);
return BaseResponse<object>.Fail("更新商品状态失败,请稍后重试");
}
}
}