using Admin.NET.Application;
|
using iWareCC.Common.Helper;
|
using iWareCC.StationService;
|
using iWareCommon.Common.Globle;
|
using iWareCommon.Utils;
|
using iWareModel;
|
using iWareSql.DBModel;
|
using iWareSql.WmsDBModel;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net.NetworkInformation;
|
using System.Reflection.Emit;
|
using System.ServiceModel.Configuration;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using XiGang.Core.Model;
|
|
namespace iWareCC.ThreadService
|
{
|
/// <summary>
|
/// BZ01工位(清灰,洗板后工位)
|
/// </summary>
|
public static class DataProcess_BZ01
|
{
|
/// <summary>
|
/// BZ01工位(清灰,洗板后工位)
|
/// </summary>
|
public static async void Handler()
|
{
|
var alertMsg = "";
|
LogType logType = LogType.DataProcess_BZ01;
|
string lastUpi = "";//最后记录的UPI数据
|
while (true)
|
{
|
Thread.Sleep(1000);//休眠2秒,将休眠写到前面,是为了下面的continue方法执行后不显示错误信息的提示!!!【EditBy shaocx,2022-05-24】
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = string.Empty;
|
try
|
{
|
if (SystemValue.isStartedModel)//SystemValue.isAllowRuning_DataProcess_BZ01 &&
|
{
|
if (FormCC.IsAllowRunOutMode == true)
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"正在干出库模式,此方法暂停。。 ";
|
continue;
|
}
|
|
var rgvLocation = StationLocationEnum.BZ01.ToString();
|
/*
|
* 1、从数据库中判断是否齐套
|
* 2、根据齐套结果,推送给PLC
|
*/
|
if (FormCC.stationView == null)
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()}-获取PLC对象为null";
|
continue;
|
}
|
var obj = FormCC.stationView.R_StationForReadCommList.Where(x => x.StationCode == rgvLocation.ToString()).FirstOrDefault();
|
if (obj.R_ReqParseData)
|
{
|
var result = obj.R_ReadCodeResult;
|
if (string.IsNullOrEmpty(result))
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()}-请求了解码,但是结果是空的,此次循环结束";
|
continue;
|
}
|
if (lastUpi == result)
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()}-重复的UPI{result}";
|
continue;
|
}
|
var upiCode = result;
|
lastUpi = upiCode;
|
|
var qitaoReault = false;//齐包结果
|
using (WmsDBModel wmsDB = new WmsDBModel())
|
{
|
var upiObj = wmsDB.mes_batchOrderUPI_new.Where(x => x.UPI == upiCode).FirstOrDefault();
|
if (upiObj == null)
|
{
|
//写入报警信息
|
await MyExtendHelper.WriteBoolPlcDataForWarning(PlcWarningAddressGloble.Num_BZ01_1);
|
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()},根据UPI:{upiCode}没有找到对象 ";
|
continue;
|
}
|
|
var isAllow = IsAllloRunOutTaskMode(wmsDB, upiObj);
|
if (isAllow)
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()},判断要执行出库模式了,此方法暂停。。 ";
|
continue;
|
}
|
|
//增加对参数配置的管理 【Editby shaocx,2024-12-13】
|
var sysConfig = wmsDB.SysConfig.Where(x => x.Code == "LastUpi_BZ01").FirstOrDefault();
|
if (sysConfig == null)
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"没有获取到 LastUpi_BZ01的配置信息 ";
|
continue;
|
}
|
var config_value = sysConfig.Value;
|
if (config_value.IndexOf('|') > -1)
|
{//格式为 包号|UPI|次序|齐包状态
|
var arr = config_value.Split('|');
|
var lastPackageCode = arr[0];
|
var lastUpi_BZ01 = arr[1];
|
var lastShelf = Convert.ToInt32(arr[2]);
|
var lastQitaoReault = arr[3];
|
|
if (upiObj.PackageCode == lastPackageCode)
|
{
|
if (upiObj.Shelf == (lastShelf + 1))
|
{
|
//齐包状态
|
if (lastQitaoReault == "齐包")
|
{
|
qitaoReault = true;
|
}
|
else
|
{
|
qitaoReault = false;
|
}
|
}
|
else
|
{//不齐包
|
qitaoReault = false;
|
}
|
}
|
else
|
{//说明是换了包号,重新验证 齐包状态
|
#region 计算是否齐包
|
qitaoReault = ValidateQiBao(wmsDB, upiObj);
|
#endregion
|
}
|
}
|
else
|
{//表示还没有配置 ,重新验证 齐包状态
|
#region 计算是否齐包
|
qitaoReault = ValidateQiBao(wmsDB, upiObj);
|
#endregion
|
}
|
|
if (qitaoReault == false)
|
{
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
}
|
else
|
{
|
upiObj.AreaCode = (int)AreaCodeEnum.码垛区域;
|
}
|
|
//查询是否齐包
|
var allList = wmsDB.mes_batchOrderUPI_new.Where(x => x.PackageCode == upiObj.PackageCode).ToList();
|
var isQiTaoList = allList.Where(x => x.AreaCode == (int)AreaCodeEnum.待缓存分拣区
|
||
|
x.AreaCode == (int)AreaCodeEnum.缓存分拣区
|
||
|
x.AreaCode == (int)AreaCodeEnum.码垛区域
|
).ToList();
|
if (isQiTaoList.Count() != allList.Count())
|
{//不齐包
|
qitaoReault = false;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
}
|
else
|
{//齐包
|
//这里再增加个逻辑,必须是按照次序进入的才真正的算齐包 [Editby shaocx,2024-12-06]
|
//寻找本包内的前一块板的位置
|
int queryAreaCode = (int)AreaCodeEnum.待缓存分拣区;
|
var lastUpiLineQueue = wmsDB.mes_upi_linequeue.Where(x => x.PackageCode == upiObj.PackageCode && x.AreaCode == queryAreaCode)
|
.OrderBy(x => x.Id).FirstOrDefault();//查询该包队列的第一块板
|
if (lastUpiLineQueue == null)
|
{//说明 队列中没有该包的数据
|
qitaoReault = false;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
}
|
else
|
{
|
if (lastUpiLineQueue.Shelf != upiObj.Shelf)
|
{
|
qitaoReault = false;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
}
|
else
|
{
|
var isJiXuValidate = true;
|
if (upiObj.Shelf != 1)
|
{
|
var qianmianList = allList.Where(x => x.Shelf < upiObj.Shelf).ToList();
|
//看以前的板在哪里,必须都在 码垛区域
|
var yiqianQuyu = AreaCodeEnum.码垛区域;
|
var count_MD = qianmianList.Where(x => x.AreaCode == (int)yiqianQuyu).Count();
|
if (count_MD != qianmianList.Count())
|
{
|
isJiXuValidate = false;
|
qitaoReault = false;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
}
|
}
|
|
if (isJiXuValidate)
|
{//是否继续验证
|
//还需要验证在这些区域中,是否正确排队,如果没有正确排队,那么就认为不齐套 【Editby shaocx,2024-12-12】
|
var queueList = wmsDB.mes_upi_linequeue.Where(x => x.PackageCode == upiObj.PackageCode && x.AreaCode == queryAreaCode)
|
.OrderBy(x => x.Id).ToList();
|
var fisrtId = queueList.OrderBy(x => x.Id).First().Id;
|
var endId = queueList.OrderByDescending(x => x.Id).First().Id;
|
//继续查询这个时间段内的队列数据
|
//重写该队列
|
queueList = wmsDB.mes_upi_linequeue.Where(x => x.Id >= fisrtId && x.Id <= endId).OrderBy(x => x.Id).ToList();
|
var syCount = allList.Where(x => x.Shelf >= upiObj.Shelf).Count();
|
if (queueList.Count() != syCount)
|
{
|
qitaoReault = false;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
}
|
else
|
{
|
//判断是否乱序
|
bool isLuanXuan = false;
|
int temp_shelf = 0;
|
for (int i = 0; i < queueList.Count; i++)
|
{
|
if (i == 0)
|
{
|
temp_shelf = queueList[i].Shelf;
|
if (temp_shelf != upiObj.Shelf)
|
{
|
qitaoReault = false;
|
isLuanXuan = true;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
break;//跳出循环
|
}
|
if (queueList[i].Upi != upiObj.UPI)
|
{
|
qitaoReault = false;
|
isLuanXuan = true;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
break;//跳出循环
|
}
|
}
|
else
|
{
|
temp_shelf = temp_shelf + 1;
|
if (temp_shelf != queueList[i].Shelf)
|
{//认为乱序了
|
qitaoReault = false;
|
isLuanXuan = true;
|
upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
|
break;//跳出循环
|
}
|
}
|
}
|
|
if (isLuanXuan == false)
|
{
|
qitaoReault = true;
|
upiObj.AreaCode = (int)AreaCodeEnum.码垛区域;
|
}
|
}
|
}
|
}
|
|
//注意:要删除该队列数据哦
|
wmsDB.mes_upi_linequeue.Remove(lastUpiLineQueue);
|
}
|
}
|
|
//if (qitaoReault == false) {
|
// //临时
|
// SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"模拟啊啊,暂停啊啊";
|
// continue;
|
//}
|
|
using (StationServiceClient client = new StationServiceClient())
|
{
|
var res = await client.WriteQiTaoInfoAsync((int)EDevice.Station, rgvLocation, qitaoReault);
|
if (!res.result)
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()}-推送齐套结果失败,WriteQiTaoInfoAsync返回:{res.resMsg}";
|
continue;
|
}
|
else
|
{
|
Log4NetHelper.WriteInfoLog(logType, $"{rgvLocation.ToString()}-推送齐套结果成功,WriteQiTaoInfoAsync,参数: 站点{rgvLocation},齐套结果:{qitaoReault}");
|
}
|
}
|
|
var str_qitaoReault = (qitaoReault ? "齐包" : "不齐包");
|
//记录下结果
|
sysConfig.Value = upiObj.PackageCode + "|" + upiObj.UPI + "|" + upiObj.Shelf + "|" + str_qitaoReault;
|
|
//删除该UPI的缓存队列数据
|
int queryAreaCode2 = (int)AreaCodeEnum.待缓存分拣区;
|
var curUpiLineQueue = wmsDB.mes_upi_linequeue.Where(x => x.Upi == upiObj.UPI && x.AreaCode == queryAreaCode2)
|
.FirstOrDefault();
|
if (curUpiLineQueue != null)
|
{
|
wmsDB.mes_upi_linequeue.Remove(curUpiLineQueue);
|
}
|
|
//更新UPI状态
|
var upiObjList = wmsDB.mes_batchOrderUPI_new.Where(x => x.UPI == upiCode).ToList();
|
foreach (var item in upiObjList)
|
{
|
if (qitaoReault)
|
{
|
item.UpiStatus = (int)UpiStatusEnum.已齐包;
|
}
|
else
|
{
|
item.UpiStatus = (int)UpiStatusEnum.不齐包;
|
}
|
}
|
//更新 最后一次在 BZ_01的系统处理的板件 数据
|
var rbRunMode = wmsDB.wms_rbline_runmode.FirstOrDefault();
|
rbRunMode.PlanNo = upiObj.PlanNo;
|
rbRunMode.OrderId = upiObj.OrderId;
|
rbRunMode.PackageCode = upiObj.PackageCode;
|
|
WmsRecordUpiProcessHandler.SaveWmsRecordUpiProcess(wmsDB, upiObj, "BZ01", "齐包验证:" + str_qitaoReault);
|
|
wmsDB.SaveChanges();
|
}
|
}
|
else
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()}-没有要处理的数据";
|
using (WmsDBModel wmsDB = new WmsDBModel())
|
{
|
var isAllow = IsAllloRunOutTaskMode(wmsDB, null);
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
SystemWarningMsg._lbl_alert_DataProcess_BZ01 += " 出现异常:" + ex.Message + SysGloble.SPLIT_STR;
|
Log4NetHelper.WriteErrorLog(logType, " 出现异常:" + ex.Message, ex);
|
}
|
}
|
}
|
|
|
/// <summary>
|
/// 是否可以执行出库模式
|
/// </summary>
|
/// <param name="wmsDB"></param>
|
/// <param name="curUpiObj"></param>
|
/// <returns></returns>
|
private static bool IsAllloRunOutTaskMode(WmsDBModel wmsDB, mes_batchOrderUPI_new curUpiObj)
|
{
|
//首先查询数据库中是否有 要出库的数据
|
var isExistOutTaskForNewCreated = wmsDB.wms_rbline_task.Where(x =>
|
(x.RbTaskType == (int)RbTaskTypeEnum.齐包出库任务 || x.RbTaskType == (int)RbTaskTypeEnum.强制出库任务)
|
&& x.TaskStatus == (int)TaskStatusEnum.新建
|
).Count();
|
if (isExistOutTaskForNewCreated == 0)
|
{
|
FormCC.IsAllowRunOutMode = false;
|
return false;//不能执行 出库模式
|
}
|
|
var rbRunMode = wmsDB.wms_rbline_runmode.FirstOrDefault();
|
var lastUpi = rbRunMode.UPI;
|
var lastPackageCode = rbRunMode.PackageCode;
|
|
if (curUpiObj == null)
|
{//表示此时无新板过来
|
if (!string.IsNullOrEmpty(lastUpi))
|
{
|
var lastObj = wmsDB.mes_batchOrderUPI_new.Where(x => x.UPI == lastUpi).FirstOrDefault();
|
if (lastObj.UpiStatus == (int)UpiStatusEnum.不齐包 || lastObj.UpiStatus == (int)UpiStatusEnum.初始)
|
{//表示最后过去的板是不齐包的板
|
//可以运行执行 出库模式
|
}
|
else
|
{//表示最后过去的板是齐包的板
|
//要继续判断后面 待分拣区域还存在相同包的板数据吗
|
var isExistSamePackageCode = wmsDB.mes_batchOrderUPI_new.Where(x => x.PackageCode == lastPackageCode
|
&& x.AreaCode == (int)AreaCodeEnum.待缓存分拣区).Count();
|
if (isExistSamePackageCode > 0)
|
{
|
FormCC.IsAllowRunOutMode = false;
|
return false;//不能执行 出库模式
|
}
|
else
|
{
|
//可以运行执行 出库模式
|
}
|
}
|
}
|
else
|
{
|
//可以运行执行 出库模式
|
}
|
}
|
else
|
{//表示此时有新板过来
|
if (!string.IsNullOrEmpty(lastUpi))
|
{
|
if (lastPackageCode == curUpiObj.PackageCode)
|
{
|
FormCC.IsAllowRunOutMode = false;
|
return false;//不能执行 出库模式
|
}
|
else
|
{//新包过来了
|
//可以运行执行 出库模式
|
}
|
}
|
else
|
{
|
//可以运行执行 出库模式
|
}
|
}
|
|
FormCC.IsAllowRunOutMode = true;
|
return true;
|
}
|
|
/// <summary>
|
/// 判断是否齐包
|
/// </summary>
|
/// <param name="wmsDB"></param>
|
/// <param name="upiObj"></param>
|
/// <returns></returns>
|
private static bool ValidateQiBao(WmsDBModel wmsDB, mes_batchOrderUPI_new upiObj)
|
{
|
if (upiObj.Shelf != 1)
|
{
|
return false;
|
}
|
//查询是否齐包
|
var allList = wmsDB.mes_batchOrderUPI_new.Where(x => x.PackageCode == upiObj.PackageCode).ToList();
|
var isQiTaoList = allList.Where(x => x.AreaCode == (int)AreaCodeEnum.待缓存分拣区
|
||
|
x.AreaCode == (int)AreaCodeEnum.码垛区域
|
).ToList();
|
if (isQiTaoList.Count() != allList.Count())
|
{//不齐包
|
return false;
|
}
|
else
|
{//齐包
|
//这里再增加个逻辑,必须是按照次序进入的才真正的算齐包 [Editby shaocx,2024-12-06]
|
//寻找本包内的前一块板的位置
|
int queryAreaCode = (int)AreaCodeEnum.待缓存分拣区;
|
var lastUpiLineQueue = wmsDB.mes_upi_linequeue.Where(x => x.PackageCode == upiObj.PackageCode && x.AreaCode == queryAreaCode)
|
.OrderBy(x => x.Id).FirstOrDefault();//查询该包队列的第一块板
|
if (lastUpiLineQueue == null)
|
{//说明 队列中没有该包的数据
|
return false;
|
}
|
else
|
{
|
if (lastUpiLineQueue.Shelf != upiObj.Shelf)
|
{
|
return false;
|
}
|
else
|
{
|
var isJiXuValidate = true;
|
if (upiObj.Shelf != 1)
|
{
|
var qianmianList = allList.Where(x => x.Shelf < upiObj.Shelf).ToList();
|
//看以前的板在哪里,必须都在 码垛区域
|
var yiqianQuyu = AreaCodeEnum.码垛区域;
|
var count_MD = qianmianList.Where(x => x.AreaCode == (int)yiqianQuyu).Count();
|
if (count_MD != qianmianList.Count())
|
{
|
isJiXuValidate = false;
|
return false;
|
}
|
}
|
|
if (isJiXuValidate)
|
{//是否继续验证
|
//还需要验证在这些区域中,是否正确排队,如果没有正确排队,那么就认为不齐套 【Editby shaocx,2024-12-12】
|
var queueList = wmsDB.mes_upi_linequeue.Where(x => x.PackageCode == upiObj.PackageCode && x.AreaCode == queryAreaCode)
|
.OrderBy(x => x.Id).ToList();
|
var fisrtId = queueList.OrderBy(x => x.Id).First().Id;
|
var endId = queueList.OrderByDescending(x => x.Id).First().Id;
|
//继续查询这个时间段内的队列数据
|
//重写该队列
|
queueList = wmsDB.mes_upi_linequeue.Where(x => x.Id >= fisrtId && x.Id <= endId).OrderBy(x => x.Id).ToList();
|
var syCount = allList.Where(x => x.Shelf >= upiObj.Shelf).Count();
|
if (queueList.Count() != syCount)
|
{
|
return false;
|
}
|
else
|
{
|
//判断是否乱序
|
bool isLuanXuan = false;
|
int temp_shelf = 0;
|
for (int i = 0; i < queueList.Count; i++)
|
{
|
if (i == 0)
|
{
|
temp_shelf = queueList[i].Shelf;
|
if (temp_shelf != upiObj.Shelf)
|
{
|
isLuanXuan = true;
|
return false;
|
}
|
if (queueList[i].Upi != upiObj.UPI)
|
{
|
isLuanXuan = true;
|
return false;
|
}
|
}
|
else
|
{
|
temp_shelf = temp_shelf + 1;
|
if (temp_shelf != queueList[i].Shelf)
|
{//认为乱序了
|
isLuanXuan = true;
|
return false;
|
}
|
}
|
}
|
|
if (isLuanXuan == false)
|
{
|
return true;
|
}
|
}
|
}
|
}
|
}
|
}
|
|
return false;
|
}
|
}
|
}
|