AdoS0PriorityRulesController.cs 6.7 KB

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