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; /// /// 商品管理控制器 /// [Route("api/[controller]")] [ApiController] [ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))] public class ProductController : BaseController { private readonly IProductService _productService; private readonly ILogger _logger; public ProductController(IProductService productService, ILogger logger) { _productService = productService; _logger = logger; } /// /// 创建商品 /// /// 商品信息 /// 创建的商品信息 [HttpPost] public async Task> 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.Fail(ex.Message); } catch (Exception ex) { _logger.LogError(ex, "创建商品系统异常,参数:{Input}", input); return BaseResponse.Fail("创建商品失败,请稍后重试"); } } /// /// 更新商品 /// /// 商品ID /// 商品信息 /// 更新后的商品信息 [HttpPut("{id}")] public async Task> 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.Fail(ex.Message); } catch (Exception ex) { _logger.LogError(ex, "更新商品系统异常,ID:{Id},参数:{Input}", id, input); return BaseResponse.Fail("更新商品失败,请稍后重试"); } } /// /// 删除商品 /// /// 商品ID /// 操作结果 [HttpDelete("{id}")] public async Task> DeleteAsync(long id) { try { await _productService.DeleteAsync(id); return Success(new object(), "删除商品成功"); } catch (BusinessException ex) { _logger.LogWarning(ex, "删除商品业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message); } catch (Exception ex) { _logger.LogError(ex, "删除商品系统异常,ID:{Id}", id); return BaseResponse.Fail("删除商品失败,请稍后重试"); } } /// /// 根据ID获取商品 /// /// 商品ID /// 商品信息 [HttpGet("{id}")] public async Task> GetByIdAsync(long id) { try { var result = await _productService.GetByIdAsync(id); return Success(result); } catch (BusinessException ex) { _logger.LogWarning(ex, "获取商品业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message); } catch (Exception ex) { _logger.LogError(ex, "获取商品系统异常,ID:{Id}", id); return BaseResponse.Fail("获取商品信息失败,请稍后重试"); } } /// /// 分页查询商品列表 /// /// 查询条件 /// 分页结果 [HttpPost("list")] public async Task>> 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>.Fail(ex.Message); } catch (Exception ex) { _logger.LogError(ex, "查询商品列表系统异常,参数:{Input}", input); return BaseResponse>.Fail("查询商品列表失败,请稍后重试"); } } }