AdoS0MfgProductionLinesController.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /// <summary>
  7. /// 生产线维护(LineMaster 语义,表 ado_s0_mfg_line_master)。
  8. /// </summary>
  9. [ApiController]
  10. [Route("api/s0/manufacturing/production-lines")]
  11. [AllowAnonymous]
  12. [NonUnify]
  13. public class AdoS0MfgProductionLinesController : ControllerBase
  14. {
  15. private readonly SqlSugarRepository<AdoS0LineMaster> _rep;
  16. private readonly AdoS0ReferenceChecker _refChecker;
  17. public AdoS0MfgProductionLinesController(SqlSugarRepository<AdoS0LineMaster> rep, AdoS0ReferenceChecker refChecker)
  18. {
  19. _rep = rep;
  20. _refChecker = refChecker;
  21. }
  22. /// <summary>
  23. /// B1-9:LineMaster 6 个 Location 变体的统一 existence check。
  24. /// 全部复用 AdoS0ReferenceChecker.LocationExistsAsync,空值放行。
  25. /// </summary>
  26. private async Task<IActionResult?> ValidateLocationReferencesAsync(AdoS0LineMasterUpsertDto dto)
  27. {
  28. var checks = new (string? Value, string Label)[]
  29. {
  30. (dto.Location, "主库位"),
  31. (dto.VLocation, "虚拟库位"),
  32. (dto.Location2, "辅助库位2"),
  33. (dto.Location3, "辅助库位3"),
  34. (dto.PickingLocation, "拣料库位"),
  35. (dto.MidLocation, "中间库位"),
  36. };
  37. foreach (var (value, label) in checks)
  38. {
  39. if (!await _refChecker.LocationExistsAsync(value))
  40. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.ReferenceNotFound,
  41. $"{label}编码 '{value}' 不存在于库位主数据");
  42. }
  43. return null;
  44. }
  45. [HttpGet]
  46. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LineMasterQueryDto q)
  47. {
  48. var page = q.EffectivePage;
  49. var pageSize = q.PageSize;
  50. (page, pageSize) = PagingGuard.Normalize(page, pageSize);
  51. var query = _rep.AsQueryable()
  52. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  53. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  54. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  55. x => x.Line.Contains(q.Keyword!) || (x.Describe != null && x.Describe.Contains(q.Keyword!)))
  56. .WhereIF(!string.IsNullOrWhiteSpace(q.Line), x => x.Line.Contains(q.Line!))
  57. .WhereIF(!string.IsNullOrWhiteSpace(q.Workshop),
  58. x => x.Workshop != null && x.Workshop.Contains(q.Workshop!))
  59. .WhereIF(q.IsEnabled.HasValue, x => x.IsActive == q.IsEnabled!.Value);
  60. var total = await query.CountAsync();
  61. var entities = await query
  62. .OrderByDescending(x => x.Id)
  63. .Skip((page - 1) * pageSize)
  64. .Take(pageSize)
  65. .ToListAsync();
  66. // GeneralizedCodeMaster 中文说明:当前库无码表实体,占位供列表展示;后续可 JOIN 补全。
  67. var list = entities.Select(x => new
  68. {
  69. x.Id,
  70. x.CompanyRefId,
  71. x.FactoryRefId,
  72. x.Domain,
  73. x.Line,
  74. describe = x.Describe,
  75. x.LineType,
  76. x.LineCategory,
  77. x.Location,
  78. x.Workshop,
  79. vLocation = x.VLocation,
  80. x.Location2,
  81. x.Location3,
  82. pickingLocation = x.PickingLocation,
  83. midLocation = x.MidLocation,
  84. isActive = x.IsActive,
  85. x.CreateUser,
  86. x.CreateTime,
  87. x.UpdateUser,
  88. x.UpdateTime,
  89. lineCategoryComments = (string?)null,
  90. workshopComments = (string?)null
  91. }).ToList();
  92. return Ok(new { total, page, pageSize, list });
  93. }
  94. [HttpGet("{id:long}")]
  95. public async Task<IActionResult> GetAsync(long id)
  96. {
  97. var item = await _rep.GetByIdAsync(id);
  98. return item == null ? NotFound() : Ok(item);
  99. }
  100. [HttpPost]
  101. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LineMasterUpsertDto dto)
  102. {
  103. var locErr = await ValidateLocationReferencesAsync(dto);
  104. if (locErr != null) return locErr;
  105. var now = DateTime.Now;
  106. var entity = new AdoS0LineMaster
  107. {
  108. CompanyRefId = dto.CompanyRefId,
  109. FactoryRefId = dto.FactoryRefId,
  110. Domain = dto.Domain.Trim(),
  111. Line = dto.Line.Trim(),
  112. Describe = string.IsNullOrWhiteSpace(dto.Describe) ? null : dto.Describe.Trim(),
  113. LineType = string.IsNullOrWhiteSpace(dto.LineType) ? null : dto.LineType.Trim(),
  114. LineCategory = string.IsNullOrWhiteSpace(dto.LineCategory) ? null : dto.LineCategory.Trim(),
  115. Location = string.IsNullOrWhiteSpace(dto.Location) ? null : dto.Location.Trim(),
  116. Workshop = string.IsNullOrWhiteSpace(dto.Workshop) ? null : dto.Workshop.Trim(),
  117. VLocation = string.IsNullOrWhiteSpace(dto.VLocation) ? null : dto.VLocation.Trim(),
  118. Location2 = string.IsNullOrWhiteSpace(dto.Location2) ? null : dto.Location2.Trim(),
  119. Location3 = string.IsNullOrWhiteSpace(dto.Location3) ? null : dto.Location3.Trim(),
  120. PickingLocation = string.IsNullOrWhiteSpace(dto.PickingLocation) ? null : dto.PickingLocation.Trim(),
  121. MidLocation = string.IsNullOrWhiteSpace(dto.MidLocation) ? null : dto.MidLocation.Trim(),
  122. IsActive = dto.IsActive,
  123. CreateUser = dto.CreateUser,
  124. CreateTime = now,
  125. UpdateUser = null,
  126. UpdateTime = null
  127. };
  128. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  129. return Ok(entity);
  130. }
  131. [HttpPut("{id:long}")]
  132. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LineMasterUpsertDto dto)
  133. {
  134. var locErr = await ValidateLocationReferencesAsync(dto);
  135. if (locErr != null) return locErr;
  136. var entity = await _rep.GetByIdAsync(id);
  137. if (entity == null) return NotFound();
  138. entity.CompanyRefId = dto.CompanyRefId;
  139. entity.FactoryRefId = dto.FactoryRefId;
  140. entity.Domain = dto.Domain.Trim();
  141. entity.Line = dto.Line.Trim();
  142. entity.Describe = string.IsNullOrWhiteSpace(dto.Describe) ? null : dto.Describe.Trim();
  143. entity.LineType = string.IsNullOrWhiteSpace(dto.LineType) ? null : dto.LineType.Trim();
  144. entity.LineCategory = string.IsNullOrWhiteSpace(dto.LineCategory) ? null : dto.LineCategory.Trim();
  145. entity.Location = string.IsNullOrWhiteSpace(dto.Location) ? null : dto.Location.Trim();
  146. entity.Workshop = string.IsNullOrWhiteSpace(dto.Workshop) ? null : dto.Workshop.Trim();
  147. entity.VLocation = string.IsNullOrWhiteSpace(dto.VLocation) ? null : dto.VLocation.Trim();
  148. entity.Location2 = string.IsNullOrWhiteSpace(dto.Location2) ? null : dto.Location2.Trim();
  149. entity.Location3 = string.IsNullOrWhiteSpace(dto.Location3) ? null : dto.Location3.Trim();
  150. entity.PickingLocation = string.IsNullOrWhiteSpace(dto.PickingLocation) ? null : dto.PickingLocation.Trim();
  151. entity.MidLocation = string.IsNullOrWhiteSpace(dto.MidLocation) ? null : dto.MidLocation.Trim();
  152. entity.IsActive = dto.IsActive;
  153. entity.UpdateUser = dto.UpdateUser;
  154. entity.UpdateTime = DateTime.Now;
  155. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  156. return Ok(entity);
  157. }
  158. [HttpPatch("{id:long}/toggle-enabled")]
  159. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  160. {
  161. var entity = await _rep.GetByIdAsync(id);
  162. if (entity == null) return NotFound();
  163. entity.IsActive = dto.IsEnabled;
  164. entity.UpdateTime = DateTime.Now;
  165. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  166. return Ok(new { entity.Id, isActive = entity.IsActive, entity.UpdateTime });
  167. }
  168. [HttpDelete("{id:long}")]
  169. public async Task<IActionResult> DeleteAsync(long id)
  170. {
  171. var item = await _rep.GetByIdAsync(id);
  172. if (item == null) return NotFound();
  173. await _rep.DeleteAsync(item);
  174. return Ok(new { message = "删除成功" });
  175. }
  176. }