服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-12-08 006c12987d7dc7c2081fbf6715ebea98b93fdca0
Tiger.Business.MES/Transaction/TestNode.cs
@@ -1,13 +1,6 @@
using Rhea.Common;
using Tiger.IBusiness;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Entitys.MES.Position;
@@ -44,56 +37,70 @@
                //工步列表为空或者工序节点工步有未完成时,优先完成工序节点工步
                if (Steps.IsNullOrEmpty() || !IsFinishNodeSteps)
                {
                    var wosns = MainDB.Queryable<BIZ_MES_WO_BATCH, BIZ_MES_WO_SN>((q, s) => new JoinQueryInfos(JoinType.Inner, q.ORDER_NO == s.WORK_ORDER && q.BATCH_NO == s.BATCH_NO))
                                                    .ByAuth(input.AuthOption)//.Where((q, s) => s.STATUS < BIZ_MES_WO_SN.STATUSs.Finished.GetValue())
                                                    .Where((q, s) => s.SN == input.SN || s.FLOW_SN == input.SN || s.TRAY_SN == input.SN).Select((q, s) => new { Batch = q, SN = s }).ToList();
                    //先判断当前工单不为空且当前岗位在当前工单是不是首站,如果是则不允许变更当前工单,尝试把条码绑定到当前工单
                    if (!CurBatch.IsNullOrEmpty() && CurBatch.IsFirstNode(PostCode))
                    {
                        //更新CurBatch
                        CurBatch?.Update();
                        //提交数据
                        action = NodeSubmit(action, input);
                        //更新工序信息
                        if (!action.IsSuccessed)
                        var wosn = wosns.FirstOrDefault(q => q.Batch.ORDER_NO == CurBatch.WO.ORDER_NO && (q.SN.STATUS == BIZ_MES_WO_SN.STATUSs.Finished.GetValue() || q.SN.STATUS == BIZ_MES_WO_SN.STATUSs.Scrap.GetValue()));
                        if (!wosn.IsNullOrEmpty())
                        {
                            var info = WoContext.GetSnOperInfo(input.SN).Data;
                            info.InputQty = CurOperInfo(input.Locale).InputQty;
                            action.Data.OperInfo = info;
                            action.Data.ShortMsg = new($"产品{wosn.SN.STATUS.GetEnumDesc<BIZ_MES_WO_SN.STATUSs>()}", ShortMessage.Types.Error);
                            action.Data.OperInfo = new();
                            action.IsSuccessed = false;
                            //action.LocaleMsg = new($"进站扫描错误,条码[{0}]{1}", input.SN);
                            action.LocaleMsg = new("MES.Transaction.CollectNode.Submit.SnStatusError", input.SN, wosn.SN.STATUS.GetEnumDesc<BIZ_MES_WO_SN.STATUSs>());
                        }
                        else if (!IsFinishAllSteps)
                        else
                        {
                            action.Data.OperInfo = SetOperNodeInfo(CurOperInfo(input.Locale));
                            //提交数据
                            action = NodeSubmit(action, input);
                            //更新工序信息
                            if (!action.IsSuccessed)
                            {
                                var info = WoContext.GetSnOperInfo(input.SN).Data;
                                info.InputQty = CurOperInfo(input.Locale).InputQty;
                                action.Data.OperInfo = info;
                            }
                            else if (Steps.Any() && !IsFinishAllSteps)
                            {
                                action.Data.OperInfo = SetOperNodeInfo(CurOperInfo(input.Locale));
                            }
                        }
                    }
                    //当前岗位在当前工单不是首站,则查找条码已绑定的工单当作当前工单
                    else
                    {
                        var wosns = Biz.Db.Queryable<BIZ_MES_WO_BATCH, BIZ_MES_WO_SN>((q, s) => new JoinQueryInfos(JoinType.Inner, q.ORDER_NO == s.WORK_ORDER && q.BATCH_NO == s.BATCH_NO))
                                                 .ByAuth(input.AuthOption).Where((q, s) => s.STATUS < BIZ_MES_WO_SN.STATUSs.Finished.GetValue())
                                                 .Where((q, s) => s.SN == input.SN || s.FLOW_SN == input.SN || s.TRAY_SN == input.SN).Select((q, s) => new { Batch = q, SN = s }).ToList();
                        //查找到条码已绑定的工单
                        if (!wosns.IsNullOrEmpty())
                        if (wosns.Any(q => q.SN.STATUS < BIZ_MES_WO_SN.STATUSs.Finished.GetValue()))
                        {
                            if (wosns.First().Batch.ACT_LINE != CurLine.LINE_CODE)
                            var curSNs = wosns.Where(q => q.SN.STATUS < BIZ_MES_WO_SN.STATUSs.Finished.GetValue());
                            if (curSNs.First().Batch.ACT_LINE != CurLine.LINE_CODE)
                            {
                                action.Data.ShortMsg = new("产线错误", ShortMessage.Types.Error);
                                action.Data.OperInfo = new();
                                action.IsSuccessed = false;
                                //action.LocaleMsg = new($"条码[{0}]已在产线[{1}]投入生产,请在正确岗位扫描");
                                action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.NotCorrectLine", input.SN, wosns.First().Batch.ACT_LINE);
                                action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.NotCorrectLine", input.SN, curSNs.First().Batch.ACT_LINE);
                            }
                            else
                            {
                                if (CurBatch?.Batch?.ORDER_NO != wosns.First().Batch.ORDER_NO)
                                if (CurBatch?.Batch?.ORDER_NO != curSNs.First().Batch.ORDER_NO)
                                {
                                    //条码已绑定的工单不等于当前工单则重新选择工单
                                    var result = await SelectOrder(new() { AuthOption = input.AuthOption, OrderNo = wosns.First().Batch.ORDER_NO });
                                    var result = await SelectOrder(new() { AuthOption = input.AuthOption, OrderNo = curSNs.First().Batch.ORDER_NO }, curSNs.First().Batch.BATCH_NO);
                                    if (!result.IsSuccessed)
                                    {
                                        action.Data.ShortMsg = new("工单异常", ShortMessage.Types.Error);
                                        action.Data.OperInfo = new();
                                        action.IsSuccessed = result.IsSuccessed;
                                        action.LocaleMsg = result.LocaleMsg;
                                        return action;
                                        return SetOutPutMqttMsg(action, input.Locale);
                                    }
                                }
                                //更新CurBatch
@@ -115,23 +122,37 @@
                        //查找不到条码已绑定的工单
                        else
                        {
                            //没有当前工单,则先选择工单后再扫描条码
                            if (CurBatch.IsNullOrEmpty())
                            var lastSn = wosns.OrderByDescending(q => q.SN.UPDATE_TIME).FirstOrDefault();
                            if (!lastSn.IsNullOrEmpty())
                            {
                                action.Data.ShortMsg = new("未选择工单", ShortMessage.Types.Error);
                                action.Data.ShortMsg = new($"产品{lastSn.SN.STATUS.GetEnumDesc<BIZ_MES_WO_SN.STATUSs>()}", ShortMessage.Types.Error);
                                action.Data.OperInfo = new();
                                action.IsSuccessed = false;
                                //action.LocaleMsg = new($"未选择工单,请先选择要生产的工单");
                                action.LocaleMsg = new("MES.Transaction.TestNode.Submit.NotSelectOrderException");
                                //action.LocaleMsg = new($"进站扫描错误,条码[{0}]{1}");
                                action.LocaleMsg = new("MES.Transaction.TestNode.Submit.SnStatusError", input.SN, lastSn.SN.STATUS.GetEnumDesc<BIZ_MES_WO_SN.STATUSs>());
                            }
                            //有当前工单且不是投入,则提示条码未投入生产,请先去首站扫描
                            else
                            {
                                action.Data.ShortMsg = new("未投入生产", ShortMessage.Types.Error);
                                action.Data.OperInfo = new();
                                action.IsSuccessed = false;
                                //action.LocaleMsg = new($"条码[{input.SN}]尚未投入生产,请先去首站扫描", input.SN);
                                action.LocaleMsg = new("MES.Transaction.TestNode.Submit.NotInputException", input.SN);
                                //没有当前工单,则先选择工单后再扫描条码
                                if (CurBatch.IsNullOrEmpty())
                                {
                                    action.Data.ShortMsg = new("未选择工单", ShortMessage.Types.Error);
                                    action.Data.OperInfo = new();
                                    action.IsSuccessed = false;
                                    //action.LocaleMsg = new($"未选择工单,请先选择要生产的工单");
                                    action.LocaleMsg = new("MES.Transaction.TestNode.Submit.NotSelectOrderException");
                                }
                                //有当前工单且不是投入,则提示条码未投入生产,请先去首站扫描
                                else
                                {
                                    action.Data.ShortMsg = new("未投入生产", ShortMessage.Types.Error);
                                    action.Data.OperInfo = new();
                                    action.IsSuccessed = false;
                                    //action.LocaleMsg = new($"条码[{input.SN}]尚未投入生产,请先去首站扫描", input.SN);
                                    action.LocaleMsg = new("MES.Transaction.TestNode.Submit.NotInputException", input.SN);
                                }
                            }
                        }
                    }
@@ -198,7 +219,7 @@
                //action.CatchExceptionWithLog(ex, $"测试工序:提交操作数据异常");
                action.CatchExceptionWithLog(ex, Biz.L("MES.Transaction.TestNode.SubmitException"));
            }
            return action;
            return SetOutPutMqttMsg(action, input.Locale);
        }
        /// <summary>
@@ -236,7 +257,7 @@
                        return woStatus;
                    }
                    var wipSNs = Biz.Db.Queryable<MES_WIP_DATA>().IncludesAllFirstLayer().Where(q => q.SN == input.SN || q.FLOW_SN == input.SN || q.TRAY_SN == input.SN).ToList();
                    var wipSNs = MainDB.Queryable<MES_WIP_DATA>().IncludesAllFirstLayer().Where(q => q.SN == input.SN || q.FLOW_SN == input.SN || q.TRAY_SN == input.SN).Where(q => q.FINISHED_FLAG != "Y").ToList();
                    if (wipSNs.IsNullOrEmpty())
                    {
                        var wipSN = new MES_WIP_DATA()
@@ -254,6 +275,7 @@
                            HOLD_FLAG = "N",
                            REWORK_FLAG = CurBatch.WO.ORDER_TYPE == BIZ_MES_WO.ORDER_TYPEs.Rework.GetValue() ? "Y" : "N",
                            FINISHED_FLAG = "N",
                            UNBIND_FLAG = "N",
                            INV_FLAG = "N",
                            DFT_FLAG = "N",
                            DFT_COUNT = 0,
@@ -331,6 +353,7 @@
                        wipSN.POST_CODE = CurPosition.POST_CODE;
                        wipSN.OPER_CODE = curNode.OPER_CODE;
                        wipSN.SEGMENT = curNode.SEGMENT;
                        wipSN.UNBIND_FLAG = "N";
                        wipSN.OPERATION_TIME = DateTime.Now;
                        var curShiftPeriod = GetShiftPeriodForNow();
                        if (!curShiftPeriod.IsNullOrEmpty())
@@ -352,7 +375,7 @@
                        {
                            var woSN = woSNs.First(q => q.WIP_ID == wipSN.ID);
                            woSN.STATUS = BIZ_MES_WO_SN.STATUSs.Finished.GetValue();
                            wipSN.STATUS = MES_WIP_DATA.STATUSs.Finished.GetValue();
                            wipSN.STATUS = MES_WIP_DATA.STATUSs.Output.GetValue();
                            wipSN.OUTLINE_DATE = DateTime.Now;
                        }
                        //如果有提交不良
@@ -398,8 +421,16 @@
                                LOCK_BATCH = wipSN.LOCK_BATCH,
                                WIP_ID = wipSN.ID,
                            };
                            wipSN.Defects.Add(defect);
                            CurDefects.Add(defect);
                        }
                    }
                    //产品被判断不良后下线,并从工单中移除正在生产的条码
                    if (CurDefects.Any() && curNodeSetting.IF_DFT_OFFLINE == "Y")
                    {
                        woStatus.Data.ShortMsg = new("产品下线", ShortMessage.Types.Error);
                        return OfflineIfDefect(action, wipSNs, input.Locale);
                    }
                    //工单开工
@@ -410,7 +441,7 @@
                    CurWipSNHiss.Clear();
                    foreach (var wipSN in wipSNs)
                    {
                        var his = new MES_WIP_HIS(wipSN, $"工单[{wipSN.WORK_ORDER}]条码[{wipSN.SN}]在岗位[{wipSN.POST_CODE}]过站工序[{wipSN.NODE_NAME}]成功");
                        var his = new MES_WIP_HIS(wipSN, $"工单[{wipSN.WORK_ORDER}]条码[{wipSN.SN}]在岗位[{wipSN.POST_CODE}]过站工序[{wipSN.NODE_NAME}]成功(TransId: {TransID} )");
                        wipSN.History.Add(his);
                        CurWipSNHiss.Add(his);
                    }
@@ -556,16 +587,138 @@
        /// <returns></returns>
        public ApiAction<SubmitOutput> DoIfFinishAllSteps(ApiAction<SubmitOutput> action, string locale)
        {
            var operInfo = SetOperNodeInfo(CurOperInfo(locale));
            Action endAction = null;
            //如果当前条码已经走到流程终点则记录条码完工
            if (operInfo.IsReachedEndNode)
            {
                //更新工单条码明细信息
                var woSNs = CurBatch.WoSNs.Where(q => CurWipSNs.Any(w => q.WIP_ID == w.ID)).ToList();
                foreach (var woSN in woSNs)
                {
                    woSN.STATUS = BIZ_MES_WO_SN.STATUSs.Finished.GetValue();
                }
                var curNode = CurBatch.Nodes.First(q => q.OPER_CODE == "EndNode");
                //条码完工
                foreach (var wipSN in CurWipSNs)
                {
                    wipSN.FINISHED_FLAG = "Y";
                    wipSN.NODE_ID = curNode.ID;
                    wipSN.NODE_NAME = curNode.NODE_NAME;
                    wipSN.OPER_CODE = curNode.OPER_CODE;
                    wipSN.SEGMENT = curNode.SEGMENT;
                }
                var wipHiss = new List<MES_WIP_HIS>();
                foreach (var wipSN in CurWipSNs)
                {
                    var his = new MES_WIP_HIS(wipSN, $"工单[{wipSN.WORK_ORDER}]条码[{wipSN.SN}]在岗位[{wipSN.POST_CODE}]过站工序[{wipSN.NODE_NAME}]成功(TransId: {TransID} )");
                    wipSN.History.Add(his);
                    wipHiss.Add(his);
                }
                //创建变量克隆对象用于传入DBSubmitAction中保存当前需要暂存的数据值
                var _woSns = woSNs.Clone();
                var _wipSns = CurWipSNs.Clone();
                var _wipHiss = wipHiss.Clone();
                //保存数据
                endAction = () =>
                {
                    //使用统一的事务DB对象
                    var db = GetCommitDB();
                    //数据保存逻辑
                    db.Storageable(_woSns, UserCode).ExecuteCommand();
                    db.Storageable(_wipSns, UserCode).ExecuteCommand();
                    db.Storageable(_wipHiss, UserCode).ExecuteCommand();
                };
            }
            //保存数据库
            SaveStepsCommitActionToDB();
            SaveStepsCommitActionToDB(endAction);
            //保存成功,返回过站消息
            CurOperInfo(locale).InputQty += CurWipSNs.Count;
            action.Data.OperInfo = SetOperNodeInfo(CurOperInfo(locale));
            operInfo.InputQty += CurWipSNs.Count;
            action.Data.OperInfo = operInfo;
            action.Data.ShortMsg = new(CurWipSNs.Any(q => q.DFT_FLAG == "Y") ? "不良过站" : "良品过站", ShortMessage.Types.Success);
            //action.LocaleMsg = new($"工单[{CurWipSN.WORK_ORDER}]的条码[{CurWipSN.SN}]在岗位[{CurWipSN.POST_CODE}]工序[{CurWipSN.NODE_NAME}]过站成功,状态[{CurWipSN.STATUS.GetEnumDesc<MES_WIP_DATA.STATUSs>()}]");
            action.LocaleMsg = new("MES.Transaction.CollectNode.ScanSn.PassSuccess", CurWipSNs.First().WORK_ORDER, CurSN, CurWipSNs.First().POST_CODE, CurWipSNs.First().NODE_NAME, CurWipSNs.First().STATUS.GetEnumDesc<MES_WIP_DATA.STATUSs>());
            action.LocaleMsg = new("MES.Transaction.TestNode.ScanSn.PassSuccess", CurWipSNs.First().WORK_ORDER, CurSN, CurWipSNs.First().POST_CODE, CurWipSNs.First().NODE_NAME, CurWipSNs.First().STATUS.GetEnumDesc<MES_WIP_DATA.STATUSs>());
            //如果当前条码已经完工,检查当前工单批次和工单是否完工
            if (action.Data.OperInfo.IsReachedEndNode)
            {
                CurBatch.CheckIsComplete(UserCode);
            }
            //重置工序
            ResetNode();
            return action;
        }
        /// <summary>
        /// 产品被判断不良后下线
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public ApiAction<SubmitOutput> OfflineIfDefect(ApiAction<SubmitOutput> action, List<MES_WIP_DATA> wipSNs, string locale)
        {
            var operInfo = SetOperNodeInfo(CurOperInfo(locale));
            operInfo.CurNode = wipSNs.FirstOrDefault()?.NODE_NAME;
            operInfo.NextNode = "下线退库";
            //更新工单条码明细信息
            var woSNs = CurBatch.WoSNs.Where(q => wipSNs.Any(w => q.WIP_ID == w.ID)).ToList();
            foreach (var woSN in woSNs)
            {
                woSN.STATUS = BIZ_MES_WO_SN.STATUSs.Offline.GetValue();
            }
            //条码下线
            foreach (var wipSN in wipSNs)
            {
                wipSN.STATUS = MES_WIP_DATA.STATUSs.Offline.GetValue();
                wipSN.UNBIND_FLAG = "Y";
            }
            var wipHiss = new List<MES_WIP_HIS>();
            foreach (var wipSN in wipSNs)
            {
                var his = new MES_WIP_HIS(wipSN, $"工单[{wipSN.WORK_ORDER}]条码[{wipSN.SN}]在岗位[{wipSN.POST_CODE}]工序[{wipSN.NODE_NAME}]因不良[{wipSN.Defects.FirstOrDefault()?.DFT_NAME}]下线(TransId: {TransID} )");
                wipSN.History.Add(his);
                wipHiss.Add(his);
            }
            //创建变量克隆对象用于传入DBSubmitAction中保存当前需要暂存的数据值
            var _woSns = woSNs.Clone();
            var _wipSns = wipSNs.Clone();
            var _wipHiss = wipHiss.Clone();
            var _defect = CurDefects.Clone();
            //使用统一的事务DB对象
            var db = GetCommitDB();
            //保存数据库
            var dbTran = db.UseTran(() =>
            {
                //数据保存逻辑
                db.Storageable(_woSns, UserCode).ExecuteCommand();
                db.Storageable(_wipSns, UserCode).ExecuteCommand();
                db.Storageable(_wipHiss, UserCode).ExecuteCommand();
                db.Storageable(_defect, UserCode).ExecuteCommand();
            });
            if (!dbTran.IsSuccess)
            {
                //抛出异常
                throw dbTran.ErrorException;
            }
            //从工单中移除正在生产的条码
            CurBatch.RemoveWipSn(wipSNs, UserCode);
            //保存成功,返回过站消息
            action.Data.OperInfo = operInfo;
            action.Data.ShortMsg = new("下线退库", ShortMessage.Types.Warning);
            //action.LocaleMsg = new($"工单[0]的条码[1]在岗位[2]工序[3]因不良[4]下线,状态[5]");
            action.LocaleMsg = new("MES.Transaction.TestNode.OfflineIfDefect", wipSNs.First().WORK_ORDER, CurSN, wipSNs.First().POST_CODE, wipSNs.First().NODE_NAME, wipSNs.First().Defects.FirstOrDefault()?.DFT_NAME, wipSNs.First().STATUS.GetEnumDesc<MES_WIP_DATA.STATUSs>());
            //重置工序
            ResetNode();
            return action;
@@ -587,6 +740,8 @@
                return action;
            }
            action.Data = CurBatch.GetNodeDefects(PostCode);
            //action.Data = new List<DefectOutput> { new DefectOutput { DFTG_CODE = "111111"} };
            //action.Data = new List<DefectOutput> { new DefectOutput { DFTG_CODE = "222222" } };
            return action;
        }