using Kean.Application.Command.ViewModels; using Kean.Application.Query.ViewModels; using Kean.Domain; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Linq; using System.Text.Json; using System.Threading.Tasks; namespace Kean.Presentation.Rest.Controllers { /// /// 订单服务 /// [ApiController, Route("api/orders")] public class OrdersController : ControllerBase { private readonly Application.Command.Interfaces.IOrderService _orderCommandService; // 订单信息命令服务 private readonly Application.Query.Interfaces.IOrderService _orderQueryService; // 订单信息查询服务 private readonly IConfiguration _configuration; /// /// 依赖注入 /// public OrdersController( Application.Command.Interfaces.IOrderService orderCommandService, IConfiguration configuration, Application.Query.Interfaces.IOrderService orderQueryService) { _orderCommandService = orderCommandService; _orderQueryService = orderQueryService; _configuration = configuration; } /// /// 获取系统行为列表 /// /// 成功 [HttpGet("actions")] [ProducesResponseType(200)] public async Task GetActionList() { var items = await _orderQueryService.GetActionList(); return StatusCode(200, new { items, total = items.Count() }); } /// /// 获取订单类型列表 /// /// 成功 [HttpGet("types")] [ProducesResponseType(200)] public async Task GetTypeList() { var items = await _orderQueryService.GetTypeList(); return StatusCode(200, new { items, total = items.Count() }); } /// /// 创建订单类型 /// /// 成功 /// 类型已存在 /// 请求内容错误 [HttpPost("types")] [ProducesResponseType(201)] [ProducesResponseType(409)] [ProducesResponseType(422)] public async Task CreateType(Application.Command.ViewModels.Ordtyp type) { if (type.Menu != null) { foreach (var key in type.Menu.Keys) { var jsonElement = (JsonElement)type.Menu[key]; type.Menu[key] = jsonElement.ValueKind switch { JsonValueKind.Number => jsonElement.GetInt32(), JsonValueKind.String => jsonElement.GetString(), _ => null }; } } var result = await _orderCommandService.CreateType(type); return result switch { { Id: > 0 } => StatusCode(201, result.Id), { Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure), _ => StatusCode(422, result.Failure) }; } /// /// 修改订单类型 /// /// 成功 /// 类型已存在 /// 类型已删除 /// 请求内容错误 /// 类型被锁定 [HttpPut("types/{id}")] [ProducesResponseType(200)] [ProducesResponseType(409)] [ProducesResponseType(410)] [ProducesResponseType(422)] [ProducesResponseType(423)] public async Task ModifyType(int id, Application.Command.ViewModels.Ordtyp type) { type.Id = id; if (type.Menu != null) { foreach (var key in type.Menu.Keys) { var jsonElement = (JsonElement)type.Menu[key]; type.Menu[key] = jsonElement.ValueKind switch { JsonValueKind.Number => jsonElement.GetInt32(), JsonValueKind.String => jsonElement.GetString(), _ => null }; } } var result = await _orderCommandService.ModifyType(type); return result switch { { Success: true } => StatusCode(200), { Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure), { Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure), { Failure.ErrorCode: nameof(ErrorCode.Locked) } => StatusCode(423, result.Failure), _ => StatusCode(422, result.Failure) }; } /// /// 删除订单类型 /// /// 成功 /// 请求内容错误 /// 类型被锁定 [HttpDelete("types/{id}")] [ProducesResponseType(204)] [ProducesResponseType(422)] [ProducesResponseType(423)] public async Task DeleteType(int id) { var result = await _orderCommandService.DeleteType(id); return result switch { { Success: true } => StatusCode(204), { Failure.ErrorCode: nameof(ErrorCode.Locked) } => StatusCode(423, result.Failure), _ => StatusCode(422, result.Failure) }; } /// /// 获取工作流列表 /// /// 成功 [HttpGet("flows")] [ProducesResponseType(200)] public async Task GetFlowList() { var items = await _orderQueryService.GetFlowList(); return StatusCode(200, new { items, total = items.Count() }); } /// /// 创建工作流 /// /// 成功 /// 工作流已存在 /// 请求内容错误 [HttpPost("flows")] [ProducesResponseType(201)] [ProducesResponseType(409)] [ProducesResponseType(422)] public async Task CreateFlow(Application.Command.ViewModels.Flow flow) { var result = await _orderCommandService.CreateFlow(flow); return result switch { { Id: > 0 } => StatusCode(201, result.Id), { Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure), _ => StatusCode(422, result.Failure) }; } /// /// 修改工作流 /// /// 成功 /// 工作流已存在 /// 工作流已删除 /// 请求内容错误 /// 工作流被锁定 [HttpPut("flows/{id}")] [ProducesResponseType(200)] [ProducesResponseType(409)] [ProducesResponseType(410)] [ProducesResponseType(422)] [ProducesResponseType(423)] public async Task ModifyFlow(int id, Application.Command.ViewModels.Flow flow) { flow.Id = id; var result = await _orderCommandService.ModifyFlow(flow); return result switch { { Success: true } => StatusCode(200), { Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure), { Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure), { Failure.ErrorCode: nameof(ErrorCode.Locked) } => StatusCode(423, result.Failure), _ => StatusCode(422, result.Failure) }; } /// /// 删除工作流 /// /// 成功 /// 请求内容错误 /// 类型被锁定 [HttpDelete("flows/{id}")] [ProducesResponseType(204)] [ProducesResponseType(422)] [ProducesResponseType(423)] public async Task DeleteFlow(int id) { var result = await _orderCommandService.DeleteFlow(id); return result switch { { Success: true } => StatusCode(204), { Failure.ErrorCode: nameof(ErrorCode.Locked) } => StatusCode(423, result.Failure), _ => StatusCode(422, result.Failure) }; } /// /// 获取实例列表 /// /// 成功 [HttpGet] [ProducesResponseType(200)] public async Task GetOrderList( [FromQuery] int? type, [FromQuery] int? flow, [FromQuery] int? node, [FromQuery] string no, [FromQuery] string creater, [FromQuery] DateTime? createTimeFrom, [FromQuery] DateTime? createTimeTo, [FromQuery] string sort, [FromQuery] int? offset, [FromQuery] int? limit) { var items = await _orderQueryService.GetOrderList(type, flow, node, no, creater, createTimeFrom, createTimeTo, sort, offset, limit); if (offset.HasValue || limit.HasValue) { var total = await _orderQueryService.GetOrderCount(type, flow, node, no, creater, createTimeFrom, createTimeTo); return StatusCode(200, new { items, total }); } else { return StatusCode(200, new { items, total = items.Count() }); } } /// /// 获取行列表 /// /// 成功 [HttpGet("{id}/lines")] [ProducesResponseType(200)] public async Task GetLineList( [FromRoute] int? id, [FromQuery] string sort, [FromQuery] int? offset, [FromQuery] int? limit) { var items = await _orderQueryService.GetLineList(id, sort, offset, limit); if (offset.HasValue || limit.HasValue) { var total = await _orderQueryService.GetLineCount(id); return StatusCode(200, new { items, total }); } else { return StatusCode(200, new { items, total = items.Count() }); } } /// /// 创建订单 /// /// 成功 /// 请求内容错误 [HttpPost] [ProducesResponseType(201)] [ProducesResponseType(422)] public async Task CreateOrder(Application.Command.ViewModels.Order order) { var result = await _orderCommandService.CreateOrder(order); if (result.Id > 0) { return StatusCode(201, result.Id); } else { return StatusCode(422, result.Failure); } } /// /// 修改订单 /// /// 成功 /// 请求内容错误 [HttpPut("{id}")] [ProducesResponseType(200)] [ProducesResponseType(422)] public async Task ModifyOrder(int id, Application.Command.ViewModels.Order order) { order.Id = id; var result = await _orderCommandService.ModifyOrder(order); if (result.Success) { return StatusCode(200); } else { return StatusCode(422, result.Failure); } } /// /// 删除订单 /// /// 成功 [HttpDelete("{id}")] [ProducesResponseType(204)] public async Task DeleteOrder(int id) { await _orderCommandService.DeleteOrder(Math.Abs(id), id > 0); return StatusCode(204); } } }