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.Net; using System.Net.Mail; using System.Text; using WIDESEA_Cache; using WIDESEA_Core.Const; using WIDESEA_DTO.WMS; using WIDESEA_IOrderRepository; using WIDESEA_IServices; using WIDESEA_Model.Models.ERP; using WIDESEA_Model.Models.Order; using WIDESEA_OrderRepository; using WIDESEAWCS_BasicInfoRepository; namespace WIDESEA_StorageTaskServices { public class MyBackgroundService : IHostedService, IDisposable { private readonly ILogger _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 Timer _timer; public MyBackgroundService(ILogger 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; _stockInfoRepository = stockInfoRepository; _areaInfoRepository = areaInfoRepository; _taskRepository = taskRepository; _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(5)); return Task.CompletedTask; } private void DoWork(object state) { try { #region 入库单据同步 List ERPPrintChecklist =SqlSugarHelper.DBERP.Queryable().Where(x => true).ToList(); var printCheckList = _inboundOrderRepository.QueryData(x => true); List inboundOrderAdd = new List(); List inboundOrderUpdate = new List(); 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.单重, 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.单据状态; inboundOrderUpdate.Add(x); } } } if (inboundOrderAdd.Count > 0) { _inboundOrderRepository.AddData(inboundOrderAdd); } if (inboundOrderUpdate.Count > 0) { _inboundOrderRepository.UpdateData(inboundOrderUpdate); } #endregion #region ERP生产领料单同步 var productionOutOrder = _productionoutboundorderRepository.Db.Queryable().Includes(x => x.Details).ToList(); List productionoutboundOrderAdd = new List(); List EngineeringERP = SqlSugarHelper.DBERP.Queryable().Where(x => true).ToList(); List productERP = SqlSugarHelper.DBERP.Queryable().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() { 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() { 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().Includes(x => x.Details).ToList(); List allocateoutboundorderAdd = new List(); List allocateoutboundorderUpdate = new List(); List ERPallocateOrder = SqlSugarHelper.DBERP.Queryable().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() { 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(string printCode) { string PrintCode = ""; var PrintSetting = SqlSugarHelper.DbWMS.Queryable().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) { _logger.LogInformation("MyBackgroundService is stopping."); _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { _timer?.Dispose(); } } }