From 99d001bbf2a0371373e15c97d3c6c7dcf45e97e3 Mon Sep 17 00:00:00 2001
From: kazelee <1847801760@qq.com>
Date: 星期五, 13 六月 2025 15:44:22 +0800
Subject: [PATCH] 修复空托上线与满托下线联调的逻辑错误

---
 api/DebugController.cs |  134 ++++++++++++++++++++++++--------------------
 1 files changed, 72 insertions(+), 62 deletions(-)

diff --git a/api/DebugController.cs b/api/DebugController.cs
index b723b09..836c7d7 100644
--- a/api/DebugController.cs
+++ b/api/DebugController.cs
@@ -200,47 +200,48 @@
             const string taskName = TaskName.T鎵樼洏_婊℃墭涓嬬嚎鍏ュ簱;
             const string startAreaName = AreaName.B鍖呰鍖�
             const string endAreaName = AreaName.M婊℃墭璐ф灦鍖�
+            const string cntrType = "鎵樼洏";
 
             try {
-                var itemCode = model.ItemCode;
-                var batchNo = model.BatchNo;
+                //var itemCode = model.ItemCode;
+                //var batchNo = model.BatchNo;
                 var cntrCode = model.CntrCode;
                 var startLocCode = model.StartLoc;
 
-                var cgDetail = new TN_CG_Detail {
-                    S_ITEM_CODE = itemCode,
-                    S_BATCH_NO = batchNo,
-                    S_CNTR_CODE = cntrCode,
-                };
+                //var cgDetail = new TN_CG_Detail {
+                //    S_ITEM_CODE = itemCode,
+                //    S_BATCH_NO = batchNo,
+                //    S_CNTR_CODE = cntrCode,
+                //};
 
-                var needInsertContainer = false;
-                var needUpdateContainer = false;
+                //var needInsertContainer = false;
+                //var needUpdateContainer = false;
                 // 鏌ュ鍣ㄤ俊鎭〃鏄惁宸茬粡鏈夎繖涓鍣�-                var cntr = db.Queryable<TN_Container>()
-                    .Where(c => c.S_CODE == cntrCode).First();
+                //var cntr = db.Queryable<TN_Container>()
+                //    .Where(c => c.S_CODE == cntrCode).First();
                 // 濡傛灉鎵句笉鍒拌瀹瑰櫒锛岄渶瑕佹坊鍔狅紝骞朵笖灏嗗鍣ㄦ潵婧愯缃负浠诲姟鍚嶇О
-                if (cntr == null) {
-                    needInsertContainer = true;
-                    LogHelper.Info($"杞锛歿taskName}锛氬鍣細{cntrCode}锛屽湪瀹瑰櫒琛ㄤ腑娌℃湁鐧昏锛岀櫥璁板苟璁剧疆瀹瑰櫒鏉ユ簮涓猴細{taskName}");
+                //if (cntr == null) {
+                //    needInsertContainer = true;
+                //    LogHelper.Info($"杞锛歿taskName}锛氬鍣細{cntrCode}锛屽湪瀹瑰櫒琛ㄤ腑娌℃湁鐧昏锛岀櫥璁板苟璁剧疆瀹瑰櫒鏉ユ簮涓猴細{taskName}");
 
-                    cntr = new TN_Container {
-                        S_CODE = cntrCode,
-                        S_TYPE = "鎵樼洏",
-                        S_SPEC = itemCode,
-                        S_SOURCE = taskName,
-                    };
-                }
-                else {
-                    // 濡傛灉鎵惧埌璇ュ鍣紝浣嗗鍣ㄧ墿鏂欑被鍨嬩笌涓嬬嚎鐗╂枡涓嶇锛岃褰曞苟鐩存帴瑕嗙洊锛堝緟瀹氾級
-                    if (!string.IsNullOrEmpty(cntr.S_SPEC) && cntr.S_SPEC != itemCode) {
-                        needUpdateContainer = true;
-                        LogHelper.Info($"杞锛歿taskName}锛氬鍣ㄨ〃涓鍣▄cntrCode}瀵瑰簲鐨勭墿鏂欎俊鎭細{cntr.S_SPEC}锛� +
-                            $"涓庢墍闇�鐨勭墿鏂欎俊鎭瘂itemCode}涓嶇锛岀洿鎺ヨ鐩栫粨鏋�);
+                //    cntr = new TN_Container {
+                //        S_CODE = cntrCode,
+                //        S_TYPE = "鎵樼洏",
+                //        S_SPEC = itemCode,
+                //        S_SOURCE = taskName,
+                //    };
+                //}
+                //else {
+                //    // 濡傛灉鎵惧埌璇ュ鍣紝浣嗗鍣ㄧ墿鏂欑被鍨嬩笌涓嬬嚎鐗╂枡涓嶇锛岃褰曞苟鐩存帴瑕嗙洊锛堝緟瀹氾級
+                //    if (!string.IsNullOrEmpty(cntr.S_SPEC) && cntr.S_SPEC != itemCode) {
+                //        needUpdateContainer = true;
+                //        LogHelper.Info($"杞锛歿taskName}锛氬鍣ㄨ〃涓鍣▄cntrCode}瀵瑰簲鐨勭墿鏂欎俊鎭細{cntr.S_SPEC}锛� +
+                //            $"涓庢墍闇�鐨勭墿鏂欎俊鎭瘂itemCode}涓嶇锛岀洿鎺ヨ鐩栫粨鏋�);
 
-                        cntr.S_SPEC = itemCode;
-                        cntr.S_SOURCE = taskName;
-                    }
-                }
+                //        cntr.S_SPEC = itemCode;
+                //        cntr.S_SOURCE = taskName;
+                //    }
+                //}
 
                 var startLoc = db.Queryable<TN_Location>()
                     .Where(l => l.S_CODE == startLocCode)
@@ -255,12 +256,17 @@
                     return info;
                 }
 
+                var locCntrRelOld = db.Queryable<TN_Loc_Container>()
+                    .Where(c => c.S_CNTR_CODE == cntrCode).First();
+
                 // 缁戝畾璐т綅鍜屽鍣ㄥ彿
                 var locCntrRel = new TN_Loc_Container {
                     S_LOC_CODE = startLocCode,
-                    S_CNTR_CODE = cgDetail.S_CNTR_CODE,
-                    S_CNTR_TYPE = "鎵樼洏",
+                    S_CNTR_CODE = cntrCode,
+                    S_CNTR_TYPE = cntrType,
                 };
+
+                startLoc.N_CURRENT_NUM = 1; // 缁戝畾鍚� 
                 var endLoc = db.Queryable<TN_Location>()
                     .Where(a => Settings.AreaMap[endAreaName].Contains(a.S_AREA_CODE))
@@ -282,29 +288,38 @@
                 LocationHelper.LockEndLoc(ref endLoc); // 缁堢偣鍏ュ簱閿� 
                 using (var tran = db.Ado.UseTran()) {
-                    if (db.Insertable<TN_CG_Detail>(cgDetail).ExecuteCommand() <= 0) {
-                        tran.RollbackTran();
-                        info = $"鎻掑叆瀹瑰櫒璐у搧淇℃伅琛ㄥけ璐ワ細鐗╂枡缂栫爜{cgDetail.S_ITEM_CODE}锛屽鍣ㄧ紪鐮亄cgDetail.S_CNTR_CODE}";
-                        LogHelper.Info(info);
-                        return info;
+                    if (locCntrRelOld != null) {
+                        if (db.Deleteable<TN_Loc_Container>(locCntrRelOld).ExecuteCommand() <= 0 &&
+                            db.Updateable<TN_Location>().SetColumns(l => l.N_CURRENT_NUM == 0).Where(l => l.S_CODE == locCntrRelOld.S_LOC_CODE).ExecuteCommand() <= 0) {
+                            tran.RollbackTran();
+                            info = $"鍒犻櫎鏃ц揣浣嶅鍣ㄥ叧绯昏〃澶辫触锛氳揣浣嶇紪鐮亄locCntrRelOld.S_LOC_CODE}锛屽鍣ㄧ紪鐮亄locCntrRelOld.S_CNTR_CODE}";
+                            LogHelper.Info(info);
+                            return info;
+                        }
                     }
+                    //if (db.Insertable<TN_CG_Detail>(cgDetail).ExecuteCommand() <= 0) {
+                    //    tran.RollbackTran();
+                    //    info = $"鎻掑叆瀹瑰櫒璐у搧淇℃伅琛ㄥけ璐ワ細鐗╂枡缂栫爜{cgDetail.S_ITEM_CODE}锛屽鍣ㄧ紪鐮亄cgDetail.S_CNTR_CODE}";
+                    //    LogHelper.Info(info);
+                    //    return info;
+                    //}
 
-                    if (needInsertContainer) {
-                        if (db.Insertable<TN_Container>(cntr).ExecuteCommand() <= 0) {
-                            info = $"鎻掑叆瀹瑰櫒琛ㄥけ璐ワ細" + JsonConvert.SerializeObject(cntr);
-                            tran.RollbackTran();
-                            LogHelper.Info(info);
-                            return info;
-                        }
-                    }
-                    else if (needUpdateContainer) {
-                        if (db.Updateable<TN_Container>(cntr).ExecuteCommand() <= 0) {
-                            info = $"鏇存柊瀹瑰櫒琛ㄥけ璐ワ細" + JsonConvert.SerializeObject(cntr);
-                            tran.RollbackTran();
-                            LogHelper.Info(info);
-                            return info;
-                        }
-                    }
+                    //if (needInsertContainer) {
+                    //    if (db.Insertable<TN_Container>(cntr).ExecuteCommand() <= 0) {
+                    //        info = $"鎻掑叆瀹瑰櫒琛ㄥけ璐ワ細" + JsonConvert.SerializeObject(cntr);
+                    //        tran.RollbackTran();
+                    //        LogHelper.Info(info);
+                    //        return info;
+                    //    }
+                    //}
+                    //else if (needUpdateContainer) {
+                    //    if (db.Updateable<TN_Container>(cntr).ExecuteCommand() <= 0) {
+                    //        info = $"鏇存柊瀹瑰櫒琛ㄥけ璐ワ細" + JsonConvert.SerializeObject(cntr);
+                    //        tran.RollbackTran();
+                    //        LogHelper.Info(info);
+                    //        return info;
+                    //    }
+                    //}
 
                     if (db.Insertable<TN_Loc_Container>(locCntrRel).ExecuteCommand() <= 0) {
                         tran.RollbackTran();
@@ -313,12 +328,7 @@
                         return info;
                     }
 
-                    if (db.Updateable<TN_Location>(startLoc).UpdateColumns(it => new {
-                        it.N_LOCK_STATE,
-                        it.S_LOCK_STATE,
-                        it.S_LOCK_OP,
-                        it.T_MODIFY
-                    }).ExecuteCommand() <= 0) {
+                    if (db.Updateable<TN_Location>(startLoc).UpdateColumns(it => new { it.N_LOCK_STATE, it.S_LOCK_STATE, it.S_LOCK_OP, it.T_MODIFY, it.N_CURRENT_NUM }).ExecuteCommand() <= 0) {
                         tran.RollbackTran();
                         info = $"鐢熸垚浠诲姟'{taskName}'澶辫触锛氭洿鏂拌捣鐐硅揣浣峽startLoc.S_CODE}閿佺姸鎬佸け璐�;
                         LogHelper.Info(info);
@@ -360,11 +370,11 @@
 
     public class AddInboundTaskInfo {
         /// <summary>
-        /// 鐗╂枡缂栫爜
+        /// 鐗╂枡缂栫爜锛堟殏鏃舵病鐢級
         /// </summary>
         public string ItemCode { get; set; }
         /// <summary>
-        /// 鎵规鍙�+        /// 鎵规鍙凤紙鏆傛椂娌$敤锛�         /// </summary>
         public string BatchNo { get; set; }
         /// <summary>

--
Gitblit v1.9.1