From 712dc2dd07a040e25203a5fb676bb98d14d424ec Mon Sep 17 00:00:00 2001
From: Rodney Chen <rodney.chen@hotmail.com>
Date: 星期二, 03 十二月 2024 21:27:53 +0800
Subject: [PATCH] 工单条码增加数量字段

---
 Tiger.Business.MES/Common/WorkBatch.cs |   49 ++++++++++++++++++++++++++++++++++---------------
 1 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/Tiger.Business.MES/Common/WorkBatch.cs b/Tiger.Business.MES/Common/WorkBatch.cs
index 50e3fe1..413ac6f 100644
--- a/Tiger.Business.MES/Common/WorkBatch.cs
+++ b/Tiger.Business.MES/Common/WorkBatch.cs
@@ -12,6 +12,7 @@
 using Tiger.Business.MES;
 using static Microsoft.CodeAnalysis.CSharp.SyntaxTokenParser;
 using Org.BouncyCastle.Ocsp;
+using System.IO;
 
 namespace Tiger.Business
 {
@@ -64,6 +65,7 @@
 
         public void Update(bool updateAll = false)
         {
+            var strat = DateTime.Now;
             var wo = Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == OrderNo).IncludesAllFirstLayer().First();
             var batch = Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(q => q.BATCH_NO == Batch.BATCH_NO).First();
             if (WoContext.WoBatchDic.ContainsKey(batch.BATCH_NO))
@@ -72,7 +74,7 @@
                 {
                     WoContext.WoBatchDic.Remove(Batch.BATCH_NO);
                 }
-                else if(updateAll || wo.UPDATE_TIME > WO.UPDATE_TIME || batch.UPDATE_TIME > Batch.UPDATE_TIME)
+                else if(updateAll || wo.CHANGE_TIME > WO.CHANGE_TIME || batch.CHANGE_TIME > Batch.CHANGE_TIME)
                 {
                     GetBatchInfo();
                 }
@@ -84,10 +86,12 @@
             }
             WO = wo;
             Batch = batch;
+            ConsoleExt.WriteLine($"{DateTime.Now:HH:mm:ss} ##### : {(DateTime.Now - strat).TotalSeconds}s", ConsoleColor.Yellow);
         }
 
         private void GetBatchInfo()
         {
+            ConsoleExt.WriteLine($"{DateTime.Now:HH:mm:ss} !!!!! ", ConsoleColor.Cyan);
             Product = Biz.Db.Queryable<BAS_ITEM>().Where(q => q.ITEM_CODE == WO.ITEM_CODE && q.AUTH_ORG == WO.AUTH_ORG).IncludesAllFirstLayer().First();
             Customer = Biz.Db.Queryable<BAS_CUSTOMER>().Where(q => q.CUST_CODE == WO.CUST_CODE).First();
             WoSNs = Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.WORK_ORDER == OrderNo).ToList();
@@ -198,8 +202,8 @@
                 var db = Biz.Db;
                 var dbTran = db.UseTran(() =>
                 {
-                    db.Updateable(WO, user).UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.STATUS, q.ACT_START_TIME }).ExecuteCommand();
-                    db.Updateable(Batch, user).UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.STATUS, q.ACT_START_TIME }).ExecuteCommand();
+                    db.Updateable(WO, user).UpdateColumns(q => new { q.STATUS, q.ACT_START_TIME }).ExecuteCommand();
+                    db.Updateable(Batch, user).UpdateColumns(q => new { q.STATUS, q.ACT_START_TIME }).ExecuteCommand();
                 });
                 if (!dbTran.IsSuccess)
                 {
@@ -601,29 +605,31 @@
             var batchs = wipList.Where(q => !q.BATCH_NO.IsNullOrEmpty()).GroupBy(q => new { q.BATCH_NO }).Select(g => new { 
                 g.Key.BATCH_NO, 
                 Qty = g.Count(),
-                FinishQty = g.Count(q => q.STATUS == MES_WIP_DATA.STATUSs.Finished.GetValue()),
+                OutQty = g.Count(q => MES_WIP_DATA.STATUSs.Output.GetValue() <= q.STATUS && q.STATUS < MES_WIP_DATA.STATUSs.InStorage.GetValue()),
             });
             var db = Biz.Db;
             var dbTran = db.UseTran(() =>
             {
                 //BIZ_MES_WO
                 wo.INPUT_QTY -= wipList.Count;
-                wo.OUTPUT_QTY -= wipList.Count(q => q.STATUS == MES_WIP_DATA.STATUSs.Finished.GetValue());
+                wo.OUTPUT_QTY -= wipList.Count(q => MES_WIP_DATA.STATUSs.Output.GetValue() <= q.STATUS && q.STATUS < MES_WIP_DATA.STATUSs.InStorage.GetValue());
                 wo.SCRAP_QTY += wipList.Count;
-                db.Updateable(wo, user).UpdateColumns(q => new { q.INPUT_QTY, q.OUTPUT_QTY, q.SCRAP_QTY, q.UPDATE_USER, q.UPDATE_TIME }).ExecuteCommand();
+                wo.STATUS = wo.STATUS.GetEnum<BIZ_MES_WO.STATUSs>() == BIZ_MES_WO.STATUSs.Closed ? BIZ_MES_WO.STATUSs.Working.GetValue() : wo.STATUS;
+                db.Updateable(wo, user).UpdateColumns(q => new { q.INPUT_QTY, q.OUTPUT_QTY, q.SCRAP_QTY, q.UPDATE_USER, q.UPDATE_TIME, q.STATUS }).ExecuteCommand();
                 //BIZ_MES_WO_BATCH
-                foreach (var batch in batchs)
+                var wobatchs = Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(q => batchs.Select(x => x.BATCH_NO).Contains(q.BATCH_NO)).ToList();
+                foreach (var batch in wobatchs)
                 {
-                    db.Updateable<BIZ_MES_WO_BATCH>(user)
-                         .SetColumns(q => q.INPUT_QTY == q.INPUT_QTY - batch.Qty)
-                         .SetColumns(q => q.OUTPUT_QTY == q.OUTPUT_QTY - batch.FinishQty)
-                         .SetColumns(q => q.SCRAP_QTY == q.SCRAP_QTY + batch.Qty)
-                         .Where(q => q.BATCH_NO == q.BATCH_NO)
-                         .ExecuteCommand();
+                    batch.INPUT_QTY -= batchs.Where(q=>q.BATCH_NO == batch.BATCH_NO).First().Qty;
+                    batch.OUTPUT_QTY -= batchs.Where(q => q.BATCH_NO == batch.BATCH_NO).First().OutQty;
+                    batch.SCRAP_QTY += batchs.Where(q => q.BATCH_NO == batch.BATCH_NO).First().Qty;
+                    batch.STATUS = batch.STATUS.GetEnum<BIZ_MES_WO_BATCH.STATUSs>() == BIZ_MES_WO_BATCH.STATUSs.Closed ? BIZ_MES_WO_BATCH.STATUSs.Working.GetValue() : batch.STATUS;
                 }
+                db.Updateable(wobatchs, user).UpdateColumns(q => new { q.INPUT_QTY, q.OUTPUT_QTY, q.SCRAP_QTY, q.UPDATE_USER, q.UPDATE_TIME, q.STATUS }).ExecuteCommand();
                 //BIZ_MES_WO_SN
                 db.Updateable<BIZ_MES_WO_SN>(user)
                             .SetColumns(q => q.STATUS == BIZ_MES_WO_SN.STATUSs.Offline.GetValue())
+                            .SetColumns(q => q.TRAY_SN == null).SetColumns(q => q.OUTER_SN == null)
                             .Where(q => q.WORK_ORDER == wo.ORDER_NO && wipIDs.Contains(q.WIP_ID))
                             .ExecuteCommand();
                 //MES_WIP_DATA & MES_WIP_HIS
@@ -631,6 +637,8 @@
                 foreach (var wipSN in wipList.Where(q => q.STATUS != MES_WIP_DATA.STATUSs.Offline.GetValue()))
                 {
                     wipSN.STATUS = MES_WIP_DATA.STATUSs.Offline.GetValue();
+                    wipSN.TRAY_SN = wipSN.INNER_SN = wipSN.CARTON_SN = wipSN.PALLET_SN = wipSN.SHIPPING_ORDER = null;
+                    wipSN.FINISHED_FLAG = wipSN.HOLD_FLAG = "N";
                     wipSN.UNBIND_FLAG = "Y";
                     wipSN.NODE_ID = "";
                     wipSN.NODE_NAME = "涓嬬嚎閫�搴�";
@@ -659,7 +667,7 @@
                     }
                 }
                 while (curpkg.Any());
-                db.Updateable(curpkg, user).ExecuteCommand();
+                db.Updateable(pkgs, user).ExecuteCommand();
                 //MES_WIP_DFT锛屽湪涓婁粨搴撹閰嶅拰缁翠慨涔嬪墠锛屽厛鎶婁笉鑹褰曟爣璁颁负宸插鐞�
                 db.Updateable<MES_WIP_DFT>(user)
                            .SetColumns(q => q.STATUS == MES_WIP_DFT.STATUSs.Resolved.GetValue())
@@ -671,7 +679,18 @@
                 //鎶涘嚭寮傚父
                 throw dbTran.ErrorException;
             }
-
+            //鏇存柊宸ュ崟姹犱腑鐨勫伐鍗曟潯鐮佽〃鏁版嵁鐨勭姸鎬�
+            foreach (var item in WoContext.WoBatchDic)
+            {
+                if (item.Value.WO.ORDER_NO == wo.ORDER_NO)
+                {
+                    foreach (var woSn in item.Value.WoSNs.Where(q => wipIDs.Contains(q.WIP_ID)))
+                    {
+                        woSn.STATUS = BIZ_MES_WO_SN.STATUSs.Offline.GetValue();
+                    }
+                    item.Value.Update();
+                }
+            }
             return action;
         }
         #endregion

--
Gitblit v1.9.3