From eb29ee75c7f2f4154b7a3d0c475f94e0b964e996 Mon Sep 17 00:00:00 2001
From: Rodney Chen <rodney.chen@hotmail.com>
Date: 星期三, 02 四月 2025 18:17:48 +0800
Subject: [PATCH] 优化事务基础写法

---
 Tiger.Business.WMS/Transaction/Out_Default.cs |  139 +++++++++++++++++++++++----------------------
 1 files changed, 71 insertions(+), 68 deletions(-)

diff --git a/Tiger.Business.WMS/Transaction/Out_Default.cs b/Tiger.Business.WMS/Transaction/Out_Default.cs
index ad368c4..20872e8 100644
--- a/Tiger.Business.WMS/Transaction/Out_Default.cs
+++ b/Tiger.Business.WMS/Transaction/Out_Default.cs
@@ -9,34 +9,24 @@
 using System.Threading.Tasks;
 using Tiger.Model;
 using Tiger.IBusiness;
-using Org.BouncyCastle.Ocsp;
-using Tiger.Model.MES.Yada;
+using Tiger.Model.Entitys.MES.Position;
 
 namespace Tiger.Business.WMS.Transaction
 {
     /// <summary>
     /// 鏍囧噯涓嬫灦浜嬪姟
     /// </summary>
-    public class Out_Default : WMSTransactionBase, IOut_Default
+    public class Out_Default : WmsTask, IOut_Default
     {
         public IOut_Default Init(string id, string userCode, string apiHost, string orgCode)
         {
-            TransID = id;
-            UserCode = userCode;
-            ApiHost = apiHost;
-            OrgCode = orgCode;
-           
+            base.Init(id, userCode, apiHost, orgCode);
             Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
             return this;
         }
 
         #region Propertys & Variables
-        public string UserCode { get; set; }
-        public long UserId { get; set; }
-        public string OrgCode { get; set; }
-        public Inventory CurInvItem { get; set; }
-        public ScanShelfInfo CurScanShelf { get; set; }
-        public string Command { get; set; } = "Normal";
+
         #endregion
 
         #region Functions
@@ -45,7 +35,7 @@
         /// </summary>
         public async Task<ApiAction<ScanOutput>> Scan(BaseInput input)
         {
-            var action = new ApiAction<ScanOutput>();
+            var action = new ApiAction<ScanOutput>(new ScanOutput());
             try
             {
                 if (input.SN.IsNullOrEmpty())
@@ -63,7 +53,7 @@
             catch (Exception ex)
             {
                 //鍙栨秷褰撳墠鎿嶄綔
-                ResetInfo();
+                ResetTrans();
                 //action.CatchExceptionWithLog(ex, $"鎵弿[{input.SN}]寮傚父");
                 action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.Scan.ScanException", input.SN));
             }
@@ -81,12 +71,12 @@
                 if (CurInvItem.IsNullOrEmpty())
                 {
                     //瑙f瀽鏉$爜
-                    Result<IInventory> result = WMS_ITEM_Biz.WmsItem.Get(input.SN, input.AuthOption, true);
+                    Result<IInventory> result = GetInventory(input.SN, input.AuthOption, true);
                     if (!result.IsSuccessed)
                     {
                         action.IsSuccessed = false;
                         action.LocaleMsg = result.LocaleMsg;
-                        return action;
+                        return SetOutPutMqttMsg(action, input.Locale);
                     }
                     var inv = result.Data as Inventory;
 
@@ -96,7 +86,7 @@
                         action.IsSuccessed = false;
                         //action.LocaleMsg = Biz.L($"鐘舵�乕{0}]寮傚父锛岃閲嶆柊鎵弿");
                         action.LocaleMsg = Biz.L("WMS.Default.ScanItem.StatusException", string.Join(',', inv.StatusList.Select(x => x.GetDesc())));
-                        return action;
+                        return SetOutPutMqttMsg(action, input.Locale);
                     }
                     //鍌ㄤ綅楠岃瘉
                     if (inv.Location.IsNullOrEmpty())
@@ -104,7 +94,7 @@
                         action.IsSuccessed = false;
                         //action.LocaleMsg = Biz.L($"鏉$爜[{0}]搴撳瓨淇℃伅寮傚父: 娌℃湁鍌ㄤ綅淇℃伅锛岃鍏堜笂鏋跺悗鍐嶆壂鎻�(鍌ㄤ綅Id: {1})");
                         action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationIsNull", inv.CurPkg.SN, inv.CurPkg.LOCATION_ID);
-                        return action;
+                        return SetOutPutMqttMsg(action, input.Locale);
                     }
                     //鐗╂枡楠岃瘉
                     if (inv.ItemInfo.IsNullOrEmpty() || inv.ItemInfo.IS_ACTIVE == "N")
@@ -112,7 +102,7 @@
                         action.IsSuccessed = false;
                         //action.LocaleMsg = Biz.L($"鐗╂枡缂栫爜[{0}]涓嶅瓨鍦ㄦ垨鑰呰鐗╂枡鏈惎鐢�");
                         action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ItemCodeNotExistsOrNotActive", inv.ItemInfo.ITEM_CODE.IsNullOrEmpty(inv.Barcode.ItemCode));
-                        return action;
+                        return SetOutPutMqttMsg(action, input.Locale);
                     }
                     CurInvItem = inv;
 
@@ -123,11 +113,11 @@
                         action.Data.Command = Command = "ComfirmQty";
                         //action.LocaleMsg = Biz.L("璇风‘璁ゆ潯鐮乕{0}]瑕佷笅鏋剁殑鏁伴噺");
                         action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ComfirmOutQty", input.SN);
-                        return action;
+                        return SetOutPutMqttMsg(action, input.Locale);
                     }
                 }
 
-                Dictionary<string, decimal> downDic = null;
+                Dictionary<string, double> downDic = null;
                 //鏇存柊涓嬫灦鍚庣殑鐗╂枡鏁伴噺
                 if (Command == "ComfirmQty")
                 {
@@ -139,7 +129,7 @@
                         action.Data.Command = Command = "ComfirmQty";
                         //action.LocaleMsg = Biz.L("涓嬫灦鏁伴噺涓嶈兘灏忎簬绛変簬闆讹紝璇烽噸鏂扮‘璁ゆ潯鐮乕{0}]鐨勪笅鏋舵暟閲�");
                         action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ReComfirmOutQty", input.SN);
-                        return action;
+                        return SetOutPutMqttMsg(action, input.Locale);
                     }
                     else if (qtyList.Any(q => q.QTY > CurInvItem.Items.First(i => i.SN == q.SN).QTY))
                     {
@@ -148,7 +138,7 @@
                         action.Data.Command = Command = "ComfirmQty";
                         //action.LocaleMsg = Biz.L("涓嬫灦鏁伴噺涓嶈兘瓒呰繃鍘熸潯鐮佹暟閲忥紝璇烽噸鏂扮‘璁ゆ潯鐮乕{0}]鐨勪笅鏋舵暟閲�");
                         action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ReComfirmExceedOutQty", input.SN);
-                        return action;
+                        return SetOutPutMqttMsg(action, input.Locale);
                     }
                     else
                     {
@@ -163,15 +153,19 @@
                     item.TRANS_CODE = "Out_Default";
                     item.TRANS_NO = $"Out_Default_{DateTime.Now:yyyyMMdd}";
                 }
-                Result<TakeDownInfo> downResult = WMS_ITEM_Biz.WmsItem.TakeDown(CurInvItem, downDic, input.AuthOption, WMS_ITEM.STATUSs.OffShelf);
+                foreach (var item in CurInvItem.Packages)
+                {
+                    item.TRANS_CODE = "Out_Default";
+                    item.TRANS_NO = $"Out_Default_{DateTime.Now:yyyyMMdd}";
+                }
+                Result downResult = TakeDown(downDic, input.AuthOption, WMS_ITEM.STATUSs.OffShelf);
                 if (!downResult.IsSuccessed)
                 {
                     action.IsSuccessed = false;
                     action.LocaleMsg = downResult.LocaleMsg;
-                    CurInvItem = null;
-                    return action;
+                    ResetTrans();
+                    return SetOutPutMqttMsg(action, input.Locale);
                 }
-                TakeDownInfo downInfo = downResult.Data;
 
                 //鐏伅
                 if (CurInvItem.Shelf.IsLightShelf)
@@ -179,44 +173,16 @@
                     await Share.Shelf.DownSingle(TransID, CurInvItem.Location);
                 }
 
-                //淇濆瓨鏁版嵁搴�
-                var db = Business.Biz.Db;
-                var dbTran = db.UseTran(() =>
-                {
-                    //涓嬫灦
-                    db.Updateable(downInfo.Items, UserCode).ExecuteCommand();
-                    db.Insertable(downInfo.History, UserCode).ExecuteCommand();
-                    db.Updateable(downInfo.Packages, UserCode).ExecuteCommand();
-                });
-                if (!dbTran.IsSuccess)
-                {
-                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
-                    throw dbTran.ErrorException;
-                }
-
-                action.Data.Data = new DefaultScanItemOutput()
-                {
-                    SN = CurInvItem.SN,
-                    ItemCode = CurInvItem.ItemInfo.ITEM_CODE,
-                    MaterialName = CurInvItem.ItemInfo.ITEM_NAME,
-                    Qty = CurInvItem.CurPkg.QTY,
-                    Unit = CurInvItem.CurPkg.UNIT,
-                    regionCode = CurInvItem.Region.REGION_CODE,
-                    locationCode = CurInvItem.Location?.LOCATION_CODE,
-                    DateCode = CurInvItem.Items[0].PROD_DATE,
-                    ScanAfCut = CurInvItem.Warehouse.SCAN_AF_CUT
-                };
-                //action.LocaleMsg = Biz.L($"鎵弿鏉$爜[{0}]浠庡偍浣峓{1}]涓嬫灦鎴愬姛");
-                action.LocaleMsg = Biz.L("WMS.Default.ScanItem.TakeDownSucceeded", CurInvItem.SN, CurInvItem.Location?.LOCATION_CODE);
-                CurInvItem = null;
+                //瀹屾垚鎵�鏈夊鐞嗗悗浣跨敤浜嬪姟淇濆瓨鏁版嵁
+                action = DoIfFinish(action, input.Locale);
             }
             catch (Exception ex)
             {
-                ResetInfo();
+                ResetTrans();
                 //action.CatchExceptionWithLog(ex, $"鎵弿鏉$爜[{input.SN}]澶嶆牳寮傚父");
                 action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanItem.ScanException", input.SN));
             }
-            return action;
+            return SetOutPutMqttMsg(action, input.Locale);
         }
 
         /// <summary>
@@ -238,7 +204,7 @@
                 CurScanShelf = new ScanShelfInfo();
 
                 // 鏌ヨ璐ф灦淇℃伅
-                var whUnit = await Biz.Db.Queryable<V_WH_UNIT>().Where(t => t.SHELF_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
+                var whUnit = await MainDB.Queryable<V_WH_UNIT>().Where(t => t.SHELF_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
 
                 // 鎵弿璐ф灦浠g爜锛屼笖涓烘櫤鑳借揣鏋�
                 if (whUnit != null && whUnit.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue())
@@ -253,7 +219,7 @@
                 // 鎵弿搴撲綅浠g爜
                 else
                 {
-                    var nLocation = await Biz.Db.Queryable<V_WH_UNIT>().Where(t => t.LOCATION_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
+                    var nLocation = await MainDB.Queryable<V_WH_UNIT>().Where(t => t.LOCATION_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
                     if (nLocation == null)
                     {
                         action.IsSuccessed = false;
@@ -270,7 +236,7 @@
                     }
                     //if (nLocation.Shelf.IsLightShelf || nLocation.Location.IS_SINGLE == "Y")
                     //{
-                    //    var locationData = Biz.Db.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == nLocation.LOCATION_ID && q.AUTH_ORG == OrgCode).First();
+                    //    var locationData = MainDB.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == nLocation.LOCATION_ID && q.AUTH_ORG == OrgCode).First();
                     //    if (!locationData.IsNullOrEmpty())
                     //    {
                     //        action.IsSuccessed = false;
@@ -296,21 +262,58 @@
             catch (Exception ex)
             {
                 //鍙栨秷褰撳墠鎿嶄綔
-                ResetInfo();
+                ResetTrans();
                 //action.CatchExceptionWithLog(ex, $"鎵弿璐ф灦/鍌ㄤ綅[{0}]寮傚父");
                 action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanShelf.ScanException", Code));
             }
             return action;
         }
 
+        /// <summary>
+        /// 瀹屾垚鎵�鏈夊鐞嗗悗浣跨敤浜嬪姟淇濆瓨鏁版嵁
+        /// </summary>
+        /// <param name="action"></param>
+        /// <param name="locale"></param>
+        /// <param name="doAfterSave"></param>
+        /// <returns></returns>
+        public ApiAction<ScanOutput> DoIfFinish(ApiAction<ScanOutput> action, string locale, Action doAfterSave = null)
+        {
+            //淇濆瓨鏁版嵁搴�
+            SaveCommitListToDB();
+
+            //淇濆瓨鏁版嵁鎴愬姛鍚庢墽琛�
+            doAfterSave?.Invoke();
+
+            // 杩斿洖鏁版嵁
+            action.Data.Data = new DefaultScanItemOutput()
+            {
+                SN = CurInvItem.SN,
+                ItemCode = CurInvItem.ItemInfo.ITEM_CODE,
+                MaterialName = CurInvItem.ItemInfo.ITEM_NAME,
+                Qty = CurInvItem.CurPkg.QTY,
+                Unit = CurInvItem.CurPkg.UNIT,
+                regionCode = CurInvItem.Region.REGION_CODE,
+                locationCode = CurInvItem.Location?.LOCATION_CODE,
+                DateCode = CurInvItem.Items[0].PROD_DATE,
+                ScanAfCut = CurInvItem.Warehouse.SCAN_AF_CUT
+            };
+
+            //action.LocaleMsg = Biz.L($"鎵弿鏉$爜[{0}]浠庡偍浣峓{1}]涓嬫灦鎴愬姛");
+            action.LocaleMsg = Biz.L("WMS.Default.ScanItem.TakeDownSucceeded", CurInvItem.SN, CurInvItem.Location?.LOCATION_CODE);
+
+            //閲嶇疆宸ュ簭
+            ResetTrans();
+            return SetOutPutMqttMsg(action, locale);
+        }
+
         #endregion
 
         /// <summary>
-        /// 閲嶇疆褰撳墠鎿嶄綔锛屾湁闇�瑕佸垯閲嶅啓姝ゆ柟娉�
+        /// 閲嶇疆浜嬪姟鏁版嵁锛屾湁闇�瑕佸垯閲嶅啓姝ゆ柟娉�
         /// </summary>
-        public override void ResetInfo()
+        public override void ResetTrans()
         {
-            base.ResetInfo();
+            base.ResetTrans();
             Command = null;
             CurInvItem = null;
             CurScanShelf = null;

--
Gitblit v1.9.3