AdoS0CategoryLeadTimesController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Supply;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Supply;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Supply;
  5. /// <summary>
  6. /// S0 品类采购前置期主数据(订单评审/MRP 取数优先配置)。
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/supply/category-lead-times")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0CategoryLeadTimesController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0CategoryLeadTime> _rep;
  15. public AdoS0CategoryLeadTimesController(SqlSugarRepository<AdoS0CategoryLeadTime> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0CategoryLeadTimeQueryDto q)
  21. {
  22. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  23. var baseQuery = _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.DomainCode), x => x.DomainCode == q.DomainCode)
  27. .WhereIF(!string.IsNullOrWhiteSpace(q.CategoryCode), x => x.CategoryCode.Contains(q.CategoryCode!))
  28. .WhereIF(!string.IsNullOrWhiteSpace(q.OwnerApplication), x => x.OwnerApplication.Contains(q.OwnerApplication!))
  29. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
  30. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  31. x => x.CategoryCode.Contains(q.Keyword!)
  32. || (x.CategoryName != null && x.CategoryName.Contains(q.Keyword!))
  33. || x.OwnerApplication.Contains(q.Keyword!));
  34. var total = await baseQuery.CountAsync();
  35. var list = await baseQuery
  36. .OrderByDescending(x => x.CreateTime)
  37. .Skip((q.Page - 1) * q.PageSize)
  38. .Take(q.PageSize)
  39. .ToListAsync();
  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. var item = await _rep.GetByIdAsync(id);
  46. if (item == null) return NotFound();
  47. return Ok(item);
  48. }
  49. [HttpPost]
  50. public async Task<IActionResult> CreateAsync([FromBody] AdoS0CategoryLeadTimeUpsertDto dto)
  51. {
  52. var uniqueErr = await EnsureUniqueAsync(dto, null);
  53. if (uniqueErr != null) return uniqueErr;
  54. var now = DateTime.Now;
  55. var entity = new AdoS0CategoryLeadTime
  56. {
  57. CompanyRefId = dto.CompanyRefId,
  58. FactoryRefId = dto.FactoryRefId,
  59. DomainCode = dto.DomainCode,
  60. CategoryCode = dto.CategoryCode.Trim(),
  61. CategoryName = dto.CategoryName,
  62. OwnerApplication = dto.OwnerApplication.Trim(),
  63. LeadTimeDays = dto.LeadTimeDays,
  64. Remarks = dto.Remarks,
  65. IsActive = dto.IsActive,
  66. CreateUser = dto.CreateUser,
  67. CreateTime = now,
  68. UpdateUser = dto.UpdateUser,
  69. UpdateTime = null
  70. };
  71. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  72. return Ok(entity);
  73. }
  74. [HttpPut("{id:long}")]
  75. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0CategoryLeadTimeUpsertDto dto)
  76. {
  77. var entity = await _rep.GetByIdAsync(id);
  78. if (entity == null) return NotFound();
  79. var uniqueErr = await EnsureUniqueAsync(dto, id);
  80. if (uniqueErr != null) return uniqueErr;
  81. entity.CompanyRefId = dto.CompanyRefId;
  82. entity.FactoryRefId = dto.FactoryRefId;
  83. entity.DomainCode = dto.DomainCode;
  84. entity.CategoryCode = dto.CategoryCode.Trim();
  85. entity.CategoryName = dto.CategoryName;
  86. entity.OwnerApplication = dto.OwnerApplication.Trim();
  87. entity.LeadTimeDays = dto.LeadTimeDays;
  88. entity.Remarks = dto.Remarks;
  89. entity.IsActive = dto.IsActive;
  90. entity.UpdateUser = dto.UpdateUser;
  91. entity.UpdateTime = DateTime.Now;
  92. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  93. return Ok(entity);
  94. }
  95. [HttpPatch("{id:long}/toggle-enabled")]
  96. public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0CategoryLeadTimeToggleActiveDto dto)
  97. {
  98. var entity = await _rep.GetByIdAsync(id);
  99. if (entity == null) return NotFound();
  100. entity.IsActive = dto.IsActive;
  101. entity.UpdateTime = DateTime.Now;
  102. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  103. return Ok(entity);
  104. }
  105. [HttpDelete("{id:long}")]
  106. public async Task<IActionResult> DeleteAsync(long id)
  107. {
  108. var item = await _rep.GetByIdAsync(id);
  109. if (item == null) return NotFound();
  110. await _rep.DeleteAsync(item);
  111. return Ok(new { message = "删除成功" });
  112. }
  113. private async Task<IActionResult?> EnsureUniqueAsync(AdoS0CategoryLeadTimeUpsertDto dto, long? id)
  114. {
  115. var categoryCode = dto.CategoryCode.Trim();
  116. var ownerApplication = dto.OwnerApplication.Trim();
  117. if (await _rep.IsAnyAsync(x =>
  118. x.Id != (id ?? 0)
  119. && x.FactoryRefId == dto.FactoryRefId
  120. && x.CategoryCode == categoryCode
  121. && x.OwnerApplication == ownerApplication))
  122. {
  123. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.InvalidRequest,
  124. "同工厂下“品类编码 + 物料属性”已存在前置期配置");
  125. }
  126. return null;
  127. }
  128. }