服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-12-03 28137093fdd7ab9ddd4edfe1774a6f7024166730
Tiger.Business.MES/Transaction/YadaPacking.cs
@@ -13,6 +13,7 @@
using Tiger.Business.MES.WorkAction;
using Tiger.Model.MES.Yada;
using System.Data;
using System.Diagnostics;
namespace Tiger.Business.MES.Transaction
{
@@ -24,6 +25,7 @@
        public new IYadaPacking Init(string id, string apiHost, string userCode, string postCode)
        {
            base.Init(id, apiHost, userCode, postCode);
            U9CDB = Biz.DataSource["YadaU9C"].Client;
            Logger.Console.Info($"User[{userCode}] start a {this.GetType().Name}[{postCode}] Transaction[ID: {TransID}]");
            return this;
        }
@@ -39,6 +41,8 @@
        public PackingAction TempPkgAction { get; set; }
        public bool IsPrintCustomerLabel { get; set; }
        public string ShippingOrder { get; set; }
        public DbClient U9CDB { get; set; }
        public Model.TraceDebug debug { get; set; }
        public DateTime curtime = DateTime.Now;
        public DateTime prevtime = DateTime.Now;
        #endregion Propertys & Variables
@@ -51,7 +55,8 @@
        /// <returns></returns>
        public async Task<ApiAction<SubmitOutput>> Submit(SubmitInput input)
        {
            ConsoleExt.WriteLine($"{curtime = prevtime = DateTime.Now:HH:mm:ss} ***** 1 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            debug = new(new StackFrame(true));
            debug.Log(new StackFrame(true), "***** 1", info => { ConsoleExt.WriteLine(info.ToString()); });
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
            try
            {
@@ -63,13 +68,13 @@
                //    action.IsSuccessed = false;
                //    action.LocaleMsg = new($"包装数量[{input.Qty}]不正确,请重新输入数量后继续包装操作");
                //    //action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.QtyError", input.Qty);
                //    return action;
                //    return SetOutPutMqttMsg(action, input.Locale);
                //}
                if (input.Options.ContainsKey("IsPrintCustomerLabel"))
                {
                    IsPrintCustomerLabel = input.Options["IsPrintCustomerLabel"].ToBoolean();
                }
                //if (input.Options.ContainsKey("IsPrintCustomerLabel"))
                //{
                //    IsPrintCustomerLabel = input.Options["IsPrintCustomerLabel"].ToBoolean();
                //}
                //if (input.Options.ContainsKey("ShippingOrder"))
                //{
                //    ShippingOrder = IsPrintCustomerLabel ? input.Options["ShippingOrder"].ToString() : "";
@@ -90,17 +95,17 @@
                {
                    //工步列表为空或者工序节点工步有未完成时,优先完成工序节点工步
                    if (Steps.IsNullOrEmpty() || !IsFinishNodeSteps)
                    {
                        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))
                    {
                        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();
                        ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 2 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                        debug.Log(new StackFrame(true), "***** 2", info => { ConsoleExt.WriteLine(info.ToString()); });
                        //先判断当前工单不为空且当前岗位在当前工单是不是首站,如果是则不允许变更当前工单,尝试把条码绑定到当前工单
                        if (!CurBatch.IsNullOrEmpty() && CurBatch.IsFirstNode(PostCode))
                        {
                            //更新CurBatch
                            CurBatch?.Update();
                            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 3 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                            debug.Log(new StackFrame(true), "***** 3", info => { ConsoleExt.WriteLine(info.ToString()); });
                            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())
                            {
@@ -114,7 +119,7 @@
                            {
                                //提交数据
                                action = NodeSubmit(action, input);
                                ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 4 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                                debug.Log(new StackFrame(true), "***** 4", info => { ConsoleExt.WriteLine(info.ToString()); });
                                //更新工序信息
                                if (!action.IsSuccessed)
                                {
@@ -142,7 +147,7 @@
                                    action.IsSuccessed = false;
                                    //action.LocaleMsg = new($"条码[{0}]是载具条码,请扫描载具中的产品条码继续包装操作");
                                    action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.OnlyMinPackage", input.SN);
                                    return action;
                                    return SetOutPutMqttMsg(action, input.Locale);
                                }
                                var curPkg = Context.ContainsKey("CurPackage") ? Context["CurPackage"] as WipPkg : null;
                                if (!curPkg.IsNullOrEmpty() && !curPkg.IsFinished && curPkg.WorkBatch != curSNs.First().Batch.BATCH_NO)
@@ -152,7 +157,7 @@
                                    action.IsSuccessed = false;
                                    //action.LocaleMsg = new($"正在包装工单批次[{0}]的产品,请先完成当前包装后再扫描其他批次[{1}]的产品[{2}]");
                                    action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.BatchError", curPkg.WorkBatch, curSNs.First().Batch.BATCH_NO, input.SN);
                                    return action;
                                    return SetOutPutMqttMsg(action, input.Locale);
                                }
                                if (curSNs.First().Batch.ACT_LINE != CurLine.LINE_CODE)
                                {
@@ -174,7 +179,7 @@
                                            action.Data.OperInfo = new();
                                            action.IsSuccessed = result.IsSuccessed;
                                            action.LocaleMsg = result.LocaleMsg;
                                            return action;
                                            return SetOutPutMqttMsg(action, input.Locale);
                                        }
                                    }
                                    //更新CurBatch
@@ -235,23 +240,23 @@
                    {
                        //更新CurBatch
                        CurBatch?.Update();
                        ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 5 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                        debug.Log(new StackFrame(true), "***** 5", info => { ConsoleExt.WriteLine(info.ToString()); });
                        var submitStep = Steps.Where(q => q.ID == input.CurStepID && !q.IsFinished).FirstOrDefault();
                        //如果客户端返回的当前工步ID找不到未完成的行为工步,则直接开始下一个行为工步
                        if (submitStep.IsNullOrEmpty())
                        {
                            action = BeginNextActionStep(input);
                            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 6 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                            debug.Log(new StackFrame(true), "***** 6", info => { ConsoleExt.WriteLine(info.ToString()); });
                        }
                        else
                        {
                            action = submitStep.Submit(input);
                            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 7 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                            debug.Log(new StackFrame(true), "***** 7", info => { ConsoleExt.WriteLine(info.ToString()); });
                            //如果当前工步已完成,开始执行下一工步
                            if (action.IsSuccessed && submitStep.IsFinished)
                            {
                                action = BeginNextActionStep(input);
                                ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 8 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                                debug.Log(new StackFrame(true), "***** 8", info => { ConsoleExt.WriteLine(info.ToString()); });
                            }
                            //如果当前工步未完成
                            else
@@ -296,8 +301,8 @@
                //action.CatchExceptionWithLog(ex, $"包装工序:提交操作数据异常");
                action.CatchExceptionWithLog(ex, Biz.L("MES.Transaction.PackingNode.SubmitException"));
            }
            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 9 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            return action;
            debug.Log(new StackFrame(true), "***** 9", info => { ConsoleExt.WriteLine(info.ToString()); });
            return SetOutPutMqttMsg(action, input.Locale);
        }
        /// <summary>
@@ -308,7 +313,7 @@
        /// <returns></returns>
        public ApiAction<SubmitOutput> NodeSubmit(ApiAction<SubmitOutput> action, SubmitInput input)
        {
            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 10 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            debug.Log(new StackFrame(true), "***** 10", info => { ConsoleExt.WriteLine(info.ToString()); });
            var curNode = CurBatch.GetNode(PostCode);
            try
            {
@@ -335,7 +340,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).Where(q => q.FINISHED_FLAG != "Y").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()
@@ -344,6 +349,7 @@
                            FLOW_SN = input.SN,
                            STATUS = MES_WIP_DATA.STATUSs.Init.GetValue(),
                            ITEM_CODE = CurBatch.WO.ITEM_CODE,
                            QTY = 1,
                            WORK_ORDER = CurBatch.Batch.ORDER_NO,
                            BATCH_NO = CurBatch.Batch.BATCH_NO,
                            FTY_CODE = CurFactory.FTY_CODE,
@@ -392,22 +398,52 @@
                    //设置当前指令为正常产品过站
                    NodeCmd = "NodeSubmit";
                    ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 11 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                    debug.Log(new StackFrame(true), "***** 11", info => { ConsoleExt.WriteLine(info.ToString()); });
                    //获取出货状态
                    DbClient db = Biz.DataSource["YadaU9C"].Client;
                    var ship = db.Queryable<mes_ShipList>().Where(q => q.MoDoc == CurBatch.WO.ORDER_NO && q.Status == 2 && SqlFunc.DateIsSame(q.BusinessDate, DateTime.Now)).First();
                    if (ship.IsNullOrEmpty()) { IsPrintCustomerLabel = false; } else
                    var ship = U9CDB.Queryable<mes_ShipList>().Where(q => q.MoDoc == CurBatch.WO.ORDER_NO && q.Status == 2 && SqlFunc.DateIsSame(q.BusinessDate, DateTime.Now)).First();
                    int CurShipQty = 0;
                    int WaitShipmentCarton = 0;
                    int WaitShipment = 0;
                    if (ship.IsNullOrEmpty()) { IsPrintCustomerLabel = false; }
                    else
                    {
                        SugarParameter[] pars = Biz.Db.Ado.GetParameters(new { BATCH = CurBatch.Batch.BATCH_NO, NodeIdOrName = curNode.NODE_NAME });
                        var list = Biz.Db.Ado.UseStoredProcedure().GetDataTable("SP_MES_GET_NODE_BATCH_COUNT", pars).AsEnumerable().ToList();
                        CurShipQty = ship.ShipQty;
                        IsPrintCustomerLabel = true;
                        SugarParameter[] pars = MainDB.Ado.GetParameters(new { BATCH = CurBatch.Batch.BATCH_NO, NodeIdOrName = curNode.NODE_NAME });
                        var list = MainDB.Ado.UseStoredProcedure().GetDataTable("SP_MES_GET_NODE_BATCH_COUNT", pars).AsEnumerable().ToList();
                        if (!list.IsNullOrEmpty())
                        {
                            var batchCount = list.Where(q => q["ProdDate"].ToString() == "Whole").FirstOrDefault();
                            IsPrintCustomerLabel = batchCount.IsNullOrEmpty() ? false : batchCount["WaitShipment"].ToInt32() >= ship.ShipQty ? false : true;
                            WaitShipmentCarton = batchCount["WaitShipmentCarton"].ToInt32();
                            WaitShipment = batchCount["WaitShipment"].ToInt32();
                            IsPrintCustomerLabel = batchCount.IsNullOrEmpty() ? IsPrintCustomerLabel : batchCount["WaitShipment"].ToInt32() >= ship.ShipQty ? false : true;
                        }
                    }
                    ShippingOrder = IsPrintCustomerLabel ? ship?.ShipDoc ?? "" : "";
                    ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 12 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                    //缓存出货数量
                    Context.SetOrAdd("CurShipQty", CurShipQty);
                    //if (Context.ContainsKey("CurShipQty"))
                    //{
                    //    Context["CurShipQty"] = CurShipQty;
                    //}
                    //else
                    //{
                    //    Context.Add("CurShipQty", CurShipQty);
                    //}
                    //缓存出货箱数
                    Context.SetOrAdd("CurWaitShipmentCarton", WaitShipmentCarton);
                    Context.SetOrAdd("CurWaitShipment", WaitShipment);
                    //if (Context.ContainsKey("CurWaitShipmentCarton"))
                    //{
                    //    Context["CurWaitShipmentCarton"] = WaitShipmentCarton;
                    //}
                    //else
                    //{
                    //    Context.Add("CurWaitShipmentCarton", WaitShipmentCarton);
                    //}
                    debug.Log(new StackFrame(true), "***** 12", info => { ConsoleExt.WriteLine(info.ToString()); });
                    //绑定条码到工单
                    foreach (var wipSN in wipSNs)
                    {
@@ -421,6 +457,7 @@
                                FLOW_SN = wipSN.SN,
                                TRAY_SN = wipSN.TRAY_SN,
                                STATUS = BIZ_MES_WO_SN.STATUSs.NotInput.GetValue(),
                                QTY = 1,
                            });
                        }
                    }
@@ -551,7 +588,7 @@
                    var _curNodeSetting = curNodeSetting.Clone();
                    var _Batch = CurBatch.Batch.Clone();
                    ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 13 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                    debug.Log(new StackFrame(true), "***** 13", info => { ConsoleExt.WriteLine(info.ToString()); });
                    //初始化工步列表
                    Steps.Clear();
                    var curStep = new WorkStep(IWorkStep.NodeTypes.Node, this)
@@ -675,7 +712,7 @@
                //action.CatchExceptionWithLog(ex, $"{curNode.NODE_NAME}:工序节点工步提交数据异常,请检查工序节点设置");
                action.CatchExceptionWithLog(ex, Biz.L("MES.Transaction.PackingNode.NodeSubmitException", curNode.NODE_NAME));
            }
            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 13.1 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            debug.Log(new StackFrame(true), "***** 13.1", info => { ConsoleExt.WriteLine(info.ToString()); });
            return action;
        }
@@ -689,7 +726,7 @@
            var curPkg = Context.ContainsKey("CurPackage") ? Context["CurPackage"] as WipPkg : null;
            var operInfo = SetOperNodeInfo(CurOperInfo(locale));
            Action endAction = null;
            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 14 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            debug.Log(new StackFrame(true), "***** 14", info => { ConsoleExt.WriteLine(info.ToString()); });
            //如果当前条码已经走到流程终点则记录条码完工
            if (operInfo.IsReachedEndNode)
@@ -757,7 +794,6 @@
            //保存数据库
            SaveStepsCommitActionToDB(endAction);
            ConsoleExt.WriteLine($"{curtime = DateTime.Now:HH:mm:ss} ***** 15 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            //保存成功,返回过站消息
            operInfo.InputQty += CurWipSNs.Count;
@@ -775,7 +811,7 @@
            //重置工序
            ResetNode();
            debug.Log(new StackFrame(true), "***** 15", info => { ConsoleExt.WriteLine(info.ToString()); });
            return action;
        }
@@ -875,7 +911,7 @@
                            action.IsSuccessed = false;
                            //action.LocaleMsg = new($"出货数量设定为[{0}],目前包装了[{1}],包装尾数的数量必需是[{2}],请装够数量后再结束包装");
                            action.LocaleMsg = new("MES.Transaction.PackingNode.NotMatchShipQty", shipQty, curQty, shipQty % ruleQty);
                            return action;
                            return SetOutPutMqttMsg(action);
                        }
                        NodeCmd = "PkgSubmit";
@@ -908,7 +944,7 @@
                action.CatchExceptionWithLog(ex, $"包装工序:手动结束包装异常");
                NodeCmd = null;
            }
            return action;
            return SetOutPutMqttMsg(action);
        }
        /// <summary>
@@ -922,9 +958,9 @@
            try
            {
                var CurPkg = Context.ContainsKey("CurPackage") ? Context["CurPackage"] as WipPkg : null;
                var LabelPV = Biz.Db.Queryable<BAS_LABEL_PV>().ToList();
                var label = CurPkg.Item.LABEL_CODE.IsNullOrEmpty() ? null : Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(q => q.LABEL_CODE == CurPkg.Item.LABEL_CODE).IncludesAllFirstLayer().First();
                var printLb =  SetLabelVariables(LabelPV, label, new PackingAction(), CurPkg);
                var LabelPV = MainDB.Queryable<BAS_LABEL_PV>().ToList();
                var label = CurPkg.Item.LABEL_CODE.IsNullOrEmpty() ? null : MainDB.Queryable<BAS_LABEL_TEMP>().Where(q => q.LABEL_CODE == CurPkg.Item.LABEL_CODE).IncludesAllFirstLayer().First();
                var printLb = SetLabelVariables(LabelPV, label, new PackingAction(), CurPkg);
                action.Data = new PackingActionOutput()
                {
                    ExecCode = "PrintTest",
@@ -939,6 +975,28 @@
            return action;
        }
        /// <summary>
        /// 获取是否打印客户标签还是入库标签
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction<ShipingInfo>> GetIsCustomerLabelAndShipQty()
        {
            var action = new ApiAction<ShipingInfo>();
            try
            {
                action.Data = new ShipingInfo()
                {
                    IsPrintCustomerLabel = IsPrintCustomerLabel,
                    ShipQty = Context.GetOrDefault("CurWaitShipment").ToInt32() >= Context.GetOrDefault("CurShipQty").ToInt32() ? CurBatch.Batch.PLAN_QTY - Context.GetOrDefault("CurShipQty").ToInt32() : Context.GetOrDefault("CurShipQty").ToInt32()
                };
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"包装工序:获取是否打印客户标签还是入库标签异常");
            }
            return action;
        }
        #endregion Functions
        /// <summary>