From 644cbe3e4eba765affa25454d69fee6286bb6094 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期四, 30 十月 2025 13:06:35 +0800
Subject: [PATCH] 1

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/AspNetCoreSchedule.cs |  335 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 325 insertions(+), 10 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/AspNetCoreSchedule.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/AspNetCoreSchedule.cs"
index 4ad04b1..72930e3 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/AspNetCoreSchedule.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/AspNetCoreSchedule.cs"
@@ -1,13 +1,25 @@
-锘縰sing Masuit.Tools;
+锘縰sing AngleSharp.Dom;
+using MailKit.Search;
+using Masuit.Tools;
 using Microsoft.Extensions.Hosting;
 using Microsoft.Extensions.Logging;
-using System.Net.Mail;
+using SixLabors.Fonts;
+using SqlSugar;
+using System;
+using System.Data;
 using System.Net;
+using System.Net.Mail;
 using System.Text;
+using System.Xml.Linq;
 using WIDESEA_Cache;
 using WIDESEA_Core.Const;
 using WIDESEA_DTO.WMS;
+using WIDESEA_IOrderRepository;
+using WIDESEA_IOrderServices;
 using WIDESEA_IServices;
+using WIDESEA_Model.Models.ERP;
+using WIDESEA_Model.Models.Order;
+using WIDESEA_OrderRepository;
 using WIDESEAWCS_BasicInfoRepository;
 
 namespace WIDESEA_StorageTaskServices
@@ -16,28 +28,35 @@
     {
         private readonly ILogger<MyBackgroundService> _logger;
         private readonly IStockInfoRepository _stockInfoRepository;
-        private readonly IDt_AreaInfoRepository _areaInfoRepository; //鍖哄煙
         private readonly IDt_TaskRepository _taskRepository;
-        private readonly IDt_StationManagerRepository _stationManagerRepository;
         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 readonly IDt_ProductionOutboundOrderService _productionOutboundOrderService;
+        private readonly IDt_AllocateOutboundOrderService _allocateOutboundOrderService;
         private Timer _timer;
 
-        public MyBackgroundService(ILogger<MyBackgroundService> logger, ILocationInfoRepository locationRepository, IStockInfoRepository stockInfoRepository, IDt_AreaInfoRepository areaInfoRepository, IDt_TaskRepository taskRepository, IDt_StationManagerRepository stationManagerRepository, ISys_ConfigService configService)
+        public MyBackgroundService(ILogger<MyBackgroundService> logger, ILocationInfoRepository locationRepository, IStockInfoRepository stockInfoRepository,IDt_TaskRepository taskRepository,ISys_ConfigService configService,IDt_InboundOrderRepository inboundOrderRepository,IDt_AllocateOutboundOrderRepository allocateoutboundorderRepository,IDt_ProductionOutboundOrderRepository productionOutboundOrderRepository,IUnitOfWorkManage unitOfWorkManage, IDt_ProductionOutboundOrderService productionOutboundOrderService, IDt_AllocateOutboundOrderService allocateOutboundOrderService)
         {
             _logger = logger;
             _locationRepository = locationRepository;
             _stockInfoRepository = stockInfoRepository;
-            _areaInfoRepository = areaInfoRepository;
             _taskRepository = taskRepository;
-            _stationManagerRepository = stationManagerRepository;
             _configService = configService;
+            _inboundOrderRepository = inboundOrderRepository;
+            _allocateoutboundorderRepository = allocateoutboundorderRepository;
+            _productionoutboundorderRepository = productionOutboundOrderRepository;
+            _unitOfWorkManage = unitOfWorkManage;
+            _productionOutboundOrderService = productionOutboundOrderService;
+            _allocateOutboundOrderService = allocateOutboundOrderService;
         }
 
         public Task StartAsync(CancellationToken cancellationToken)
         {
-            _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(5));
+            _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
             return Task.CompletedTask;
         }
 
@@ -45,12 +64,295 @@
         {
             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>();
+                foreach (var item in ERPPrintChecklist)
+                {
+                    var x = printCheckList.Where(x => x.MaterialNo == item.鏂欏彿 && x.UpperOrderNo == item.鍗曞彿 && x.WarehouseName == item.鍏ュ簱浠撳簱鍚嶇О).FirstOrDefault();
+                    if (x == null)
+                    {
+                        Dt_InboundOrder Print = new Dt_InboundOrder()
+                        {
+                            OrderNo = GetOrderPintCode("OrderNoIn"),
+                            UpperOrderNo = item.鍗曞彿,
+                            DemandClassification = item.闇�姹傚垎绫�,
+                            OrderType = item.鍗曟嵁绫诲瀷,
+                            WarehouseName = item.鍏ュ簱浠撳簱鍚嶇О,
+                            Datetime = item.鏃ユ湡.ToString(),
+                            LineNumber = item.琛屽彿,
+                            ProductDrawingNumber = item.浜у搧鍥惧彿,
+                            MaterialNo = item.鏂欏彿,
+                            MaterialName = item.鍝佸悕,
+                            Weight = item.鍗曢噸,
+                            WareHouseId = item.鍏ュ簱浠撳簱缂栫爜,
+                            Specs = item.瑙勬牸,
+                            Unit = item.鍗曚綅,
+                            Texture = item.鐢ㄥ弸鏉愯川,
+                            Quantity = item.鍏ュ簱鏁伴噺,
+                            OrderStatus = item.鍗曟嵁鐘舵��
+
+                        };
+                        inboundOrderAdd.Add(Print);
+                    }
+                    else
+                    {
+                        if (x.OrderNo == null || x.OrderNo == "")
+                        {
+                            x.OrderNo = GetOrderPintCode("OrderNoIn");
+                            x.UpperOrderNo = item.鍗曞彿;
+                            x.DemandClassification = item.闇�姹傚垎绫�;
+                            x.OrderType = item.鍗曟嵁绫诲瀷;
+                            x.WarehouseName = item.鍏ュ簱浠撳簱鍚嶇О;
+                            x.Datetime = item.鏃ユ湡.ToString();
+                            x.LineNumber = item.琛屽彿;
+                            x.ProductDrawingNumber = item.浜у搧鍥惧彿;
+                            x.MaterialNo = item.鏂欏彿;
+                            x.MaterialName = item.鍝佸悕;
+                            x.Weight = item.鍗曢噸;
+                            x.Specs = item.瑙勬牸;
+                            x.Unit = item.鍗曚綅;
+                            x.Texture = item.鐢ㄥ弸鏉愯川;
+                            x.Quantity = item.鍏ュ簱鏁伴噺;
+                            x.OrderStatus = item.鍗曟嵁鐘舵��;
+                            x.WareHouseId = item.鍏ュ簱浠撳簱缂栫爜;
+                            inboundOrderUpdate.Add(x);
+                        }
+                    }
+                }
+                #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.璁㈠崟鏂欏彿).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 = item.鍗曞彿,
+                                UpperOrderNo = item.鍗曞彿,
+                                OrderName = item.璁㈠崟鍝佸悕,
+                                DemandClassification = item.闇�姹傚悕绉�,
+                                OrderPartNumber = 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.瀛愪欢鍗曢噸,
+                                        ERPOrderId = item.澶囨枡鍗旾D,
+                                        WareHouseId =item.瀛樺偍鍦扮偣ID,
+                                        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.瀛愪欢鍗曢噸,
+                                ERPOrderId = item.澶囨枡鍗旾D,
+                                WareHouseId = item.瀛樺偍鍦扮偣ID,
+                                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 = item.鍗曞彿,
+                                UpperOrderNo = item.鍗曞彿,
+                                OrderName = item.璁㈠崟鍝佸悕,
+                                DemandClassification = item.闇�姹傚悕绉�,
+                                OrderPartNumber = 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.瀛愪欢鍗曢噸,
+                                        ERPOrderId = item.澶囨枡鍗旾D,
+                                        WareHouseId =item.瀛樺偍鍦扮偣ID,
+                                        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.瀛愪欢鍗曢噸,
+                                ERPOrderId = item.澶囨枡鍗旾D,
+                                WareHouseId = item.瀛樺偍鍦扮偣ID,
+                                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.璁㈠崟鏂欏彿).FirstOrDefault();
+                    if (x == null)
+                    {
+                        if (allocateoutboundorderAdd.Where(x => x.UpperOrderNo == item.鍗曞彿).FirstOrDefault() == null)
+                        {
+                            Dt_AllocateOutboundOrder outOrder = new Dt_AllocateOutboundOrder()
+                            {
+                                OrderNo = item.鍗曞彿,
+                                UpperOrderNo = item.鍗曞彿,
+                                OrderName = item.璁㈠崟鍝佸悕,
+                                DemandClassification = item.闇�姹傚悕绉�,
+                                OrderPartNumber = 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.瀛愪欢鍗曢噸,
+                                        ERPOrderId = item.澶囨枡鍗旾D,
+                                        WareHouseId =item.瀛樺偍鍦扮偣ID,
+                                        OrderQuantity = item.瀛愪欢瀹為檯鐢ㄩ噺,
+                                        NotOutQuantity = item.瀛愪欢鏈彂鏁伴噺,
+                                        OverOutQuantity = item.瀛愪欢鍙戞斁鏁伴噺,
+                                    }
+                                }
+                            };
+                            allocateoutboundorderAdd.Add(outOrder);
+                        }
+                        else
+                        {
+                            var existingOrder = allocateoutboundorderAdd.Where(x => x.UpperOrderNo == item.鍗曞彿 && x.DemandClassification == item.闇�姹傚悕绉� && x.OrderPartNumber == item.璁㈠崟鏂欏彿).FirstOrDefault();
+                            existingOrder.Details.Add(new Dt_AllocateOutboundOrderDetail()
+                            {
+                                MaterielCode = item.瀛愪欢鏂欏彿,
+                                MaterielName = item.瀛愪欢鍝佸悕,
+                                Specs = item.瀛愪欢瑙勬牸,
+                                Weight = item.瀛愪欢鍗曢噸,
+                                ERPOrderId = item.澶囨枡鍗旾D,
+                                WareHouseId = item.瀛樺偍鍦扮偣ID,
+                                OrderQuantity = item.瀛愪欢瀹為檯鐢ㄩ噺,
+                                NotOutQuantity = item.瀛愪欢鏈彂鏁伴噺,
+                                OverOutQuantity = item.瀛愪欢鍙戞斁鏁伴噺,
+                            });
+                        }
+                    }
+                }
+                #endregion
+
+                #region 鏁版嵁澶勭悊
+
+                if (inboundOrderAdd.Count > 0)
+                {
+                    PrintInbound(inboundOrderAdd);
+                    _inboundOrderRepository.AddData(inboundOrderAdd);
+                }
+                if (inboundOrderUpdate.Count > 0)
+                {
+                    PrintInbound(inboundOrderUpdate);
+                    _inboundOrderRepository.UpdateData(inboundOrderUpdate);
+                }
+
+                if (productionoutboundOrderAdd.Count > 0)
+                {
+                    SqlSugarHelper.DbWMS.InsertNav(productionoutboundOrderAdd).Include(x => x.Details).ExecuteCommand();
+                }
+                if (allocateoutboundorderAdd.Count > 0)
+                {
+                    SqlSugarHelper.DbWMS.InsertNav(allocateoutboundorderAdd).Include(x => x.Details).ExecuteCommand();
+                }
+
+                #endregion
+
             }
             catch (Exception ex)
             {
                 ConsoleHelper.WriteErrorLine($"閿欒淇℃伅锛�" + ex.Message);
             }
+        }
+
+        public string GetOrderPintCode(string printCode)
+        {
+            string PrintCode = "";
+            var PrintSetting = SqlSugarHelper.DbWMS.Queryable<Dt_PrintSetting>().Where(x => x.PrintCode == printCode).ToList().FirstOrDefault();
+
+            if (PrintSetting.Spare1 == DateTime.Now.ToString("yyyyMMdd"))
+            {
+                PrintCode = PrintSetting.Spare1 + PrintSetting.PrintNo.ToString().PadLeft(PrintSetting.Spare2, '0');
+                PrintSetting.PrintNo = PrintSetting.PrintNo + 1;
+            }
+            else
+            {
+                PrintSetting.Spare1 = DateTime.Now.ToString("yyyyMMdd");
+                PrintSetting.PrintNo = 2;
+                PrintCode = PrintSetting.Spare1 + 1.ToString().PadLeft(PrintSetting.Spare2, '0');
+            }
+            SqlSugarHelper.DbWMS.Updateable(PrintSetting).ExecuteCommand();
+            return PrintCode;
         }
         
         public Task StopAsync(CancellationToken cancellationToken)
@@ -60,6 +362,19 @@
             return Task.CompletedTask;
         }
 
+        public void PrintInbound(List<Dt_InboundOrder> orders)
+        {
+            var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
+            var Base = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.PrintIPAddress)?.ConfigValue;
+            var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.PrintInboundOrder)?.ConfigValue;
+            if (Base == null || ipAddress == null)
+            {
+                throw new InvalidOperationException("WMS IP 鏈厤缃�");
+            }
+            var IpAddress = Base + ipAddress;
+            var result = HttpHelper.PostAsync(IpAddress, orders.ToJsonString()).Result;
+        }
+
         public void Dispose()
         {
             _timer?.Dispose();

--
Gitblit v1.9.3