AdoS0MaterialPlanCyclesController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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>S0 物料计划周期标准(与订单评审/排程周期维度一致:工厂 + 订单类型 + std_hours)。</summary>
  6. [ApiController]
  7. [Route("api/s0/supply/material-plan-cycles")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class AdoS0MaterialPlanCyclesController : ControllerBase
  11. {
  12. private readonly SqlSugarRepository<AdoS0MaterialPlanCycle> _rep;
  13. public AdoS0MaterialPlanCyclesController(SqlSugarRepository<AdoS0MaterialPlanCycle> rep) => _rep = rep;
  14. private static string NormalizeOrderType(string? orderType) => orderType?.Trim() ?? string.Empty;
  15. [HttpGet]
  16. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MaterialPlanCycleQueryDto q)
  17. {
  18. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  19. var query = _rep.AsQueryable()
  20. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  21. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  22. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  23. .WhereIF(!string.IsNullOrWhiteSpace(q.OrderType), x => x.OrderType == q.OrderType)
  24. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive!.Value)
  25. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  26. x => x.OrderType.Contains(q.Keyword!)
  27. || (x.DomainCode != null && x.DomainCode.Contains(q.Keyword!))
  28. || (x.Remarks != null && x.Remarks.Contains(q.Keyword!)));
  29. var total = await query.CountAsync();
  30. var list = await query.OrderByDescending(x => x.CreateTime).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  31. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  32. }
  33. [HttpGet("{id:long}")]
  34. public async Task<IActionResult> GetAsync(long id)
  35. {
  36. var item = await _rep.GetByIdAsync(id);
  37. return item == null ? NotFound() : Ok(item);
  38. }
  39. [HttpPost]
  40. public async Task<IActionResult> CreateAsync([FromBody] AdoS0MaterialPlanCycleUpsertDto dto)
  41. {
  42. var orderType = NormalizeOrderType(dto.OrderType);
  43. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.OrderType == orderType))
  44. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "该工厂与订单类型组合已存在物料计划周期标准");
  45. var now = DateTime.Now;
  46. var entity = new AdoS0MaterialPlanCycle
  47. {
  48. CompanyRefId = dto.CompanyRefId,
  49. FactoryRefId = dto.FactoryRefId,
  50. DomainCode = dto.DomainCode,
  51. OrderType = orderType,
  52. StdHours = dto.StdHours,
  53. Remarks = dto.Remarks?.Trim(),
  54. IsActive = dto.IsActive,
  55. CreateUser = dto.CreateUser,
  56. CreateTime = now,
  57. UpdateUser = dto.UpdateUser,
  58. UpdateTime = null
  59. };
  60. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  61. return Ok(entity);
  62. }
  63. [HttpPut("{id:long}")]
  64. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MaterialPlanCycleUpsertDto dto)
  65. {
  66. var entity = await _rep.GetByIdAsync(id);
  67. if (entity == null) return NotFound();
  68. var orderType = NormalizeOrderType(dto.OrderType);
  69. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.OrderType == orderType))
  70. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "该工厂与订单类型组合已存在其他记录");
  71. entity.CompanyRefId = dto.CompanyRefId;
  72. entity.FactoryRefId = dto.FactoryRefId;
  73. entity.DomainCode = dto.DomainCode;
  74. entity.OrderType = orderType;
  75. entity.StdHours = dto.StdHours;
  76. entity.Remarks = dto.Remarks?.Trim();
  77. entity.IsActive = dto.IsActive;
  78. entity.UpdateUser = dto.UpdateUser;
  79. entity.UpdateTime = DateTime.Now;
  80. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  81. return Ok(entity);
  82. }
  83. [HttpPatch("{id:long}/toggle-enabled")]
  84. public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0MaterialPlanCycleToggleActiveDto dto)
  85. {
  86. var entity = await _rep.GetByIdAsync(id);
  87. if (entity == null) return NotFound();
  88. entity.IsActive = dto.IsActive;
  89. entity.UpdateTime = DateTime.Now;
  90. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  91. return Ok(entity);
  92. }
  93. [HttpDelete("{id:long}")]
  94. public async Task<IActionResult> DeleteAsync(long id)
  95. {
  96. var item = await _rep.GetByIdAsync(id);
  97. if (item == null) return NotFound();
  98. await _rep.DeleteAsync(item);
  99. return Ok(new { message = "删除成功" });
  100. }
  101. }