ResourceCheckResultWriter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using Yitter.IdGenerator;
  2. namespace Admin.NET.Plugin.AiDOP.Order;
  3. /// <summary>将资源检查结果写入 b_examine_result / b_bom_child_examine。</summary>
  4. public class ResourceCheckResultWriter : ITransient
  5. {
  6. private readonly ISqlSugarClient _db;
  7. private readonly CapacityDateCalculator _capacityCalc;
  8. public ResourceCheckResultWriter(ISqlSugarClient db, CapacityDateCalculator capacityCalc)
  9. {
  10. _db = db;
  11. _capacityCalc = capacityCalc;
  12. }
  13. public async Task<OrderResourceCheckResult> WriteAsync(
  14. OrderWorkOrderGenerationService.OrderHeader order,
  15. OrderWorkOrderGenerationService.OrderEntryLine entry,
  16. string workOrd,
  17. long? morderId,
  18. IReadOnlyList<ResourceCheckBomLine> lines,
  19. string account,
  20. DateTime now)
  21. {
  22. await InvalidatePreviousAsync(entry.Id, entry.TenantId, workOrd, now);
  23. var examineId = YitIdHelper.NextId();
  24. var needQty = entry.Qty ?? 0;
  25. var needTime = entry.SysCapacityDate ?? entry.PlanDate ?? now;
  26. // 对根行(Level=1)执行产能计算,更新 KittingTime
  27. var rootLine = lines.FirstOrDefault(x => x.Level == 1 && x.IsUse == 1 && x.LackQty > 0);
  28. if (rootLine != null)
  29. {
  30. var planDate = entry.PlanDate ?? now;
  31. rootLine.KittingTime = await _capacityCalc.CalculateAsync(
  32. rootLine.ItemNumber, rootLine.LackQty, needTime, planDate, entry.TenantId);
  33. }
  34. var kittingTime = lines
  35. .Where(x => x.IsUse == 1 && x.LackQty > 0)
  36. .Select(x => x.KittingTime)
  37. .DefaultIfEmpty(needTime)
  38. .Max();
  39. var factoryId = entry.FactoryId ?? order.FactoryId ?? entry.TenantId;
  40. var companyId = entry.CompanyId ?? factoryId;
  41. await _db.Ado.ExecuteCommandAsync(
  42. """
  43. INSERT INTO b_examine_result (
  44. Id, sorderid, bill_no, sentry_id, entry_seq, morder_id, morder_no, bom_number,
  45. need_qty, need_time, earliest_times, latest_times, order_statr_time, kitting_times,
  46. sys_capacity_date, sys_material_date,
  47. create_by_name, create_time, update_by_name, update_time,
  48. tenant_id, factory_id, company_id, IsDeleted
  49. ) VALUES (
  50. @Id, @SorderId, @BillNo, @SentryId, @EntrySeq, @MorderId, @MorderNo, @BomNumber,
  51. @NeedQty, @NeedTime, @NeedTime, @NeedTime, @NeedTime, @KittingTime,
  52. @NeedTime, @KittingTime,
  53. @User, @Now, @User, @Now,
  54. @TenantId, @FactoryId, @CompanyId, 0
  55. )
  56. """,
  57. new SugarParameter("@Id", examineId),
  58. new SugarParameter("@SorderId", order.Id),
  59. new SugarParameter("@BillNo", order.BillNo ?? entry.BillNo ?? string.Empty),
  60. new SugarParameter("@SentryId", entry.Id),
  61. new SugarParameter("@EntrySeq", entry.EntrySeq ?? 0),
  62. new SugarParameter("@MorderId", morderId ?? (object)DBNull.Value),
  63. new SugarParameter("@MorderNo", workOrd),
  64. new SugarParameter("@BomNumber", entry.BomNumber ?? (object)DBNull.Value),
  65. new SugarParameter("@NeedQty", needQty),
  66. new SugarParameter("@NeedTime", needTime),
  67. new SugarParameter("@KittingTime", kittingTime ?? needTime),
  68. new SugarParameter("@User", account),
  69. new SugarParameter("@Now", now),
  70. new SugarParameter("@TenantId", entry.TenantId),
  71. new SugarParameter("@FactoryId", factoryId),
  72. new SugarParameter("@CompanyId", companyId));
  73. var activeLines = lines.Where(x => x.IsUse == 1).ToList();
  74. foreach (var line in lines)
  75. {
  76. var childId = YitIdHelper.NextId();
  77. await _db.Ado.ExecuteCommandAsync(
  78. """
  79. INSERT INTO b_bom_child_examine (
  80. Id, examine_id, fid, parent_id, bom_child_id, bom_number, item_number, level, sentry_id,
  81. num, type, item_name, model, unit, erp_cls, erp_cls_name, backflush,
  82. qty, sqty, use_qty, self_lack_qty, lack_qty, mo_qty, make_qty, purchase_qty, purchase_occupy_qty,
  83. kitting_time, satisfy_time, isbom, haveicsubs, substitute_code, needCount, needCountNoloss,
  84. wastage, scrap, is_use, Op,
  85. create_by_name, create_time, update_by_name, update_time,
  86. tenant_id, factory_id, company_id, IsDeleted
  87. ) VALUES (
  88. @Id, @ExamineId, @Fid, @ParentId, @BomChildId, @BomNumber, @ItemNumber, @Level, @SentryId,
  89. @Num, @Type, @ItemName, @Model, @Unit, @ErpCls, @ErpClsName, @Backflush,
  90. @Qty, @Sqty, @UseQty, @SelfLackQty, @LackQty, @MoQty, @MakeQty, @PurchaseQty, @PurchaseOccupyQty,
  91. @KittingTime, @SatisfyTime, @IsBom, @HaveIcSubs, @SubstituteCode, @NeedCount, @NeedCountNoLoss,
  92. @Wastage, @Scrap, @IsUse, 0,
  93. @User, @Now, @User, @Now,
  94. @TenantId, @FactoryId, @CompanyId, 0
  95. )
  96. """,
  97. new SugarParameter("@Id", childId),
  98. new SugarParameter("@ExamineId", examineId),
  99. new SugarParameter("@Fid", line.Fid),
  100. new SugarParameter("@ParentId", line.ParentFid ?? (object)DBNull.Value),
  101. new SugarParameter("@BomChildId", line.BomChildId ?? (object)DBNull.Value),
  102. new SugarParameter("@BomNumber", line.BomNumber ?? (object)DBNull.Value),
  103. new SugarParameter("@ItemNumber", line.ItemNumber),
  104. new SugarParameter("@Level", line.Level),
  105. new SugarParameter("@SentryId", entry.Id),
  106. new SugarParameter("@Num", line.Num),
  107. new SugarParameter("@Type", line.Type),
  108. new SugarParameter("@ItemName", line.ItemName ?? (object)DBNull.Value),
  109. new SugarParameter("@Model", line.Model ?? (object)DBNull.Value),
  110. new SugarParameter("@Unit", line.Unit ?? (object)DBNull.Value),
  111. new SugarParameter("@ErpCls", line.ErpCls),
  112. new SugarParameter("@ErpClsName", line.ErpClsName ?? (object)DBNull.Value),
  113. new SugarParameter("@Backflush", line.Backflush),
  114. new SugarParameter("@Qty", line.Qty),
  115. new SugarParameter("@Sqty", line.Sqty),
  116. new SugarParameter("@UseQty", line.UseQty),
  117. new SugarParameter("@SelfLackQty", line.SelfLackQty),
  118. new SugarParameter("@LackQty", line.LackQty),
  119. new SugarParameter("@MoQty", line.MoQty),
  120. new SugarParameter("@MakeQty", line.MakeQty),
  121. new SugarParameter("@PurchaseQty", line.PurchaseQty),
  122. new SugarParameter("@PurchaseOccupyQty", line.PurchaseOccupyQty),
  123. new SugarParameter("@KittingTime", line.KittingTime ?? needTime),
  124. new SugarParameter("@SatisfyTime", line.SatisfyTime ?? needTime),
  125. new SugarParameter("@IsBom", line.IsBom),
  126. new SugarParameter("@HaveIcSubs", line.HaveIcSubs),
  127. new SugarParameter("@SubstituteCode", line.SubstituteCode ?? (object)DBNull.Value),
  128. new SugarParameter("@NeedCount", line.NeedCount),
  129. new SugarParameter("@NeedCountNoLoss", line.NeedCountNoLoss),
  130. new SugarParameter("@Wastage", line.Wastage),
  131. new SugarParameter("@Scrap", line.Scrap),
  132. new SugarParameter("@IsUse", line.IsUse),
  133. new SugarParameter("@User", account),
  134. new SugarParameter("@Now", now),
  135. new SugarParameter("@TenantId", entry.TenantId),
  136. new SugarParameter("@FactoryId", factoryId),
  137. new SugarParameter("@CompanyId", companyId));
  138. }
  139. var hasShortage = activeLines.Any(x => x.LackQty > 0);
  140. await UpdateMesMorderMaterialSituationAsync(workOrd, entry.TenantId, hasShortage, account, now);
  141. return new OrderResourceCheckResult
  142. {
  143. ExamineId = examineId,
  144. LineCount = activeLines.Count,
  145. HasShortage = hasShortage,
  146. KittingTime = kittingTime
  147. };
  148. }
  149. private async Task InvalidatePreviousAsync(long entryId, long tenantId, string morderNo, DateTime now)
  150. {
  151. var oldIds = await _db.Ado.SqlQueryAsync<long>(
  152. """
  153. SELECT Id FROM b_examine_result
  154. WHERE tenant_id = @TenantId AND IsDeleted = 0
  155. AND (sentry_id = @EntryId OR morder_no = @MorderNo)
  156. """,
  157. new SugarParameter("@EntryId", entryId),
  158. new SugarParameter("@TenantId", tenantId),
  159. new SugarParameter("@MorderNo", morderNo));
  160. if (oldIds.Count == 0)
  161. return;
  162. foreach (var examineId in oldIds)
  163. {
  164. await _db.Ado.ExecuteCommandAsync(
  165. """
  166. UPDATE b_bom_child_examine
  167. SET is_use = 0, update_time = @Now
  168. WHERE examine_id = @ExamineId
  169. """,
  170. new SugarParameter("@ExamineId", examineId),
  171. new SugarParameter("@Now", now));
  172. }
  173. await _db.Ado.ExecuteCommandAsync(
  174. """
  175. UPDATE b_examine_result
  176. SET IsDeleted = 1, update_time = @Now
  177. WHERE sentry_id = @EntryId AND tenant_id = @TenantId AND IsDeleted = 0
  178. """,
  179. new SugarParameter("@EntryId", entryId),
  180. new SugarParameter("@TenantId", tenantId),
  181. new SugarParameter("@Now", now));
  182. }
  183. private async Task UpdateMesMorderMaterialSituationAsync(
  184. string workOrd,
  185. long tenantId,
  186. bool hasShortage,
  187. string account,
  188. DateTime now)
  189. {
  190. var situation = hasShortage ? "缺料" : "齐套";
  191. await _db.Ado.ExecuteCommandAsync(
  192. """
  193. UPDATE mes_morder
  194. SET MaterialSituation = @Situation,
  195. update_by_name = @User,
  196. update_time = @Now
  197. WHERE morder_no = @WorkOrd AND tenant_id = @TenantId AND IsDeleted = 0
  198. """,
  199. new SugarParameter("@Situation", situation),
  200. new SugarParameter("@User", account),
  201. new SugarParameter("@Now", now),
  202. new SugarParameter("@WorkOrd", workOrd),
  203. new SugarParameter("@TenantId", tenantId));
  204. }
  205. }