From eb64567d5da7d6a5c80b7579b486234b89b0e6ee Mon Sep 17 00:00:00 2001
From: Ben Lin <maobin001@msn.com>
Date: 星期一, 25 十一月 2024 15:19:01 +0800
Subject: [PATCH] 包装完成标志

---
 Tiger.Business.MES/Transaction/YadaPacking.cs |  167 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 149 insertions(+), 18 deletions(-)

diff --git a/Tiger.Business.MES/Transaction/YadaPacking.cs b/Tiger.Business.MES/Transaction/YadaPacking.cs
index e1fef5d..a186824 100644
--- a/Tiger.Business.MES/Transaction/YadaPacking.cs
+++ b/Tiger.Business.MES/Transaction/YadaPacking.cs
@@ -11,6 +11,8 @@
 using Tiger.Model;
 using Tiger.Model.Entitys.MES.Position;
 using Tiger.Business.MES.WorkAction;
+using Tiger.Model.MES.Yada;
+using System.Data;
 
 namespace Tiger.Business.MES.Transaction
 {
@@ -36,6 +38,7 @@
         public string NodeCmd { get; set; }
         public PackingAction TempPkgAction { get; set; }
         public bool IsPrintCustomerLabel { get; set; }
+        public string ShippingOrder { get; set; }
         #endregion Propertys & Variables
 
         #region Functions
@@ -49,20 +52,25 @@
             var action = new ApiAction<SubmitOutput>(new SubmitOutput());
             try
             {
-                if (input.Qty.ToInt32() <= 0)
-                {
-                    action.Data.ShortMsg = new("璇疯緭鍏ュ寘瑁呮暟閲�", ShortMessage.Types.Error);
-                    action.Data.OperInfo = new();
-                    action.Data.Data = "QtyError";
-                    action.IsSuccessed = false;
-                    action.LocaleMsg = new($"鍖呰鏁伴噺[{input.Qty}]涓嶆纭紝璇烽噸鏂拌緭鍏ユ暟閲忓悗缁х画鍖呰鎿嶄綔");
-                    //action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.QtyError", input.Qty);
-                    return action;
-                }
+                //if (input.Qty.ToInt32() <= 0)
+                //{
+                //    action.Data.ShortMsg = new("璇疯緭鍏ュ寘瑁呮暟閲�", ShortMessage.Types.Error);
+                //    action.Data.OperInfo = new();
+                //    action.Data.Data = "QtyError";
+                //    action.IsSuccessed = false;
+                //    action.LocaleMsg = new($"鍖呰鏁伴噺[{input.Qty}]涓嶆纭紝璇烽噸鏂拌緭鍏ユ暟閲忓悗缁х画鍖呰鎿嶄綔");
+                //    //action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.QtyError", input.Qty);
+                //    return action;
+                //}
                 if (input.Options.ContainsKey("IsPrintCustomerLabel"))
                 {
                     IsPrintCustomerLabel = input.Options["IsPrintCustomerLabel"].ToBoolean();
                 }
+          
+                //if (input.Options.ContainsKey("ShippingOrder"))
+                //{
+                //    ShippingOrder = IsPrintCustomerLabel ? input.Options["ShippingOrder"].ToString() : "";
+                //}
                 //PkgSubmit锛氭墜鍔ㄧ粨鏉熷寘瑁咃紝鎵嬪姩淇濆瓨鏆傚瓨鐨勫寘瑁呮暟鎹�
                 if (NodeCmd == "PkgSubmit")
                 {
@@ -318,7 +326,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.STATUS < MES_WIP_DATA.STATUSs.Finished.GetValue()).ToList();
+                    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();
                     if (wipSNs.IsNullOrEmpty())
                     {
                         var wipSN = new MES_WIP_DATA()
@@ -376,6 +384,21 @@
                     //璁剧疆褰撳墠鎸囦护涓烘甯镐骇鍝佽繃绔�
                     NodeCmd = "NodeSubmit";
 
+                    //鑾峰彇鍑鸿揣鐘舵��
+                    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
+                    {
+                        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();
+                        if (!list.IsNullOrEmpty())
+                        {
+                            var batchCount = list.Where(q => q["ProdDate"].ToString() == "Whole").FirstOrDefault();
+                            IsPrintCustomerLabel = batchCount.IsNullOrEmpty() ? false : batchCount["WaitShipment"].ToInt32() >= ship.ShipQty ? false : true;
+                        }
+                    }
+                    ShippingOrder = IsPrintCustomerLabel ? ship?.ShipDoc ?? "" : "";
+
                     //缁戝畾鏉$爜鍒板伐鍗�
                     foreach (var wipSN in wipSNs)
                     {
@@ -418,6 +441,8 @@
                         wipSN.OPER_CODE = curNode.OPER_CODE;
                         wipSN.SEGMENT = curNode.SEGMENT;
                         wipSN.OPERATION_TIME = DateTime.Now;
+                        wipSN.SHIPPING_ORDER = ShippingOrder;
+                        wipSN.UNBIND_FLAG = "N";
                         var curShiftPeriod = GetShiftPeriodForNow();
                         if (!curShiftPeriod.IsNullOrEmpty())
                         {
@@ -438,7 +463,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;
                         }
                         //濡傛灉鏈夋彁浜や笉鑹�
@@ -484,8 +509,16 @@
                                 OBA_BATCH = wipSN.OBA_BATCH,
                                 LOCK_BATCH = wipSN.LOCK_BATCH,
                             };
+                            wipSN.Defects.Add(defect);
                             CurDefects.Add(defect);
                         }
+                    }
+
+                    //浜у搧琚垽鏂笉鑹悗涓嬬嚎锛屽苟浠庡伐鍗曚腑绉婚櫎姝e湪鐢熶骇鐨勬潯鐮�
+                    if (CurDefects.Any() && curNodeSetting.IF_DFT_OFFLINE == "Y")
+                    {
+                        woStatus.Data.ShortMsg = new("浜у搧涓嬬嚎", ShortMessage.Types.Error);
+                        return OfflineIfDefect(action, wipSNs, input.Locale);
                     }
 
                     //宸ュ崟寮�宸�
@@ -664,7 +697,7 @@
                 //鏉$爜瀹屽伐
                 foreach (var wipSN in CurWipSNs)
                 {
-                    wipSN.STATUS = MES_WIP_DATA.STATUSs.Finished.GetValue();
+                    wipSN.FINISHED_FLAG = "Y";
                     wipSN.NODE_ID = curNode.ID;
                     wipSN.NODE_NAME = curNode.NODE_NAME;
                     wipSN.OPER_CODE = curNode.OPER_CODE;
@@ -684,7 +717,8 @@
                 var _woSns = woSNs.Clone();
                 var _wipSns = CurWipSNs.Clone();
                 var _wipHiss = wipHiss.Clone();
-                var _pkgList = curPkg?.IsFinished == true ? GetPackageList(curPkg.Item) : new();
+                var _pkgList = curPkg?.IsFinished == true ? GetPackageList(curPkg.Item).Clone() : new();
+                var _wipIDs = curPkg?.IsFinished == true ? curPkg.Item.GetWipSnList().Select(q => q.Key).ToList().Clone() : new();
                 //淇濆瓨鏁版嵁
                 endAction = () =>
                 {
@@ -696,14 +730,14 @@
                     db.Storageable(_wipHiss, UserCode).ExecuteCommand();
                     if (_pkgList.Any())
                     {
-                        var wipIDs = _pkgList.Where(q => !q.WIP_ID.IsNullOrEmpty()).Select(q => q.WIP_ID).ToList();
+                        //var wipIDs = _pkgList.Where(q => !q.WIP_ID.IsNullOrEmpty()).Select(q => q.WIP_ID).ToList();
                         var wo = _pkgList.First().WORK_ORDER;
                         //鍖呰淇℃伅淇濆瓨鍒扮敓浜ц繃绋嬭褰�
                         db.Updateable<MES_WIP_HIS>(UserCode)
                             .SetColumns(q => q.TRAY_SN == curPkg.Item.Package.SN)
                             .SetColumns(q => q.INNER_SN == q.CARTON_SN)
                             .SetColumns(q => q.CARTON_SN == curPkg.Item.Package.SN)
-                            .Where(q => q.WORK_ORDER == wo && q.OPER_CODE == "EndNode" && wipIDs.Contains(q.WIP_ID))
+                            .Where(q => q.WORK_ORDER == wo && q.OPER_CODE == "EndNode" && _wipIDs.Contains(q.WIP_ID))
                             .ExecuteCommand();
                     }
                 };
@@ -713,7 +747,7 @@
             SaveStepsCommitActionToDB(endAction);
 
             //淇濆瓨鎴愬姛锛岃繑鍥炶繃绔欐秷鎭�
-            CurOperInfo(locale).InputQty += CurWipSNs.Count;
+            operInfo.InputQty += CurWipSNs.Count;
             action.Data.Data = new PackingActionOutput() { PkgInfo = curPkg };
             action.Data.OperInfo = operInfo;
             action.Data.ShortMsg = new(CurWipSNs.Any(q => q.DFT_FLAG == "Y") ? "涓嶈壇杩囩珯" : "鑹搧杩囩珯", ShortMessage.Types.Success);
@@ -729,6 +763,75 @@
             //閲嶇疆宸ュ簭
             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}]涓嬬嚎锛圱ransId锛� {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();
+            //浣跨敤缁熶竴鐨勪簨鍔B瀵硅薄
+            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;
+            }
+
+            //浠庡伐鍗曚腑绉婚櫎姝e湪鐢熶骇鐨勬潯鐮�
+            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;
         }
 
@@ -751,7 +854,7 @@
                         TempPkgAction.Init(this);
 
                         int shipQty = input.Data.ToInt32();
-                        int curQty = CurPkg.Item.TotalQty;
+                        double curQty = CurPkg.Item.TotalQty;
                         int ruleQty = TempPkgAction.PkgRule.GetTotalQty();
                         //鏈夊~鍑鸿揣鏁伴噺鍒欓獙璇佸綋鍓嶅寘瑁呮暟閲忓繀椤荤瓑浜庯紙鍑鸿揣鏁伴噺/鍖呰瑙勫垯鏁伴噺锛夌殑浣欐暟
                         if (shipQty > 0 && curQty != shipQty % ruleQty)
@@ -795,6 +898,34 @@
             return action;
         }
 
+        /// <summary>
+        /// 鎵撳嵃娴嬭瘯鏍囩
+        /// </summary>
+        /// <param name="input"></param>
+        /// <returns></returns>
+        public async Task<ApiAction<PackingActionOutput>> TestPrint()
+        {
+            var action = new ApiAction<PackingActionOutput>();
+            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);
+                action.Data = new PackingActionOutput()
+                {
+                    ExecCode = "PrintTest",
+                    PkgInfo = CurPkg,
+                    PrintLable = printLb,
+                };
+            }
+            catch (Exception ex)
+            {
+                action.CatchExceptionWithLog(ex, $"鍖呰宸ュ簭锛氭墦鍗版祴璇曟爣绛惧紓甯�");
+            }
+            return action;
+        }
+
         #endregion Functions
 
         /// <summary>

--
Gitblit v1.9.3