You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

481 lines
23 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
using Microsoft.IdentityModel.Logging;
using Admin.Core.Socket;
using Admin.Core.Service;
using TouchSocket.Core;
namespace Aucma.Core.BoxFoam.Business
{
public class InStoreBusiness
{
#region 单例实现
private static readonly InStoreBusiness lazy = new InStoreBusiness();
public static InStoreBusiness Instance
{
get
{
return lazy;
}
}
#endregion
/// <summary>
/// 已下传的任务信息
/// </summary>
private List<RealTaskInfo> TaskInfos = new List<RealTaskInfo>();
private Dictionary<string, int> KeyValuePairs = new Dictionary<string, int>();
#region 事件
/// <summary>
/// 扫码信息刷新
/// </summary>
/// <param name="materialBarCode">条码</param>
/// <param name="materialCode">编码</param>
/// <param name="materialName">名称</param>
/// <param name="spaceName">货到名称</param>
public delegate Task RefreshDataGridDelegate();
public static event RefreshDataGridDelegate RefreshDataGridDelegateEvent;
/// <summary>
/// 扫码信息刷新
/// </summary>
/// <param name="materialBarCode">条码</param>
/// <param name="materialCode">编码</param>
/// <param name="materialName">名称</param>
/// <param name="spaceName">货到名称</param>
public delegate void RefreshScanMateriaCode(string materialBarCode, string materialCode, string materialName, string spaceName, string msg);
public static event RefreshScanMateriaCode RefreshScanMateriaCodeEvent;
/// <summary>
/// 入库日志事件
/// </summary>
/// <param name="msg"></param>
public delegate void LogDelegate(string msg, string color);
public static event LogDelegate LogDelegateEvent;
/// <summary>
/// 库存信息
/// </summary>
/// <param name="msg"></param>
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<IBaseSpaceInfoServices>();
private readonly IRealTaskInfoServices? _realTaskInfoService = App.ServiceProvider.GetService<IRealTaskInfoServices>();
private readonly IProductPlanInfoServices? _productPlanInfoServices = App.ServiceProvider.GetService<IProductPlanInfoServices>();
private readonly IRecordInStoreServices? _recordInstoreServices = App.ServiceProvider.GetService<IRecordInStoreServices>();
private readonly IBaseStoreInfoServices? _baseStoreInfoServices = App.ServiceProvider.GetService<IBaseStoreInfoServices>();
private readonly IBaseSpaceDetailServices? _baseSpaceDetailServices = App.ServiceProvider.GetService<IBaseSpaceDetailServices>();
// 过点数据表物料完成记录MaterialCompletion
private readonly IMaterialCompletionServices? _iMaterialCompletionServices = App.ServiceProvider.GetService<IMaterialCompletionServices>();
private readonly IPrintBarCodeServices? _printBarCodeServices = App.ServiceProvider.GetService<IPrintBarCodeServices>();
private readonly IBaseMaterialInfoServices? _baseMaterialInfoServices = App.ServiceProvider.GetService<IBaseMaterialInfoServices>();
#endregion
#region 初始化对象
private PlcSpaceConfig spaceConfig = PlcSpaceConfig.Instance;
private SemaphoreSlim semaphore = new SemaphoreSlim(0);
private List<BaseSpaceInfo> allSpaces = null;
private List<SpaceAddress> spaceAddresses = new List<SpaceAddress>();
private SemaphoreSlim _lock = new SemaphoreSlim(1);
#endregion
#region 构造函数
public InStoreBusiness()
{
TouchSocketService.ReceiveCodeDelegateEvent += NewTask;
storeCode = Appsettings.app("StoreInfo", "BeforeStoreCode");//泡前库code
#region 更新库数据
Task.Run(async () =>
{
await RealTimeUpdateStore();
});
#endregion
}
#endregion
#region 根据条码入库
/// <summary>
/// 根据条码入库——泡前库始终只有一条任务在
/// </summary>
/// <param name="materialBarCode"></param>
/// <returns></returns>
public async void NewTask(string IP,string materialBarCode)
{
if (string.IsNullOrEmpty(materialBarCode))
{
LogDelegateEvent?.Invoke("未扫到二维码信息!","Red");
Console.WriteLine($"【{DateTime.Now}】未扫到二维码信息,请重新扫码!");
return;
}
if (materialBarCode.Contains("h")|| materialBarCode.Contains("e")|| materialBarCode.Contains("a")|| materialBarCode.Contains("r")||
materialBarCode.Contains("t") || materialBarCode.Contains("b") || materialBarCode.Contains("e") || materialBarCode.Contains("a"))
{
return ;
}
bool flag = true;
//创建入库任务
Console.WriteLine($"【{DateTime.Now}】扫码成功,物料条码:{materialBarCode}");
logHelper.Info($"扫码成功,物料条码:{materialBarCode}");
LogDelegateEvent?.Invoke($"扫码成功,物料条码:{materialBarCode}", "White");
#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}", "White");
bool result= obj.plc.WriteString("DB200.132", materialCode);//写入物料号
if (result)
{
int space = 0;
do
{
space = obj.plc.ReadInt16("DB200.438");//读取货道号大约1.5s 可以获取
} while (space==0);
Console.WriteLine($"物料MES码{materialBarCode}》入库货道:{space}");
logHelper.Info($"物料MES码{materialBarCode}》入库货道:{space}");
LogDelegateEvent?.Invoke($"物料MES码{materialBarCode}》物料码:{materialCode}》入库货道:{space}", "White");
//更新货道入库
await EnterStore(materialBarCode, space);
//更新过点数
await UpdateComplete(materialBarCode);
Console.WriteLine($"更新物料信息成功!");
RefreshDataGridDelegateEvent?.Invoke();
}
else
{
Console.WriteLine($"向PLC写入物料编码失败");
}
}
catch (Exception ex)
{
logHelper.Error($"入库业务异常:{ex}");
LogDelegateEvent?.Invoke($"入库业务异常:{ex}", "Red");
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"));
var foamLine1 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine1Plc"));//发泡线1
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");
if (foamLine1 != null)
{
if (foamLine1.plc.IsConnected)
{
item.MaterialType = foamLine1.plc.ReadString("DB400.16.0").Replace("\n","");
if (!string.IsNullOrEmpty(item.MaterialType))
{
var printBarCode001 = _printBarCodeServices.FirstAsync(d => d.MaterialCode == item.MaterialType).Result;
if (printBarCode001 != null)
{
item.typeNameA = printBarCode001.MaterialName;
}
}
}
}
break;
case "PB01_002":
item.SpaceStock = obj.plc.ReadInt16("DB200.442");
item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.90");
if (foamLine1 != null)
{
if (foamLine1.plc.IsConnected)
{
item.MaterialType = foamLine1.plc.ReadString("DB400.28.0").Replace("\n", "");
if (!string.IsNullOrEmpty(item.MaterialType))
{
var printBarCode002 = _printBarCodeServices.FirstAsync(d => d.MaterialCode == item.MaterialType).Result;
if (printBarCode002 != null)
{
item.typeNameA = printBarCode002.MaterialName;
}
}
}
}
break;
case "PB01_003":
item.SpaceStock = obj.plc.ReadInt16("DB200.444");
item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.92");
if (foamLine1 != null)
{
if (foamLine1.plc.IsConnected)
{
item.MaterialType = foamLine1.plc.ReadString("DB400.40.0").Replace("\n", "");
if (!string.IsNullOrEmpty(item.MaterialType))
{
var printBarCode003 = _printBarCodeServices.FirstAsync(d => d.MaterialCode == item.MaterialType).Result;
if (printBarCode003 != null)
{
item.typeNameA = printBarCode003.MaterialName;
}
}
}
}
break;
case "PB01_004":
item.SpaceStock = obj.plc.ReadInt16("DB200.446");
item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.94");
if (foamLine1 != null)
{
if (foamLine1.plc.IsConnected)
{
item.MaterialType = foamLine1.plc.ReadString("DB400.52.0").Replace("\n", "");
if (!string.IsNullOrEmpty(item.MaterialType))
{
var printBarCode004 = _printBarCodeServices.FirstAsync(d => d.MaterialCode == item.MaterialType).Result;
if (printBarCode004 != null)
{
item.typeNameA = printBarCode004.MaterialName;
}
}
}
}
break;
case "PB01_005":
item.SpaceStock = obj.plc.ReadInt16("DB200.448");
item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.96");
if (foamLine1 != null)
{
if (foamLine1.plc.IsConnected)
{
item.MaterialType = foamLine1.plc.ReadString("DB400.64.0").Replace("\n", "");
if (!string.IsNullOrEmpty(item.MaterialType))
{
var printBarCode005 = _printBarCodeServices.FirstAsync(d => d.MaterialCode == item.MaterialType).Result;
if (printBarCode005 != null)
{
item.typeNameA = printBarCode005.MaterialName;
}
}
}
}
break;
case "PB01_006":
item.SpaceStock = obj.plc.ReadInt16("DB200.450");
item.OnRouteAmount = item.SpaceStock - obj.plc.ReadInt16("DB200.98");
if (foamLine1 != null)
{
if (foamLine1.plc.IsConnected)
{
item.MaterialType = foamLine1.plc.ReadString("DB400.76.0").Replace("\n", "");
if (!string.IsNullOrEmpty(item.MaterialType))
{
var printBarCode006 = _printBarCodeServices.FirstAsync(d => d.MaterialCode == item.MaterialType).Result;
if (printBarCode006 != null)
{
item.typeNameA = printBarCode006.MaterialName;
}
}
}
}
break;
default:
break;
}
item.UpdatedTime = DateTime.Now;
bool r= await _baseSpaceInfoServices.UpdateAsync(item);
}
//刷新库存信息
RefreshPageDelegateEvent?.Invoke();
}
}
}
Thread.Sleep(3000);
}
catch (Exception ex)
{
Console.WriteLine($"更新泡前库库库存,类型异常:{ex.Message}");
}
finally { flag = true; }
}
}
#endregion
#region 更新过点数
/// <summary>
/// 更新过点数
/// </summary>
/// <param name="materialBarCode"></param>
/// <returns></returns>
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,int space)
{
//有获取不到情况,需要一直获取
string spaceCode = string.Empty;
switch (space)
{
case 1:
spaceCode = "PB01_001";
break;
case 2:
spaceCode = "PB01_002";
break;
case 3:
spaceCode = "PB01_003";
break;
case 4:
spaceCode = "PB01_004";
break;
case 5:
spaceCode = "PB01_005";
break;
case 6:
spaceCode = "PB01_006";
break;
default:
break;
}
//根据条码获取物料详情
var material = _printBarCodeServices.FirstAsync(d => d.MaterialBarcode == barCode).Result;
RecordInStore recordInstore = new RecordInStore();
recordInstore.StoreCode = storeCode;
recordInstore.StoreArea = "";
recordInstore.SpaceCode = 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, spaceCode, message); //刷新界面扫码信息
LogDelegateEvent?.Invoke($"物料[{material.MaterialName}], 入库成功!","White");
RefreshDataGridDelegateEvent?.Invoke();
}
else
{
LogDelegateEvent?.Invoke($"物料[{material.MaterialName}], 入库失败,任务回滚!", "Red");
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
}
}