AdoS0OrderPriorityRulesController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. Id = Guid.NewGuid().ToString("D").ToUpperInvariant(),
  62. DomainCode = dto.DomainCode,
  63. Descr = dto.Descr ?? string.Empty,
  64. Value = dto.Value,
  65. Priority = dto.Priority,
  66. OrderBy = dto.OrderBy,
  67. SourceTable = dto.SourceTable,
  68. SourceColumn = dto.SourceColumn,
  69. SourceType = dto.SourceType,
  70. SourceId = dto.SourceId,
  71. ValueType = dto.ValueType,
  72. ValueId = dto.ValueId,
  73. IsActive = dto.IsActive,
  74. CreateUser = dto.CreateUser,
  75. LegacyCreateUser = dto.LegacyCreateUser,
  76. CreateTime = now,
  77. UpdateUser = dto.UpdateUser,
  78. UpdateTime = null
  79. };
  80. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  81. return Ok(entity);
  82. }
  83. [HttpPut("{id}")]
  84. public async Task<IActionResult> UpdateAsync(string id, [FromBody] AdoS0PriorityCodeUpsertDto dto)
  85. {
  86. var entity = await _rep.GetByIdAsync(id);
  87. if (entity == null) return NotFound();
  88. entity.DomainCode = dto.DomainCode;
  89. entity.Descr = dto.Descr ?? string.Empty;
  90. entity.Value = dto.Value;
  91. entity.Priority = dto.Priority;
  92. entity.OrderBy = dto.OrderBy;
  93. entity.SourceTable = dto.SourceTable;
  94. entity.SourceColumn = dto.SourceColumn;
  95. entity.SourceType = dto.SourceType;
  96. entity.SourceId = dto.SourceId;
  97. entity.ValueType = dto.ValueType;
  98. entity.ValueId = dto.ValueId;
  99. entity.IsActive = dto.IsActive;
  100. entity.UpdateUser = dto.UpdateUser;
  101. entity.LegacyCreateUser = dto.LegacyCreateUser;
  102. entity.UpdateTime = DateTime.Now;
  103. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  104. return Ok(entity);
  105. }
  106. [HttpPatch("{id}/toggle-enabled")]
  107. public async Task<IActionResult> ToggleActiveAsync(string id, [FromBody] AdoS0PriorityCodeToggleActiveDto dto)
  108. {
  109. var entity = await _rep.GetByIdAsync(id);
  110. if (entity == null) return NotFound();
  111. entity.IsActive = dto.IsActive;
  112. entity.UpdateTime = DateTime.Now;
  113. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  114. return Ok(entity);
  115. }
  116. [HttpDelete("{id}")]
  117. public async Task<IActionResult> DeleteAsync(string id)
  118. {
  119. var item = await _rep.GetByIdAsync(id);
  120. if (item == null) return NotFound();
  121. await _rep.DeleteAsync(item);
  122. return Ok(new { message = "删除成功" });
  123. }
  124. }