AdoS0OrderPriorityRulesController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
  5. /// <summary>
  6. /// S0 订单优先规则
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/sales/order-priority-rules")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0OrderPriorityRulesController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0OrderPriorityRule> _rep;
  15. public AdoS0OrderPriorityRulesController(SqlSugarRepository<AdoS0OrderPriorityRule> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0OrderPriorityRuleQueryDto q)
  21. {
  22. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  23. var query = _rep.AsQueryable()
  24. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId.Value)
  25. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId.Value)
  26. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
  27. .WhereIF(!string.IsNullOrWhiteSpace(q.SourceEntity), x => x.SourceEntity == q.SourceEntity)
  28. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled.Value);
  29. var total = await query.CountAsync();
  30. var list = await query
  31. .OrderByDescending(x => x.CreatedAt)
  32. .Skip((q.Page - 1) * q.PageSize)
  33. .Take(q.PageSize)
  34. .ToListAsync();
  35. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  36. }
  37. [HttpGet("{id:long}")]
  38. public async Task<IActionResult> GetAsync(long id)
  39. {
  40. var item = await _rep.GetByIdAsync(id);
  41. return item == null ? NotFound() : Ok(item);
  42. }
  43. [HttpPost]
  44. public async Task<IActionResult> CreateAsync([FromBody] AdoS0OrderPriorityRuleUpsertDto dto)
  45. {
  46. var code = AdoS0SalesRules.ResolveOrderPriorityRuleCodeForCreate(
  47. dto.Code,
  48. DateTime.Now,
  49. Random.Shared.Next(1000, 9999));
  50. if (await _rep.IsAnyAsync(x =>
  51. x.CompanyRefId == dto.CompanyRefId &&
  52. x.FactoryRefId == dto.FactoryRefId &&
  53. x.Code == code))
  54. {
  55. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.OrderPriorityRuleCodeExists, "订单优先规则编码已存在");
  56. }
  57. var entity = new AdoS0OrderPriorityRule
  58. {
  59. CompanyRefId = dto.CompanyRefId,
  60. FactoryRefId = dto.FactoryRefId,
  61. Code = code,
  62. Name = dto.Name,
  63. PriorityLevel = dto.PriorityLevel,
  64. SortDirection = dto.SortDirection,
  65. SourceEntity = dto.SourceEntity,
  66. SourceField = dto.SourceField,
  67. SourceFieldType = dto.SourceFieldType,
  68. SourceLinkField = dto.SourceLinkField,
  69. WorkOrderField = dto.WorkOrderField,
  70. WorkOrderFieldType = dto.WorkOrderFieldType,
  71. WorkOrderLinkField = dto.WorkOrderLinkField,
  72. RuleExpr = dto.RuleExpr,
  73. Remark = dto.Remark,
  74. IsEnabled = dto.IsEnabled,
  75. CreatedAt = DateTime.Now
  76. };
  77. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  78. return Ok(entity);
  79. }
  80. [HttpPut("{id:long}")]
  81. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0OrderPriorityRuleUpsertDto dto)
  82. {
  83. var entity = await _rep.GetByIdAsync(id);
  84. if (entity == null) return NotFound();
  85. var code = AdoS0SalesRules.ResolveOrderPriorityRuleCodeForUpdate(dto.Code, entity.Code);
  86. if (await _rep.IsAnyAsync(x =>
  87. x.Id != id &&
  88. x.CompanyRefId == dto.CompanyRefId &&
  89. x.FactoryRefId == dto.FactoryRefId &&
  90. x.Code == code))
  91. {
  92. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.OrderPriorityRuleCodeExists, "订单优先规则编码已存在");
  93. }
  94. entity.CompanyRefId = dto.CompanyRefId;
  95. entity.FactoryRefId = dto.FactoryRefId;
  96. entity.Code = code;
  97. entity.Name = dto.Name;
  98. entity.PriorityLevel = dto.PriorityLevel;
  99. entity.SortDirection = dto.SortDirection;
  100. entity.SourceEntity = dto.SourceEntity;
  101. entity.SourceField = dto.SourceField;
  102. entity.SourceFieldType = dto.SourceFieldType;
  103. entity.SourceLinkField = dto.SourceLinkField;
  104. entity.WorkOrderField = dto.WorkOrderField;
  105. entity.WorkOrderFieldType = dto.WorkOrderFieldType;
  106. entity.WorkOrderLinkField = dto.WorkOrderLinkField;
  107. entity.RuleExpr = dto.RuleExpr;
  108. entity.Remark = dto.Remark;
  109. entity.IsEnabled = dto.IsEnabled;
  110. entity.UpdatedAt = DateTime.Now;
  111. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  112. return Ok(entity);
  113. }
  114. [HttpPatch("{id:long}/toggle-enabled")]
  115. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  116. {
  117. var entity = await _rep.GetByIdAsync(id);
  118. if (entity == null) return NotFound();
  119. entity.IsEnabled = dto.IsEnabled;
  120. entity.UpdatedAt = DateTime.Now;
  121. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  122. return Ok(entity);
  123. }
  124. [HttpDelete("{id:long}")]
  125. public async Task<IActionResult> DeleteAsync(long id)
  126. {
  127. var item = await _rep.GetByIdAsync(id);
  128. if (item == null) return NotFound();
  129. await _rep.DeleteAsync(item);
  130. return Ok(new { message = "删除成功" });
  131. }
  132. }