From fc6dd85a865c4cadae0b9a07d56e2988d2262f10 Mon Sep 17 00:00:00 2001
From: kazelee <1847801760@qq.com>
Date: 星期二, 22 七月 2025 17:28:52 +0800
Subject: [PATCH] 优化数据库事务帮助类 , 查询/日志生成类

---
 wms/WCSHelper.cs |  185 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 172 insertions(+), 13 deletions(-)

diff --git a/wms/WCSHelper.cs b/wms/WCSHelper.cs
index 06549ea..38fdc5c 100644
--- a/wms/WCSHelper.cs
+++ b/wms/WCSHelper.cs
@@ -53,44 +53,194 @@
         }
 
         /// <summary>
-        /// 璧风偣鍑哄簱閿侊紙寮哄埗璧嬪�锛屼笉浼氭鏌oc!=null锛岄攣鐘舵�=鏃狅紝闇�浼犲弬鍓嶇‘璁わ級
+        /// <!--寮冪敤|鐞嗙敱:涓嶇伒娲�娑夊強涓氬姟杩囦簬鍏蜂綋-->妫�煡瀹瑰櫒绫诲瀷鏄惁姝g‘
+        /// </summary>
+        /// <param name="cntrCode"></param>
+        /// <param name="cntrType"></param>
+        /// <param name="errMsg"></param>
+        /// <returns></returns>
+        private static bool CheckCntrType(string cntrCode, string cntrType, out string errMsg) {
+            var db = new SqlHelper<object>().GetInstance();
+            errMsg = string.Empty;
+
+            var cntr = db.Queryable<TN_Container>().Where(c => c.S_CODE == cntrType).First();
+            if (cntr == null) {
+                errMsg = $"瀹瑰櫒'{cntrCode}'鍦╗瀹瑰櫒琛╙涓笉瀛樺湪,璇峰湪鍓嶅彴椤甸潰涓淮鎶�";
+                return false;
+            }
+            if (cntr.S_TYPE != cntrType) {
+                errMsg = $"瀹瑰櫒'{cntrCode}'鍦╗瀹瑰櫒琛╙涓殑绫诲瀷鏄�{cntr.S_TYPE},涓嶆槸'{cntrType}'!";
+                return false;
+            }
+
+            return true;
+        }
+
+        /// <summary>
+        /// 鏍规嵁 [ 瀹瑰櫒鍙�] , 鏌ヨ褰撳墠瀹瑰櫒鍏宠仈鐨�[ 璐т綅 ] [ 璐т綅瀹瑰櫒 ] [ 瀹瑰櫒璐у搧 ] 淇℃伅 ( 鍙煡璇�1 鏉�)
+        /// </summary>
+        /// <param name="cntrCode">瀹瑰櫒鍙�/param>
+        /// <param name="skipCgDetail">鏄惁璺宠繃 [ 瀹瑰櫒璐у搧 ] 鐨勬煡璇�( 褰撳鍣ㄥ彿鏉ヨ嚜 [ 瀹瑰櫒璐у搧 ] 鏃�)</param>
+        /// <returns></returns>
+        public static LocCntrCg GetLocCntrCg(string cntrCode, bool skipCgDetail = false) {
+            var db = new SqlHelper<object>().GetInstance();
+
+            TN_CG_Detail cgDetail = null;
+            if (!skipCgDetail) {
+                cgDetail = db.Queryable<TN_CG_Detail>().Where(d => d.S_CNTR_CODE == cntrCode).First();
+            }
+
+            var locCntrRel = db.Queryable<TN_Loc_Container>().Where(c => c.S_CNTR_CODE == cntrCode).First();
+
+            TN_Location location = null;
+            if (locCntrRel != null) {
+                location = db.Queryable<TN_Location>().Where(l => l.S_CODE == locCntrRel.S_LOC_CODE).First();
+                if (location == null) {
+                    LogHelper.Warn($"鑾峰彇瀹瑰櫒鍏宠仈淇℃伅:[璐т綅瀹瑰櫒鍏崇郴]瀛樺湪,浣哰璐т綅]涓嶅瓨鍦�瀹瑰櫒='{cntrCode}',璐т綅='{locCntrRel.S_LOC_CODE}'");
+                }
+            }
+            if (location != null) {
+                location.N_CURRENT_NUM = 0;
+                location.T_MODIFY = DateTime.Now;
+            }
+
+            return new LocCntrCg {
+                CgDetail = cgDetail,
+                LocCntrRel = locCntrRel,
+                Location = location
+            };
+        }
+
+        /// <summary>
+        /// 缁戝畾 [ 璐т綅瀹瑰櫒 ] 淇℃伅 ( 璁剧疆 [ 璐т綅鏁伴噺 ] = 1 )
+        /// </summary>
+        /// <remarks><b>[ 娉ㄦ剰 ]</b> 涓嶄細妫�煡 loc 鈮�null</remarks>
+        /// <param name="loc"></param>
+        /// <param name="cntrCode"></param>
+        /// <returns></returns>
+        public static TN_Loc_Container BindLocCntr(TN_Location loc, string cntrCode) {
+            var locCntrRel = new TN_Loc_Container {
+                S_LOC_CODE = loc.S_CODE,
+                S_CNTR_CODE = cntrCode,
+            };
+
+            if (loc.N_CURRENT_NUM != 0) {
+                LogHelper.Warn($"缁戝畾璐т綅瀹瑰櫒:绋嬪簭姝e湪灏濊瘯缁橻瀹瑰櫒鏁伴噺]涓嶆槸0鐨勮揣浣�璁剧疆[瀹瑰櫒鏁伴噺]涓�!!璐т綅='{loc.S_CODE}',瀹瑰櫒鏁伴噺={loc.N_CURRENT_NUM}");
+            }
+
+            loc.N_CURRENT_NUM = 1;
+            loc.T_MODIFY = DateTime.Now;
+
+            return locCntrRel;
+        }
+
+        /// <summary>
+        /// 缁戝畾 [ 璐т綅瀹瑰櫒 ] 淇℃伅 ( 璁剧疆 [ 璐т綅鏁伴噺 ] + list.Count )
         /// </summary>
         /// <param name="loc"></param>
+        /// <param name="cntrCodes"></param>
+        /// <returns></returns>
+        public static TN_Loc_Container BindLocCntrs(TN_Location loc, List<string> cntrCodes) {
+            var locCntrRel = new TN_Loc_Container {
+                S_LOC_CODE = loc.S_CODE,
+                S_CNTR_CODE = string.Join(",", cntrCodes)
+            };
+
+            if (loc.N_CURRENT_NUM != 0) {
+                LogHelper.Warn($"缁戝畾璐т綅瀹瑰櫒:绋嬪簭姝e湪灏濊瘯缁橻瀹瑰櫒鏁伴噺]涓嶆槸0鐨勮揣浣�璁剧疆[瀹瑰櫒鏁伴噺]涓�!!璐т綅='{loc.S_CODE}',瀹瑰櫒鏁伴噺={loc.N_CURRENT_NUM}");
+            }
+
+            loc.N_CURRENT_NUM = 1;
+            loc.T_MODIFY = DateTime.Now;
+
+            return locCntrRel;
+        }
+
+        /// <summary>
+        /// 璧风偣鍑哄簱閿�+        /// </summary>
+        /// <remarks>瑕佹眰: loc != null; 閿佺姸鎬�'鏃�;</remarks>
+        /// <param name="loc"></param>
         /// <param name="lockSource"></param>
-        public static void LockStartLoc(ref TN_Location loc, string lockSource = "") {
+        public static void LockStartLoc(TN_Location loc, string lockSource = "") {
+            if (loc.N_LOCK_STATE != 0 || loc.S_LOCK_STATE != "鏃�) {
+                LogHelper.Warn($"璧风偣鍑哄簱閿�绋嬪簭姝e湪灏濊瘯缁欏綋鍓峓閿佺姸鎬乚鈮�鏃�鐨勮揣浣嶄笂閿�璐т綅='{loc.S_CODE}',閿佺姸鎬�({loc.N_LOCK_STATE},{loc.S_LOCK_STATE})");
+            }
+
             loc.N_LOCK_STATE = 2; // 璧风偣鍑哄簱閿�             loc.S_LOCK_STATE = TN_Location.GetLockStateStr(2); // 璧风偣鍑哄簱閿�             loc.S_LOCK_OP = lockSource;
             loc.T_MODIFY = System.DateTime.Now;
+
+            LogHelper.Info($"璧风偣鍑哄簱閿�璧风偣璐т綅'{loc.S_CODE}'璁剧疆[鍑哄簱閿乚");
         }
 
         /// <summary>
-        /// 缁堢偣鍏ュ簱閿侊紙寮哄埗璧嬪�锛屼笉浼氭鏌oc!=null锛岄攣鐘舵�=鏃狅紝闇�浼犲弬鍓嶇‘璁わ級
+        /// 缁堢偣鍏ュ簱閿�         /// </summary>
+        /// <remarks>瑕佹眰 : loc 鈮�null ; 閿佺姸鎬�'鏃� ;</remarks>
         /// <param name="loc"></param>
         /// <param name="lockSource"></param>
-        public static void LockEndLoc(ref TN_Location loc, string lockSource = "") {
-            loc.N_LOCK_STATE = 1; // 缁堢偣鍑哄簱閿�-            loc.S_LOCK_STATE = TN_Location.GetLockStateStr(1); // 缁堢偣鍑哄簱閿�+        public static void LockEndLoc(TN_Location loc, string lockSource = "") {
+            if (loc.N_LOCK_STATE != 0 || loc.S_LOCK_STATE != "鏃�) {
+                LogHelper.Warn($"缁堢偣鍏ュ簱閿�绋嬪簭姝e湪灏濊瘯缁欏綋鍓峓閿佺姸鎬乚鈮�鏃�鐨勮揣浣嶄笂閿�璐т綅='{loc.S_CODE}',閿佺姸鎬�({loc.N_LOCK_STATE},{loc.S_LOCK_STATE})");
+            }
+
+            loc.N_LOCK_STATE = 1; // 缁堢偣鍏ュ簱閿�+            loc.S_LOCK_STATE = TN_Location.GetLockStateStr(1); // 缁堢偣鍏ュ簱閿�             loc.S_LOCK_OP = lockSource;
             loc.T_MODIFY = System.DateTime.Now;
+
+            LogHelper.Info($"缁堢偣鍏ュ簱閿�缁堢偣璐т綅='{loc.S_CODE}',鏉ユ簮='{lockSource}'");
         }
 
-        public static TN_Task BuildTask(TN_Location startLoc, TN_Location endLoc, string cntId, string type, int pri = 3, int agvType = 1) {
-            TN_Task TN_Task = new TN_Task() {
+        /// <summary>
+        /// 鍒涘缓浠诲姟
+        /// </summary>
+        /// <remarks>瑕佹眰 : <br/>(1) startLoc / endLoc 鈮�null<br/>(2) startLoc / endLoc 瀛樺湪 S_CODE , S_AREA_CODE 瀛楁</remarks>
+        /// <param name="startLoc">璧风偣璐т綅:鑷冲皯鎻愪緵:S_CODE,S_AREA_CODE</param>
+        /// <param name="endLoc">缁堢偣璐т綅:鑷冲皯鎻愪緵:S_CODE,S_AREA_CODE</param>
+        /// <param name="cntId">瀹瑰櫒鍙�/param>
+        /// <param name="type">浠诲姟绫诲瀷(鍚嶇О)</param>
+        /// <param name="pri">浼樺厛绾�/param>
+        /// <param name="agv">AGV绫诲瀷</param>
+        /// <returns></returns>
+        public static TN_Task BuildTask(TN_Location startLoc, TN_Location endLoc, string cntId, string type, int pri = 3, int agv = 1) {
+            TN_Task task = new TN_Task() {
                 S_CODE = GenerateTaskNo(),
                 S_START_AREA = startLoc.S_AREA_CODE,
                 S_END_AREA = endLoc.S_AREA_CODE,
                 S_START_LOC = startLoc.S_CODE,
                 S_END_LOC = endLoc.S_CODE,
+                S_CNTR_CODE = cntId,
                 S_TYPE = type,
                 N_PRIORITY = pri,
-                N_SCHEDULE_TYPE = agvType,
-                N_B_STATE = 0, // 浠诲姟鍒涘缓鏃讹紝榛樿绛夊緟
-                S_CNTR_CODE = cntId,
+                N_SCHEDULE_TYPE = agv,
+                N_B_STATE = 0, // 浠诲姟鍒涘缓鏃�榛樿绛夊緟
             };
 
-            return TN_Task;
+            LogHelper.Info($"鍒涘缓浠诲姟:浠诲姟鍙�'{task.S_CODE}',璧风偣璐т綅='',缁堢偣璐т綅='',瀹瑰櫒鍙�'',浠诲姟绫诲瀷");
+
+            return task;
+        }
+
+        /// <summary>
+        /// 鍒涘缓浠诲姟 + 璐т綅閿�+        /// </summary>
+        /// <remarks>瑕佹眰 : <br/>(1) startLoc / endLoc 鈮�null<br/>(2) startLoc / endLoc 瀛樺湪 S_CODE , S_AREA_CODE 瀛楁</remarks>
+        /// <param name="startLoc">璧风偣璐т綅 : 鑷冲皯鎻愪緵 : S_CODE , S_AREA_CODE</param>
+        /// <param name="endLoc">缁堢偣璐т綅 : 鑷冲皯鎻愪緵 : S_CODE , S_AREA_CODE</param>
+        /// <param name="cntId">瀹瑰櫒鍙�/param>
+        /// <param name="type">浠诲姟绫诲瀷(鍚嶇О)</param>
+        /// <param name="pri">浼樺厛绾�/param>
+        /// <param name="agv">AGV绫诲瀷</param>
+        /// <returns></returns>
+        public static TN_Task BuildTaskWithLocLock(TN_Location startLoc, TN_Location endLoc, string cntId, string type, int pri = 3, int agv = 1) {
+            var task = BuildTask(startLoc, endLoc, cntId, type, pri);
+            LockStartLoc(startLoc, task.S_CODE);
+            LockEndLoc(endLoc, task.S_CODE);
+
+            return task;
         }
 
         internal static bool CheckActionRecordExist(string no, int code) {
@@ -122,7 +272,7 @@
         internal static void Fail(TN_Task task) {
             var db = new SqlHelper<TN_Task>().GetInstance();
             if (task != null) {
-                //鍒ゆ柇鏈夋病鏈夊彇璐у畬鎴愶紝娌℃湁灏卞彉鎴愬け璐ャ�鏈夊彇璐у畬鎴愰粯璁ゅ畬鎴愪簡锛堣窡鎹」鐩�瀹氾紝鏈変簺椤圭洰浜哄伐鎷夎蛋浜嗕篃娌℃湁鏀惧埌缁堢偣锛夈�
+                //鍒ゆ柇鏈夋病鏈夊彇璐у畬鎴�娌℃湁灏卞彉鎴愬け璐ャ�鏈夊彇璐у畬鎴愰粯璁ゅ畬鎴愪簡 (璺熸嵁椤圭洰鑰屽畾,鏈変簺椤圭洰浜哄伐鎷夎蛋浜嗕篃娌℃湁鏀惧埌缁堢偣) 銆�                 task.N_B_STATE = 4;
                 task.S_B_STATE = TN_Task.GetStateStr(task.N_B_STATE);
                 db.Updateable(task).UpdateColumns(it => new { it.N_B_STATE, it.S_B_STATE }).ExecuteCommand();
@@ -146,4 +296,13 @@
             return db.Queryable<TN_Task>().Where(a => a.N_B_STATE == 0 && (a.S_B_STATE == "绛夊緟" || a.S_B_STATE == "寰呮帹閫�)).ToList();
         }
     }
+
+    /// <summary>
+    /// [璐т綅/瀹瑰櫒/鐗╂枡]淇℃伅
+    /// </summary>
+    public class LocCntrCg {
+        public TN_CG_Detail CgDetail { get; set; } = null;
+        public TN_Loc_Container LocCntrRel { get; set; } = null;
+        public TN_Location Location { get; set; } = null;
+    }
 }

--
Gitblit v1.9.1