huangxiaoqiang
2025-10-27 387731cab892804912e68cb91e6fb804411c4756
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/AspNetCoreSchedule.cs
@@ -1,4 +1,5 @@
using MailKit.Search;
using AngleSharp.Dom;
using MailKit.Search;
using Masuit.Tools;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@@ -30,14 +31,13 @@
        private readonly ISys_ConfigService _configService;
        private readonly ILocationInfoRepository _locationRepository;
        private readonly IDt_InboundOrderRepository _inboundOrderRepository;
        private readonly IDt_OtherOutboundOrderRepository _otheroutboundorderRepository;
        private readonly IDt_ProductionOutboundOrderRepository _productionoutboundorderRepository;
        private readonly IDt_InventoryOutboundOrderRepository _inventoryoutboundorderRepository;
        private readonly IDt_AllocateOutboundOrderRepository _allocateoutboundorderRepository;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private Timer _timer;
        public MyBackgroundService(ILogger<MyBackgroundService> logger, ILocationInfoRepository locationRepository, IStockInfoRepository stockInfoRepository, IDt_AreaInfoRepository areaInfoRepository, IDt_TaskRepository taskRepository, IDt_StationManagerRepository stationManagerRepository, ISys_ConfigService configService,IDt_InboundOrderRepository inboundOrderRepository, IDt_OtherOutboundOrderRepository otheroutboundorderRepository, IDt_AllocateOutboundOrderRepository allocateoutboundorderRepository, IDt_InventoryOutboundOrderRepository inventoryOutboundOrderRepository,IDt_ProductionOutboundOrderRepository productionOutboundOrderRepository)
        public MyBackgroundService(ILogger<MyBackgroundService> logger, ILocationInfoRepository locationRepository, IStockInfoRepository stockInfoRepository, IDt_AreaInfoRepository areaInfoRepository, IDt_TaskRepository taskRepository, IDt_StationManagerRepository stationManagerRepository, ISys_ConfigService configService,IDt_InboundOrderRepository inboundOrderRepository,IDt_AllocateOutboundOrderRepository allocateoutboundorderRepository,IDt_ProductionOutboundOrderRepository productionOutboundOrderRepository,IUnitOfWorkManage unitOfWorkManage)
        {
            _logger = logger;
            _locationRepository = locationRepository;
@@ -47,10 +47,9 @@
            _stationManagerRepository = stationManagerRepository;
            _configService = configService;
            _inboundOrderRepository = inboundOrderRepository;
            _otheroutboundorderRepository = otheroutboundorderRepository;
            _allocateoutboundorderRepository = allocateoutboundorderRepository;
            _inventoryoutboundorderRepository = inventoryOutboundOrderRepository;
            _productionoutboundorderRepository = productionOutboundOrderRepository;
            _unitOfWorkManage = unitOfWorkManage;
        }
        public Task StartAsync(CancellationToken cancellationToken)
@@ -130,28 +129,28 @@
                }
                #endregion
                #region ERP生产领料单同步
                var productionOutOrder = _productionoutboundorderRepository.Db.Queryable<Dt_ProductionOutboundOrder>().Includes(x => x.Details).ToList();
                List<Dt_ProductionOutboundOrder> EngineeringoutboundOrderAdd = new List<Dt_ProductionOutboundOrder>();
                List<Dt_ProductionOutboundOrder> EngineeringoutboundOrderUpdate = new List<Dt_ProductionOutboundOrder>();
                List<Dt_ProductionOutboundOrder> productionoutboundOrderAdd = new List<Dt_ProductionOutboundOrder>();
                List<Dt_ProductionOutboundOrder> productionoutboundOrderUpdate = new List<Dt_ProductionOutboundOrder>();
                List<WMS_用友工程发货单一览表_ST> EngineeringERP = SqlSugarHelper.DBERP.Queryable<WMS_用友工程发货单一览表_ST>().Where(x => true).ToList();
                List<WMS_用友生产领料单一览表_ST> productERP = SqlSugarHelper.DBERP.Queryable<WMS_用友生产领料单一览表_ST>().Where(x => true).ToList();
                foreach (var item in EngineeringERP)
                {
                    var x = productionOutOrder.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault();
                    var x = productionOutOrder.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号 && x.OrderSpec == item.订单规格).FirstOrDefault();
                    if (x == null)
                    {
                        if(productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault()==null)
                        if(productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号 && x.OrderSpec == item.订单规格).FirstOrDefault()==null)
                        {
                            Dt_ProductionOutboundOrder outOrder = new Dt_ProductionOutboundOrder()
                            {
                                OrderNo = GetOrderPintCode("OrderNoOut"),
                                UpperOrderNo = item.单号,
                                OrderName = item.订单品名,
                                DemandClassification = item.需求名称,
                                OrderPartNumber = item.订单料号,
                                OrderSpec = item.订单规格,
                                OrderStatus = (int)OrderStateEmun.未开始,
                                OrderType = (int)OrderTypeEmun.生产领料单,
                                CreateType = (int)OrderCreateTypeEmun.ERP推送,
@@ -161,100 +160,8 @@
                                    {
                                        MaterielCode = item.子件料号,
                                        MaterielName = item.子件品名,
                                        DemandClassification = item.需求名称,
                                        OrderQuantity = item.子件实际用量,
                                        NotOutQuantity = item.子件未发数量,
                                        OverOutQuantity = item.子件发放数量,
                                    }
                                }
                            };
                            EngineeringoutboundOrderAdd.Add(outOrder);
                        }
                        else
                        {
                            var existingOrder = EngineeringoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault();
                            existingOrder.Details.Add(new Dt_ProductionOutboundOrderDetail()
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                DemandClassification = item.需求名称,
                                OrderQuantity = item.子件实际用量,
                                NotOutQuantity = item.子件未发数量,
                                OverOutQuantity = item.子件发放数量,
                            });
                        }
                    }
                    //else
                    //{
                    //    foreach (var detail in x.Details)
                    //    {
                    //        if (detail.MaterielCode != item.子件料号 && detail.MaterielName != item.子件品名)
                    //        {
                    //            if (EngineeringoutboundOrderUpdate.Where(o => o.UpperOrderNo == item.单号).FirstOrDefault() == null)
                    //            {
                    //                x.Details = new List<Dt_ProductionOutboundOrderDetail>()
                    //                        {
                    //                            new Dt_ProductionOutboundOrderDetail()
                    //                            {
                    //                                MaterielCode = item.子件料号,
                    //                                MaterielName = item.子件品名,
                    //                                DemandClassification = item.需求名称,
                    //                                OrderQuantity = item.子件实际用量,
                    //                                NotOutQuantity = item.子件未发数量,
                    //                                OverOutQuantity = item.子件发放数量,
                    //                            }
                    //                        };
                    //                EngineeringoutboundOrderUpdate.Add(x);
                    //            }
                    //            else
                    //            {
                    //                var existingOrder = EngineeringoutboundOrderUpdate.Where(o => o.UpperOrderNo == item.单号).FirstOrDefault();
                    //                existingOrder.Details.Add(new Dt_ProductionOutboundOrderDetail()
                    //                {
                    //                    MaterielCode = item.子件料号,
                    //                    MaterielName = item.子件品名,
                    //                    DemandClassification = item.需求名称,
                    //                    OrderQuantity = item.子件实际用量,
                    //                    NotOutQuantity = item.子件未发数量,
                    //                    OverOutQuantity = item.子件发放数量,
                    //                });
                    //            }
                    //        }
                    //    }
                    //}
                }
                if (EngineeringoutboundOrderAdd.Count > 0)
                {
                    foreach (var order in EngineeringoutboundOrderAdd)
                    {
                        _productionoutboundorderRepository.AddDataNavAsync(order);
                    }
                }
                if (EngineeringoutboundOrderUpdate.Count > 0)
                {
                    _productionoutboundorderRepository.UpdateDataNav(EngineeringoutboundOrderUpdate);
                }
                foreach (var item in productERP)
                {
                    var x = productionOutOrder.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault();
                    if (x == null)
                    {
                        if (productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault() == null)
                        {
                            Dt_ProductionOutboundOrder outOrder = new Dt_ProductionOutboundOrder()
                            {
                                OrderNo = GetOrderPintCode("OrderNoOut"),
                                UpperOrderNo = item.单号,
                                OrderStatus = (int)OrderStateEmun.未开始,
                                OrderType = (int)OrderTypeEmun.生产领料单,
                                CreateType = (int)OrderCreateTypeEmun.ERP推送,
                                Details = new List<Dt_ProductionOutboundOrderDetail>()
                                {
                                    new Dt_ProductionOutboundOrderDetail()
                                    {
                                        MaterielCode = item.子件料号,
                                        MaterielName = item.子件品名,
                                        DemandClassification = item.需求名称,
                                        Specs = item.子件规格,
                                        Weight = item.子件单重,
                                        OrderQuantity = item.子件实际用量,
                                        NotOutQuantity = item.子件未发数量,
                                        OverOutQuantity = item.子件发放数量,
@@ -270,67 +177,152 @@
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                DemandClassification = item.需求名称,
                                Specs = item.子件规格,
                                Weight = item.子件单重,
                                OrderQuantity = item.子件实际用量,
                                NotOutQuantity = item.子件未发数量,
                                OverOutQuantity = item.子件发放数量,
                            });
                        }
                    }
                    //else
                    //{
                    //    foreach (var detail in x.Details)
                    //    {
                    //        if (productionoutboundOrderUpdate.Where(o => o.UpperOrderNo == item.单号).FirstOrDefault() == null)
                    //        {
                    //            x.Details = new List<Dt_ProductionOutboundOrderDetail>()
                    //                        {
                    //                            new Dt_ProductionOutboundOrderDetail()
                    //                            {
                    //                                MaterielCode = item.子件料号,
                    //                                MaterielName = item.子件品名,
                    //                                DemandClassification = item.需求名称,
                    //                                OrderQuantity = item.子件实际用量,
                    //                                NotOutQuantity = item.子件未发数量,
                    //                                OverOutQuantity = item.子件发放数量,
                    //                            }
                    //                        };
                    //            productionoutboundOrderUpdate.Add(x);
                    //        }
                    //        else
                    //        {
                    //            var existingOrder = productionoutboundOrderUpdate.Where(o => o.UpperOrderNo == item.单号).FirstOrDefault();
                    //            existingOrder.Details.Add(new Dt_ProductionOutboundOrderDetail()
                    //            {
                    //                MaterielCode = item.子件料号,
                    //                MaterielName = item.子件品名,
                    //                DemandClassification = item.需求名称,
                    //                OrderQuantity = item.子件实际用量,
                    //                NotOutQuantity = item.子件未发数量,
                    //                OverOutQuantity = item.子件发放数量,
                    //            });
                    //        }
                    //    }
                    //}
                }
                foreach (var item in productERP)
                {
                    var x = productionOutOrder.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault();
                    if (x == null)
                    {
                        if (productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault() == null)
                        {
                            Dt_ProductionOutboundOrder outOrder = new Dt_ProductionOutboundOrder()
                            {
                                OrderNo = GetOrderPintCode("OrderNoOut"),
                                UpperOrderNo = item.单号,
                                OrderName = item.订单品名,
                                DemandClassification = item.需求名称,
                                OrderPartNumber = item.订单料号,
                                //OrderSpec = item.订单规格,
                                OrderStatus = (int)OrderStateEmun.未开始,
                                OrderType = (int)OrderTypeEmun.生产领料单,
                                CreateType = (int)OrderCreateTypeEmun.ERP推送,
                                Details = new List<Dt_ProductionOutboundOrderDetail>()
                                {
                                    new Dt_ProductionOutboundOrderDetail()
                                    {
                                        MaterielCode = item.子件料号,
                                        MaterielName = item.子件品名,
                                        Specs = item.子件规格,
                                        Weight = item.子件单重,
                                        OrderQuantity = item.子件实际用量,
                                        NotOutQuantity = item.子件未发数量,
                                        OverOutQuantity = item.子件发放数量,
                                    }
                                }
                            };
                            productionoutboundOrderAdd.Add(outOrder);
                        }
                        else
                        {
                            var existingOrder = productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault();
                            existingOrder.Details.Add(new Dt_ProductionOutboundOrderDetail()
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                Specs = item.子件规格,
                                Weight = item.子件单重,
                                OrderQuantity = item.子件实际用量,
                                NotOutQuantity = item.子件未发数量,
                                OverOutQuantity = item.子件发放数量,
                            });
                        }
                }
                
                }
                #endregion
                #region ERP调拨出库单同步
                var allocateoutboundorder = _allocateoutboundorderRepository.Db.Queryable<Dt_AllocateOutboundOrder>().Includes(x => x.Details).ToList();
                List<Dt_AllocateOutboundOrder> allocateoutboundorderAdd = new List<Dt_AllocateOutboundOrder>();
                List<Dt_AllocateOutboundOrder> allocateoutboundorderUpdate = new List<Dt_AllocateOutboundOrder>();
                List<WMS_用友电控柜调拨一览表_ST> ERPallocateOrder = SqlSugarHelper.DBERP.Queryable<WMS_用友电控柜调拨一览表_ST>().Where(x => true).ToList();
                foreach (var item in ERPallocateOrder)
                {
                    var x = allocateoutboundorder.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号 && x.OrderSpec == item.订单规格).FirstOrDefault();
                    if (x == null)
                    {
                        if (allocateoutboundorderAdd.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault() == null)
                        {
                            Dt_AllocateOutboundOrder outOrder = new Dt_AllocateOutboundOrder()
                            {
                                OrderNo = GetOrderPintCode("OrderNoOut"),
                                UpperOrderNo = item.单号,
                                OrderName=item.订单品名,
                                DemandClassification=item.需求名称,
                                OrderPartNumber =item.订单料号,
                                OrderSpec =item.订单规格,
                                OrderStatus = (int)OrderStateEmun.未开始,
                                OrderType = (int)OrderTypeEmun.调拨出库单,
                                CreateType = (int)OrderCreateTypeEmun.ERP推送,
                                Details = new List<Dt_AllocateOutboundOrderDetail>()
                                {
                                    new Dt_AllocateOutboundOrderDetail()
                                    {
                                        MaterielCode = item.子件料号,
                                        MaterielName = item.子件品名,
                                        Specs = item.子件规格,
                                        Weight = item.子件单重,
                                        OrderQuantity = item.子件实际用量,
                                        NotOutQuantity = item.子件未发数量,
                                        OverOutQuantity = item.子件发放数量,
                                    }
                                }
                            };
                            allocateoutboundorderAdd.Add(outOrder);
                        }
                        else
                        {
                            var existingOrder = allocateoutboundorderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号 && x.OrderSpec == item.订单规格).FirstOrDefault();
                            existingOrder.Details.Add(new Dt_AllocateOutboundOrderDetail()
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                Specs = item.子件规格,
                                Weight = item.子件单重,
                                OrderQuantity = item.子件实际用量,
                                NotOutQuantity = item.子件未发数量,
                                OverOutQuantity = item.子件发放数量,
                            });
                        }
                    }
                }
                #endregion
                #region æ•°æ®å¤„理
                _unitOfWorkManage.BeginTran();
                if (productionoutboundOrderAdd.Count > 0)
                {
                    foreach (var order in productionoutboundOrderAdd)
                    //foreach (var order in productionoutboundOrderAdd)
                    //{
                    //    _productionoutboundorderRepository.AddDataNavAsync(order);
                    //}
                    SqlSugarHelper.DbWMS.InsertNav(productionoutboundOrderAdd).Include(x => x.Details).ExecuteCommandAsync();
                }
                if (allocateoutboundorderAdd.Count > 0)
                    {
                        _productionoutboundorderRepository.AddDataNavAsync(order);
                    SqlSugarHelper.DbWMS.InsertNav(allocateoutboundorderAdd).Include(x=>x.Details).ExecuteCommandAsync();
                    } 
                }
                if (productionoutboundOrderUpdate.Count > 0)
                {
                    _productionoutboundorderRepository.UpdateDataNav(productionoutboundOrderUpdate);
                }
                _unitOfWorkManage.CommitTran();
                #endregion
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                ConsoleHelper.WriteErrorLine($"错误信息:" + ex.Message);
            }
        }