using System.Globalization; using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using MES.Service.util; using SqlSugar; namespace MES.Service.service.BasicData; public class ProductionOrderManager : Repository { //当前类已经继承了 Repository 增、删、查、改的方法 private readonly ProductionOrderSubManager _productionOrderSubManager = new(); //ErpWYOrder public bool Save(ErpWYOrder wyOrder) { var erpProductionOrderDto = wyOrder.OrderDto; var mesRohIn = ConvertErpToProductionOrder(erpProductionOrderDto); var mesRohInDatas = ConvertErpToProductionOrderSub(wyOrder.Items); return UseTransaction(db => { switch (erpProductionOrderDto.Type) { // case "2": // return InsertData(db, mesRohIn, mesRohInDatas, // rohInErpRohIn.FBILLTYPE) // ? 1 // : 0; case "3": return UpdateData(db, mesRohIn, mesRohInDatas) ? 1 : 0; case "2": case "4": return SaveOrUpdateData(db, mesRohIn, mesRohInDatas, erpProductionOrderDto.Type) ? 1 : 0; default: throw new NotImplementedException( $"type没有{erpProductionOrderDto.Type}这个类型"); } }) > 0; } private bool UpdateData(SqlSugarScope db, ProductionOrder mesRohIn, List mesRohInDatas) { var decimals = mesRohInDatas.Select(s => s.Guid).ToArray(); var update = db.Deleteable() .Where(s => s.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, ProductionOrder mesRohIn, List mesRohInDatas, string type) { if (StringUtil.CheckGuid(mesRohIn.Guid)) db.Deleteable() .Where(s => s.Guid == mesRohIn.Guid) .ExecuteCommand(); if (mesRohInDatas.Count > 0) db.Deleteable() .Where(s => s.ErpHeaderId == mesRohIn.ErpId).ExecuteCommand(); var orUpdate = base.Insert(mesRohIn); var baOrUpdate = _productionOrderSubManager.InsertRange(mesRohInDatas); if (orUpdate && baOrUpdate) return true; throw new NotImplementedException("插入或更新失败"); } // 批量保存记录的方法 public bool SaveList(List rohIns) { var result = rohIns.Select(Save).ToList(); return result.All(b => b); } private ProductionOrder ConvertErpToProductionOrder( ErpProductionOrderDto erpDto) { DateTime parsedDate; // 时间格式转换函数,ERP时间格式为 "yyyy-MM-dd HH:mm:ss.fff" DateTime? ParseDateTime(string dateStr) { if (DateTime.TryParseExact(dateStr, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate)) return parsedDate; return null; // 如果转换失败,返回null } var productionOrder = new ProductionOrder { OrderNo = erpDto.FBillNo, Warehouse = erpDto.FStockID, OrderDate = ParseDateTime(erpDto.FDate) ?? null, OrderType = erpDto.FBillType, BusinessStatus = erpDto.FStatus, ProductCode = erpDto.FMaterialId, Unit = erpDto.FUnitID, WorkOrderQty = Convert.ToDecimal(erpDto.FQty), PlanningGroup = erpDto.FWorkGroupId, Planner = erpDto.FPlannerID, EstimatedStartTime = ParseDateTime(erpDto.FPlanStartDate) ?? null, EstimatedEndTime = ParseDateTime(erpDto.FPlanFinishDate) ?? null, StorageUpperLimit = Convert.ToDecimal(erpDto.FStockInLimitH), StorageLowerLimit = Convert.ToDecimal(erpDto.FStockInLimitL), TrackingNo = erpDto.FMTONO, BatchNo = erpDto.FLot, BomVersion = erpDto.FBomId, SalesOrderNo = erpDto.F_UNW_XSDDH, GenerationMethod = erpDto.FCreateType, ErpProductionOrderId = erpDto.FSUBID, ErpProductionOrderLineNo = erpDto.FSUBBILLNOSEQ, ErpProductionOrderNo = erpDto.FSUBBILLNO, SourceOrderType = erpDto.FSrcBillType, SourceOrderNo = erpDto.FSrcBillNo, SourceOrderEntryNo = erpDto.FSrcBillEntrySeq, DemandOrderNo = erpDto.FSALEORDERNO, DemandOrderLineNo = erpDto.FSaleOrderEntrySeq, ClosingPerson = erpDto.FFORCECLOSERID, ClosingType = erpDto.FCloseType, Remarks = erpDto.FDescription, ErpId = erpDto.FPPOMID, ErpProductionEntryCode = erpDto.FSUBENTRYID, Purchaseorderno = erpDto.FPurOrderNo, Purchaseorderentryseq = Convert.ToInt32(erpDto.FPurOrderEntrySeq), Stockinqty = Convert.ToDecimal(erpDto.FBaseStockInQty), Nostockinqty = Convert.ToDecimal(erpDto.FBaseNoStockInQty), Stockowner = erpDto.FInStockOwnerId }; var single = base.GetSingle(it => it.ErpId == erpDto.FPPOMID); if (single != null) productionOrder.Guid = single.Guid; return productionOrder; } private List ConvertErpToProductionOrderSub( List erpDtoList) { var productionOrderSubList = new List(); foreach (var erpDto in erpDtoList) { var productionOrderSub = new ProductionOrderSub { SequenceNo = Convert.ToInt32(erpDto.FSEQ), MaterialCode = erpDto.FMaterialID2, RequiredQty = Convert.ToDecimal(erpDto.FMustQty), IssuedQty = Convert.ToDecimal(erpDto.FPickedQty), LocationNo = erpDto.FPositionNO, StockOwner = erpDto.FOwnerID, TrackingNo = erpDto.FMTONO, BatchNo = erpDto.FLot, Warehouse = erpDto.FStockID, IssuingMethod = erpDto.FIssueType, Unit = erpDto.FUnitID2, Unit2 = erpDto.FUnitID3, SupplyingType = erpDto.FSupplyType, Numerator = Convert.ToDecimal(erpDto.FNumerator), Denominator = Convert.ToDecimal(erpDto.FDenominator), ErpId = erpDto.FPPOMENTRYID, ErpHeaderId = erpDto.FPPOMID, FixedLoss = Convert.ToDecimal(erpDto.FFixScrapQty), VariableLossRate = Convert.ToDecimal(erpDto.FScrapRate), SubItemType = erpDto.FMaterialType, ItemNo = erpDto.FReplaceGroup, OwnerType = erpDto.FOwnerTypeId, Owner = erpDto.FOwnerID2, ErpProductionEntryCode = erpDto.FSUBENTRYID }; var single = _productionOrderSubManager.GetSingle(it => it.ErpId == productionOrderSub.ErpId); if (single != null) productionOrderSub.Guid = single.Guid; productionOrderSubList.Add(productionOrderSub); } return productionOrderSubList; } }