using Masuit.Tools; using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using SqlSugar; using System.Globalization; namespace MES.Service.service.BasicData; public class MesRohInManager : Repository { private readonly MesRohInDataManager rohInDataManager = new(); // Save 方法用于保存单个 RohIn 记录,根据类型执行不同的操作 public bool Save(RohIn rohIn) { var rohInErpRohIn = rohIn.ErpRohIn; var mesRohIn = GetMesRohIn(rohInErpRohIn); var mesRohInDatas = GetMesRohInDatas(rohIn.ErpRohinDatas); return UseTransaction(db => { return rohInErpRohIn.Type switch { "2" or "4" or "5" => SaveOrUpdateData(db, mesRohIn, mesRohInDatas) ? 1 : 0, "3" => UpdateData(db, mesRohIn, mesRohInDatas) ? 1 : 0, _ => throw new NotImplementedException( $"type没有{rohInErpRohIn.Type}这个类型") }; }) > 0; } // 更新数据的方法 private bool UpdateData(SqlSugarScope db, MesRohIn mesRohIn, List mesRohInDatas) { var decimals = mesRohInDatas.Select(s => s.Guid).ToArray(); var update = db.Deleteable() .Where(a => a.Guid == mesRohIn.Guid) .ExecuteCommand() > 0; var insertOrUpdate = db .Deleteable() .Where(s => decimals.Contains(s.Guid)) .ExecuteCommand() > 0; if (update && insertOrUpdate) return true; throw new NotImplementedException("更新失败"); } // 插入或更新数据的方法 private bool SaveOrUpdateData(SqlSugarScope db, MesRohIn mesRohIn, List mesRohInDatas) { if (mesRohIn.Guid != null) db.Deleteable().Where(s => s.Guid == mesRohIn.Guid) .ExecuteCommand(); if (mesRohInDatas.Count > 0) db.Deleteable() .Where(s => s.ErpId == mesRohIn.EbelnK3id).ExecuteCommand(); var orUpdate = db.Insertable(mesRohIn) .IgnoreColumns(true).ExecuteCommand() > 0; var baOrUpdate = db.Insertable(mesRohInDatas).PageSize(1) .IgnoreColumnsNull() .ExecuteCommand() > 0; if (orUpdate && baOrUpdate) return true; throw new NotImplementedException("插入或更新失败"); } // 批量保存记录的方法 public bool SaveList(List rohIns) { var result = rohIns.Select(Save).ToList(); return result.All(b => b); } // 将 ErpRohIn 对象转换为 MesRohIn 对象的方法 private MesRohIn GetMesRohIn(ErpRohIn rohIn) { var eid = long.Parse(rohIn.id); var mesRohIn = new MesRohIn(); var single = base.GetSingle(it => it.EbelnK3id == eid); if (single != null) mesRohIn.Guid = single.Guid; mesRohIn.EbelnK3id = eid; mesRohIn.BillNo = rohIn.FBillNo; mesRohIn.DocumentStatus = rohIn.FDocumentStatus; mesRohIn.DocumentType = rohIn.FBillTypeID; mesRohIn.BusinessType = rohIn.FBusinessType; /*if (rohIn.FDate != null) mesRohIn.PurchaseDate = DateTime.ParseExact(rohIn.FDate, "yyyy-MM-d H:m:s", null);*/ // 1. 处理 fDate(采购日期) if (!rohIn.FDate.IsNullOrEmpty()) { if (!DateTime.TryParseExact(rohIn.FDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime purchaseDate)) { throw new FormatException( $"采购日期(FDate)解析失败!值:【{rohIn.FDate}】,支持格式:yyyy-MM-d H:m:s"); } mesRohIn.PurchaseDate = purchaseDate; } else { mesRohIn.PurchaseDate = null; } var mesSupplier = Db.Queryable() .Where(s => s.SuppNo == rohIn.FSupplierId) .First(); if (mesSupplier != null) { mesRohIn.Supplier = mesSupplier.Id.ToString(); } mesRohIn.CloseStatus = rohIn.FCloseStatus; mesRohIn.PurchaseOrg = rohIn.FPurchaseOrgId; mesRohIn.PurchaseDept = rohIn.FPurchaseDeptId; mesRohIn.PurchaseGroup = rohIn.FPurchaserGroupId; mesRohIn.Purchaser = rohIn.FPurchaserId; mesRohIn.SettlementParty = rohIn.FSettleId; mesRohIn.PaymentParty = rohIn.FChargeId; mesRohIn.Email = rohIn.FProviderEMail; mesRohIn.Remarks = rohIn.Remarks; mesRohIn.CancellationStatus = rohIn.FCancelStatus; mesRohIn.CancellationPerson = rohIn.FCancellerId; /*if (rohIn.FCancelDate != null) if (!mesRohIn.CancellationPerson.IsNullOrEmpty()) mesRohIn.CancellationDate = DateTime.ParseExact(rohIn.FCancelDate, "yyyy-MM-d H:m:s", null); mesRohIn.CreateBy = rohIn.FCreatorId; if (rohIn.FCreateDate != null) mesRohIn.CreateDate = DateTime.ParseExact(rohIn.FCreateDate, "yyyy-MM-d H:m:s", null); mesRohIn.LastupdateBy = rohIn.FModifierId; if (rohIn.FModifyDate != null) mesRohIn.LastupdateDate = DateTime.ParseExact(rohIn.FModifyDate, "yyyy-MM-d H:m:s", null); mesRohIn.Prearrivaldate = rohIn.Prearrivaldate != null ? DateTime.ParseExact(rohIn.Prearrivaldate, "yyyy-MM-d H:m:s", null) : null;*/ if (!rohIn.FCancelDate.IsNullOrEmpty() && !mesRohIn.CancellationPerson.IsNullOrEmpty()) { if (!DateTime.TryParseExact(rohIn.FCancelDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime cancelDate)) { throw new FormatException( $"取消日期(FCancelDate)解析失败!值:【{rohIn.FCancelDate}】,支持格式:yyyy-MM-d H:m:s"); } mesRohIn.CancellationDate = cancelDate; } else { mesRohIn.CancellationDate = null; } // 3. 处理 fCreateDate(创建日期) if (!rohIn.FCreateDate.IsNullOrEmpty()) { if (!DateTime.TryParseExact(rohIn.FCreateDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime createDate)) { throw new FormatException( $"创建日期(FCreateDate)解析失败!值:【{rohIn.FCreateDate}】,支持格式:yyyy-MM-d H:m:s"); } mesRohIn.CreateDate = createDate; } else { mesRohIn.CreateDate = null; } // 4. 处理 fModifyDate(修改日期) if (!rohIn.FModifyDate.IsNullOrEmpty()) { if (!DateTime.TryParseExact(rohIn.FModifyDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime modifyDate)) { throw new FormatException( $"修改日期(FModifyDate)解析失败!值:【{rohIn.FModifyDate}】,支持格式:yyyy-MM-d H:m:s"); } mesRohIn.LastupdateDate = modifyDate; } else { mesRohIn.LastupdateDate = null; } // 6. 处理 prearrivaldate(预计到货日期) if (!rohIn.Prearrivaldate.IsNullOrEmpty()) { if (!DateTime.TryParseExact(rohIn.Prearrivaldate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime preArrivalDate)) { throw new FormatException( $"预计到货日期(Prearrivaldate)解析失败!值:【{rohIn.Prearrivaldate}】,支持格式:yyyy-MM-d H:m:s"); } mesRohIn.Prearrivaldate = preArrivalDate; } else { mesRohIn.Prearrivaldate = null; } mesRohIn.ErpCheckBy = rohIn.FApproverId; mesRohIn.ErpCheckDate = rohIn.FApproveDate; mesRohIn.Changereason = rohIn.FChangeReason; mesRohIn.ReceiveOrgId = rohIn.FReceiveOrgId; mesRohIn.ProviderId = rohIn.FProviderId; mesRohIn.Anred = rohIn.FTContact; mesRohIn.Telf1 = rohIn.Fmobilephone; mesRohIn.FixedTelephone = rohIn.FixedTelephone; mesRohIn.Address = rohIn.Address; mesRohIn.Acctype = rohIn.Acctype; mesRohIn.SynchronousDate = DateTime.Now; return mesRohIn; } // 将 ErpRohinData 对象转换为 MesRohInData 对象的方法 private List GetMesRohInDatas( List erpRohinDatas) { return erpRohinDatas.Select(s => { var entity = new MesRohInData { EbelnK3id = Convert.ToDecimal(s.id), ErpId = Convert.ToDecimal(s.Eid), BillNo = s.FBillNo, ItemId = s.FMaterialId, PurchaseUnit = s.FUnitId, PurchaseQty = Convert.ToDecimal(s.FQty), InventoryUnit = s.FStockUnitID, PricingUnit = s.FPriceUnitId, PricingQty = Convert.ToDecimal(s.FPriceUnitQty), /* DeliveryDate = s.FDeliveryDate != null ? DateTime.ParseExact(s.FDeliveryDate, "yyyy-MM-d H:m:s", null) : null, EarliestDeliveryDate = s.FDeliveryEarlyDate != null ? DateTime.ParseExact(s.FDeliveryEarlyDate, "yyyy-MM-d H:m:s", null) : null, LatestDeliveryDate = s.FDeliveryLastDate != null ? DateTime.ParseExact(s.FDeliveryLastDate, "yyyy-MM-d H:m:s", null) : null, */ // 1. 处理 fDeliveryDate(交货日期) DeliveryDate = !s.FDeliveryDate.IsNullOrEmpty() ? (DateTime.TryParseExact(s.FDeliveryDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime deliveryDate) ? deliveryDate : throw new FormatException( $"交货日期(FDeliveryDate)解析失败!值:【{s.FDeliveryDate}】,支持格式:yyyy-MM-d H:m:s")) : null, // 2. 处理 fDeliveryEarlyDate(最早交货日期) EarliestDeliveryDate = !s.FDeliveryEarlyDate.IsNullOrEmpty() ? (DateTime.TryParseExact(s.FDeliveryEarlyDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime earlyDate) ? earlyDate : throw new FormatException( $"最早交货日期(FDeliveryEarlyDate)解析失败!值:【{s.FDeliveryEarlyDate}】,支持格式:yyyy-MM-d H:m:s")) : null, // 3. 处理 fDeliveryLastDate(最晚交货日期) LatestDeliveryDate = !s.FDeliveryLastDate.IsNullOrEmpty() ? (DateTime.TryParseExact(s.FDeliveryLastDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime lastDate) ? lastDate : throw new FormatException( $"最晚交货日期(FDeliveryLastDate)解析失败!值:【{s.FDeliveryLastDate}】,支持格式:yyyy-MM-d H:m:s")) : null, IsGift = s.FGiveAway, Remarks = s.FEntryNote, SupplierItemCode = s.FSupMatId, SupplierItemName = s.FSupMatName, OutsourcingOrderId = s.FSUBREQBILLNO, BatchNumber = s.FLot, BusinessClose = s.FMRPCloseStatus, BusinessFreeze = s.FMRPFreezeStatus, Freezer = s.FFreezerId, BusinessTerminate = s.FMRPTerminateStatus, Terminator = s.FTerminaterId, TotalReceivedQty = Convert.ToDecimal(s.FReceiveQty), //累计收料数 RemainingReceivedQty = Convert.ToDecimal(s.FRemainReceiveQty), TotalStoredQty = Convert.ToDecimal(s.FStockInQty), //累计入库数 RemainingStoredQty = Convert.ToDecimal(s.FRemainStockINQty), TotalReturnedQty = Convert.ToDecimal(s.FMrbQty), ReturnableReceivedQty = Convert.ToDecimal(s.FCHECKRETQTY), //收料可退数 ReturnableStoredQty = Convert.ToDecimal(s.FSTOCKRETQTY), //库存可退数 SourceDocumentType = s.FSrcBillTypeId, SourceDocumentId = s.FSrcBillNo, DemandTrackingId = s.FReqTraceNo, PlanTrackingId = s.FMtoNo, ChangeFlag = s.FChangeFlag, DemandSource = s.FDEMANDTYPE, DemandDocumentId = s.FDEMANDBILLNO, DemandDocumentLineId = s.FDEMANDBILLENTRYSEQ, DemandOrg = s.FRequireOrgId, ReceivingOrg = s.FReceiveOrgId, SettlementOrg = s.FEntrySettleOrgId, PurchaseOrderLineNumber = s.FSEQ, Demand = s.FRequireOrgId, Receiving = s.FReceiveOrgId, Settlement = s.FSETTLEORGID, DemandDepartment = s.FRequireDeptId, ReceivingDepartment = s.FReceiveDeptId, SalesOrderId = s.SalesOrderId, OrderLineId = s.OrderLineId, FSUBREQENTRYID = s.FSUBREQENTRYID }; /*if (s.FFreezeDate != null) if (!s.FFreezerId.IsNullOrEmpty()) entity.FreezeTime = DateTime.ParseExact(s.FFreezeDate, "yyyy-MM-d H:m:s", null); if (s.FTerminateDate != null) if (!s.FTerminaterId.IsNullOrEmpty()) entity.TerminateTime = DateTime.ParseExact(s.FTerminateDate, "yyyy-MM-d H:m:s", null);*/ // 4. 处理 fFreezeDate(冻结日期) if (!s.FFreezeDate.IsNullOrEmpty() && !s.FFreezerId.IsNullOrEmpty()) { if (!DateTime.TryParseExact(s.FFreezeDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime freezeTime)) { throw new FormatException( $"冻结日期(FFreezeDate)解析失败!值:【{s.FFreezeDate}】,支持格式:yyyy-MM-d H:m:s"); } entity.FreezeTime = freezeTime; } else { entity.FreezeTime = null; } // 5. 处理 fTerminateDate(终止日期) if (!s.FTerminateDate.IsNullOrEmpty() && !s.FTerminaterId.IsNullOrEmpty()) { if (!DateTime.TryParseExact(s.FTerminateDate, "yyyy-MM-d H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime terminateTime)) { throw new FormatException( $"终止日期(FTerminateDate)解析失败!值:【{s.FTerminateDate}】,支持格式:yyyy-MM-d H:m:s"); } entity.TerminateTime = terminateTime; } else { entity.TerminateTime = null; } //ItemId var mesItems = Db.Queryable() .Where(s => s.ItemNo == entity.ItemId) .First(); if (mesItems != null) { entity.ItemId = mesItems.Id.ToString(); } var single = rohInDataManager.GetSingle(it => it.EbelnK3id == entity.EbelnK3id); if (single != null) entity.Guid = single.Guid; return entity; }).ToList(); } }