AdoS0OrderPriorityRulesController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 订单优先级配置(PriorityCode 语义)
  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<AdoS0PriorityCode> _rep;
  15. public AdoS0OrderPriorityRulesController(SqlSugarRepository<AdoS0PriorityCode> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0PriorityCodeQueryDto q)
  21. {
  22. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  23. var query = _rep.AsQueryable()
  24. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  25. .WhereIF(!string.IsNullOrWhiteSpace(q.Descr), x => x.Descr.Contains(q.Descr!))
  26. .WhereIF(q.Priority.HasValue, x => x.Priority == q.Priority.Value)
  27. .WhereIF(q.OrderBy.HasValue, x => x.OrderBy == q.OrderBy.Value)
  28. .WhereIF(!string.IsNullOrWhiteSpace(q.SourceTable), x => x.SourceTable != null && x.SourceTable.Contains(q.SourceTable!))
  29. .WhereIF(!string.IsNullOrWhiteSpace(q.SourceColumn), x => x.SourceColumn != null && x.SourceColumn.Contains(q.SourceColumn!))
  30. .WhereIF(!string.IsNullOrWhiteSpace(q.Value), x => x.Value != null && x.Value.Contains(q.Value!))
  31. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
  32. .WhereIF(
  33. !string.IsNullOrWhiteSpace(q.Keyword),
  34. x => x.Descr.Contains(q.Keyword!) ||
  35. (x.SourceTable != null && x.SourceTable.Contains(q.Keyword!)) ||
  36. (x.Value != null && x.Value.Contains(q.Keyword!)));
  37. var total = await query.CountAsync();
  38. var list = await query
  39. .OrderByDescending(x => x.CreateTime)
  40. .Skip((q.Page - 1) * q.PageSize)
  41. .Take(q.PageSize)
  42. .ToListAsync();
  43. foreach (var x in list)
  44. x.OrderByText = AdoS0SalesRules.PriorityCodeOrderByText(x.OrderBy);
  45. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  46. }
  47. [HttpGet("{id}")]
  48. public async Task<IActionResult> GetAsync(string id)
  49. {
  50. var item = await _rep.GetByIdAsync(id);
  51. if (item == null) return NotFound();
  52. item.OrderByText = AdoS0SalesRules.PriorityCodeOrderByText(item.OrderBy);
  53. return Ok(item);
  54. }
  55. [HttpPost]
  56. public async Task<IActionResult> CreateAsync([FromBody] AdoS0PriorityCodeUpsertDto dto)
  57. {
  58. var now = DateTime.Now;
  59. var entity = new AdoS0PriorityCode
  60. {
  61. DomainCode = dto.DomainCode,
  62. Descr = dto.Descr ?? string.Empty,
  63. Value = dto.Value,
  64. Priority = dto.Priority,
  65. OrderBy = dto.OrderBy,
  66. SourceTable = dto.SourceTable,
  67. SourceColumn = dto.SourceColumn,
  68. SourceType = dto.SourceType,
  69. SourceId = dto.SourceId,
  70. ValueType = dto.ValueType,
  71. ValueId = dto.ValueId,
  72. IsActive = dto.IsActive,
  73. CreateUser = dto.CreateUser,
  74. LegacyCreateUser = dto.LegacyCreateUser,
  75. CreateTime = now,
  76. UpdateUser = dto.UpdateUser,
  77. UpdateTime = null
  78. };
  79. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  80. return Ok(entity);
  81. }
  82. [HttpPut("{id}")]
  83. public async Task<IActionResult> UpdateAsync(string id, [FromBody] AdoS0PriorityCodeUpsertDto dto)
  84. {
  85. var entity = await _rep.GetByIdAsync(id);
  86. if (entity == null) return NotFound();
  87. entity.DomainCode = dto.DomainCode;
  88. entity.Descr = dto.Descr ?? string.Empty;
  89. entity.Value = dto.Value;
  90. entity.Priority = dto.Priority;
  91. entity.OrderBy = dto.OrderBy;
  92. entity.SourceTable = dto.SourceTable;
  93. entity.SourceColumn = dto.SourceColumn;
  94. entity.SourceType = dto.SourceType;
  95. entity.SourceId = dto.SourceId;
  96. entity.ValueType = dto.ValueType;
  97. entity.ValueId = dto.ValueId;
  98. entity.IsActive = dto.IsActive;
  99. entity.UpdateUser = dto.UpdateUser;
  100. entity.LegacyCreateUser = dto.LegacyCreateUser;
  101. entity.UpdateTime = DateTime.Now;
  102. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  103. return Ok(entity);
  104. }
  105. [HttpPatch("{id}/toggle-enabled")]
  106. public async Task<IActionResult> ToggleActiveAsync(string id, [FromBody] AdoS0PriorityCodeToggleActiveDto dto)
  107. {
  108. var entity = await _rep.GetByIdAsync(id);
  109. if (entity == null) return NotFound();
  110. entity.IsActive = dto.IsActive;
  111. entity.UpdateTime = DateTime.Now;
  112. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  113. return Ok(entity);
  114. }
  115. [HttpDelete("{id}")]
  116. public async Task<IActionResult> DeleteAsync(string id)
  117. {
  118. var item = await _rep.GetByIdAsync(id);
  119. if (item == null) return NotFound();
  120. await _rep.DeleteAsync(item);
  121. return Ok(new { message = "删除成功" });
  122. }
  123. }