参数过多-分批传参

cs 复制代码
 /// <summary>
 /// 保存质检单主表及明细(草稿/正式通用)
 /// 规则:一旦存在正式单,无论提交草稿/正式,全部更新正式单,禁止Insert
 /// </summary>
 public async Task<string> SaveXcquatityMainAndInfo(XcQualityTestMainTabSaveDto entity)
 {
     var db = this.BaseRepository("Data");
     try
     {
         db.BeginTrans();
         var now = DateTime.Now;
         var userId = GetUserId().ToString();
         bool isUpdate = false;
         XcQualityTestMainTabEntity existMain = null;
         if (!string.IsNullOrEmpty(entity.QualityTestId))
         {
             existMain = (await db.FindList<XcQualityTestMainTabEntity>(x => x.QualityTestId == entity.QualityTestId)).FirstOrDefault();
             isUpdate = existMain != null;
         }

         //主表保存
         if (isUpdate)
         {
             if (existMain.DraftFlag == 0 && entity.DraftFlag == 1)
                 throw new ApplicationException("该报检物料已存在正式质检单据,不允许回退保存草稿");
             existMain.ModifyDate = now;
             existMain.ModifyId = userId;
             existMain.Version = (existMain.Version ?? 0) + 1;
             existMain.TestEndDate = entity.DraftFlag == 0 ? now : null;
             existMain.QualityTestUserId = userId;
             existMain.TheNum = entity.TheNum ?? 0;
             existMain.TheZlPassNum = entity.TheZlPassNum ?? 0;
             existMain.ThePassNum = entity.ThePassNum ?? 0;
             existMain.TheUnPassNum = entity.TheUnPassNum ?? 0;
             existMain.TestEndDate = entity.DraftFlag == 0 ? now : null;
             // 更新主表的质检状态
             if (!string.IsNullOrEmpty(entity.QualityTestStatus))
             {
                 existMain.QualityTestStatus = entity.QualityTestStatus;
             }
             await db.Update(existMain);
         }
         else
         {
             var xcQualityTestMainTab = new XcQualityTestMainTabEntity
             {
                 QualityTestId = await bLLBase.GetRuleCode("QualityTestId"),
                 Uuid = Guid.NewGuid().ToString(),
                 Flag = true,
                 Version = 1,
                 CreateDate = now,
                 CreateId = userId,
                 ModifyDate = now,
                 ModifyId = userId,
                 QualityTestUserId = userId,
                 InspectionId = entity.InspectionId,
                 DraftFlag = entity.DraftFlag,
                 TestStartDate = now,
                 ItemId = entity.ItemId,
                 ProductId = entity.ProductId,
                 Barcode = entity.Barcode,
                 ReceivingNo = entity.ReceivingNo,
                 DeliveryNo = entity.DeliveryNo,
                 ReceivingWarehouse = entity.ReceivingWarehouse,
                 QualityTestType = "01", //默认01来料质检
                 QualityTestStatus = entity.QualityTestStatus ?? "01",
                 TheNum = entity.TheNum ?? 0,
                 TheZlPassNum = entity.TheZlPassNum ?? 0,
                 ThePassNum = entity.ThePassNum ?? 0,
                 TheUnPassNum = entity.TheUnPassNum ?? 0,
                 TestEndDate = entity.DraftFlag == 0 ? now : null
             };
             await db.Insert(xcQualityTestMainTab);
             entity.QualityTestId = xcQualityTestMainTab.QualityTestId;
         }

         // 保存检测明细(分批处理,避免参数过多)
         if (entity.XcQualityTestDetailTabList != null && entity.XcQualityTestDetailTabList.Count > 0)
         {
             var newDetails = new List<XcQualityTestDetailTabEntity>();
             var updateDetails = new List<XcQualityTestDetailTabEntity>();

             // 获取现有明细
             var existingDetails = await db.FindList<XcQualityTestDetailTabEntity>(x => x.QualityTestId == entity.QualityTestId);
             var existingDict = existingDetails.ToDictionary(x => $"{x.QTSortId}_{x.QTInfoId}", x => x);

             // 获取最大行号
             var maxRowNo = existingDetails.Any() ? existingDetails.Max(x => x.RowNo ?? 0) : 0;
             var nextRowNo = maxRowNo + 1;

             foreach (var d in entity.XcQualityTestDetailTabList)
             {
                 if (string.IsNullOrEmpty(d.QTSortId) || string.IsNullOrEmpty(d.QTInfoId))
                     continue;

                 var key = $"{d.QTSortId}_{d.QTInfoId}";

                 if (existingDict.TryGetValue(key, out var existDetail))
                 {
                     // 更新已存在的明细
                     existDetail.QualityValue = d.QualityValue ?? "";
                     existDetail.Badreasons = d.Badreasons ?? "";
                     existDetail.QTSortId = d.QTSortId;
                     existDetail.QTInfoId = d.QTInfoId;
                     existDetail.Remark = d.Remark ?? "";
                     existDetail.InspectionQuantity = d.InspectionQuantity ?? 0;
                     existDetail.UpperLimit = d.UpperLimit ?? d.MaxKey ?? 0;
                     existDetail.LowerLimit = d.LowerLimit ?? d.MinKey ?? 0;
                     existDetail.ControlType = d.ControlType ?? "";
                     existDetail.AqlSampleSize = d.AqlSampleSize ?? 0;
                     existDetail.AqlAcceptNum = d.AqlAcceptNum ?? 0;
                     existDetail.AqlRejectNum = d.AqlRejectNum ?? 0;
                     existDetail.MaxKey = d.MaxKey ?? 0;
                     existDetail.MinKey = d.MinKey ?? 0;
                     existDetail.Defectrate = d.Defectrate ?? 0;
                     existDetail.ModifyDate = now;
                     existDetail.ModifyId = userId;
                     existDetail.Version = (d.Version ?? 1) + 1;
                     updateDetails.Add(existDetail);
                 }
                 else
                 {
                     // 新增明细
                     newDetails.Add(new XcQualityTestDetailTabEntity
                     {
                         Uuid = Guid.NewGuid().ToString(),
                         QualityTestId = entity.QualityTestId,
                         Version = 1,
                         Flag = true,
                         QTSortId = d.QTSortId,
                         QTInfoId = d.QTInfoId,
                         QTInfoBaseName = d.QTInfoBaseName,
                         QualityValue = d.QualityValue ?? "",
                         Badreasons = d.Badreasons ?? "",
                         Remark = d.Remark ?? "",
                         InspectionQuantity = d.InspectionQuantity ?? 0,
                         ControlType = d.ControlType ?? "",
                         UpperLimit = d.UpperLimit ?? d.MaxKey ?? 0,
                         LowerLimit = d.LowerLimit ?? d.MinKey ?? 0,
                         MaxKey = d.MaxKey ?? 0,
                         MinKey = d.MinKey ?? 0,
                         AqlSampleSize = d.AqlSampleSize ?? 0,
                         AqlAcceptNum = d.AqlAcceptNum ?? 0,
                         AqlRejectNum = d.AqlRejectNum ?? 0,
                         Defectrate = d.Defectrate ?? 0,
                         CreateDate = now,
                         CreateId = userId,
                         ModifyDate = now,
                         ModifyId = userId,
                         RowNo = nextRowNo++,
                     });
                 }
             }

             int batchSize = 50;
             // 批量更新 - 分批处理
             if (updateDetails.Any())
             {
                 int totalBatches = (int)Math.Ceiling((double)updateDetails.Count / batchSize);
                 for (int i = 0; i < totalBatches; i++)
                 {
                     var batch = updateDetails.Skip(i * batchSize).Take(batchSize).ToList();
                     await db.Updates(batch);
                 }
             }

             // 批量插入 - 分批处理
             if (newDetails.Any())
             {
                 int totalBatches = (int)Math.Ceiling((double)newDetails.Count / batchSize);
                 for (int i = 0; i < totalBatches; i++)
                 {
                     var batch = newDetails.Skip(i * batchSize).Take(batchSize).ToList();
                     await db.Inserts(batch);
                 }
             }
         }

         // 回写报检单(草稿和正式单都需要回写)
         if (!string.IsNullOrEmpty(entity.InspectionId))
         {
             var inspection = (await db.FindList<XcInspectionReportTabEntity>(x => x.InspectionId == entity.InspectionId)).FirstOrDefault();
             if (inspection != null)
             {
                 inspection.QualityTestId = entity.QualityTestId;
                 inspection.QualityTestStatus = entity.QualityTestStatus;
                 inspection.QualityTestResult = entity.QualityTestStatus;
                 inspection.ThePassNum = entity.ThePassNum;
                 inspection.TheUnPassNum = entity.TheUnPassNum;
                 inspection.QualityTestDate = now;
                 inspection.ModifyDate = now;
                 inspection.ModifyId = userId;

                 if (entity.DraftFlag == 1)
                 {
                     // 保存草稿→质检中
                     inspection.InspectionStatus = "02";
                 }
                 else if (entity.DraftFlag == 0)
                 {
                     // 正式提交→已质检
                     inspection.InspectionStatus = "03";
                 }
                 await db.Update(inspection);
             }
         }

         // 正式单:生成不合格单据
         if (entity.DraftFlag == 0)
         {
             decimal unPassNum = entity.TheUnPassNum ?? 0;

             if (unPassNum > 0)
             {
                 // 不合格数量大于0且有收货信息---更新到货单细表质检列信息
                 if (entity.ReceivingDetailTabList != null && entity.ReceivingDetailTabList.Count > 0)
                 {
                     List<ReceivingBoxInfoEntity> updateReceivingBoxList = new List<ReceivingBoxInfoEntity>();
                     var receivingBoxList = await db.FindList<ReceivingBoxInfoEntity>(x => x.ItemId == entity.ItemId && x.DeliveryNo == entity.DeliveryNo);
                     if (receivingBoxList.Any())
                     {
                         foreach (var receivingBox in receivingBoxList)
                         {
                             var findEntity = entity.ReceivingDetailTabList.Find(x => x.Barcode == receivingBox.BoxNo);
                             if (findEntity != null)
                             {
                                 receivingBox.ThePassNum = findEntity.ThePassNum;
                                 receivingBox.TheUnPassNum = findEntity.TheUnPassNum;
                             }
                             receivingBox.QualityTestStatus = entity.QualityTestStatus;
                             updateReceivingBoxList.Add(receivingBox);
                         }

                         // 分批更新收货信息
                         if (updateReceivingBoxList.Any())
                         {
                             int boxBatchSize = 50;
                             int totalBatches = (int)Math.Ceiling((double)updateReceivingBoxList.Count / boxBatchSize);
                             for (int i = 0; i < totalBatches; i++)
                             {
                                 var batch = updateReceivingBoxList.Skip(i * boxBatchSize).Take(boxBatchSize).ToList();
                                 await db.Updates(batch);
                             }
                         }
                     }
                 }

                 // 生成不合格单据
                 var nonConformityEntity = new XcNonConformityFlowTabEntity
                 {
                     UUID = Guid.NewGuid().ToString("N"),
                     Version = 1,
                     Flag = 1,
                     QualitytestId = entity.QualityTestId,
                     InspectionId = entity.InspectionId ?? "",
                     DeliveryNo = entity.DeliveryNo ?? "",
                     ItemId = entity.ItemId ?? "",
                     ItemName = entity.ItemName ?? "",
                     theUnPassNum = unPassNum,
                     thePassNum = entity.ThePassNum ?? 0,
                     qualityTestStatus = entity.QualityTestStatus ?? "03",
                     NcNo = await bLLBase.GetRuleCode("NcNo"),
                     FlowStep = "01", // 01未发起
                     Action = "01",   // 01发起
                     CreateId = userId,
                     CreateDate = now,
                     ModifyId = userId,
                     ModifyDate = now
                 };
                 await db.Insert(nonConformityEntity);
             }
         }

         db.Commit();
         return entity.QualityTestId;
     }
     catch
     {
         db.Rollback();
         throw;
     }
 }
        
相关推荐
典典分享指南1 小时前
短视频分镜提示词:让 AI 出片的产品口播
java·c#·bash·composer·symfony
Z5998178412 小时前
c#软件开发学习笔记--WPF(Canvas、数据绑定、MVVM模式、值转换器)
笔记·学习·c#
淡海水3 小时前
05-03-栈队列-PriorityQueue-TElement-TPriority-NET6优先队列语义与四叉堆实现
服务器·前端·c#·priorityqueue·clr·telement
leonkay16 小时前
C# 特性(Attribute)——【1】基础讲解
开发语言·青少年编程·面试·c#·.net·个人开发
CSDN_RTKLIB17 小时前
数据库七大符号表
c#
leonkay18 小时前
C# 特性(Attribute)——【2】工业设备参数框架设计
经验分享·面试·架构·c#·学习方法·设计
2501_9065651219 小时前
小创 · 智能助手
开发语言·c#
datalover1 天前
【无标题】
c#·linq
FlYFlOWERANDLEAF1 天前
微软CommunityToolkit.Mvvm和DevExpress中GenerateViewModel使用
c#