using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Common.Exceptions; using F9WebAiTest.Application.CQRS.SYS.YuanliaoZhuangkous.Commands; using F9WebAiTest.Application.CQRS.SYS.YuanliaoZhuangkous.Queries; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace F9WebAiTest.Api.Controllers; /// 原料庄口管理(DDD + CQRS):按年度/产地/站点/季别维护原料庄口,编号自动生成 [ApiController] [Route("api/sys/yuanliaozhuangkou")] [Authorize] public class SysYuanliaoZhuangkouController : ControllerBase { private readonly IMediator _mediator; public SysYuanliaoZhuangkouController(IMediator mediator) => _mediator = mediator; [HttpGet] public async Task GetPaged([FromQuery] int year = 0, [FromQuery] string chandi = "", [FromQuery] string zhandian = "", [FromQuery] string jibie = "", [FromQuery] string like = "", [FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20) => Ok(ApiResult.Ok(await _mediator.Send(new GetYuanliaoZhuangkouPagedQuery(year, chandi ?? string.Empty, zhandian ?? string.Empty, jibie ?? string.Empty, like ?? string.Empty, pageIndex, pageSize)))); /// 物资(茧别)下拉选项:物资目录中名称含"茧"的物资 [HttpGet("wuzi-options")] public async Task GetWuziOptions([FromQuery] string keyword = "") => Ok(ApiResult.Ok(await _mediator.Send(new GetYuanliaoZhuangkouWuziOptionsQuery(keyword ?? string.Empty)))); [HttpGet("{id}")] public async Task GetById(long id) { var result = await _mediator.Send(new GetYuanliaoZhuangkouByIdQuery(id)); return result == null ? NotFound(ApiResult.Fail("原料庄口不存在", 404)) : Ok(ApiResult.Ok(result)); } [HttpPost] public async Task Create([FromBody] YuanliaoZhuangkou_CreateCommand command) { try { return Ok(ApiResult.Ok(new { id = await _mediator.Send(command) }, "新增成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } [HttpPut("{id}")] public async Task Update(long id, [FromBody] YuanliaoZhuangkou_UpdateCommand command) { try { return Ok(ApiResult.Ok(await _mediator.Send(command with { Id = id }), "修改成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } [HttpDelete("{id}")] public async Task Delete(long id) { try { return Ok(ApiResult.Ok(await _mediator.Send(new YuanliaoZhuangkou_DelCommand(id)), "删除成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } }