using System.Data; using System.Globalization; using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using SqlSugar; using static Microsoft.EntityFrameworkCore.DbLoggerCategory; using DbType = System.Data.DbType; 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.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,string type) { 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) { if ("4".Equals(type)||"3".Equals(type)||"2".Equals(type)) { //调用存储过程 try { // 定义输出参数 var outputResult = new SugarParameter("C_RESULT", null, DbType.Int32, ParameterDirection.Output, 4000); var outputMessage = new SugarParameter("MSG", null, DbType.String, ParameterDirection.Output, 4000); // 定义输入参数 var parameters = new List { new("P_ID",mesRohIn.ErpId , DbType.String, ParameterDirection.Input), outputResult, outputMessage }; // 使用 SqlSugar 执行存储过程 Db.Ado.ExecuteCommand( "BEGIN PRC_MES_WW_UPDATE_BS(:P_ID, :C_RESULT, :MSG); END;", parameters.ToArray()); // 获取输出参数的值 var resultValue = outputResult.Value?.ToString(); var messageValue = outputMessage.Value?.ToString(); if ("1".Equals(resultValue)) throw new Exception("更新失败"); return true; } catch (Exception ex) { throw new Exception(ex.Message); } } 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" DateTime? ParseDateTime(string dateStr) { if (DateTime.TryParseExact(dateStr, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate)) return parsedDate; return null; // 如果转换失败,返回null } var productionOrder = new ProductionOrder { OrderNo = erpDto.FBillNo, SmtProcessAttr = erpDto.F_UNW_Text_tpgy, Warehouse = erpDto.FStockID, OrderDate = ParseDateTime(erpDto.FDate) ?? null, OrderType = erpDto.FBillType, BusinessStatus = erpDto.FStatus, ProductCode = erpDto.FMaterialId, Unit = erpDto.FUnitID, WorkOrderQty = Convert.ToDouble(erpDto.FQty), PlanningGroup = erpDto.FWorkGroupId, Planner = erpDto.FPlannerID, PlanStartDate = ParseDateTime(erpDto.FPlanStartDate) ?? null, PlanFinishDate = ParseDateTime(erpDto.FPlanFinishDate) ?? null, StorageUpperLimit = Convert.ToDouble(erpDto.FStockInLimitH), StorageLowerLimit = Convert.ToDouble(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 = long.Parse(erpDto.FPurOrderEntrySeq), // StandardPoints = // Convert.ToDecimal(erpDto.F_UNW_BaseProperty_bzds), // PricingPoints = // Convert.ToDecimal(erpDto.F_UNW_BaseProperty_jjds), StockInQty = Convert.ToDecimal(erpDto.FBaseStockInQty), NoStockInQty = Convert.ToDouble(erpDto.FBaseNoStockInQty), StockOwner = erpDto.FInStockOwnerId, SUPPLIER = erpDto.FSUPPLIERID }; 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 { SequenceNo = long.Parse(erpDto.FSEQ), MaterialCode = erpDto.FMaterialID2, RequiredQty = Convert.ToDouble(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, Process = erpDto.F_UNW_Text_tpgy_Z, Unit = erpDto.FUnitID2, SupplyingType = erpDto.FSupplyType, Numerator = Convert.ToDouble(erpDto.FNumerator), Denominator = Convert.ToDouble(erpDto.FDenominator), ErpId = erpDto.FPPOMENTRYID, ErpHeaderId = erpDto.FPPOMID, FixedLoss = Convert.ToDecimal(erpDto.FFixScrapQty), VariableLossRate = erpDto.FScrapRate, SubItemType = erpDto.FMaterialType, SalesOrderNo = erpDto.F_UNW_Text_xsddh, ItemNo = erpDto.FReplaceGroup, OwnerType = erpDto.FOwnerTypeId, Owner = erpDto.FOwnerID2 }; var single = _productionOrderSubManager.GetSingle(it => it.ErpId == productionOrderSub.ErpId); if (single != null) productionOrderSub.Id = single.Id; productionOrderSubList.Add(productionOrderSub); } return productionOrderSubList; } }