ResourceCheckResultWriter.cs 9.6 KB

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