AdoS0PriorityRulesController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System.Text.Json;
  2. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
  4. using Admin.NET.Plugin.AiDOP.Infrastructure;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
  6. /// <summary>
  7. /// S0 订单优先级业务规则(ado_s0_sales_order_priority_rule)
  8. /// </summary>
  9. [ApiController]
  10. [Route("api/s0/sales/priority-rules")]
  11. [AllowAnonymous]
  12. [NonUnify]
  13. public class AdoS0PriorityRulesController : ControllerBase
  14. {
  15. private readonly SqlSugarRepository<AdoS0PriorityRule> _rep;
  16. public AdoS0PriorityRulesController(SqlSugarRepository<AdoS0PriorityRule> rep)
  17. {
  18. _rep = rep;
  19. }
  20. [HttpGet]
  21. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0PriorityRuleQueryDto q)
  22. {
  23. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  24. var query = _rep.AsQueryable()
  25. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  26. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  27. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value)
  28. .WhereIF(
  29. !string.IsNullOrWhiteSpace(q.Keyword),
  30. x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!));
  31. var total = await query.CountAsync();
  32. var list = await query
  33. .OrderBy(x => x.PriorityLevel)
  34. .Skip((q.Page - 1) * q.PageSize)
  35. .Take(q.PageSize)
  36. .ToListAsync();
  37. foreach (var item in list)
  38. FillRuleExprLabels(item);
  39. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  40. }
  41. [HttpGet("{id:long}")]
  42. public async Task<IActionResult> GetAsync(long id)
  43. {
  44. var item = await _rep.GetByIdAsync(id);
  45. if (item == null) return NotFound();
  46. FillRuleExprLabels(item);
  47. return Ok(item);
  48. }
  49. [HttpPost]
  50. public async Task<IActionResult> CreateAsync([FromBody] AdoS0PriorityRuleUpsertDto dto)
  51. {
  52. var now = DateTime.Now;
  53. var entity = MapToEntity(dto);
  54. entity.CreatedAt = now;
  55. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  56. FillRuleExprLabels(entity);
  57. return Ok(entity);
  58. }
  59. [HttpPut("{id:long}")]
  60. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0PriorityRuleUpsertDto dto)
  61. {
  62. var entity = await _rep.GetByIdAsync(id);
  63. if (entity == null) return NotFound();
  64. ApplyUpsert(entity, dto);
  65. entity.UpdatedAt = DateTime.Now;
  66. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  67. FillRuleExprLabels(entity);
  68. return Ok(entity);
  69. }
  70. [HttpPatch("{id:long}/toggle-enabled")]
  71. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0PriorityRuleToggleDto dto)
  72. {
  73. var entity = await _rep.GetByIdAsync(id);
  74. if (entity == null) return NotFound();
  75. entity.IsEnabled = dto.IsEnabled;
  76. entity.UpdatedAt = DateTime.Now;
  77. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  78. return Ok(entity);
  79. }
  80. [HttpDelete("{id:long}")]
  81. public async Task<IActionResult> DeleteAsync(long id)
  82. {
  83. var item = await _rep.GetByIdAsync(id);
  84. if (item == null) return NotFound();
  85. await _rep.DeleteAsync(item);
  86. return Ok(new { message = "删除成功" });
  87. }
  88. // ── helpers ──────────────────────────────────────────────────────────────
  89. private static AdoS0PriorityRule MapToEntity(AdoS0PriorityRuleUpsertDto dto)
  90. {
  91. var entity = new AdoS0PriorityRule();
  92. ApplyUpsert(entity, dto);
  93. return entity;
  94. }
  95. private static void ApplyUpsert(AdoS0PriorityRule entity, AdoS0PriorityRuleUpsertDto dto)
  96. {
  97. entity.CompanyRefId = dto.CompanyRefId;
  98. entity.FactoryRefId = dto.FactoryRefId;
  99. entity.Code = dto.Code;
  100. entity.Name = dto.Name;
  101. entity.PriorityLevel = dto.PriorityLevel;
  102. entity.SortDirection = dto.SortDirection;
  103. entity.SourceEntity = dto.SourceEntity;
  104. entity.SourceField = dto.SourceField;
  105. entity.SourceFieldType = dto.SourceFieldType;
  106. entity.SourceLinkField = dto.SourceLinkField;
  107. entity.WorkOrderField = dto.WorkOrderField;
  108. entity.WorkOrderFieldType = dto.WorkOrderFieldType;
  109. entity.WorkOrderLinkField = dto.WorkOrderLinkField;
  110. entity.IsEnabled = dto.IsEnabled;
  111. entity.Remark = dto.Remark;
  112. entity.RuleExpr = BuildRuleExpr(dto.CustomerType, dto.OrderType, dto.DueStatus);
  113. }
  114. private static string? BuildRuleExpr(string? customerType, string? orderType, string? dueStatus)
  115. {
  116. if (string.IsNullOrWhiteSpace(customerType) &&
  117. string.IsNullOrWhiteSpace(orderType) &&
  118. string.IsNullOrWhiteSpace(dueStatus))
  119. return null;
  120. return JsonSerializer.Serialize(new
  121. {
  122. customerType,
  123. orderType,
  124. dueStatus,
  125. });
  126. }
  127. private static void FillRuleExprLabels(AdoS0PriorityRule item)
  128. {
  129. if (string.IsNullOrWhiteSpace(item.RuleExpr)) return;
  130. try
  131. {
  132. var doc = JsonSerializer.Deserialize<JsonElement>(item.RuleExpr);
  133. if (doc.TryGetProperty("customerType", out var ct)) item.CustomerTypeLabel = ct.GetString();
  134. if (doc.TryGetProperty("orderType", out var ot)) item.OrderTypeLabel = ot.GetString();
  135. if (doc.TryGetProperty("dueStatus", out var ds)) item.DueStatusLabel = ds.GetString();
  136. }
  137. catch { /* 忽略格式错误的历史数据 */ }
  138. }
  139. }