From fbb7c2253c86d58583d7e3b0a8a44d0342c43c73 Mon Sep 17 00:00:00 2001
From: kazelee <1847801760@qq.com>
Date: 星期五, 25 七月 2025 17:20:52 +0800
Subject: [PATCH] 完善单容器多物料场景的优化重构, 优化ERP回报信息的处理

---
 api/DebugController.cs |  241 +++++++++++++++++++-----------------------------
 1 files changed, 96 insertions(+), 145 deletions(-)

diff --git a/api/DebugController.cs b/api/DebugController.cs
index b59eb96..f83777f 100644
--- a/api/DebugController.cs
+++ b/api/DebugController.cs
@@ -4,13 +4,15 @@
 using System.Web.Http;
 
 using HH.WCS.Mobox3.AnGang.dispatch;
-using HH.WCS.Mobox3.AnGang.wms;
 using HH.WCS.Mobox3.AnGang.models;
+using HH.WCS.Mobox3.AnGang.util;
+using HH.WCS.Mobox3.AnGang.wms;
 
 using Newtonsoft.Json;
 
+using SqlSugar;
+
 using static HH.WCS.Mobox3.AnGang.api.ApiModel;
-using HH.WCS.Mobox3.AnGang.util;
 
 namespace HH.WCS.Mobox3.AnGang.api {
     /// <summary>
@@ -106,147 +108,26 @@
         /// <returns></returns>
         [HttpPost]
         [Route("CreateDatabase")]
-        public string CreateDatabase(CoverInfo model) {
+        public string CreateDatabase() {
             try {
                 var db = new SqlHelper<object>().GetInstance();
-                var cover = model.IsCover;
 
-                var entityTypes = new Type[] {
-                    typeof(TN_CAR_IN),
-                    typeof(TN_CG_Detail),
-                    typeof(TN_Container),
-                    typeof(TN_Loc_Container),
-                    typeof(TN_Location),
-                    typeof(TN_Task),
-                    typeof(TN_Task_Action),
-                    typeof(SYSHelper.OI_SYS_MAXID),
+                var entityTypes = new Type[] { };
 
-                };
-
-                using (var tran = db.Ado.UseTran()) {
-                    if (cover) {
-                        // 鍒犻櫎鎵�湁琛紙鎸変緷璧栧叧绯诲�搴忥級
-                        var tables = db.DbMaintenance.GetTableInfoList();
-                        foreach (var table in tables.OrderByDescending(t => t.Name)) {
-                            db.DbMaintenance.DropTable(table.Name);
-                        }
-
-                        // 鍒涘缓鏂拌〃
-                        db.CodeFirst.InitTables(entityTypes);
-
-                        //db.CodeFirst.BackupTable().InitTables(entityTypes);
-                    }
-                    else {
-                        db.CodeFirst.InitTables(entityTypes);
-                    }
-
-                    tran.CommitTran();
-                }
-
+                //db.CodeFirst.InitTables(entityTypes);
             }
             catch (Exception ex) {
                 LogHelper.Info($"鍙戠敓浜嗗紓甯�);
                 return "鍒濆鍖栨暟鎹簱閿欒" + ex.Message;
             }
-
             return "鎴愬姛";
         }
 
         /// <summary>
-        /// DEBUG锛氭彃鍏ヨ揣浣嶃�瀹瑰櫒銆佽揣鍝佷俊鎭�+        /// DEBUG锛氭牴鎹换鍔″彿璁剧疆鐗╂枡閲嶉噺
         /// </summary>
+        /// <param name="model"></param>
         /// <returns></returns>
-        [HttpPost]
-        [Route("InsertLocCntrCg")]
-        public string InsertLocCntrCg(LocCntrCg locCntrCg) {
-            var db = new SqlHelper<object>().GetInstance();
-
-            try {
-                using (var tran = db.UseTran()) {
-                    LogHelper.Info("LogCntrCg锛� + JsonConvert.SerializeObject(locCntrCg));
-                    if (string.IsNullOrEmpty(locCntrCg.LocCode)) {
-                        return "鍙傛暟闈炴硶";
-                    }
-
-                    var loc = db.Queryable<TN_Location>().First(a => a.S_CODE == locCntrCg.LocCode);
-                    if (loc == null) {
-                        var newLoc = new TN_Location {
-                            S_CODE = locCntrCg.LocCode,
-                            S_AREA_CODE = locCntrCg.LocArea ?? ""
-                        };
-
-                        if (db.Insertable<TN_Location>(newLoc).ExecuteCommand() <= 0) {
-                            tran.RollbackTran();
-                            LogHelper.Info($"鎻掑叆浣嶇疆{locCntrCg.LocCode}澶辫触");
-                            return "鎻掑叆澶辫触";
-                        }
-
-                        loc = newLoc;
-                    }
-
-                    //if (loc.N_CURRENT_NUM == 0) {
-                    //    loc.N_CURRENT_NUM = 1;
-                    //    if (db.Updateable<TN_Location>(loc).UpdateColumns(
-                    //        it => new { it.N_CURRENT_NUM, it.T_MODIFY }).ExecuteCommand() <= 0) {
-                    //        tran.RollbackTran();
-                    //        LogHelper.Info($"淇敼浣嶇疆{locCntrCg.LocCode}澶辫触");
-                    //        continue;
-                    //    }
-                    //}
-
-                    if (string.IsNullOrEmpty(locCntrCg.CntrCode)) {
-                        tran.CommitTran();
-                        LogHelper.Info("瀹瑰櫒鍙蜂负绌猴紝涓嶅啀璇诲彇鍚庨潰鐨勬暟鎹�);
-                        return "瀹瑰櫒鍙蜂负绌猴紝涓嶅啀璇诲彇鍚庨潰鐨勬暟鎹�;
-                    }
-
-                    var locCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_LOC_CODE == locCntrCg.LocCode
-                        && a.S_CNTR_CODE == locCntrCg.CntrCode);
-
-                    if (locCntrRel == null) {
-                        var newLocCntrRel = new TN_Loc_Container {
-                            S_LOC_CODE = locCntrCg.LocCode,
-                            S_CNTR_CODE = locCntrCg.CntrCode
-                        };
-
-                        loc.N_CURRENT_NUM = 1;
-
-                        if (db.Insertable<TN_Loc_Container>(newLocCntrRel).ExecuteCommand() <= 0
-                            && db.Updateable<TN_Location>(loc).UpdateColumns(c => c.N_CURRENT_NUM).ExecuteCommand() <= 0) {
-                            tran.RollbackTran();
-                            LogHelper.Info($"鎻掑叆浣嶇疆鎵樼洏鍏崇郴{locCntrCg.LocCode}-{locCntrCg.CntrCode}澶辫触");
-                            return "鎻掑叆澶辫触";
-                        }
-                    }
-
-                    if (string.IsNullOrEmpty(locCntrCg.ItemCode)) {
-                        tran.CommitTran();
-                        LogHelper.Info("鐗╂枡鍙蜂负绌猴紝涓嶅啀璇诲彇鍚庨潰鐨勬暟鎹�);
-                        return "鐗╂枡鍙蜂负绌猴紝涓嶅啀璇诲彇鍚庨潰鐨勬暟鎹�;
-                    }
-
-                    var cgDetail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == locCntrCg.CntrCode
-                        && a.S_ITEM_CODE == locCntrCg.ItemCode);
-                    if (cgDetail == null) {
-                        var locList = new List<TN_CG_Detail>();
-                        locList.Add(new TN_CG_Detail { S_CNTR_CODE = locCntrCg.CntrCode, S_ITEM_CODE = locCntrCg.ItemCode, S_BATCH_NO = locCntrCg.BatchNo ?? "" });
-                        if (db.Insertable<TN_CG_Detail>(locList).ExecuteCommand() <= 0) {
-                            tran.RollbackTran();
-                            LogHelper.Info($"鎻掑叆鎵樼洏鐗╂枡鍏崇郴{locCntrCg.CntrCode}-{locCntrCg}澶辫触");
-                            return "鎻掑叆澶辫触";
-                        }
-                    }
-                    tran.CommitTran();
-                }
-
-                return "鎻掑叆鏁版嵁鎴愬姛";
-
-            }
-            catch (Exception ex) {
-                return $"{ex.Message}";
-            }
-        }
-
         [HttpPost]
         [Route("SetTaskWeight")]
         public string SetTaskWeight(SetTaskWeightInfo model) {
@@ -258,21 +139,103 @@
                 return "鎵句笉鍒板搴旂殑鐗╂枡淇℃伅";
             }
 
-            cgDetail.F_QTY = model.Weight;
+            var task = db.Queryable<TN_Task>()
+                .Where(t => t.S_CODE == model.TaskNo).First();
+
+            if (cgDetail == null) {
+                return "鎵句笉鍒板搴旂殑浠诲姟鍙�;
+            }
+
+            cgDetail.F_WEIGHT = model.Weight;
+            task.F_WEIGHT = model.Weight;
 
             try {
-                if (db.Updateable<TN_CG_Detail>(cgDetail).UpdateColumns(it => it.F_QTY)
+                using (var tran = db.Ado.UseTran()) {
+                    if (db.Updateable<TN_CG_Detail>(cgDetail).UpdateColumns(it => it.F_WEIGHT)
                     .ExecuteCommand() <= 0) {
-                    return "淇敼澶辫触";
-                }
-                return "淇敼鎴愬姛";
+                        tran.RollbackTran();
+                        return "淇敼澶辫触";
+                    }
 
+                    if (db.Updateable<TN_Task>(task).UpdateColumns(it => it.F_WEIGHT).ExecuteCommand() <= 0) {
+                        tran.RollbackTran();
+                        return "淇敼澶辫触";
+                    }
+                    tran.CommitTran();
+                }
+                LogHelper.Info($"妯℃嫙AGV浼犻�鐗╂枡閲嶉噺锛氫慨鏀逛换鍔″彿{task.S_CODE}閲嶉噺涓簕model.Weight}", "HosttoagvTask");
+                return "淇敼鎴愬姛";
             }
+
             catch (Exception ex) {
 
                 return ex.Message;
             }
         }
+
+        [HttpPost]
+        public string AddCntrData() {
+            var db = new SqlHelper<object>().GetInstance();
+            try {
+                //var locCntrRelList = db.Queryable<TN_Loc_Container>();
+                //var result = db.Queryable<TN_CG_Detail>()
+                //    .GroupBy(d => d.S_CNTR_CODE)  // 鎸�S_CNTR_CODE 鍒嗙粍
+                //    .Select(d => new CntrCodeCountResult {
+                //        S_CNTR_CODE = d.S_CNTR_CODE,
+                //        Count = SqlFunc.AggregateCount(d.S_ITEM_CODE)  // 璁$畻姣忕粍鐨勬潯鐩暟
+                //    })
+                //    .ToList();
+
+                //var cntrList = locCntrRelList.Select(c => c.S_CNTR_CODE);
+
+                var result = db.Queryable<TN_CG_Detail>()
+                    .LeftJoin<TN_Loc_Container>((detail, container) => detail.S_CNTR_CODE == container.S_CNTR_CODE)
+                    .GroupBy(detail => detail.S_CNTR_CODE)
+                    .Select((detail, container) => new CntrCodeCountResult {
+                        S_CNTR_CODE = detail.S_CNTR_CODE,
+                        Count = SqlFunc.AggregateCount(detail.S_ITEM_CODE)
+                    })
+                    .ToList();
+
+                var cntrList = db.Queryable<TN_Container>().ToList();
+                
+                var cntrListToUpdate = new List<TN_Container>();
+                var cntrListToInsert = new List<TN_Container>();
+                foreach (var cntr in result) {
+                    var oldCntr = cntrList.Where(c => c.S_CODE == cntr.S_CNTR_CODE).First();
+                    if (oldCntr == null) {
+                        cntrListToUpdate.Add(new TN_Container { S_CODE = cntr.S_CNTR_CODE, N_DETAIL_COUNT = cntr.Count });
+                    }
+
+                    if (oldCntr.N_DETAIL_COUNT != cntr.Count) {
+                        cntrListToUpdate.Add(new TN_Container { S_CODE = cntr.S_CNTR_CODE, N_DETAIL_COUNT = cntr.Count });
+                    }
+                }
+
+                using (var tran = db.Ado.UseTran()) {
+                    if (cntrListToUpdate.Count > 0 && db.Updateable(cntrListToUpdate).ExecuteCommand() <= 0) { 
+                        tran.RollbackTran();
+                        return "鏇存柊澶辫触";
+                    }
+                    if (cntrListToInsert.Count > 0 && db.Insertable(cntrListToInsert).ExecuteCommand() <= 0) {
+                        tran.RollbackTran();
+                        return "鎻掑叆澶辫触";
+                    }
+                    tran.CommitTran();
+                }
+            }
+            catch (Exception ex) {
+
+                return ex.Message;
+            }
+
+            return "success";
+        }
+    }
+
+    public class CntrCodeCountResult {
+        public string S_CNTR_CODE { get; set; }  // 瀹瑰櫒浠g爜
+        public int Count { get; set; }           // 璇ュ鍣ㄤ唬鐮佸搴旂殑鏉$洰鏁�     }
 
     public class ReturnResults {
@@ -281,14 +244,6 @@
 
     public class GzResults {
         public List<GzResult> GzResultList { set; get; }
-    }
-
-    public class LocCntrCg {
-        public string LocCode { get; set; }
-        public string LocArea { get; set; }
-        public string CntrCode { get; set; }
-        public string ItemCode { get; set; }
-        public string BatchNo { get; set; }
     }
 
     /// <summary>
@@ -307,10 +262,6 @@
         /// AGV 涓嬩竴涓姸鎬�         /// </summary>
         public int NextState { set; get; }
-    }
-
-    public class CoverInfo {
-        public bool IsCover { set; get; } = true;
     }
 
     public class SetTaskWeightInfo {

--
Gitblit v1.9.1