1
huangxiaoqiang
10 天以前 dda095e27b535d5748340f127c5f83c0d1324226
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/AspNetCoreSchedule.cs
@@ -1,17 +1,21 @@
using MailKit.Search;
using AngleSharp.Dom;
using MailKit.Search;
using Masuit.Tools;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
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;
@@ -24,30 +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,IDt_InboundOrderRepository inboundOrderRepository)
        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(1));
            _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
            return Task.CompletedTask;
        }
@@ -55,22 +64,21 @@
        {
            try
            {
                List<WMS_采购入成品入库自动打印清单_ST> ERPPrintChecklist =SqlSugarHelper.DBERP.Queryable<WMS_采购入成品入库自动打印清单_ST>().Where(x => true).ToList();
                #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.入库仓库名称,
@@ -80,6 +88,7 @@
                            MaterialNo = item.料号,
                            MaterialName = item.品名,
                            Weight = item.单重,
                            WareHouseId = item.存储地点ID,
                            Specs = item.规格,
                            Unit = item.单位,
                            Texture = item.用友材质,
@@ -91,10 +100,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.入库仓库名称;
@@ -113,14 +122,211 @@
                        }
                    }
                }
                #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.备料单ID,
                                        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.备料单ID,
                                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.备料单ID,
                                        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.备料单ID,
                                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.备料单ID,
                                        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.备料单ID,
                                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)
            {
@@ -128,10 +334,10 @@
            }
        }
        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 +348,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;
@@ -155,6 +361,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();