From 61e0b3dd3446986ff893c5fcf65223cd0dd5e4c1 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期三, 15 四月 2026 17:32:23 +0800
Subject: [PATCH] 优化成品库移库任务状态更新逻辑

---
 代码管理/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskMethods.cs |  161 +++++++++++++++++++++++++++++++++--------------------
 1 files changed, 100 insertions(+), 61 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskMethods.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskMethods.cs"
index 661f39d..24a178f 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskMethods.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskMethods.cs"
@@ -1,6 +1,7 @@
 锘縰sing Autofac.Core;
 using Magicodes.IE.Core;
 using NetTaste;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -8,6 +9,7 @@
 using System.Reflection.Metadata;
 using System.Text;
 using System.Text.Json;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using WIDESEAWCS_Common;
 using WIDESEAWCS_Common.LocationEnum;
@@ -83,7 +85,7 @@
                                 task.TaskState = (int)TaskStatusEnum.HktransportIng;
                                 BaseDal.UpdateData(task);
                             }
-                            else
+                            else if (task.TaskState == (int)TaskStatusEnum.Execut)
                             {
                                 task.TaskState = (int)TaskStatusEnum.Takeing;
                                 TaskFeedback(task, 1);
@@ -176,7 +178,7 @@
                                     BaseDal.UpdateData(task);
                                 }
                                 // //鍥涙ゼ鍑烘彁鍗囨満 鍏ュ簱 涔嬪墠鏄疉GV_ToExecute  //瑕佸啓鎴愬彇璐у畬鎴愮姸鎬乀akeFinish
-                                else if ((task.TaskType == TaskTypeEnum.Q1TSJ4.ObjToInt() || task.TaskType == TaskTypeEnum.F03.ObjToInt()) && task.TaskState == (int)TaskStatusEnum.AGV_ToExecute)
+                                else if ((task.TaskType == TaskTypeEnum.Q1TSJ4.ObjToInt() || task.TaskType == TaskTypeEnum.F03.ObjToInt()) && task.TaskState == (int)TaskStatusEnum.TakeFinish)
                                 {
                                     var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
                                     if (device == null) task.ExceptionMessage = "鏈壘鍒版彁鍗囨満淇℃伅";
@@ -283,7 +285,7 @@
                         case "intolift": //杩涘叆鐢垫
                             {
                                 //鍏ゼ鍒颁竴妤肩殑鍏ュ簱浠诲姟
-                                if (task.TaskType == (int)TaskTypeEnum.Q3RK || task.TaskType == (int)TaskTypeEnum.MOVE || task.TaskType == (int)TaskTypeEnum.F01)
+                                if (task.TaskType == (int)TaskTypeEnum.Q3RK || task.TaskType == (int)TaskTypeEnum.MOVE || task.TaskType == (int)TaskTypeEnum.F01 || task.TaskType == (int)TaskTypeEnum.Q3CK)
                                 {
                                     task.TaskState = TaskStatusEnum.Hkintolift.ObjToInt();
                                     BaseDal.UpdateData(task);
@@ -292,7 +294,7 @@
                             break;
                         case "leavelift": //绂诲紑鐢垫
                             {
-                                if (task.TaskType == (int)TaskTypeEnum.Q3RK || task.TaskType == (int)TaskTypeEnum.MOVE || task.TaskType == (int)TaskTypeEnum.F01)
+                                if (task.TaskType == (int)TaskTypeEnum.Q3RK || task.TaskType == (int)TaskTypeEnum.MOVE || task.TaskType == (int)TaskTypeEnum.F01 || task.TaskType == (int)TaskTypeEnum.Q3CK)
                                 {
                                     task.TaskState = TaskStatusEnum.HKleavelift.ObjToInt();
                                     BaseDal.UpdateData(task);
@@ -315,31 +317,40 @@
                     var task = BaseDal.QueryFirst(x => x.PalletCode == Rgv.content.podID);
                     if (task == null) throw new Exception($"鏈壘鍒颁换鍔�,浠诲姟缂栧彿銆恵Rgv.content.taskID}銆�");
                     //绉诲簱
-                    if (task.TaskType == TaskTypeEnum.CPMoveInventory.ObjToInt() && Rgv.messageType == 10 && Rgv.content.status == 4)
+                    if (task.TaskType == TaskTypeEnum.CPMoveInventory.ObjToInt())
                     {
-                        deviceName += "绉诲簱";
-                        WMSMoveLocationFeedback wMSMoveLocationFeedback = new WMSMoveLocationFeedback()
+                        if (Rgv.messageType == 72)
                         {
-                            containerCode = task.PalletCode,
-                            fromStationCode = task.SourceAddress,
-                            toLocationCode = task.TargetAddress
-                        };
-                        Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSMoveLocationFeedback));
-                        WMSReturn agvContent = null;
-                        if (apiInfo == null) task.ExceptionMessage = "鏈壘鍒扮Щ搴撳畬鎴愬弽棣圵MS鎺ュ彛閰嶇疆淇℃伅锛佽妫�鏌ユ帴鍙i厤缃�";
-                        else
+                            task.TaskState = TaskStatusEnum.TakeFinish.ObjToInt();
+                            BaseDal.UpdateData(task);
+                            UpdateLocationStatus(task.SourceAddress, task.PalletCode, deviceTypeEnum, LocationStatusEnum.Free);
+                        }
+                        if (Rgv.messageType == 10 && Rgv.content.status == 4)
                         {
-                            task.TaskState = TaskStatusEnum.Finish.ObjToInt();
-                            string response = HttpHelper.Post(apiInfo.ApiAddress, wMSMoveLocationFeedback.Serialize());
-                            agvContent = response.DeserializeObject<WMSReturn>();
-                            content.OK(data: agvContent);
-                            if (agvContent.code != 200)
+                            deviceName += "绉诲簱";
+                            WMSMoveLocationFeedback wMSMoveLocationFeedback = new WMSMoveLocationFeedback()
                             {
-                                task.ExceptionMessage = agvContent.message;
-                                content.Error(agvContent.message);
+                                containerCode = task.PalletCode,
+                                fromStationCode = task.SourceAddress,
+                                toLocationCode = task.TargetAddress
+                            };
+                            Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSMoveLocationFeedback));
+                            WMSReturn agvContent = null;
+                            if (apiInfo == null) task.ExceptionMessage = "鏈壘鍒扮Щ搴撳畬鎴愬弽棣圵MS鎺ュ彛閰嶇疆淇℃伅锛佽妫�鏌ユ帴鍙i厤缃�";
+                            else
+                            {
+                                task.TaskState = TaskStatusEnum.Finish.ObjToInt();
+                                string response = HttpHelper.Post(apiInfo.ApiAddress, wMSMoveLocationFeedback.Serialize());
+                                agvContent = response.DeserializeObject<WMSReturn>();
+                                content.OK(data: agvContent);
+                                if (agvContent.code != 200)
+                                {
+                                    task.ExceptionMessage = agvContent.message;
+                                    content.Error(agvContent.message);
+                                }
+                                UpdateLocationStatus(task.TargetAddress, task.PalletCode, deviceTypeEnum, LocationStatusEnum.InStock);
+                                BaseDal.DeleteAndMoveIntoHty(task, OperateTypeEnum.鑷姩瀹屾垚);
                             }
-                            UpdateLocationStatus(task.TargetAddress, task.PalletCode, deviceTypeEnum, LocationStatusEnum.InStock);
-                            BaseDal.DeleteAndMoveIntoHty(task, OperateTypeEnum.鑷姩瀹屾垚);
                         }
                     }
                     else
@@ -391,6 +402,29 @@
                                     TaskCompleted(task, deviceTypeEnum);
                                 }
                             }
+                            if (Rgv.content.status == 5)
+                            {
+                                task.ExceptionMessage = Rgv.content.errorReason;
+                                var Error = _taskErrorMessageService.Repository.QueryFirst(x => x.ErrorCode == Rgv.content.errorCode && x.DeviceType == (int)deviceTypeEnum);
+                                if (task.TaskType == (int)TaskTypeEnum.CPOutbound && task.TaskState == (int)TaskStatusEnum.Execut && Rgv.content.errorCode == 1330003)//鎴愬搧鍑哄簱浠诲姟涓嬪彂鍚庡弽棣堟湁闃荤鎵橈紝淇敼浠诲姟鐘舵�佷负鏂板缓閲嶆柊鏌ヨ闃荤鎵樺叧绯�
+                                {
+                                    task.TaskState = (int)TaskStatusEnum.New;
+                                    BaseDal.UpdateData(task);
+                                }
+                                else
+                                {
+                                    if (Error != null)
+                                    {
+                                        task.ExceptionMessage = Error.ExceptionMessage;
+                                        MatchCollection matches = Regex.Matches(Rgv.content.errorReason, @"\(([^)]+)\)");
+                                        if (matches.Count > 0)
+                                            task.ExceptionMessage = ReplacePlaceholders(Rgv.content.errorReason, Error.ExceptionMessage);
+                                        ErrorTaskFeedback(task, true, Error.ErrorCode.ToString());
+                                    }
+                                    else
+                                        ErrorTaskFeedback(task, true);
+                                }
+                            }
                         }
                     }
                 }
@@ -405,6 +439,30 @@
                 _trackloginfoService.AddTrackLog(TaskDTO, content, $"{deviceName}浠诲姟鐘舵�佸弽棣�", "", "");
             }
             return content;
+        }
+        #endregion
+
+        #region 寮傚父淇℃伅杞崲
+        /// <summary>
+        /// 寮傚父淇℃伅()鍐呭杞崲
+        /// </summary>
+        /// <param name="source"></param>
+        /// <param name="targetTemplate"></param>
+        /// <returns></returns>
+        public string ReplacePlaceholders(string source, string targetTemplate)
+        {
+            // 鎻愬彇婧愬瓧绗︿覆涓殑鎵�鏈夋嫭鍙峰��
+            var values = Regex.Matches(source, @"\(([^)]+)\)")
+                              .Cast<Match>()
+                              .Select(m => m.Groups[1].Value)
+                              .ToList();
+
+            // 鏇挎崲鐩爣妯℃澘涓殑鎵�鏈� (%v)
+            int idx = 0;
+            string result = Regex.Replace(targetTemplate, @"\(\%v\)", _ =>
+                idx < values.Count ? values[idx++] : "");
+
+            return result;
         }
         #endregion
 
@@ -597,38 +655,22 @@
                         dt_RGVLocationInfo.LocationStatus = locationStatusEnum.ObjToInt();
                         _rGVLocationInfoService.Repository.UpdateData(dt_RGVLocationInfo);
                     }
-                }
-                else//骞冲簱搴撲綅
-                {
 
-                }
-                return WebResponseContent.Instance.OK();
-            }
-            catch (Exception ex)
-            {
-                throw new Exception(ex.Message);
-            }
-        }
-        #endregion
-
-        #region 鏇存柊涓�妤肩珯鍙扮姸鎬�
-        public WebResponseContent UpdateStandStatus(Dt_Task task, string stationCode, LocationStatusEnum locationStatusEnum)
-        {
-            try
-            {
-                Dt_StationManger? stationManger = _stationMangerService.Repository.QueryFirst(x => x.StationCode == task.TargetAddress);
-                //stationManger.IsOccupied = LocationStatusEnum.Lock.ObjToInt();
-                if (stationManger != null)
-                {
-                    if (locationStatusEnum == LocationStatusEnum.Free) //濡傛灉璧风偣瀹屾垚灏辩粰璐т綅璧嬪�兼墭鐩樺彿
+                    Dt_StationManger stationManger = _stationMangerService.Repository.QueryFirst(x => x.StationCode == locationCode);
+                    if (stationManger != null)
                     {
-                        stationManger.IsOccupied = LocationStatusEnum.Lock.ObjToInt();
+                        if (locationStatusEnum == LocationStatusEnum.Free) //濡傛灉璧风偣瀹屾垚灏辩粰璐т綅璧嬪�兼墭鐩樺彿
+                        {
+                            stationManger.Remark = "";
+                            stationManger.IsOccupied = LocationStatusEnum.Free.ObjToInt();
+                        }
+                        else
+                        {
+                            stationManger.Remark = palletCode;
+                            stationManger.IsOccupied = LocationStatusEnum.Lock.ObjToInt();
+                        }
+                        _stationMangerService.UpdateData(stationManger);
                     }
-                    else
-                    {
-                        stationManger.IsOccupied = LocationStatusEnum.Free.ObjToInt();
-                    }
-                    _stationMangerService.UpdateData(stationManger);
                 }
                 return WebResponseContent.Instance.OK();
             }
@@ -640,7 +682,7 @@
         #endregion
 
         #region 涓婃姤WMS
-        public WebResponseContent TaskFeedback(Dt_Task dt_Task, int Status, bool Del = true)
+        public WebResponseContent TaskFeedback(Dt_Task dt_Task, int Status, bool Del = false)
         {
             WebResponseContent content = new WebResponseContent();
             WMSInOutBoundCompleteFeedback boundCompleteFeedback = new WMSInOutBoundCompleteFeedback();
@@ -683,7 +725,7 @@
         }
         #endregion
         #region 澶辫触浠诲姟涓婃姤WMS骞跺垹闄や换鍔�
-        public WebResponseContent ErrorTaskFeedback(Dt_Task dt_Task, bool Del)
+        public WebResponseContent ErrorTaskFeedback(Dt_Task dt_Task, bool Del = false, string ErrorCode = "")
         {
             WebResponseContent content = new WebResponseContent();
             WMSInOutBoundCompleteFeedback boundCompleteFeedback = new WMSInOutBoundCompleteFeedback();
@@ -699,7 +741,7 @@
                     boundCompleteFeedback.fromStationCode = dt_Task.SourceAddress;
                     boundCompleteFeedback.toLocationCode = dt_Task.TargetAddress;
                     boundCompleteFeedback.status = 3;
-                    boundCompleteFeedback.custStatus = "";
+                    boundCompleteFeedback.custStatus = ErrorCode;
                     boundCompleteFeedback.memo = dt_Task.ExceptionMessage;
                     string response = HttpHelper.Post(apiInfo.ApiAddress, boundCompleteFeedback.Serialize());
                     agvContent = response.DeserializeObject<WMSReturn>();
@@ -720,15 +762,12 @@
             finally
             {
                 _trackloginfoService.AddTrackLog(boundCompleteFeedback, content, "鍑哄叆搴撲换鍔$姸鎬佸弽棣圵MS", "", "");
-                if (agvContent != null && agvContent.code == 200 && Del) BaseDal.DeleteAndMoveIntoHty(dt_Task, OperateTypeEnum.鑷姩鍒犻櫎);
+                if (agvContent != null && agvContent.code == 200 && Del)
+                    BaseDal.DeleteAndMoveIntoHty(dt_Task, OperateTypeEnum.鑷姩鍒犻櫎);
                 else BaseDal.UpdateData(dt_Task);
             }
             return content;
         }
         #endregion
-
-
-
-
     }
 }

--
Gitblit v1.9.3