AdoS0MfgRoutingsController.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Entity.S0.Sales;
  5. using Admin.NET.Plugin.AiDOP.Infrastructure;
  6. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing;
  7. [ApiController]
  8. [Route("api/s0/manufacturing/routings")]
  9. [AllowAnonymous]
  10. [NonUnify]
  11. public class AdoS0MfgRoutingsController : ControllerBase
  12. {
  13. private readonly SqlSugarRepository<AdoS0MfgRouting> _routingRep;
  14. private readonly SqlSugarRepository<AdoS0MfgRoutingOperation> _opRep;
  15. private readonly SqlSugarRepository<AdoS0Material> _materialRep;
  16. private readonly SqlSugarRepository<AdoS0MfgStandardOperation> _stdOpRep;
  17. public AdoS0MfgRoutingsController(
  18. SqlSugarRepository<AdoS0MfgRouting> routingRep,
  19. SqlSugarRepository<AdoS0MfgRoutingOperation> opRep,
  20. SqlSugarRepository<AdoS0Material> materialRep,
  21. SqlSugarRepository<AdoS0MfgStandardOperation> stdOpRep)
  22. {
  23. _routingRep = routingRep;
  24. _opRep = opRep;
  25. _materialRep = materialRep;
  26. _stdOpRep = stdOpRep;
  27. }
  28. [HttpGet]
  29. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MfgRoutingQueryDto q)
  30. {
  31. var page = q.EffectivePage;
  32. var pageSize = q.PageSize;
  33. (page, pageSize) = PagingGuard.Normalize(page, pageSize);
  34. var query = _routingRep.AsQueryable()
  35. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  36. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  37. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  38. x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
  39. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value);
  40. var total = await query.CountAsync();
  41. var list = await query.OrderByDescending(x => x.CreatedAt).Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
  42. return Ok(new { total, page, pageSize, list });
  43. }
  44. [HttpGet("{id:long}")]
  45. public async Task<IActionResult> GetDetailAsync(long id)
  46. {
  47. var header = await _routingRep.GetByIdAsync(id);
  48. if (header == null) return NotFound();
  49. var operations = await _opRep.AsQueryable()
  50. .Where(x => x.RoutingId == id)
  51. .OrderBy(x => x.SortNo)
  52. .ToListAsync();
  53. return Ok(new { header, operations });
  54. }
  55. [HttpPost]
  56. public async Task<IActionResult> CreateAsync([FromBody] AdoS0MfgRoutingUpsertDto dto)
  57. {
  58. var err = ValidateRouting(dto);
  59. if (err != null) return BadRequest(new { message = err });
  60. var fkErr = await ValidateRoutingFksAsync(dto);
  61. if (fkErr != null) return BadRequest(new { message = fkErr });
  62. var db = _routingRep.Context;
  63. await db.Ado.BeginTranAsync();
  64. try
  65. {
  66. var routing = new AdoS0MfgRouting
  67. {
  68. CompanyRefId = dto.CompanyRefId,
  69. FactoryRefId = dto.FactoryRefId,
  70. Code = dto.Code,
  71. Name = dto.Name,
  72. MaterialId = dto.MaterialId,
  73. BizVersion = dto.BizVersion,
  74. DocStatus = dto.DocStatus,
  75. Remark = dto.Remark,
  76. IsEnabled = dto.IsEnabled,
  77. CreatedAt = DateTime.Now
  78. };
  79. routing = await _routingRep.AsInsertable(routing).ExecuteReturnEntityAsync();
  80. foreach (var line in dto.Operations)
  81. {
  82. var row = MapRoutingOp(routing.Id, dto.CompanyRefId, dto.FactoryRefId, line);
  83. await _opRep.AsInsertable(row).ExecuteCommandAsync();
  84. }
  85. await db.Ado.CommitTranAsync();
  86. return await GetDetailAsync(routing.Id);
  87. }
  88. catch
  89. {
  90. await db.Ado.RollbackTranAsync();
  91. throw;
  92. }
  93. }
  94. [HttpPut("{id:long}")]
  95. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MfgRoutingUpsertDto dto)
  96. {
  97. var err = ValidateRouting(dto);
  98. if (err != null) return BadRequest(new { message = err });
  99. var header = await _routingRep.GetByIdAsync(id);
  100. if (header == null) return NotFound();
  101. var fkErr = await ValidateRoutingFksAsync(dto);
  102. if (fkErr != null) return BadRequest(new { message = fkErr });
  103. var db = _routingRep.Context;
  104. await db.Ado.BeginTranAsync();
  105. try
  106. {
  107. header.CompanyRefId = dto.CompanyRefId;
  108. header.FactoryRefId = dto.FactoryRefId;
  109. header.Code = dto.Code;
  110. header.Name = dto.Name;
  111. header.MaterialId = dto.MaterialId;
  112. header.BizVersion = dto.BizVersion;
  113. header.DocStatus = dto.DocStatus;
  114. header.Remark = dto.Remark;
  115. header.IsEnabled = dto.IsEnabled;
  116. header.UpdatedAt = DateTime.Now;
  117. await _routingRep.AsUpdateable(header).ExecuteCommandAsync();
  118. await _opRep.AsDeleteable().Where(x => x.RoutingId == id).ExecuteCommandAsync();
  119. foreach (var line in dto.Operations)
  120. {
  121. var row = MapRoutingOp(id, dto.CompanyRefId, dto.FactoryRefId, line);
  122. await _opRep.AsInsertable(row).ExecuteCommandAsync();
  123. }
  124. await db.Ado.CommitTranAsync();
  125. return await GetDetailAsync(id);
  126. }
  127. catch
  128. {
  129. await db.Ado.RollbackTranAsync();
  130. throw;
  131. }
  132. }
  133. [HttpPatch("{id:long}/toggle-enabled")]
  134. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  135. {
  136. var entity = await _routingRep.GetByIdAsync(id);
  137. if (entity == null) return NotFound();
  138. entity.IsEnabled = dto.IsEnabled;
  139. entity.UpdatedAt = DateTime.Now;
  140. await _routingRep.AsUpdateable(entity).ExecuteCommandAsync();
  141. return Ok(entity);
  142. }
  143. [HttpDelete("{id:long}")]
  144. public async Task<IActionResult> DeleteAsync(long id)
  145. {
  146. var header = await _routingRep.GetByIdAsync(id);
  147. if (header == null) return NotFound();
  148. var db = _routingRep.Context;
  149. await db.Ado.BeginTranAsync();
  150. try
  151. {
  152. await _opRep.AsDeleteable().Where(x => x.RoutingId == id).ExecuteCommandAsync();
  153. await _routingRep.DeleteAsync(header);
  154. await db.Ado.CommitTranAsync();
  155. return Ok(new { message = "删除成功" });
  156. }
  157. catch
  158. {
  159. await db.Ado.RollbackTranAsync();
  160. throw;
  161. }
  162. }
  163. private static string? ValidateRouting(AdoS0MfgRoutingUpsertDto dto)
  164. {
  165. if (dto.Operations == null || dto.Operations.Count == 0)
  166. return "工艺路线至少包含一道工序";
  167. return null;
  168. }
  169. private async Task<string?> ValidateRoutingFksAsync(AdoS0MfgRoutingUpsertDto dto)
  170. {
  171. if (dto.MaterialId.HasValue)
  172. {
  173. var ok = await _materialRep.IsAnyAsync(m =>
  174. m.Id == dto.MaterialId!.Value && m.CompanyRefId == dto.CompanyRefId && m.FactoryRefId == dto.FactoryRefId);
  175. if (!ok) return "物料主数据引用无效";
  176. }
  177. var opIds = dto.Operations.Select(o => o.OperationId).Distinct().ToList();
  178. var opCount = await _stdOpRep.AsQueryable()
  179. .Where(o => opIds.Contains(o.Id) && o.CompanyRefId == dto.CompanyRefId && o.FactoryRefId == dto.FactoryRefId)
  180. .CountAsync();
  181. return opCount == opIds.Count ? null : "存在无效的标准工序引用";
  182. }
  183. private static AdoS0MfgRoutingOperation MapRoutingOp(long routingId, long companyRefId, long factoryRefId, AdoS0MfgRoutingOperationUpsertDto line)
  184. {
  185. return new AdoS0MfgRoutingOperation
  186. {
  187. CompanyRefId = companyRefId,
  188. FactoryRefId = factoryRefId,
  189. RoutingId = routingId,
  190. OperationId = line.OperationId,
  191. SortNo = line.SortNo,
  192. WorkCenterId = line.WorkCenterId,
  193. ProductionLineId = line.ProductionLineId,
  194. StdTime = line.StdTime,
  195. Remark = line.Remark,
  196. IsReportOperation = line.IsReportOperation,
  197. Activity1 = line.Activity1,
  198. Activity1Qty = line.Activity1Qty,
  199. Activity1Unit = line.Activity1Unit,
  200. Activity2 = line.Activity2,
  201. Activity2Qty = line.Activity2Qty,
  202. Activity2Unit = line.Activity2Unit,
  203. Activity3 = line.Activity3,
  204. Activity3Qty = line.Activity3Qty,
  205. Activity3Unit = line.Activity3Unit,
  206. BaseQty = line.BaseQty,
  207. IsOutsourced = line.IsOutsourced,
  208. SupplierId = line.SupplierId,
  209. OutsourcedLeadTime = line.OutsourcedLeadTime,
  210. IsEnabled = line.IsEnabled,
  211. CreatedAt = DateTime.Now
  212. };
  213. }
  214. }