using Admin.Core.Common.Config; using Admin.Core.Common; using Admin.Core.IService; using Admin.Core.Model; using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using StackExchange.Profiling.Internal; using Aucma.Core.HwPLc; using Org.BouncyCastle.Asn1.Tsp; using NetTaste; using System.Windows.Media.Media3D; using System.Windows.Forms; using Admin.Core.Model.Model_New; using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData; namespace Aucma.Core.BoxFoam.Business { public class InStoreBusiness { #region 单例实现 private static readonly InStoreBusiness lazy = new InStoreBusiness(); public static InStoreBusiness Instance { get { return lazy; } } #endregion /// /// 已下传的任务信息 /// private List TaskInfos = new List(); private Dictionary KeyValuePairs = new Dictionary(); #region 事件 /// /// 扫码信息刷新 /// /// 条码 /// 编码 /// 名称 /// 货到名称 public delegate Task RefreshDataGridDelegate(); public static event RefreshDataGridDelegate RefreshDataGridDelegateEvent; /// /// 扫码信息刷新 /// /// 条码 /// 编码 /// 名称 /// 货到名称 public delegate void RefreshScanMateriaCode(string materialBarCode, string materialCode, string materialName, string spaceName, string msg); public static event RefreshScanMateriaCode RefreshScanMateriaCodeEvent; /// /// 初始化入库任务 /// /// public delegate void RefreshInStoreTask(RealTaskInfo taskInfos); public static event RefreshInStoreTask RefreshInStoreTaskEvent; /// /// 日志信息刷新 /// /// public delegate void RefreshLogMessage(string message); public static event RefreshLogMessage RefreshLogMessageEvent; /// /// 入库日志事件 /// /// public delegate void LogDelegate(string msg); public static event LogDelegate LogDelegateEvent; /// /// 库存信息 /// /// public delegate void RefreshPageDelegate(); public static event RefreshPageDelegate RefreshPageDelegateEvent; #endregion private string storeCode = string.Empty; private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(InStoreBusiness)); #region 初始化 private readonly IBaseSpaceInfoServices? _baseSpaceInfoServices = App.ServiceProvider.GetService(); private readonly IRealTaskInfoServices? _realTaskInfoService = App.ServiceProvider.GetService(); private readonly IProductPlanInfoServices? _productPlanInfoServices = App.ServiceProvider.GetService(); private readonly IRecordInStoreServices? _recordInstoreServices = App.ServiceProvider.GetService(); private readonly IBaseStoreInfoServices? _baseStoreInfoServices = App.ServiceProvider.GetService(); private readonly IBaseSpaceDetailServices? _baseSpaceDetailServices = App.ServiceProvider.GetService(); // 过点数据表,物料完成记录MaterialCompletion private readonly IMaterialCompletionServices? _iMaterialCompletionServices = App.ServiceProvider.GetService(); private readonly IPrintBarCodeServices? _printBarCodeServices = App.ServiceProvider.GetService(); private readonly IBaseMaterialInfoServices? _baseMaterialInfoServices = App.ServiceProvider.GetService(); #endregion #region 初始化对象 private PlcSpaceConfig spaceConfig = PlcSpaceConfig.Instance; private SemaphoreSlim semaphore = new SemaphoreSlim(0); private List allSpaces = null; private List spaceAddresses = new List(); private SemaphoreSlim _lock = new SemaphoreSlim(1); #endregion #region 构造函数 public InStoreBusiness() { MvCodeHelper.PQKReceiveCodeEvent += NewTask; storeCode = Appsettings.app("StoreInfo", "BeforeStoreCode");//泡前库code #region 更新库数据 Task.Run(async () => { await RealTimeUpdateStore(); }); #endregion } #endregion #region 根据条码入库 /// /// 根据条码入库——泡前库始终只有一条任务在 /// /// /// public async Task NewTask(string materialBarCode) { if (string.IsNullOrEmpty(materialBarCode)) { LogDelegateEvent?.Invoke("未扫到二维码信息!"); Console.WriteLine($"【{DateTime.Now}】未扫到二维码信息,请重新扫码!"); return; } bool flag = true; //创建入库任务 Console.WriteLine($"【{DateTime.Now}】扫码成功,物料条码:{materialBarCode}"); logHelper.Info($"扫码成功,物料条码:{materialBarCode}"); LogDelegateEvent?.Invoke($"扫码成功,物料条码:{materialBarCode}"); #region 执行任务 string materialCode = SubString(materialBarCode); var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("PqPlc")); if (obj != null) { if (obj.plc.IsConnected) { if (flag) { flag = false; try { LogDelegateEvent?.Invoke($"获取物料编码:{materialCode}"); Console.WriteLine($"获取物料编码:{materialCode}"); bool r= obj.plc.WriteString("DB200.132", materialCode);//写入物料号 Console.WriteLine($"写入物料编码:{r}"); if (r) { //更新货道入库 await EnterStore(materialBarCode); //更新过点数 await UpdateComplete(materialBarCode); Console.WriteLine($"更新物料信息成功!"); } else { Console.WriteLine($"向PLC写入物料编码失败!"); } } catch (Exception ex) { logHelper.Error($"入库业务异常:{ex}"); LogDelegateEvent?.Invoke($"入库业务异常:{ex}"); Console.WriteLine($"入库业务异常:{ex.Message}"); } finally { flag = true; } } } } else { Console.WriteLine("PLC连接中..."); Thread.Sleep(3000); } #endregion await Task.CompletedTask; } #endregion #region 更新库数 public async Task RealTimeUpdateStore() { bool flag = true; while (true) { try { if (flag) { flag = false; var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("PqPlc")); if (obj != null) { if (obj.plc.IsConnected) { var storeList = await _baseSpaceInfoServices.QueryAsync(d => d.StoreCode == storeCode); foreach (var item in storeList) { switch (item.SpaceCode) { case "PB01_001": item.SpaceStock= obj.plc.ReadInt16("DB200.440"); item.OnRouteAmount = item.SpaceStock-obj.plc.ReadInt16("DB200.88"); break; case "PB01_002": item.SpaceStock = obj.plc.ReadInt16("DB200.442"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.90"); break; case "PB01_003": item.SpaceStock = obj.plc.ReadInt16("DB200.444"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.92"); break; case "PB01_004": item.SpaceStock = obj.plc.ReadInt16("DB200.446"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.94"); break; case "PB01_005": item.SpaceStock = obj.plc.ReadInt16("DB200.448"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.96"); break; case "PB01_006": item.SpaceStock = obj.plc.ReadInt16("DB200.450"); item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.98"); break; default: break; } item.UpdatedTime = DateTime.Now; bool r= await _baseSpaceInfoServices.UpdateAsync(item); if (r) { Console.WriteLine("更新库存信息成功!"); } } //刷新库存信息 RefreshPageDelegateEvent?.Invoke(); } } } Thread.Sleep(1000); } catch (Exception ex) { Console.WriteLine($"异常:{ex.Message}"); } finally { flag = true; } } } #endregion #region 更新过点数 /// /// 更新过点数 /// /// /// public async Task UpdateComplete(string materialBarCode) { #region 更新过点数据 PrintBarCode print = await _printBarCodeServices.FirstAsync(x => x.MaterialBarcode == materialBarCode); MaterialCompletion completion = new MaterialCompletion(); completion.OrderCode = print.OrderCode; completion.MaterialBarcode = materialBarCode; completion.MaterialCode = print.MaterialCode; completion.MaterialName = print.MaterialName; completion.StationName = "1004"; completion.ProductLineCode = "CX_02"; completion.isDownLine = 0; completion.CompleteDate = DateTime.Now; int i = await _iMaterialCompletionServices.AddAsync(completion); Console.WriteLine($"过点数更新结果为{i > 0}"); #endregion } #endregion #region 任务执行入库 public async Task EnterStore(string barCode) { //根据条码获取物料详情 var material = _printBarCodeServices.FirstAsync(d => d.MaterialBarcode == barCode).Result; RecordInStore recordInstore = new RecordInStore(); recordInstore.StoreCode = storeCode; recordInstore.StoreArea = ""; recordInstore.SpaceCode = ""; string materialType = SubString(barCode);//截取中间物料条码 recordInstore.MaterialType = materialType; recordInstore.MaterialCode = barCode; recordInstore.InStoreTime = DateTime.Now; recordInstore.BarCodeCode = barCode; recordInstore.MaterialName = material.MaterialName; recordInstore.InStoreAmount =1; recordInstore.EntryPattern = 0; recordInstore.IsFlag = 0; recordInstore.CreatedTime = DateTime.Now; int result = await _recordInstoreServices.AddAsync(recordInstore); if (result > 0) { string message = $"物料[{material.MaterialName}],入库成功!"; RefreshScanMateriaCodeEvent?.Invoke(material.MaterialCode, barCode, material.MaterialName, "", message); //刷新界面扫码信息 LogDelegateEvent?.Invoke($"物料[{material.MaterialName}], 入库成功!"); RefreshDataGridDelegateEvent?.Invoke(); } else { LogDelegateEvent?.Invoke($"物料[{material.MaterialName}], 入库失败,任务回滚!"); logHelper.Error($"物料[{material.MaterialName}], 入库失败,任务回滚!"); } } #endregion #region 截取物料编码 public string SubString(string barCode) { string result = string.Empty; if (!string.IsNullOrEmpty(barCode)) { result = barCode.Substring(7, 10); } return result; } #endregion } }