using Rhea.Common;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Tiger.IBusiness;
|
using Tiger.IBusiness.MES.WorkAction;
|
using Tiger.Model;
|
using Tiger.Model.Entitys.MES.Position;
|
|
namespace Tiger.Business.MES.WorkAction
|
{
|
public class InputQty : IInputQty
|
{
|
#region Propertys & Variables
|
#region 固定写法,工序中的必要信息
|
public DbClient MainDB { get; set; }
|
public bool IsFinished { get; set; } = false;
|
public IWorkStep CurStep { get; set; }
|
public IPosition CurPosition { get; set; }
|
public MES_WO_NODE_ACT NodeAct { get; set; }
|
public MES_WO_ACTION Setting { get; set; }
|
#endregion
|
public double NewQty { get; set; }
|
public double NewWoInputQty { get; set; }
|
public double NewBatchInputQty { get; set; }
|
#endregion Propertys & Variables
|
|
#region Functions
|
/// <summary>
|
/// 初始化工序行为
|
/// </summary>
|
/// <returns></returns>
|
public void Init(IWorkStep curStep, IPosition position, MES_WO_NODE_ACT nodeAct, MES_WO_ACTION setting)
|
{
|
#region 固定写法,给默认变量赋值
|
MainDB = position.MainDB;
|
CurStep = curStep;
|
CurPosition = position;
|
NodeAct = nodeAct;
|
Setting = setting;
|
#endregion
|
|
CurStep.Message = Biz.L("等待输入条码数量");
|
CurStep.Status = StepStatus.Normal;
|
}
|
|
/// <summary>
|
/// 获取行为开始的提示信息
|
/// </summary>
|
/// <returns></returns>
|
public Locale GetBeginMsg()
|
{
|
//var msg = new Locale($"请输入条码[{0}]的数量");
|
var msg = new Locale("MES.WorkAction.InputQty.BeginMsg", CurPosition.CurSN);
|
|
return msg;
|
}
|
|
/// <summary>
|
/// 尝试开始执行工序行为
|
/// </summary>
|
/// <returns></returns>
|
public ApiAction<SubmitOutput> TryBegin(SubmitInput input)
|
{
|
var action = new ApiAction<SubmitOutput>(new SubmitOutput());
|
|
action.LocaleMsg = GetBeginMsg();
|
|
//如果是包装条码则不允许修改数量
|
if (MainDB.Queryable<MES_WIP_PKG>().Any(q => q.PARENT_SN == CurPosition.CurSN && q.AUTH_ORG == CurPosition.WorkBatch.WO.AUTH_ORG))
|
{
|
CurStep.Message = Biz.L("包装条码不允许修改数量");
|
CurStep.Status = StepStatus.Error;
|
action.IsSuccessed = false;
|
action.Data.ShortMsg = new("包装条码不允许修改数量", ShortMessage.Types.Error);
|
//action.LocaleMsg = new($"包装条码[{0}]不允许修改数量,请重新扫描正确的条码");
|
action.LocaleMsg = new("MES.WorkAction.InputQty.NotAllowPackage", CurPosition.CurSN);
|
CurPosition.ResetNode();
|
}
|
else
|
{
|
//如果返回成功则认为当前行为可以开始执行,否则返回失败
|
CurStep.Message = Biz.L("请输入数量");
|
CurStep.Status = StepStatus.Normal;
|
action.IsSuccessed = true;
|
action.Data.ShortMsg = new("请输入数量", ShortMessage.Types.Success);
|
}
|
return action;
|
}
|
|
/// <summary>
|
/// 工序行为提交数据
|
/// </summary>
|
/// <returns></returns>
|
public ApiAction<SubmitOutput> Submit(SubmitInput input)
|
{
|
var action = new ApiAction<SubmitOutput>(new SubmitOutput());
|
|
action = SaveSnQty(input, action);
|
//上料数据保存失败
|
if (!action.IsSuccessed)
|
{
|
//如果行为设置为出错需要重置工序操作
|
if (NodeAct.NEED_RESET == "Y")
|
{
|
CurPosition.ResetNode();
|
}
|
}
|
else
|
{
|
action = End(input);
|
}
|
return action;
|
}
|
|
public ApiAction<SubmitOutput> SaveSnQty(SubmitInput input, ApiAction<SubmitOutput> action)
|
{
|
try
|
{
|
NewQty = input?.Data?.ToInt32() ?? 0;
|
if (NewQty > 0)
|
{
|
var newInput = CurPosition.CurWipSNs.Sum(q => NewQty);
|
var woInput = CurPosition.WorkBatch.WoSNs.Where(q => q.WORK_ORDER == CurPosition.WorkBatch.WO.ORDER_NO && BIZ_MES_WO_SN.STATUSs.NotInput.GetValue() < q.STATUS && q.STATUS <= BIZ_MES_WO_SN.STATUSs.Finished.GetValue())
|
.Where(q => !CurPosition.CurWipSNs.Any(sn => sn.ID == q.WIP_ID)).DistinctBy(q => q.WIP_ID).Sum(q => q.QTY);
|
var batchInput = CurPosition.WorkBatch.WoSNs.Where(q => q.BATCH_NO == CurPosition.WorkBatch.Batch.BATCH_NO && BIZ_MES_WO_SN.STATUSs.NotInput.GetValue() < q.STATUS && q.STATUS <= BIZ_MES_WO_SN.STATUSs.Finished.GetValue())
|
.Where(q => !CurPosition.CurWipSNs.Any(sn => sn.ID == q.WIP_ID)).DistinctBy(q => q.WIP_ID).Sum(q => q.QTY);
|
|
|
//工单批次投入数量减去报废数量如果大于等于计划数量,则不允许生产
|
if ((NewBatchInputQty = batchInput + newInput) > CurPosition.WorkBatch.Batch.PLAN_QTY)
|
{
|
CurStep.Message = Biz.L("重新输入数量");
|
CurStep.Status = StepStatus.Error;
|
action.IsSuccessed = false;
|
action.Data.ShortMsg = new("重新输入数量", ShortMessage.Types.Error);
|
//action.LocaleMsg = new($"条码更新数量[{0}]后,工单批次[{1}]投入数量[{2}]超出计划数量[{3}],请重新输入正确的数量");
|
action.LocaleMsg = new("MES.WorkAction.InputQty.BatchInputEnough", input.Data, CurPosition.WorkBatch.Batch.BATCH_NO, batchInput + newInput, CurPosition.WorkBatch.Batch.PLAN_QTY);
|
}
|
//工单投入数量减去报废数量如果大于等于计划数量,则不允许生产
|
else if ((NewWoInputQty = woInput + newInput) > CurPosition.WorkBatch.WO.PLAN_QTY)
|
{
|
CurStep.Message = Biz.L("重新输入数量");
|
CurStep.Status = StepStatus.Error;
|
action.IsSuccessed = false;
|
action.Data.ShortMsg = new("重新输入数量", ShortMessage.Types.Error);
|
//action.LocaleMsg = new($"条码更新数量[{0}]后,工单[{1}]投入数量[{2}]超出计划数量[{3}],请重新输入正确的数量");
|
action.LocaleMsg = new("MES.WorkAction.InputQty.WoInputEnough", input.Data, CurPosition.WorkBatch.WO.ORDER_NO, woInput + newInput, CurPosition.WorkBatch.WO.PLAN_QTY);
|
}
|
else
|
{
|
action = End(input);
|
}
|
}
|
else
|
{
|
CurStep.Message = Biz.L("重新输入数量");
|
CurStep.Status = StepStatus.Error;
|
action.IsSuccessed = false;
|
action.Data.ShortMsg = new("重新输入数量", ShortMessage.Types.Error);
|
//action.LocaleMsg = new($"数量[{0}]必须是大于0的整数,请重新输入正确的数量");
|
action.LocaleMsg = new("MES.WorkAction.InputQty.QtyError", input.Data);
|
|
}
|
}
|
catch (System.Exception ex)
|
{
|
CurStep.Message = Biz.L("更新数量异常");
|
CurStep.Status = StepStatus.Error;
|
action.Data.ShortMsg = new("更新数量异常", ShortMessage.Types.Error);
|
//action.CatchExceptionWithLog(ex, $"更新数量行为:更新条码[{0}]数量[{1}]异常");
|
action.LocaleMsg = new("MES.WorkAction.InputQty.SaveQtyException", CurPosition.CurSN, input?.Data.ToString());
|
}
|
return action;
|
}
|
|
/// <summary>
|
/// 结束执行工序行为
|
/// </summary>
|
/// <returns></returns>
|
public ApiAction<SubmitOutput> End(SubmitInput input)
|
{
|
var action = new ApiAction<SubmitOutput>(new SubmitOutput());
|
//记录行为操作记录
|
var wipActs = new List<MES_WIP_ACT>();
|
var wipVars = new List<MES_WIP_ACT_VAR>();
|
foreach (var wipSn in CurPosition.CurWipSNs)
|
{
|
wipSn.QTY = NewQty;
|
foreach (var item in CurPosition.CurWipSNHiss.Where(q => q.WIP_ID == wipSn.ID))
|
{
|
item.QTY = NewQty;
|
}
|
foreach (var item in CurPosition.WorkBatch.WoSNs.Where(q => q.WIP_ID == wipSn.ID))
|
{
|
item.QTY = NewQty;
|
}
|
var wipAct = new MES_WIP_ACT()
|
{
|
AUTH_ORG = CurPosition.WorkBatch.WO.AUTH_ORG,
|
AUTH_PROD = CurPosition.CurLine.LINE_CODE,
|
WIP_ID = wipSn.ID,
|
HIS_ID = CurPosition.CurWipSNHiss.First(q => q.SN == wipSn.SN).ID,
|
SN = wipSn.SN,
|
STATUS = wipSn.STATUS,
|
ITEM_CODE = wipSn.ITEM_CODE,
|
WORK_ORDER = wipSn.WORK_ORDER,
|
BATCH_NO = wipSn.BATCH_NO,
|
ROT_CODE = wipSn.ROT_CODE,
|
NODE_ID = wipSn.NODE_ID,
|
NODE_NAME = wipSn.NODE_NAME,
|
ACT_ID = NodeAct.ID,
|
ACT_NAME = NodeAct.ACT_NAME,
|
FTY_CODE = wipSn.FTY_CODE,
|
WS_CODE = wipSn.WS_CODE,
|
LINE_CODE = wipSn.LINE_CODE,
|
POST_CODE = wipSn.POST_CODE,
|
OPER_CODE = wipSn.OPER_CODE,
|
SEGMENT = wipSn.SEGMENT,
|
FLOW_SN = wipSn.FLOW_SN,
|
TRAY_SN = wipSn.TRAY_SN,
|
INNER_SN = wipSn.INNER_SN,
|
CARTON_SN = wipSn.CARTON_SN,
|
PALLET_SN = wipSn.PALLET_SN,
|
OPERATION_TIME = DateTime.Now,
|
SFTS_CODE = wipSn.SFTS_CODE,
|
SFT_CODE = wipSn.SFT_CODE,
|
PRD_CODE = wipSn.PRD_CODE,
|
ACT_TYPE = NodeAct.ACT_TYPE,
|
ACT_SN = wipSn.SN,
|
ACT_VAR_DIC = CurStep.ActionDic.ToJson(),
|
ACT_RESULT = "Y",
|
ELAPSED_TIME = CurStep.GetElapsedTime().TotalMilliseconds.ToInt64(),
|
TRACE_INFO = $"条码[{CurPosition.CurSN}]更新数量为[{NewQty}]保存成功",
|
};
|
wipActs.Add(wipAct);
|
foreach (var item in NodeAct.Variables)
|
{
|
var wipVar = new MES_WIP_ACT_VAR()
|
{
|
AUTH_ORG = CurPosition.WorkBatch.WO.AUTH_ORG,
|
AUTH_PROD = CurPosition.CurLine.LINE_CODE,
|
HIS_ID = CurPosition.CurWipSNHiss.First(q => q.SN == wipSn.SN).ID,
|
WIP_ID = wipSn.ID,
|
SN = wipSn.SN,
|
ITEM_CODE = wipSn.ITEM_CODE,
|
WORK_ORDER = wipSn.WORK_ORDER,
|
BATCH_NO = wipSn.BATCH_NO,
|
CUST_CODE = CurPosition.WorkBatch.WO.CUST_CODE,
|
ROT_CODE = wipSn.ROT_CODE,
|
NODE_ID = wipSn.NODE_ID,
|
NODE_NAME = wipSn.NODE_NAME,
|
ACT_ID = NodeAct.ID,
|
ACT_NAME = NodeAct.ACT_NAME,
|
FTY_CODE = wipSn.FTY_CODE,
|
WS_CODE = wipSn.WS_CODE,
|
LINE_CODE = wipSn.LINE_CODE,
|
POST_CODE = wipSn.POST_CODE,
|
OPER_CODE = wipSn.OPER_CODE,
|
SEGMENT = wipSn.SEGMENT,
|
FLOW_SN = wipSn.FLOW_SN,
|
TRAY_SN = wipSn.TRAY_SN,
|
INNER_SN = wipSn.INNER_SN,
|
CARTON_SN = wipSn.CARTON_SN,
|
PALLET_SN = wipSn.PALLET_SN,
|
OPERATION_TIME = DateTime.Now,
|
SFTS_CODE = wipSn.SFTS_CODE,
|
SFT_CODE = wipSn.SFT_CODE,
|
PRD_CODE = wipSn.PRD_CODE,
|
VAR_CODE = item.VAR_CODE,
|
VAR_NAME = item.VAR_NAME,
|
VAR_CONTROL = item.VAR_CONTROL,
|
VALUE_TYPE = item.VALUE_TYPE,
|
PROCESS_VAR = item.PROCESS_VAR,
|
INPUT_VALUE = CurStep.ActionDic.GetOrDefault(item.VAR_CODE),
|
TRACE_INFO = CurStep.ActionDic.ToJson(),
|
};
|
wipVars.Add(wipVar);
|
}
|
}
|
|
//创建变量克隆对象用于传入DBSubmitAction中保存当前需要暂存的数据值
|
var _wipActs = wipActs.Clone();
|
var _wipVars = wipVars.Clone();
|
var wo = CurPosition.WorkBatch.WO.ORDER_NO;
|
var batch = CurPosition.WorkBatch.Batch.BATCH_NO;
|
var nodeID = NodeAct.NODE_ID;
|
var _wipIDs = CurPosition.CurWipSNs.Select(q => q.ID).ToList().Clone();
|
//保存数据
|
CurStep.DBSubmitAction = () =>
|
{
|
var db = CurPosition.GetCommitDB();
|
//在制品信息表
|
db.Updateable<MES_WIP_DATA>(CurPosition.UserCode)
|
.SetColumns(q => q.QTY == NewQty)
|
.Where(q => q.WORK_ORDER == wo && _wipIDs.Contains(q.ID))
|
.ExecuteCommand();
|
//生产过程记录
|
db.Updateable<MES_WIP_HIS>(CurPosition.UserCode)
|
.SetColumns(q => q.QTY == NewQty)
|
.Where(q => q.WORK_ORDER == wo && q.NODE_ID == nodeID && _wipIDs.Contains(q.WIP_ID))
|
.ExecuteCommand();
|
//生产包装记录
|
db.Updateable<MES_WIP_PKG>(CurPosition.UserCode)
|
.SetColumns(q => q.QTY == NewQty)
|
.Where(q => q.WORK_ORDER == wo && _wipIDs.Contains(q.ID))
|
.ExecuteCommand();
|
//工单表
|
db.Updateable<BIZ_MES_WO>(CurPosition.UserCode)
|
.SetColumns(q => q.INPUT_QTY == NewWoInputQty)
|
.Where(q => q.ORDER_NO == wo)
|
.ExecuteCommand();
|
//工单批次表
|
db.Updateable<BIZ_MES_WO_BATCH>(CurPosition.UserCode)
|
.SetColumns(q => q.INPUT_QTY == NewBatchInputQty)
|
.Where(q => q.BATCH_NO == batch)
|
.ExecuteCommand();
|
//工单条码明细表
|
db.Updateable<BIZ_MES_WO_SN>(CurPosition.UserCode)
|
.SetColumns(q => q.QTY == NewQty)
|
.Where(q => q.WORK_ORDER == wo && _wipIDs.Contains(q.WIP_ID))
|
.ExecuteCommand();
|
db.Storageable(_wipActs, CurPosition.UserCode).ExecuteCommand();
|
db.Storageable(_wipVars, CurPosition.UserCode).ExecuteCommand();
|
};
|
|
IsFinished = true;
|
CurStep.Message = Biz.L("更新数量成功");
|
CurStep.Status = StepStatus.Finished;
|
action.Data.ShortMsg = new("更新数量成功", ShortMessage.Types.Success);
|
//action.LocaleMsg = new($"条码[{0}]更新数量为[{1}]保存成功");
|
action.LocaleMsg = new("MES.WorkAction.InputQty.SaveSuccess", CurPosition.CurSN, NewQty);
|
return action;
|
}
|
|
#endregion Functions
|
}
|
}
|