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,10 +31,13 @@
        private readonly ISys_ConfigService _configService;
        private readonly ILocationInfoRepository _locationRepository;
        private readonly IDt_InboundOrderRepository _inboundOrderRepository;
        private readonly IDt_ProductionOutboundOrderRepository _productionoutboundorderRepository;
        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)
        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;
@@ -43,11 +47,14 @@
            _stationManagerRepository = stationManagerRepository;
            _configService = configService;
            _inboundOrderRepository = inboundOrderRepository;
            _allocateoutboundorderRepository = allocateoutboundorderRepository;
            _productionoutboundorderRepository = productionOutboundOrderRepository;
            _unitOfWorkManage = unitOfWorkManage;
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
            _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(5));
            return Task.CompletedTask;
        }
@@ -55,22 +62,21 @@
        {
            try
            {
                #region å…¥åº“单据同步
                List<WMS_采购入成品入库自动打印清单_ST> ERPPrintChecklist =SqlSugarHelper.DBERP.Queryable<WMS_采购入成品入库自动打印清单_ST>().Where(x => true).ToList();
                
                var printCheckList = _inboundOrderRepository.QueryData(x => true);
                List<Dt_InboundOrder> inboundOrderAdd = new List<Dt_InboundOrder>();
                List<Dt_InboundOrder> inboundOrderUpdate = new List<Dt_InboundOrder>();
                if (ERPPrintChecklist != null) return;
                foreach (var item in ERPPrintChecklist)
                {
                    var x = printCheckList.Where(x => x.MaterialNo == item.料号 && x.OrderNo == item.单号 && x.WarehouseName == item.入库仓库名称).FirstOrDefault();
                    var x = printCheckList.Where(x => x.MaterialNo == item.料号 && x.UpperOrderNo == item.单号 && x.WarehouseName == item.入库仓库名称).FirstOrDefault();
                    if (x == null)
                    {
                        Dt_InboundOrder Print = new Dt_InboundOrder()
                        {
                            PrintCode = GetOrderPintCode(),
                            OrderNo = item.单号,
                            OrderNo = GetOrderPintCode("OrderNoIn"),
                            UpperOrderNo = item.单号,
                            DemandClassification = item.需求分类,
                            OrderType = item.单据类型,
                            WarehouseName = item.入库仓库名称,
@@ -91,10 +97,10 @@
                    }
                    else
                    {
                        if (x.PrintCode == null || x.PrintCode == "")
                        if (x.OrderNo == null || x.OrderNo == "")
                        {
                            x.PrintCode = GetOrderPintCode();
                            x.OrderNo = item.单号;
                            x.OrderNo = GetOrderPintCode("OrderNoIn");
                            x.UpperOrderNo = item.单号;
                            x.DemandClassification = item.需求分类;
                            x.OrderType = item.单据类型;
                            x.WarehouseName = item.入库仓库名称;
@@ -121,17 +127,210 @@
                {
                    _inboundOrderRepository.UpdateData(inboundOrderUpdate);
                }
                #endregion
                #region ERP生产领料单同步
                var productionOutOrder = _productionoutboundorderRepository.Db.Queryable<Dt_ProductionOutboundOrder>().Includes(x => x.Details).ToList();
                List<Dt_ProductionOutboundOrder> productionoutboundOrderAdd = 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.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号 && x.OrderSpec == item.订单规格).FirstOrDefault();
                    if (x == 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推送,
                                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.单号).FirstOrDefault();
                            existingOrder.Details.Add(new Dt_ProductionOutboundOrderDetail()
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                Specs = item.子件规格,
                                Weight = 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)
                    //{
                    //    _productionoutboundorderRepository.AddDataNavAsync(order);
                    //}
                    SqlSugarHelper.DbWMS.InsertNav(productionoutboundOrderAdd).Include(x => x.Details).ExecuteCommandAsync();
                }
                if (allocateoutboundorderAdd.Count > 0)
                {
                    SqlSugarHelper.DbWMS.InsertNav(allocateoutboundorderAdd).Include(x=>x.Details).ExecuteCommandAsync();
                }
                _unitOfWorkManage.CommitTran();
                #endregion
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                ConsoleHelper.WriteErrorLine($"错误信息:" + ex.Message);
            }
        }
        public string GetOrderPintCode()
        public string GetOrderPintCode(string printCode)
        {
            string PrintCode = "";
            var PrintSetting = SqlSugarHelper.DbWMS.Queryable<Dt_PrintSetting>().Where(x => x.PrintCode == "OrderNo").ToList().FirstOrDefault();
            var PrintSetting = SqlSugarHelper.DbWMS.Queryable<Dt_PrintSetting>().Where(x => x.PrintCode == printCode).ToList().FirstOrDefault();
            if (PrintSetting.Spare1 == DateTime.Now.ToString("yyyyMMdd"))
            {
@@ -142,7 +341,7 @@
            {
                PrintSetting.Spare1 = DateTime.Now.ToString("yyyyMMdd");
                PrintSetting.PrintNo = 2;
                PrintCode = PrintSetting.PrintCode + PrintSetting.Spare1 + 1.ToString().PadLeft(PrintSetting.Spare2, '0');
                PrintCode = PrintSetting.Spare1 + 1.ToString().PadLeft(PrintSetting.Spare2, '0');
            }
            SqlSugarHelper.DbWMS.Updateable(PrintSetting).ExecuteCommand();
            return PrintCode;