AdoS0OrderPriorityRulesController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 entity = new AdoS0OrderPriorityRule
  47. {
  48. CompanyRefId = dto.CompanyRefId,
  49. FactoryRefId = dto.FactoryRefId,
  50. Code = AdoS0SalesRules.ResolveOrderPriorityRuleCodeForCreate(
  51. dto.Code,
  52. DateTime.Now,
  53. Random.Shared.Next(1000, 9999)),
  54. Name = dto.Name,
  55. PriorityLevel = dto.PriorityLevel,
  56. SortDirection = dto.SortDirection,
  57. SourceEntity = dto.SourceEntity,
  58. SourceField = dto.SourceField,
  59. SourceFieldType = dto.SourceFieldType,
  60. SourceLinkField = dto.SourceLinkField,
  61. WorkOrderField = dto.WorkOrderField,
  62. WorkOrderFieldType = dto.WorkOrderFieldType,
  63. WorkOrderLinkField = dto.WorkOrderLinkField,
  64. RuleExpr = dto.RuleExpr,
  65. Remark = dto.Remark,
  66. IsEnabled = dto.IsEnabled,
  67. CreatedAt = DateTime.Now
  68. };
  69. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  70. return Ok(entity);
  71. }
  72. [HttpPut("{id:long}")]
  73. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0OrderPriorityRuleUpsertDto dto)
  74. {
  75. var entity = await _rep.GetByIdAsync(id);
  76. if (entity == null) return NotFound();
  77. entity.CompanyRefId = dto.CompanyRefId;
  78. entity.FactoryRefId = dto.FactoryRefId;
  79. entity.Code = AdoS0SalesRules.ResolveOrderPriorityRuleCodeForUpdate(dto.Code, entity.Code);
  80. entity.Name = dto.Name;
  81. entity.PriorityLevel = dto.PriorityLevel;
  82. entity.SortDirection = dto.SortDirection;
  83. entity.SourceEntity = dto.SourceEntity;
  84. entity.SourceField = dto.SourceField;
  85. entity.SourceFieldType = dto.SourceFieldType;
  86. entity.SourceLinkField = dto.SourceLinkField;
  87. entity.WorkOrderField = dto.WorkOrderField;
  88. entity.WorkOrderFieldType = dto.WorkOrderFieldType;
  89. entity.WorkOrderLinkField = dto.WorkOrderLinkField;
  90. entity.RuleExpr = dto.RuleExpr;
  91. entity.Remark = dto.Remark;
  92. entity.IsEnabled = dto.IsEnabled;
  93. entity.UpdatedAt = DateTime.Now;
  94. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  95. return Ok(entity);
  96. }
  97. [HttpPatch("{id:long}/toggle-enabled")]
  98. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  99. {
  100. var entity = await _rep.GetByIdAsync(id);
  101. if (entity == null) return NotFound();
  102. entity.IsEnabled = dto.IsEnabled;
  103. entity.UpdatedAt = DateTime.Now;
  104. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  105. return Ok(entity);
  106. }
  107. [HttpDelete("{id:long}")]
  108. public async Task<IActionResult> DeleteAsync(long id)
  109. {
  110. var item = await _rep.GetByIdAsync(id);
  111. if (item == null) return NotFound();
  112. await _rep.DeleteAsync(item);
  113. return Ok(new { message = "删除成功" });
  114. }
  115. }