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 | 220 +++++++++++++++++++++++++++++++++--------------------- 1 files changed, 135 insertions(+), 85 deletions(-) diff --git a/Tiger.Business.WMS/Transaction/Out_Default.cs b/Tiger.Business.WMS/Transaction/Out_Default.cs index a12de29..20872e8 100644 --- a/Tiger.Business.WMS/Transaction/Out_Default.cs +++ b/Tiger.Business.WMS/Transaction/Out_Default.cs @@ -9,33 +9,24 @@ using System.Threading.Tasks; using Tiger.Model; using Tiger.IBusiness; -using Org.BouncyCastle.Ocsp; +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 @@ -44,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()) @@ -62,7 +53,7 @@ catch (Exception ex) { //鍙栨秷褰撳墠鎿嶄綔 - ResetInfo(); + ResetTrans(); //action.CatchExceptionWithLog(ex, $"鎵弿[{input.SN}]寮傚父"); action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.Scan.ScanException", input.SN)); } @@ -77,39 +68,83 @@ var action = new ApiAction<ScanOutput>(new ScanOutput() { Command = Command }); try { - //瑙f瀽鏉$爜 - Result<IInventory> result = WMS_ITEM_Biz.WmsItem.Get(input.SN, input.AuthOption, true); - if (!result.IsSuccessed) + if (CurInvItem.IsNullOrEmpty()) { - action.IsSuccessed = false; - action.LocaleMsg = result.LocaleMsg; - return action; - } - CurInvItem = result.Data as Inventory; + //瑙f瀽鏉$爜 + Result<IInventory> result = GetInventory(input.SN, input.AuthOption, true); + if (!result.IsSuccessed) + { + action.IsSuccessed = false; + action.LocaleMsg = result.LocaleMsg; + return SetOutPutMqttMsg(action, input.Locale); + } + var inv = result.Data as Inventory; - //楠岃瘉鏉$爜鏄惁姝g‘ - if (!CurInvItem.isNormalStatus || CurInvItem.Status != WMS_ITEM.STATUSs.InStore) - { - action.IsSuccessed = false; - //action.LocaleMsg = Biz.L($"鐘舵�乕{0}]寮傚父锛岃閲嶆柊鎵弿"); - action.LocaleMsg = Biz.L("WMS.Default.ScanItem.StatusException", string.Join(',', CurInvItem.StatusList.Select(x => x.GetDesc()))); - return action; + //楠岃瘉鏉$爜鏄惁姝g‘ + if (!inv.isNormalStatus || inv.Status != WMS_ITEM.STATUSs.InStore) + { + 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 SetOutPutMqttMsg(action, input.Locale); + } + //鍌ㄤ綅楠岃瘉 + if (inv.Location.IsNullOrEmpty()) + { + 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 SetOutPutMqttMsg(action, input.Locale); + } + //鐗╂枡楠岃瘉 + if (inv.ItemInfo.IsNullOrEmpty() || inv.ItemInfo.IS_ACTIVE == "N") + { + 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 SetOutPutMqttMsg(action, input.Locale); + } + CurInvItem = inv; + + //鎶婄墿鏂欐暟閲忚繑鍥炲墠绔‘璁よ涓嬫灦鐨勬暟閲� + if (CurInvItem.Items.Any(q => q.QTY > 1)) + { + action.Data.Data = CurInvItem.Items.Select(q => new { q.SN, q.QTY }); + action.Data.Command = Command = "ComfirmQty"; + //action.LocaleMsg = Biz.L("璇风‘璁ゆ潯鐮乕{0}]瑕佷笅鏋剁殑鏁伴噺"); + action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ComfirmOutQty", input.SN); + return SetOutPutMqttMsg(action, input.Locale); + } } - //鍌ㄤ綅楠岃瘉 - if (CurInvItem.Location.IsNullOrEmpty()) + + Dictionary<string, double> downDic = null; + //鏇存柊涓嬫灦鍚庣殑鐗╂枡鏁伴噺 + if (Command == "ComfirmQty") { - action.IsSuccessed = false; - //action.LocaleMsg = Biz.L($"鏉$爜[{0}]搴撳瓨淇℃伅寮傚父: 娌℃湁鍌ㄤ綅淇℃伅锛岃鍏堜笂鏋跺悗鍐嶆壂鎻�(鍌ㄤ綅Id: {1})"); - action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationIsNull", CurInvItem.CurPkg.SN, CurInvItem.CurPkg.LOCATION_ID); - return action; - } - //鐗╂枡楠岃瘉 - if (CurInvItem.ItemInfo.IsNullOrEmpty() || CurInvItem.ItemInfo.IS_ACTIVE == "N") - { - action.IsSuccessed = false; - //action.LocaleMsg = Biz.L($"鐗╂枡缂栫爜[{0}]涓嶅瓨鍦ㄦ垨鑰呰鐗╂枡鏈惎鐢�"); - action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ItemCodeNotExistsOrNotActive", CurInvItem.ItemInfo.ITEM_CODE.IsNullOrEmpty(CurInvItem.Barcode.ItemCode)); - return action; + var qtyList = (input.Data ?? "").JsonToObject<List<WMS_ITEM>>() ?? new List<WMS_ITEM>(); + if (!qtyList.Any() || qtyList.Sum(q => q.QTY) <= 0) + { + action.IsSuccessed = false; + action.Data.Data = CurInvItem.Items.Select(q => new { q.SN, q.QTY }); + action.Data.Command = Command = "ComfirmQty"; + //action.LocaleMsg = Biz.L("涓嬫灦鏁伴噺涓嶈兘灏忎簬绛変簬闆讹紝璇烽噸鏂扮‘璁ゆ潯鐮乕{0}]鐨勪笅鏋舵暟閲�"); + action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ReComfirmOutQty", input.SN); + return SetOutPutMqttMsg(action, input.Locale); + } + else if (qtyList.Any(q => q.QTY > CurInvItem.Items.First(i => i.SN == q.SN).QTY)) + { + action.IsSuccessed = false; + action.Data.Data = CurInvItem.Items.Select(q => new { q.SN, q.QTY }); + action.Data.Command = Command = "ComfirmQty"; + //action.LocaleMsg = Biz.L("涓嬫灦鏁伴噺涓嶈兘瓒呰繃鍘熸潯鐮佹暟閲忥紝璇烽噸鏂扮‘璁ゆ潯鐮乕{0}]鐨勪笅鏋舵暟閲�"); + action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ReComfirmExceedOutQty", input.SN); + return SetOutPutMqttMsg(action, input.Locale); + } + else + { + downDic = qtyList.ToDictionary(k => k.SN, v => v.QTY); + Command = "Normal"; + } } //鍑哄簱涓嬫灦 @@ -118,14 +153,19 @@ item.TRANS_CODE = "Out_Default"; item.TRANS_NO = $"Out_Default_{DateTime.Now:yyyyMMdd}"; } - Result<TakeDownInfo> downResult = WMS_ITEM_Biz.WmsItem.TakeDown(CurInvItem, input.AuthOption, WMS_ITEM.STATUSs.Sended); + 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; - return action; + ResetTrans(); + return SetOutPutMqttMsg(action, input.Locale); } - TakeDownInfo downInfo = downResult.Data; //鐏伅 if (CurInvItem.Shelf.IsLightShelf) @@ -133,43 +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); + //瀹屾垚鎵�鏈夊鐞嗗悗浣跨敤浜嬪姟淇濆瓨鏁版嵁 + 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> @@ -191,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()) @@ -206,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; @@ -223,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; @@ -249,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