From fb726bc28268c7efb73629f6ae12b25232a18538 Mon Sep 17 00:00:00 2001
From: Rodney Chen <rodney.chen@hotmail.com>
Date: 星期六, 22 二月 2025 15:34:20 +0800
Subject: [PATCH] Add OPERATION_END property and update database handling

---
 Tiger.Business.MES/Transaction/YadaPacking.cs |  154 ++++++++++++++++++++++++++++++---------------------
 1 files changed, 90 insertions(+), 64 deletions(-)

diff --git a/Tiger.Business.MES/Transaction/YadaPacking.cs b/Tiger.Business.MES/Transaction/YadaPacking.cs
index 7ea9974..72b1ea5 100644
--- a/Tiger.Business.MES/Transaction/YadaPacking.cs
+++ b/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)
         {
-            curtime = prevtime = DateTime.Now; Logger.Console.Info($"{CurSN} : ***** 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();
-                        curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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();
-                            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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);
-                                curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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($"姝e湪鍖呰宸ュ崟鎵规[{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();
-                        curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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);
-                            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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);
-                            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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);
-                                curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 8 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
+                                debug.Log(new StackFrame(true), "***** 8", info => { ConsoleExt.WriteLine(info.ToString()); });
                             }
                             //濡傛灉褰撳墠宸ユ鏈畬鎴�
                             else
@@ -293,11 +298,14 @@
             {
                 action.Data.ShortMsg = new("宸ュ簭寮傚父", ShortMessage.Types.Error);
                 action.Data.OperInfo = CurOperInfo(input.Locale);
+                RemoveCurSnFromCurPackage(Context.ContainsKey("CurPackage") ? (Context["CurPackage"] as WipPkg).Item : null);
+                ResetNode();
                 //action.CatchExceptionWithLog(ex, $"鍖呰宸ュ簭锛氭彁浜ゆ搷浣滄暟鎹紓甯�");
                 action.CatchExceptionWithLog(ex, Biz.L("MES.Transaction.PackingNode.SubmitException"));
+
             }
-            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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 +316,7 @@
         /// <returns></returns>
         public ApiAction<SubmitOutput> NodeSubmit(ApiAction<SubmitOutput> action, SubmitInput input)
         {
-            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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 +343,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()
@@ -392,52 +400,44 @@
 
                     //璁剧疆褰撳墠鎸囦护涓烘甯镐骇鍝佽繃绔�
                     NodeCmd = "NodeSubmit";
-                    curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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();
-                    int CurShipQty = 0;
+                    //var ship = U9CDB.Queryable<mes_ShipList>().Where(q => q.MoDoc == CurBatch.WO.ORDER_NO && q.Status == 2 && SqlFunc.DateIsSame(q.BusinessDate, DateTime.Now)).First();
+                    var ships = U9CDB.Queryable<mes_ShipList>().Where(q => q.MoDoc == CurBatch.WO.ORDER_NO).ToList();
+                    var shippingOrders = ships.Where(q=> q.Status != 2).Select(x=>x.ShipDoc).ToList();
+                    var oldShipment = ships.Any(q => q.Status != 2) ? ships.Where(x => x.Status != 2).Sum(t => t.ShipQty) : 0;
+                    var ship = ships.Where(q => q.Status == 2 && q.BusinessDate.Date == DateTime.Now.Date).FirstOrDefault();
+                    decimal CurShipQty = 0;
                     int WaitShipmentCarton = 0;
                     int WaitShipment = 0;
-                    if (ship.IsNullOrEmpty()) { IsPrintCustomerLabel = false; } else
+                    int oWaitShipmentCarton = 0;
+                    if (ship.IsNullOrEmpty()) { IsPrintCustomerLabel = false; }
+                    else
                     {
                         CurShipQty = ship.ShipQty;
                         IsPrintCustomerLabel = true;
-                        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();
+                        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 sublist = list.Where(q=> shippingOrders.Contains(q["SHIPPING_ORDER"]) && q["ProdDate"].ToString() != "Whole").ToList();
+                            oWaitShipmentCarton = sublist.IsNullOrEmpty() ? 0 : sublist.Sum(q => q["WaitShipmentCarton"].ToInt32());
                             var batchCount = list.Where(q => q["ProdDate"].ToString() == "Whole").FirstOrDefault();
-                            WaitShipmentCarton = batchCount["WaitShipmentCarton"].ToInt32();
+                            WaitShipmentCarton = batchCount["WaitShipmentCarton"].ToInt32()- oWaitShipmentCarton;
                             WaitShipment = batchCount["WaitShipment"].ToInt32();
-                            IsPrintCustomerLabel = batchCount.IsNullOrEmpty() ? IsPrintCustomerLabel : batchCount["WaitShipment"].ToInt32() >= ship.ShipQty ? false : true;
+                            IsPrintCustomerLabel = batchCount.IsNullOrEmpty() ? IsPrintCustomerLabel : batchCount["WaitShipment"].ToInt32() - oldShipment >= ship.ShipQty ? false : true;
                         }
                     }
                     ShippingOrder = IsPrintCustomerLabel ? ship?.ShipDoc ?? "" : "";
                     //缂撳瓨鍑鸿揣鏁伴噺
+                    Context.SetOrAdd("CuroldShipment", oldShipment);
                     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);
-                    //}
 
-                    curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 12 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
+                    debug.Log(new StackFrame(true), "***** 12", info => { ConsoleExt.WriteLine(info.ToString()); });
                     //缁戝畾鏉$爜鍒板伐鍗�
                     foreach (var wipSN in wipSNs)
                     {
@@ -480,6 +480,7 @@
                         wipSN.OPER_CODE = curNode.OPER_CODE;
                         wipSN.SEGMENT = curNode.SEGMENT;
                         wipSN.OPERATION_TIME = DateTime.Now;
+                        wipSN.OPERATION_END = DateTime.Now;
                         wipSN.SHIPPING_ORDER = ShippingOrder;
                         wipSN.UNBIND_FLAG = "N";
                         var curShiftPeriod = GetShiftPeriodForNow();
@@ -581,7 +582,7 @@
                     var _curNodeSetting = curNodeSetting.Clone();
                     var _Batch = CurBatch.Batch.Clone();
 
-                    curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 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)
@@ -701,11 +702,12 @@
             catch (Exception ex)
             {
                 action.Data.ShortMsg = new("宸ユ寮傚父", ShortMessage.Types.Error);
+                RemoveCurSnFromCurPackage(Context.ContainsKey("CurPackage") ? (Context["CurPackage"] as WipPkg).Item : null);
                 ResetNode();
                 //action.CatchExceptionWithLog(ex, $"{curNode.NODE_NAME}锛氬伐搴忚妭鐐瑰伐姝ユ彁浜ゆ暟鎹紓甯革紝璇锋鏌ュ伐搴忚妭鐐硅缃�");
                 action.CatchExceptionWithLog(ex, Biz.L("MES.Transaction.PackingNode.NodeSubmitException", curNode.NODE_NAME));
             }
-            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 13.1 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
+            debug.Log(new StackFrame(true), "***** 13.1", info => { ConsoleExt.WriteLine(info.ToString()); });
             return action;
         }
 
@@ -719,7 +721,7 @@
             var curPkg = Context.ContainsKey("CurPackage") ? Context["CurPackage"] as WipPkg : null;
             var operInfo = SetOperNodeInfo(CurOperInfo(locale));
             Action endAction = null;
-            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 14 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
+            debug.Log(new StackFrame(true), "***** 14", info => { ConsoleExt.WriteLine(info.ToString()); });
 
             //濡傛灉褰撳墠鏉$爜宸茬粡璧板埌娴佺▼缁堢偣鍒欒褰曟潯鐮佸畬宸�
             if (operInfo.IsReachedEndNode)
@@ -745,6 +747,7 @@
                     wipSN.OPER_CODE = curNode.OPER_CODE;
                     wipSN.SEGMENT = curNode.SEGMENT;
                     wipSN.OPERATION_TIME = DateTime.Now;
+                    wipSN.OPERATION_END = DateTime.Now;
                 }
 
                 var wipHiss = new List<MES_WIP_HIS>();
@@ -768,7 +771,7 @@
                     var db = GetCommitDB();
                     //鏁版嵁淇濆瓨閫昏緫
                     db.Updateable(_woSns, UserCode).UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.STATUS }).ExecuteCommand();
-                    db.Updateable(_wipSns, UserCode).UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.STATUS, q.NODE_ID, q.NODE_NAME, q.OPER_CODE, q.SEGMENT, q.OPERATION_TIME }).ExecuteCommand();
+                    db.Updateable(_wipSns, UserCode).UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.STATUS, q.NODE_ID, q.NODE_NAME, q.OPER_CODE, q.SEGMENT, q.OPERATION_TIME, q.OPERATION_END }).ExecuteCommand();
                     db.Storageable(_wipHiss, UserCode).ExecuteCommand();
                     if (_pkgList.Any())
                     {
@@ -784,10 +787,10 @@
                     }
                 };
             }
-
+            //throw new Exception("鍝�");
             //淇濆瓨鏁版嵁搴�
             SaveStepsCommitActionToDB(endAction);
-            
+
             //淇濆瓨鎴愬姛锛岃繑鍥炶繃绔欐秷鎭�
             operInfo.InputQty += CurWipSNs.Count;
             action.Data.Data = new PackingActionOutput() { PkgInfo = curPkg };
@@ -804,7 +807,7 @@
 
             //閲嶇疆宸ュ簭
             ResetNode();
-            curtime = DateTime.Now; Logger.Console.Info($"{CurSN} : ***** 15 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
+            debug.Log(new StackFrame(true), "***** 15", info => { ConsoleExt.WriteLine(info.ToString()); });
             return action;
         }
 
@@ -904,7 +907,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";
@@ -937,7 +940,7 @@
                 action.CatchExceptionWithLog(ex, $"鍖呰宸ュ簭锛氭墜鍔ㄧ粨鏉熷寘瑁呭紓甯�");
                 NodeCmd = null;
             }
-            return action;
+            return SetOutPutMqttMsg(action);
         }
 
         /// <summary>
@@ -951,9 +954,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_PROCESS_VAR>().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",
@@ -977,9 +980,12 @@
             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() };
+                action.Data = new ShipingInfo()
+                {
+                    IsPrintCustomerLabel = IsPrintCustomerLabel,
+                    OldShipQty = Context.GetOrDefault("CuroldShipment").ToInt32(),
+                    ShipQty = Context.GetOrDefault("CurWaitShipment").ToInt32() - Context.GetOrDefault("CuroldShipment").ToInt32() >= Context.GetOrDefault("CurShipQty").ToInt32() ? CurBatch.Batch.PLAN_QTY - Context.GetOrDefault("CuroldShipment").ToInt32() - Context.GetOrDefault("CurShipQty").ToInt32() : Context.GetOrDefault("CurShipQty").ToInt32()
+                };
             }
             catch (Exception ex)
             {
@@ -999,6 +1005,26 @@
             NodeCmd = null;
         }
 
+        public void RemoveCurSnFromCurPackage(WipPkgItem pkgItem)
+        {
+            if (!pkgItem.IsNullOrEmpty())
+            {
+                var delItem = pkgItem.Items.Where(q => CurWipSNs.Any(sn => q.WipSNs.ContainsKey(sn.ID))).ToList();
+                if (delItem.Any())
+                {
+                    pkgItem.Items.RemoveAll(q => CurWipSNs.Any(sn => q.WipSNs.ContainsKey(sn.ID)));
+                    pkgItem.IsFinished = false;
+                }
+                else
+                {
+                    foreach (var item in pkgItem.Items)
+                    {
+                        RemoveCurSnFromCurPackage(item);
+                    }
+                }
+            }
+        }
+
         public override bool Close(bool needSaveHistoryLog = false)
         {
             //needSaveHistoryLog = true;

--
Gitblit v1.9.3