using System.Globalization; using AngleSharp.Css; using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; 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) ? 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.Id).ToArray(); var update = base.DeleteById(mesRohIn.Id); var insertOrUpdate = db .Deleteable().In(decimals) .ExecuteCommand() > 0; if (update && insertOrUpdate) return true; throw new NotImplementedException("更新失败"); } // 插入或更新数据的方法 private bool SaveOrUpdateData(SqlSugarScope db, ProductionOrder mesRohIn, List mesRohInDatas) { if (mesRohIn.Id != null) base.DeleteById(mesRohIn.Id); 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, OrderDate = ParseDateTime(erpDto.FDate) ?? null, OrderType = erpDto.FBillType, PlanningGroup = erpDto.FWorkGroupId, Planner = erpDto.FPlannerID, BusinessStatus = erpDto.FStatus, FProductType= erpDto.FProductType, ProductCode = erpDto.FMaterialId, Unit = erpDto.FUnitID, WorkOrderQty = Convert.ToDouble(erpDto.FQty), Warehouse = erpDto.FStockID, EstimatedStartTime = ParseDateTime(erpDto.FPlanStartDate) ?? null, EstimatedEndTime = ParseDateTime(erpDto.FPlanFinishDate) ?? null, //Fconveydate = ParseDateTime(erpDto.FConveyDate) ?? null, Fconveydate = erpDto.FConveyDate, StorageUpperLimit = Convert.ToDouble(erpDto.FStockInLimitH), StorageLowerLimit = Convert.ToDouble(erpDto.FStockInLimitL), TrackingNo = erpDto.FMTONO, BatchNo = erpDto.FLot, BomVersion = erpDto.FBomId, Fpriority = erpDto.FPriority, GenerationMethod = erpDto.FCreateType, SourceOrderType = erpDto.FSrcBillType, SourceOrderNo = erpDto.FSrcBillNo, SourceOrderEntryNo = erpDto.FSrcBillEntrySeq, Freqsrc = erpDto.FReqSrc, DemandOrderNo = erpDto.FSALEORDERNO, DemandOrderLineNo = erpDto.FSaleOrderEntrySeq, ClosingPerson = erpDto.FFORCECLOSERID, ClosingType = erpDto.FCloseType, Fclosereason = erpDto.FCloseReason, Fsrcsplitbillno = erpDto.FSrcSplitBillNo, Fsrcsplitseq = erpDto.FSrcSplitSeq, Remarks = erpDto.FDescription, ErpId = erpDto.FPPOMID, ErpProductionEntryCode = erpDto.FSUBENTRYID, Purchaseorderno = erpDto.FPurOrderNo, Purchaseorderentryseq = long.Parse(erpDto.FPurOrderEntrySeq), Stockinqty = Convert.ToDecimal(erpDto.FStockInQty), Nostockinqty = Convert.ToDouble(erpDto.FNoStockInQty), Stockowner = erpDto.FInStockOwnerId, Fstockinorgid = erpDto.FStockInOrgId, Fpurorgid = erpDto.FPurorgId, ErpProductionOrderId = erpDto.FSUBID, ErpProductionOrderLineNo = erpDto.FSUBBILLNOSEQ, ErpProductionOrderNo = erpDto.FSUBBILLNO, Texta = erpDto.TEXT_A, Textb = erpDto.TEXT_B, }; var single = base.GetSingle(it => it.ErpId == erpDto.FPPOMID); if (single != null) productionOrder.Id = single.Id; return productionOrder; } private List ConvertErpToProductionOrderSub( List erpDtoList) { var productionOrderSubList = new List(); foreach (var erpDto in erpDtoList) { var productionOrderSub = new ProductionOrderSub { Fbillno = erpDto.FSubReqBillNO, Fsubreqentryseq = erpDto.FSUBREQENTRYSEQ, Freplacegroup = erpDto.FReplaceGroup, Fmaterialid = !string.IsNullOrEmpty(erpDto.FMaterialID2) ? Convert.ToDecimal(erpDto.FMaterialID2) : null, Fmustqty = !string.IsNullOrEmpty(erpDto.FMustQty) ? Convert.ToDecimal(erpDto.FMustQty) : null, Fpickedqty = !string.IsNullOrEmpty(erpDto.FPickedQty) ? Convert.ToDecimal(erpDto.FPickedQty) : null, Fnumerator = !string.IsNullOrEmpty(erpDto.FNumerator) ? Convert.ToDecimal(erpDto.FNumerator) : null, Fdenominator = !string.IsNullOrEmpty(erpDto.FDenominator) ? Convert.ToDecimal(erpDto.FDenominator) : null, Ffixscrapqty = !string.IsNullOrEmpty(erpDto.FFixScrapQty) ? Convert.ToDecimal(erpDto.FFixScrapQty) : null, Fscraprate = !string.IsNullOrEmpty(erpDto.FScrapRate) ? Convert.ToDecimal(erpDto.FScrapRate) : null, Funitid2 = !string.IsNullOrEmpty(erpDto.FUnitID2) ? Convert.ToDecimal(erpDto.FUnitID2) : null, Fmaterialtype = erpDto.FMaterialType, Fstdqty = erpDto. FSTDQTY, Fiskeyitem = erpDto.FISKEYITEM, Fsupplytype = erpDto.FSupplyType, Fchildsupplyorgid = erpDto.FCHILDSUPPLYORGID, Fissuetype = erpDto.FIssueType, Fsupplyorg = erpDto.FSUPPLYORG, Fisgetscrap = erpDto.FISGETSCRAP, Fiskeycomponent = erpDto.FISKEYCOMPONENT, Fownertypeid = erpDto.FOwnerTypeId, Fownerid = erpDto.FOwnerID, Flot = erpDto.FLot, Fstockid = erpDto.FStockID, Fsrctransorgid = erpDto.FSRCTRANSORGID, Fsrctransstockid = erpDto.FSRCTRANSSTOCKID, Fstockstatusid = erpDto.FSTOCKSTATUSID, Fneeddate = erpDto.FNEEDDATE, Freservetype = erpDto.FRESERVETYPE, Fmemo = erpDto.FMEMO, Fmtono = erpDto.FMTONO, Fpositionno = erpDto.FPositionNO, ErpId = erpDto.FPPOMENTRYID, ErpHeaderId = erpDto.FPPOMID, Fsaleorderno = erpDto.FSALEORDERNO }; var single = _productionOrderSubManager.GetSingle(it => it.ErpId == productionOrderSub.ErpId); if (single != null) productionOrderSub.Id = single.Id; productionOrderSubList.Add(productionOrderSub); } return productionOrderSubList; } }