AdoS0MfgRoutingOpDetailsController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Manufacturing;
  2. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Entity.S0.Manufacturing;
  4. using Admin.NET.Plugin.AiDOP.Infrastructure;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing;
  6. [ApiController]
  7. [Route("api/s0/manufacturing/routing-op-details")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class AdoS0MfgRoutingOpDetailsController : ControllerBase
  11. {
  12. private readonly SqlSugarRepository<AdoS0MfgRoutingOpDetail> _rep;
  13. public AdoS0MfgRoutingOpDetailsController(SqlSugarRepository<AdoS0MfgRoutingOpDetail> rep)
  14. {
  15. _rep = rep;
  16. }
  17. [HttpGet]
  18. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MfgRoutingOpDetailQueryDto q)
  19. {
  20. var page = q.EffectivePage;
  21. var pageSize = q.PageSize;
  22. (page, pageSize) = PagingGuard.Normalize(page, 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 =>
  27. x.RouteCode.Contains(q.Keyword!) ||
  28. x.RouteName.Contains(q.Keyword!) ||
  29. x.MaterialCode.Contains(q.Keyword!) ||
  30. x.OperationCode.Contains(q.Keyword!) ||
  31. x.OperationDescription.Contains(q.Keyword!) ||
  32. x.WorkCenterCode.Contains(q.Keyword!))
  33. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value);
  34. var total = await query.CountAsync();
  35. var list = await query.OrderBy(x => x.RouteCode).OrderBy(x => x.SortNo).OrderBy(x => x.Id)
  36. .Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
  37. return Ok(new { total, page, pageSize, list });
  38. }
  39. [HttpGet("{id:long}")]
  40. public async Task<IActionResult> GetAsync(long id)
  41. {
  42. var item = await _rep.GetByIdAsync(id);
  43. return item == null ? NotFound() : Ok(item);
  44. }
  45. [HttpPost]
  46. public async Task<IActionResult> CreateAsync([FromBody] AdoS0MfgRoutingOpDetailUpsertDto dto)
  47. {
  48. var err = ValidateUpsert(dto);
  49. if (err != null) return BadRequest(new { message = err });
  50. var entity = MapToEntity(dto);
  51. entity.CreatedAt = DateTime.Now;
  52. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  53. return Ok(entity);
  54. }
  55. [HttpPut("{id:long}")]
  56. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MfgRoutingOpDetailUpsertDto dto)
  57. {
  58. var err = ValidateUpsert(dto);
  59. if (err != null) return BadRequest(new { message = err });
  60. var entity = await _rep.GetByIdAsync(id);
  61. if (entity == null) return NotFound();
  62. ApplyUpsert(entity, dto);
  63. entity.UpdatedAt = DateTime.Now;
  64. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  65. return Ok(entity);
  66. }
  67. [HttpPatch("{id:long}/toggle-enabled")]
  68. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  69. {
  70. var entity = await _rep.GetByIdAsync(id);
  71. if (entity == null) return NotFound();
  72. entity.IsEnabled = dto.IsEnabled;
  73. entity.UpdatedAt = DateTime.Now;
  74. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  75. return Ok(entity);
  76. }
  77. [HttpDelete("{id:long}")]
  78. public async Task<IActionResult> DeleteAsync(long id)
  79. {
  80. var item = await _rep.GetByIdAsync(id);
  81. if (item == null) return NotFound();
  82. await _rep.DeleteAsync(item);
  83. return Ok(new { message = "删除成功" });
  84. }
  85. private static string? ValidateUpsert(AdoS0MfgRoutingOpDetailUpsertDto dto)
  86. {
  87. if (string.IsNullOrWhiteSpace(dto.RouteCode)) return "工艺路线编码不能为空";
  88. if (string.IsNullOrWhiteSpace(dto.RouteName)) return "工艺路线名称不能为空";
  89. if (string.IsNullOrWhiteSpace(dto.MaterialCode)) return "物料编码不能为空";
  90. if (string.IsNullOrWhiteSpace(dto.OperationCode)) return "工序编码不能为空";
  91. return null;
  92. }
  93. private static AdoS0MfgRoutingOpDetail MapToEntity(AdoS0MfgRoutingOpDetailUpsertDto dto)
  94. {
  95. var e = new AdoS0MfgRoutingOpDetail();
  96. ApplyUpsert(e, dto);
  97. return e;
  98. }
  99. private static void ApplyUpsert(AdoS0MfgRoutingOpDetail e, AdoS0MfgRoutingOpDetailUpsertDto dto)
  100. {
  101. e.CompanyRefId = dto.CompanyRefId;
  102. e.FactoryRefId = dto.FactoryRefId;
  103. e.RouteCode = dto.RouteCode.Trim();
  104. e.RouteName = dto.RouteName.Trim();
  105. e.MaterialCode = dto.MaterialCode.Trim();
  106. e.OperationCode = dto.OperationCode.Trim();
  107. e.OperationDescription = (dto.OperationDescription ?? string.Empty).Trim();
  108. e.WorkCenterCode = (dto.WorkCenterCode ?? string.Empty).Trim();
  109. e.MilestoneOperation = string.IsNullOrWhiteSpace(dto.MilestoneOperation) ? null : dto.MilestoneOperation.Trim();
  110. e.Ufld1 = dto.Ufld1;
  111. e.Ufld2 = dto.Ufld2;
  112. e.UDeci1 = dto.UDeci1;
  113. e.Ufld3 = dto.Ufld3;
  114. e.Ufld4 = dto.Ufld4;
  115. e.Ufld5 = dto.Ufld5;
  116. e.UDeci2 = dto.UDeci2;
  117. e.Ufld6 = dto.Ufld6;
  118. e.Ufld7 = dto.Ufld7;
  119. e.Ufld8 = dto.Ufld8;
  120. e.UDeci3 = dto.UDeci3;
  121. e.Ufld9 = dto.Ufld9;
  122. e.UDeci4 = dto.UDeci4;
  123. e.UDeci5 = dto.UDeci5;
  124. e.SupplierCode = string.IsNullOrWhiteSpace(dto.SupplierCode) ? null : dto.SupplierCode.Trim();
  125. e.OutsourcedLeadTime = dto.OutsourcedLeadTime;
  126. e.SortNo = dto.SortNo;
  127. e.IsEnabled = dto.IsEnabled;
  128. e.IsConfirm = dto.IsConfirm;
  129. e.CommentIndex = dto.CommentIndex;
  130. e.CreateUser = dto.CreateUser;
  131. e.UpdateUser = dto.UpdateUser;
  132. }
  133. }