using SqlSugar; using System; using System.Collections.Generic; using MES.Service.DB; using MES.Service.Modes; using MES.Service.Dto.webApi; using MES.Service.service.BasicData; using System.Globalization; namespace MES.Service.service { public class SalesDeliveryNoticeManager : Repository { //当前类已经继承了 Repository 增、删、查、改的方法 private readonly SalesDeliveryNoticeDetailManager _SalesDeliveryDetailManager = new(); //ErpSalesRerurn public bool Save(ErpSalesDelivery SalesDelivery) { var erpSalesDeliveryDto = SalesDelivery.OrderDto; var mesSalesDelivery = ConvertErpToSalesDelivery(SalesDelivery.OrderDto); var mesSalesDeliveryDatas = ConvertErpToSalesDeliveryDetail(SalesDelivery.Items); return UseTransaction(db => { switch (erpSalesDeliveryDto.Type) { // case "2": // return InsertData(db, mesSalesReturn, mesSalesReturnDatas, // rohInErpRohIn.FBILLTYPE) // ? 1 // : 0; case "3": return UpdateData(db, mesSalesDelivery, mesSalesDeliveryDatas) ? 1 : 0; case "2": case "4": return SaveOrUpdateData(db, mesSalesDelivery, mesSalesDeliveryDatas, erpSalesDeliveryDto.Type) ? 1 : 0; default: throw new NotImplementedException( $"type没有{erpSalesDeliveryDto.Type}这个类型"); } }) > 0; } private bool UpdateData(SqlSugarScope db, SalesDeliveryNotice mesSalesDelivery, List mesSalesDeliveryDatas) { var decimals = mesSalesDeliveryDatas.Select(s => s.Id).ToArray(); var update = base.DeleteById(mesSalesDelivery.Id); var insertOrUpdate = db .Deleteable().In(decimals) .ExecuteCommand() > 0; if (update && insertOrUpdate) return true; throw new NotImplementedException("更新失败"); } // 插入或更新数据的方法 private bool SaveOrUpdateData(SqlSugarScope db, SalesDeliveryNotice mesSalesDelivery, List mesSalesDeliveryDatas, string type) { if (mesSalesDelivery.Id != null) base.DeleteById(mesSalesDelivery.Id); if (mesSalesDeliveryDatas.Count > 0) db.Deleteable() .Where(s => s.ErpHeadId == mesSalesDelivery.ErpId).ExecuteCommand(); var orUpdate = base.Insert(mesSalesDelivery); //var baOrUpdate = _SalesDeliveryDetailManager.InsertRange(mesSalesDeliveryDatas); foreach (var item in mesSalesDeliveryDatas) { _SalesDeliveryDetailManager.Insert(item); } //if (orUpdate && baOrUpdate) return true; return orUpdate; throw new NotImplementedException("插入或更新失败"); } // 批量保存记录的方法 public bool SaveList(List salesOrder) { var result = salesOrder.Select(Save).ToList(); return result.All(b => b); } private SalesDeliveryNotice ConvertErpToSalesDelivery( ErpSalesDeliveryDto 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 salesOrder = new SalesDeliveryNotice { BillNo = erpDto.FBillNo, ErpId = erpDto.ErpID, FDate = ParseDateTime(erpDto.FDate) ?? null, BillType = erpDto.FBillTypeID, // 单据类型 //Currency = erpDto.FSettleCurrld, // 结算币别 SalesDept = erpDto.FSaleDeptId, //销售部门 //Customer = erpDto.FCustomerID, // 客户 DeliveryMethod = erpDto.FHeadDeliveryWay, // 交货方式 //DeliveryLocation = erpDto.FHeadLocId, // 交货地点 //Carrier = erpDto.FCarrierID, // 承运商 //TransportNo = erpDto.FCarriageNO, // 运输单号 //BillNoType = erpDto.FCarriageNO, // 单据状态 //DeliveryDept = erpDto.FDeliveryDeptID, // 发货部门 //InventoryGroup = erpDto.FStockerGroupId, // 库存组 //WarehouseManager = erpDto.FStockerId, // 仓管员 //SalesGroup = erpDto.FSaleGroupId, // 销售组 SalesPerson = erpDto.FSalerId,//销售员 //Receiver = erpDto.FReceiveCusId, // 收货方 //ReceiverContact = erpDto.FReceiveCusContact, // 收货方联系人 ReceiverAddress = erpDto.FReceiveAddress, // 收货方地址 //ReceiverName = erpDto.FLinkMan, // 收货方姓名 //LinkPhone = erpDto.FLinkPhone,//联系电话 //SettleParty = erpDto.FSettleID, // 结算方 //PayerParty = erpDto.FPayerID,//付款方 //CreatedBy = erpDto.FCreatorId, // 创建人 CreatedDate = ParseDateTime(erpDto.FCreateDate) ?? null, // 创建时间 //ModifiedBy = erpDto.FModifierId, // 修改人 //ModifiedDate = ParseDateTime(erpDto.FModifyDate) ?? null, // 修改时间 //ClosedBy = erpDto.FCloserId, // 关闭人 //CloseReason = erpDto.FCloseReason, // 关闭原因 // CloseDate = ParseDateTime(erpDto.FCloseDate) ?? null, // 关闭日期 ApprovedBy = erpDto.FApproverId, // 审核人 ApprovedDate = ParseDateTime(erpDto.FApproveDate) ?? null, // 审核日期 //CancelStatus = erpDto.FCancelStatus, // 作废状态 //CancelledBy = erpDto.FCancellerId, // 作废人 //CancelDate = ParseDateTime(erpDto.FCancelDate) ?? null, // 作废日期 //CloseStatus = erpDto.FBillCloseStatus, // 关闭状态 BillStatus = erpDto.FDocumentStatus, //单据状态 DeliveryOrg = erpDto.FDeliveryOrgID, //发货组织 //SalesOrg = erpDto.FSaleOrgId //销售组织 FSourceCategory = erpDto.FSourceCategory, FSourceDocument = erpDto.FSourceDocument, FDeliveryDate = erpDto.FDeliveryDate, FProject = erpDto.FProject, FCustomerPo = erpDto.FCustomerPo, FShippingPolicy = erpDto.FShippingPolicy, FShippingPort = erpDto.FShippingPort, FDestinationPort = erpDto.FDestinationPort, FReceiverContactID = erpDto.FReceiverContactID, FEntryNote = erpDto.FEntryNote, FVersionNo = erpDto.FVersionNo, FBusinessType = erpDto.FBusinessType, FHEADLOCID = erpDto.FHEADLOCID, FCustId = erpDto.FCustId, FNote = erpDto.FNote }; var single = base.GetSingle(it => it.ErpId == erpDto.ErpID); if (single != null) salesOrder.Id = single.Id; if (salesOrder.ErpId is null || salesOrder.ErpId=="") { throw new NotImplementedException( $"表头ErpID不能为空"); } return salesOrder; } private List ConvertErpToSalesDeliveryDetail( List erpDtoList) { var salesOrderSubList = new List(); 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 } foreach (var erpDto in erpDtoList) { var salesOrderSub = new SalesDeliveryNoticeDetail { ErpLineId = erpDto.ErpID,//ERP行ID ErpHeadId = erpDto.EHID,//ERP头ID MaterialId = erpDto.FMaterialId,//物料编号 //MaterialName = erpDto.FMaterialName,// 物料名称 //MaterialSpecification = erpDto.FMaterialModel,//规格型号 SalesUnitId = erpDto.FUnitID,//销售单位 SalesQuantity = Convert.ToDecimal(erpDto.FQty),//销售数量 //IsFree = erpDto.FIsFree,//是否赠品 //DeliveryDate = ParseDateTime(erpDto.FDeliverydate),//要货日期 //Warehouse = erpDto.FStockId,//出货仓库 //PlanTrackingNumber = erpDto.FMtoNo,//计划跟踪号 //LotNumber = erpDto.FLot,//批号 SrcBillNo = erpDto.FSrcBillNo,//源单编号 //PriceQty = erpDto.FPriceUnitQty,//计价数量 OwnerId = erpDto.FOwnerIdHead,//货主 //InventoryUnit = erpDto.FStockUnitID,//库存单位 //InventoryQuantity = Convert.ToDecimal(erpDto.FStockQty),//库存数量 //MaterialCategory = erpDto.FMaterialType,//物料类别 PlanDeliveryDate = ParseDateTime(erpDto.FPlanDeliveryDate),//计划发货日期 //OutLmtUnit = erpDto.FOutLmtUnit,//超发控制单位 //OutMaxQty = erpDto.FOutMaxQty,//出库上限 //OutMinQty = erpDto.FOutMinQty,//出库下限 DeliveryLoc = erpDto.FDeliveryLoc,//交货地点 DeliverylAddress = erpDto.FDeliveryLAddress,//交货地址 SourceBillNo = erpDto.FSrcBillNo,//原单单号 //OrderBillNo = erpDto.FOrderNo,//订单单号 //TerminationStatus = erpDto.FTerminationStatus,//终止状态 //TerminationDate = ParseDateTime(erpDto.FTerminateDate),//业务终止日期 //SumOutQty = erpDto.FSumOutQty,//累计出库数量 //RemainOutQty = erpDto.FRemainOutQty,//未出库数量 //NOTE = erpDto.FEntrynote//备注 FDeliveryDate = erpDto.FDeliveryDate, FMapId = erpDto.FMapId, FMapName = erpDto.FMapName, FMapId2 = erpDto.FMapId2, FProject = erpDto.FProject, FStorageLocation = erpDto.FStorageLocation, FFreeItemType = erpDto.FFreeItemType, FBodyNote = erpDto.FBodyNote, FAvailableStock = erpDto.FAvailableStock, FInventoryQueryTime = erpDto.FInventoryQueryTime, FLineStatus = erpDto.FLineStatus, FReceiveAddressFull = erpDto.FReceiveAddressFull, FReceiverContact = erpDto.FReceiverContact, FReturnExchangeAllowed = erpDto.FReturnExchangeAllowed, FOutsourcedProcessQty = erpDto.FOutsourcedProcessQty, FAvailableDate = erpDto.FAvailableDate, FCommitmentDate = erpDto.FCommitmentDate, FCustomerConfirmShipDate = erpDto.FCustomerConfirmShipDate, FPlannedOrg = erpDto.FPlannedOrg, FVendorDirectShip = erpDto.FVendorDirectShip, FSupplySource = erpDto.FSupplySource, FSupplyType = erpDto.FSupplyType, FSupplyOrg = erpDto.FSupplyOrg, FSupplier = erpDto.FSupplier, FShippingMethod = erpDto.FShippingMethod, FMRPDR = erpDto.FMRPDR, FDemandCategory = erpDto.FDemandCategory, FInventoryPlanning = erpDto.FInventoryPlanning, FLineRemark = erpDto.FLineRemark, FSourceDocCategory = erpDto.FSourceDocCategory, FSourceDocNo = erpDto.FSourceDocNo, FSourceDocLineNo = erpDto.FSourceDocLineNo, FLINE_NO = erpDto.FLINE_NO }; if (salesOrderSub.ErpLineId is null || salesOrderSub.ErpLineId=="") { throw new NotImplementedException( $"明细行ErpID(ERP行ID)不能为空"); } if (salesOrderSub.ErpHeadId is null || salesOrderSub.ErpHeadId == "") { throw new NotImplementedException( $"明细行EHID(ERP头ID)不能为空"); } var single = _SalesDeliveryDetailManager.GetSingle(it => it.ErpLineId == salesOrderSub.ErpLineId); if (single != null) salesOrderSub.Id = single.Id; var mesLinkU9 = Db.Queryable() .Where(x => x.U9Id == erpDto.FMaterialId && x.OrgId == erpDto.FSupplyOrg && x.TableType == "MES_ITEMS").First(); if (mesLinkU9 != null) salesOrderSub.MaterialId = mesLinkU9.MesId; var mesLinkU92 = Db.Queryable() .Where(x => x.U9Id == erpDto.FProject && x.OrgId == erpDto.FSupplyOrg && x.TableType == "MES_PROJECT").First(); if (mesLinkU92 != null) salesOrderSub.FProject = mesLinkU92.MesId; salesOrderSubList.Add(salesOrderSub); } return salesOrderSubList; } } }