修改了background代码!!!!import 修改了库存日志数据库,前端修改,添加库存日志导出功能
This commit is contained in:
@@ -61,209 +61,114 @@ namespace DOAN.ServiceCore
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 功能:检测传感器信号,判断箱子数
|
||||
/// 功能:检测传感器信号,判断箱子数并更新库存及日志
|
||||
/// </summary>
|
||||
/// <param name="stoppingToken"></param>
|
||||
/// <param name="stoppingToken">取消令牌</param>
|
||||
/// <returns></returns>
|
||||
private async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
|
||||
try {
|
||||
//int index = 1;
|
||||
//int indexMax = await DbScoped.SugarScope.CopyNew()
|
||||
// .Queryable<Storagelocation>().CountAsync();
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// 读取PLC I/O状态 12个数组
|
||||
byte[] plcSensorValues = pLCTool.ReadAllValue("VB100", 12);
|
||||
|
||||
//读取PLC I/O状态 12个数组
|
||||
Byte[] getPLCValueByteArray=pLCTool.ReadAllValue("VB100",12);
|
||||
//判断 每个料架每层的箱子数
|
||||
|
||||
//1. 获取所有料架层 合并的箱子 料架层翻倍
|
||||
// 获取所有料架层
|
||||
List<Storagelocation> storagelocationList = await DbScoped.SugarScope.CopyNew().Queryable<Storagelocation>().ToListAsync();
|
||||
|
||||
|
||||
//2. 获取点位表
|
||||
// 获取点位表
|
||||
List<PlcAddressTable> pointPositionList = await DbScoped.SugarScope.CopyNew().Queryable<PlcAddressTable>().ToListAsync();
|
||||
|
||||
List<Storagelocation> updateStoragelocationList=new List<Storagelocation>();
|
||||
List<Inventorylog> inventorylogs = new List<Inventorylog>();
|
||||
List<Storagelocation> updateStoragelocationList = [];
|
||||
List<Inventorylog> inventoryLogs = [];
|
||||
|
||||
if (storagelocationList.Count() > 0) {
|
||||
foreach (Storagelocation layerItem in storagelocationList)
|
||||
{
|
||||
// 获取这个料架层的点位表
|
||||
List<PlcAddressTable> layerPoints = pointPositionList.Where(it => it.FkStorageId == layerItem.Id).ToList();
|
||||
|
||||
foreach (Storagelocation layerItem in storagelocationList)
|
||||
if (layerPoints != null && layerPoints.Count > 0)
|
||||
{
|
||||
//2 获取这个料架层的点位表
|
||||
List<PlcAddressTable> thisLayItemPointPositionList= pointPositionList.Where(it => it.FkStorageId == layerItem.Id).ToList();
|
||||
int currentPackageCount = 1; // 默认最小值为1
|
||||
|
||||
//3 判断这个料架层箱子的个数
|
||||
if(thisLayItemPointPositionList!=null&& thisLayItemPointPositionList.Count()>0)
|
||||
foreach (PlcAddressTable point in layerPoints)
|
||||
{
|
||||
// 这一层箱子数 默认最小值为1
|
||||
int packNum = 1;
|
||||
foreach(PlcAddressTable i in thisLayItemPointPositionList)
|
||||
int row = point.ByteNum - 100;
|
||||
int col = point.BitNum - 1;
|
||||
|
||||
if (plcSensorValues != null)
|
||||
{
|
||||
|
||||
int row = i.ByteNum-100;
|
||||
int col = i.BitNum - 1;
|
||||
|
||||
// Console.WriteLine($"row={row},col={col},PLCValueByteArray[row]={getPLCValueByteArray.Length}");
|
||||
packNum= packNum+( GetInvertedBit(getPLCValueByteArray[row], col) ? 1 : 0);
|
||||
//Console.WriteLine($"row:{row}-col:{col} packNum:{packNum} ");
|
||||
currentPackageCount += GetInvertedBit(plcSensorValues[row], col) ? 1 : 0;
|
||||
}
|
||||
// 记录日志
|
||||
if(packNum> layerItem.PackageNum)
|
||||
{
|
||||
Storagelocation storagelocation = layerItem;
|
||||
|
||||
storagelocation.PackageNum = packNum;
|
||||
storagelocation.UpdatedBy = "补料";
|
||||
storagelocation.UpdatedTime = DateTime.Now;
|
||||
updateStoragelocationList.Add(storagelocation);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Inventorylog inventorylog = new Inventorylog();
|
||||
//入库
|
||||
inventorylog.Id= SnowFlakeSingle.Instance.NextId().ToString();
|
||||
inventorylog.RackCode = storagelocation.RackCode;
|
||||
inventorylog.Operation = 2;
|
||||
inventorylog.PackageNum = packNum- layerItem.PackageNum;
|
||||
inventorylog.CreatedBy = "补料";
|
||||
inventorylog.CreatedTime = DateTime.Now.ToLocalTime();
|
||||
inventorylogs.Add(inventorylog);
|
||||
|
||||
}
|
||||
else if(packNum < layerItem.PackageNum)
|
||||
{
|
||||
|
||||
Storagelocation storagelocation = layerItem;
|
||||
storagelocation.PackageNum = packNum;
|
||||
storagelocation.UpdatedBy = "出料";
|
||||
storagelocation.UpdatedTime = DateTime.Now;
|
||||
updateStoragelocationList.Add(storagelocation);
|
||||
|
||||
//出库
|
||||
Inventorylog inventorylog = new Inventorylog();
|
||||
inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString();
|
||||
inventorylog.RackCode = storagelocation.RackCode;
|
||||
inventorylog.Operation = 1;
|
||||
inventorylog.PackageNum =layerItem.PackageNum - packNum ;
|
||||
inventorylog.CreatedBy = "出料";
|
||||
inventorylog.CreatedTime = DateTime.Now.ToLocalTime();
|
||||
inventorylogs.Add(inventorylog);
|
||||
}
|
||||
|
||||
// 检查箱子数量变化并记录日志
|
||||
if (currentPackageCount > layerItem.PackageNum)
|
||||
{
|
||||
UpdateAndLog(layerItem, currentPackageCount, 2, "补料", inventoryLogs);
|
||||
updateStoragelocationList.Add(layerItem);
|
||||
}
|
||||
else if (currentPackageCount < layerItem.PackageNum)
|
||||
{
|
||||
UpdateAndLog(layerItem, currentPackageCount, 1, "出料", inventoryLogs);
|
||||
updateStoragelocationList.Add(layerItem);
|
||||
}
|
||||
}
|
||||
|
||||
//修正库存
|
||||
if (updateStoragelocationList.Count() > 0)
|
||||
{
|
||||
await DbScoped.SugarScope.CopyNew().Updateable(updateStoragelocationList).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
//增加库存变更日志
|
||||
if(inventorylogs.Count()>0)
|
||||
{
|
||||
await DbScoped.SugarScope.CopyNew().Insertable(inventorylogs).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 更新库存
|
||||
if (updateStoragelocationList.Count > 0)
|
||||
{
|
||||
await DbScoped.SugarScope.CopyNew().Updateable(updateStoragelocationList).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
// 插入库存变更日志
|
||||
if (inventoryLogs.Count > 0)
|
||||
{
|
||||
await DbScoped.SugarScope.CopyNew().Insertable(inventoryLogs).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//List <Storagelocation> storagelocationList = await DbScoped.SugarScope.CopyNew()
|
||||
//.Queryable<Storagelocation>().Where(it => it.Id == index).ToListAsync();
|
||||
//if (storagelocationList.Count > 0)
|
||||
//{
|
||||
|
||||
// foreach (var storagelocation in storagelocationList)
|
||||
// {
|
||||
|
||||
// //遮挡不亮 false
|
||||
// bool result = pLCTool.ReadBit(storagelocation.PlcAddress2);
|
||||
// // 写补料日志
|
||||
// Inventorylog inventorylog = new Inventorylog();
|
||||
// inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString();
|
||||
// inventorylog.RackCode = storagelocation.RackCode;
|
||||
// // 合并货架的id
|
||||
// //int[] ids = {1,2,3,4,19,20,21,22 };
|
||||
// int PackageLine = 2;
|
||||
// //if (ids.Contains(storagelocation.Id))
|
||||
// //{
|
||||
// // PackageLine = 1;
|
||||
// //}
|
||||
// if (result)
|
||||
// {
|
||||
// //缺料,需要补料
|
||||
// // 仓库库存修正
|
||||
// if (storagelocation.PackageNum > PackageLine)
|
||||
// {
|
||||
// storagelocation.PackageNum = PackageLine;
|
||||
// /*inventorylog.Operation = 1;
|
||||
// inventorylog.PackageNum = 1;
|
||||
// inventorylog.CreatedBy = "PLC";
|
||||
// inventorylog.CreatedTime = DateTime.Now.ToLocalTime();*/
|
||||
// await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync();
|
||||
// //await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync();
|
||||
|
||||
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // 仓库库存修正
|
||||
// if (storagelocation.PackageNum <= PackageLine)
|
||||
// {
|
||||
// //不需要补料 补料成功
|
||||
// storagelocation.PackageNum = storagelocation.MaxCapacity;
|
||||
// inventorylog.Operation = 2;
|
||||
// inventorylog.PackageNum = storagelocation.MaxCapacity;
|
||||
// inventorylog.CreatedBy = "PLC";
|
||||
// inventorylog.CreatedTime = DateTime.Now.ToLocalTime();
|
||||
// await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync();
|
||||
// await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync();
|
||||
// }
|
||||
|
||||
// }
|
||||
// //Console.WriteLine("永驻线程,正在读取对射传感器plc值 " + result + "地址:" + storagelocation.PlcAddress2);
|
||||
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//}
|
||||
|
||||
|
||||
|
||||
//// await Task.Delay(500, stoppingToken);
|
||||
|
||||
|
||||
//index++;
|
||||
//if (index > indexMax)
|
||||
//{
|
||||
// index = 1;
|
||||
//}
|
||||
|
||||
// 添加延迟以避免频繁查询
|
||||
await Task.Delay(200, stoppingToken);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine("DoanBackGround线程ExecuteAsync异常:" + ex.Message);
|
||||
}
|
||||
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Console.WriteLine("任务已取消");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"DoanBackGround线程ExecuteAsync异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新料架位置的箱子数量并记录库存日志
|
||||
/// </summary>
|
||||
/// <param name="storageLocation">料架位置对象</param>
|
||||
/// <param name="newPackageCount">新的箱子数量</param>
|
||||
/// <param name="operation">操作类型 (1-出库, 2-入库)</param>
|
||||
/// <param name="operatorName">操作员名称</param>
|
||||
/// <param name="inventoryLogs">库存日志列表</param>
|
||||
private void UpdateAndLog(Storagelocation storageLocation, int newPackageCount, int operation, string operatorName, List<Inventorylog> inventoryLogs)
|
||||
{
|
||||
storageLocation.PackageNum = newPackageCount;
|
||||
storageLocation.UpdatedBy = operatorName;
|
||||
storageLocation.UpdatedTime = DateTime.Now;
|
||||
|
||||
Inventorylog inventoryLog = new Inventorylog
|
||||
{
|
||||
Id = SnowFlakeSingle.Instance.NextId().ToString(),
|
||||
RackCode = storageLocation.RackCode,
|
||||
Partnumber = storageLocation.Partnumber,
|
||||
Operation = operation,
|
||||
PackageNum = Math.Abs((int)(newPackageCount - storageLocation.PackageNum)),
|
||||
CreatedBy = operatorName,
|
||||
CreatedTime = DateTime.Now.ToLocalTime()
|
||||
};
|
||||
|
||||
inventoryLogs.Add(inventoryLog);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user