using DOAN.Infrastructure.PLC;
using DOAN.Model.PBL;
using DOAN.Model.PBL.Dto;
using DOAN.Service.PBL.IService;
using DOAN.ServiceCore.Signalr;
using Infrastructure.Attribute;
using Mapster;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json.Linq;
using System;
using System.Security.Cryptography.X509Certificates;
using static System.Formats.Asn1.AsnWriter;
namespace DOAN.Service.PBL
{
///
/// 料架表Service业务层处理
///
[AppService(ServiceType = typeof(IMESInteractionServcie), ServiceLifetime = LifeTime.Transient)]
public class MESInteractionServcie : BaseService, IMESInteractionServcie
{
private readonly IHubContext notificationHubContext;
public MESInteractionServcie(IHubContext _notificationHubContext)
{
notificationHubContext = _notificationHubContext;
}
public bool TestPLc(string address, PLCTool pLCTool)
{
bool isSucesss = pLCTool.ReadBit(address);
return isSucesss;
}
///
/// MES亮灯
///
///
///
///
public bool MESLightUp(LightUpDto light, PLCTool pLCTool)
{
try
{
Context.Ado.BeginTran();
int result = 0;
// 1.记录MES交互记录
MES_Interation_Log item = light.Adapt();
item.Id = XUEHUA;
item.CreatedTime = DateTime.Now;
Context.Insertable(item).ExecuteCommand();
// 2.根据总成零件号 ,版本 查询对应零件号,使得对应料架亮灯
List MirrorshellShelfList = Context
.Queryable()
.Where(it =>
it.Partnumber
== SqlFunc
.Subqueryable()
.Where(It => It.Productcode == light.AssemblyPartNumber)
.Select(it => it.MirrorshellCode)
)
.ToList();
if (MirrorshellShelfList != null && MirrorshellShelfList.Count() > 0)
{
Storagelocation storagelocation = new();
// 是否合并料架
bool isMergeRack = MirrorshellShelfList.Count > 1;
if (isMergeRack)
{
Storagelocation leftShelf = MirrorshellShelfList[0];
Storagelocation rightShelf = MirrorshellShelfList[1];
// 料架今天是否有补料信息 (补料完成信号判定)
var today = DateTime.Today;
bool hasAlarm = Context.Queryable()
.Where(it => it.ActionTime != null &&
it.ActionTime >= today &&
it.ActionTime < today.AddDays(1))
.Where(it => it.StoragelocationId == leftShelf.Id || it.StoragelocationId == rightShelf.Id)
.OrderByDescending(it => it.ActionTime)
.Any();
int packageTotal = leftShelf.PackageNum.Value + rightShelf.PackageNum.Value;
//1. 在今日有补料信号的前提下 料架>6箱(已补料,未消耗最新箱,先消耗旧箱,哪边存在报警补哪边)
if (packageTotal == 8 && hasAlarm)
{
if (leftShelf.IsLackAlarm == 1)
{
storagelocation = leftShelf;
}
else if (rightShelf.IsLackAlarm == 1)
{
storagelocation = rightShelf;
}
else
{
storagelocation = rightShelf;
}
}
else if (packageTotal == 7 && hasAlarm)
{
if (leftShelf.IsLackAlarm == 1)
{
storagelocation = leftShelf;
}
else if (rightShelf.IsLackAlarm == 1)
{
storagelocation = rightShelf;
}
else
{
storagelocation = rightShelf;
}
// 切换报警
DoChangeAlarmStatus(leftShelf, rightShelf);
}
else if (packageTotal <= 6 && packageTotal > 4 && hasAlarm)
{
if (leftShelf.IsLackAlarm == 1)
{
storagelocation = rightShelf;
}
else if (rightShelf.IsLackAlarm == 1)
{
storagelocation = leftShelf;
}
else
{
storagelocation = rightShelf;
}
}
else
{
// 默认情况
// 合并料架 判断 20250224 左先出原则
foreach (var shelf in MirrorshellShelfList)
{
// 第一个有箱子的料架
if (shelf.PackageNum > 0)
{
storagelocation = shelf;
break;
}
}
if (storagelocation.RackCode == null)
{
storagelocation = MirrorshellShelfList[^1];
}
}
}
else
{
// 单独料架
storagelocation = MirrorshellShelfList[0];
}
// 3.对应料架亮灯
bool isSucesss = pLCTool.WriteBit(storagelocation.PlcAddress, true);
if (isSucesss)
{
storagelocation.IsLight = 1;
result += Context.Updateable(storagelocation).ExecuteCommand();
}
//亮灯日志
Light_Log light_Log = new Light_Log();
light_Log.Id = XUEHUA;
light_Log.LightOperation = 1;
light_Log.Operationer = "PBL";
light_Log.CreatedTime = DateTime.Now;
light_Log.ShelfCode = storagelocation.RackCode;
light_Log.LayerNum = storagelocation.LayerNum;
light_Log.IsSuccess = isSucesss;
result += Context.Insertable(light_Log).ExecuteCommand();
}
else
{
// 发送socket 通知
string message =
$"MES产品编号{light.AssemblyPartNumber}或者版本{light.Version},在PBL中找不到。请维护PBL料架信息--{DateTime.Now}";
notificationHubContext.Clients.All.SendAsync("PBL_bom_except", message);
Context.Ado.RollbackTran();
return false;
}
notificationHubContext.Clients.All.SendAsync("PBL_storagelocation_change");
Context.Ado.CommitTran();
return result > 0;
}
catch (Exception)
{
Context.Ado.RollbackTran();
return false;
}
}
///
/// MES灭灯
///
///
///
public bool MESLightDown(string scan_code, PLCTool pLCTool)
{
int result = 0;
// 1.记录MES交互记录
MES_Interation_Log item = new MES_Interation_Log();
item.Id = XUEHUA;
item.ScanCode = scan_code;
item.CreatedTime = DateTime.Now;
Context.Insertable(item).ExecuteCommand();
//2 找到对应的亮着的料架 进行灭灯
Storagelocation storagelocation = Context
.Queryable()
.Where(it => it.Partnumber.Contains(scan_code))
.Where(it => it.IsLight == 1)
.First();
if (storagelocation == null)
{
// 发送socket 通知
string message =
$"未找到{scan_code}对应的亮灯料架。请检查信息--{DateTime.Now}";
notificationHubContext.Clients.All.SendAsync("PBL_bom_except", message);
return false;
}
// TODO PLC 交互
bool isSuccess = pLCTool.WriteBit(storagelocation.PlcAddress, false);
if (isSuccess)
{
storagelocation.IsLight = 0;
result += Context.Updateable(storagelocation).ExecuteCommand();
}
//灭灯日志
Light_Log light_Log = new Light_Log
{
Id = XUEHUA,
LightOperation = 2,
LayerNum = storagelocation.LayerNum,
ShelfCode = storagelocation.RackCode,
IsSuccess = isSuccess,
Operationer = "PBL",
CreatedTime = DateTime.Now
};
result += Context.Insertable(light_Log).ExecuteCommand();
notificationHubContext.Clients.All.SendAsync("PBL_storagelocation_change");
return result > 0;
}
///
/// 交换合并料架报警配置
///
///
///
///
public int DoChangeAlarmStatus(Storagelocation leftShelf, Storagelocation rightShelf)
{
int result = 0;
// 都判断完后切换报警
leftShelf.IsLackAlarm = leftShelf.IsLackAlarm == 1 ? 0 : 1;
leftShelf.AlarmNum = leftShelf.IsLackAlarm == 1 ? rightShelf.AlarmNum : 0;
result += Context.Updateable(leftShelf).ExecuteCommand();
rightShelf.IsLackAlarm = rightShelf.IsLackAlarm == 1 ? 0 : 1;
rightShelf.AlarmNum = rightShelf.IsLackAlarm == 1 ? leftShelf.AlarmNum : 0;
result += Context.Updateable(rightShelf).ExecuteCommand();
return result;
}
}
}