using Microsoft.AspNetCore.Http;
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Entitys.MES.Position;
namespace Tiger.Business
{
///
/// 工单 上下文
///
public class WoContext : IWoContext
{
#region 工单管理
///
/// 工单批次缓存字典
///
public static Dictionary WoBatchDic { get; set; } = new Dictionary();
public Dictionary GetWoBatchDic() => WoBatchDic.ToDictionary(k => k.Key, v => v.Value as IWorkBatch);
///
/// 从工单批次字典中获取一个工单批次对象
///
///
///
///
public static WorkBatch GetBatch(string workorder, string lineCode, string batchNo = "", bool canDoWork = true)
{
if (ExistsBatch(workorder, lineCode, batchNo))
{
return WoBatchDic.WhereIF(canDoWork, q => q.Value.Batch?.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch?.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue())
.FirstOrDefault(q => !q.Value.Batch.IsNullOrEmpty() && q.Value.Batch.ORDER_NO == workorder && q.Value.Batch.ACT_LINE == lineCode && (batchNo.IsNullOrEmpty() || q.Value.Batch.BATCH_NO == batchNo)).Value;
}
return null;
}
///
/// 判断工单批次是否存在于工单批次字典中
///
///
///
///
///
///
public static bool ExistsBatch(string workorder, string lineCode, string batchNo = "", bool canDoWork = false)
{
return WoBatchDic.WhereIF(canDoWork, q => !q.Value.Batch.IsNullOrEmpty() && (q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue()))
.Any(q => q.Value.Batch?.ORDER_NO == workorder && q.Value.Batch?.ACT_LINE == lineCode && (batchNo.IsNullOrEmpty() || q.Value.Batch?.BATCH_NO == batchNo));
}
/// 根据工单号,增加一个工单对象到工单批次字典中
///
///
///
public static WorkBatch Add(string workorder)
{
var wb = new WorkBatch(workorder).Init("");
WoBatchDic.Add(workorder, wb);
return wb;
}
/// 根据工单号和产线,增加一个工单批次对象到工单批次字典中
///
///
///
///
public static WorkBatch Add(string workorder, string linecode)
{
var wb = new WorkBatch(workorder).Init(linecode);
WoBatchDic.Add(wb.Batch.BATCH_NO, wb);
return wb;
}
/// 根据工单号,增加一个工单对象到工单批次字典中
///
///
///
public IWorkBatch AddWo(string workorder)
{
return Add(workorder);
}
/// 根据工单号和产线,增加一个工单批次对象到工单批次字典中
///
///
///
///
public IWorkBatch AddBatch(string workorder, string linecode)
{
return Add(workorder, linecode);
}
/// 从工单批次字典中删除一个工单的所有批次对象
///
///
///
public static bool RemoveAll(string workorder)
{
try
{
var list = WoBatchDic.Keys.ToList().Where(q => q.Contains(workorder));
foreach (var batch in list)
{
WoBatchDic.Remove(batch);
}
return true;
}
catch (Exception ex)
{
Logger.Default.Fatal(ex, $"从工单批次字典中删除一个工单[{workorder}]对象异常");
return false;
}
}
/// 从工单批次字典中删除一个工单的所有批次对象
///
///
///
public bool RemoveWo(string workorder)
{
return RemoveAll(workorder);
}
/// 从工单批次字典中删除一个工单批次对象
///
///
///
public static bool Remove(string batchNo)
{
try
{
if (WoBatchDic.ContainsKey(batchNo))
{
WoBatchDic.Remove(batchNo);
}
return true;
}
catch (Exception ex)
{
Logger.Default.Fatal(ex, $"从工单批次字典中删除一个工单批次[{batchNo}]对象异常");
return false;
}
}
/// 从工单批次字典中删除一个工单批次对象
///
///
///
public bool RemoveBatch(string batchNo)
{
return Remove(batchNo);
}
///
/// 获取SN的下一个工序节点列表
///
///
///
public static ApiAction> GetSnNextNodes(string sn)
{
var action = new ApiAction>(new List());
try
{
var wipSNs = Biz.Db.Queryable().IncludesAllFirstLayer().Where(q => (q.SN == sn || q.FLOW_SN == sn || q.TRAY_SN == sn || q.INNER_SN == sn || q.CARTON_SN == sn || q.PALLET_SN == sn)).ToList();
if (wipSNs.IsNullOrEmpty())
{
var woSNs = Biz.Db.Queryable().Where(q => (q.SN == sn || q.TRAY_SN == sn || q.OUTER_SN == sn)).ToList();
//查找到条码已绑定的工单
if (!woSNs.IsNullOrEmpty())
{
foreach (var woSN in woSNs)
{
var wipSN = new MES_WIP_DATA()
{
SN = sn,
FLOW_SN = sn,
STATUS = MES_WIP_DATA.STATUSs.Init.GetValue(),
ITEM_CODE = woSN.ITEM_CODE,
WORK_ORDER = woSN.WORK_ORDER,
BATCH_NO = woSN.BATCH_NO,
HOLD_FLAG = "N",
FINISHED_FLAG = "N",
UNBIND_FLAG = "N",
INV_FLAG = "N",
DFT_FLAG = "N",
DFT_COUNT = 0,
};
wipSNs.Add(wipSN);
}
}
}
if (wipSNs.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = new($"找不到条码[{sn}]的生产信息", sn);
action.LocaleMsg = new("MES.WoContext.SnNotFound", sn);
}
else
{
if (!WoBatchDic.Any(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER))
{
Add(wipSNs.First().WORK_ORDER);
}
var wo = WoBatchDic.FirstOrDefault(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER).Value;
action.Data = wo.GetNextNodes(wipSNs.First());
}
}
catch (Exception ex)
{
//action.CatchExceptionWithLog(ex, $"获取条码[{sn}]的下一个工序节点列表异常");
action.CatchExceptionWithLog(ex, Biz.L("MES.WoContext.GetSnNextNodesException", sn));
}
return action;
}
///
/// 设置当前条码的工序信息
///
public static ApiAction GetSnOperInfo(string sn)
{
var action = new ApiAction(new OperInfo());
try
{
var wipSNs = Biz.Db.Queryable().IncludesAllFirstLayer().Where(q => (q.SN == sn || q.FLOW_SN == sn || q.TRAY_SN == sn || q.INNER_SN == sn || q.CARTON_SN == sn || q.PALLET_SN == sn)).ToList();
if (wipSNs.IsNullOrEmpty())
{
var woSNs = Biz.Db.Queryable().Where(q => (q.SN == sn || q.TRAY_SN == sn || q.OUTER_SN == sn)).ToList();
//查找到条码已绑定的工单
if (!woSNs.IsNullOrEmpty())
{
foreach (var woSN in woSNs)
{
var wipSN = new MES_WIP_DATA()
{
SN = sn,
FLOW_SN = sn,
STATUS = MES_WIP_DATA.STATUSs.Init.GetValue(),
ITEM_CODE = woSN.ITEM_CODE,
WORK_ORDER = woSN.WORK_ORDER,
BATCH_NO = woSN.BATCH_NO,
HOLD_FLAG = "N",
FINISHED_FLAG = "N",
UNBIND_FLAG = "N",
INV_FLAG = "N",
DFT_FLAG = "N",
DFT_COUNT = 0,
};
wipSNs.Add(wipSN);
}
}
}
if (wipSNs.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = new($"找不到条码[{sn}]的生产信息", sn);
action.LocaleMsg = new("MES.WoContext.SnNotFound", sn);
}
else
{
if (!WoBatchDic.Any(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER))
{
Add(wipSNs.First().WORK_ORDER);
}
var wo = WoBatchDic.FirstOrDefault(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER).Value;
action.Data = new()
{
CurNode = wipSNs.First().NODE_NAME,
NextNode = string.Join(",", wo.GetNextNodes(wipSNs.First()).Select(q => q.NODE_NAME)),
};
}
}
catch (Exception ex)
{
//action.CatchExceptionWithLog(ex, $"获取条码[{sn}]的工序信息异常");
action.CatchExceptionWithLog(ex, Biz.L("MES.WoContext.GetSnOperInfoException", sn));
}
return action;
}
#endregion
}
}