using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Common.Exceptions; using F9WebAiTest.Application.CQRS.SYS.Gongyingshangs.Commands; using F9WebAiTest.Application.CQRS.SYS.Gongyingshangs.Queries; using F9WebAiTest.Domain.Enums.SYS.Gongyingshangs; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace F9WebAiTest.Api.Controllers; /// 往来单位(供应商/客户)管理 [ApiController] [Route("api/sys/gongyingshang")] [Authorize] public class SysGongyingshangController : ControllerBase { private readonly IMediator _mediator; public SysGongyingshangController(IMediator mediator) => _mediator = mediator; /// 分页查询(业务类型 + 销售类型 + 关键字) [HttpGet] public async Task GetPaged([FromQuery] Gys_Type_Enum type = Gys_Type_Enum.干茧, [FromQuery] Gys_SaleType_Enum? saleType = null, [FromQuery] string like = "", [FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20) => Ok(ApiResult.Ok(await _mediator.Send(new GetGongyingshangPagedQuery(type, saleType, like ?? string.Empty, pageIndex, pageSize)))); [HttpGet("{id}")] public async Task GetById(long id) { var result = await _mediator.Send(new GetGongyingshangByIdQuery(id)); return result == null ? NotFound(ApiResult.Fail("往来单位不存在", 404)) : Ok(ApiResult.Ok(result)); } [HttpPost] public async Task Create([FromBody] Gongyingshang_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] Gongyingshang_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 Gongyingshang_DelCommand(id)), "删除成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } }