From b513ce3a4527e998da66c6f179a279472c7262a8 Mon Sep 17 00:00:00 2001
From: wangxinhui <wangxinhui@hnkhzn.com>
Date: 星期二, 18 十一月 2025 10:59:46 +0800
Subject: [PATCH] 代码更新优化

---
 项目代码/WCS/WCSServices/WIDESEAWCS_Tasks/原料库/StackerCraneJob_YLSC2.cs |  117 ++++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 85 insertions(+), 32 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs"
index 4c1a6ca..f346954 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServices/WIDESEAWCS_Tasks/\345\216\237\346\226\231\345\272\223/StackerCraneJob_YLSC2.cs"
@@ -23,6 +23,8 @@
 using SqlSugar.Extensions;
 using WIDESEAWCS_Tasks.ConveyorLineJob;
 using WIDESEAWCS_QuartzJob.Repository;
+using WIDESEAWCS_QuartzJob.DTO;
+using WIDESEAWCS_QuartzJob.StackerCrane;
 
 namespace WIDESEAWCS_Tasks
 {
@@ -57,25 +59,35 @@
                     {
                         commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//璁㈤槄浠诲姟瀹屾垚浜嬩欢
                     }
-                    commonStackerCrane.CheckStackerCraneTaskCompleted();//闃叉浠诲姟瀹屾垚浜嬩欢鐩戞祴瓒呮椂锛屽啀鎵嬪姩瑙﹀彂涓�娆�
-                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
+                    //commonStackerCrane.CheckStackerCraneTaskCompleted();//闃叉浠诲姟瀹屾垚浜嬩欢鐩戞祴瓒呮椂锛屽啀鎵嬪姩瑙﹀彂涓�娆�
+                    //鑾峰彇褰撳墠鍫嗗灈鏈虹姸鎬�
+                    if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.WorkCompleted)
                     {
-                        Dt_Task? task = GetTask(commonStackerCrane);
-                        if (task != null)
+                        StackerCraneTaskCompletedEventArgs e = new StackerCraneTaskCompletedEventArgs(commonStackerCrane.CurrentTaskNum);
+                        CommonStackerCrane_StackerCraneTaskCompletedEventHandler(commonStackerCrane, e);
+                    }
+                    else if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)//鍒ゆ柇涓嬪彂
+                    {
+                        short stackerError = commonStackerCrane.Communicator.Read<short>("DB1000.54.0");
+                        if (stackerError==0)
                         {
-                            StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
-                            if (stackerCraneTaskCommand != null)
+                            Dt_Task? task = GetTask(commonStackerCrane);
+                            if (task != null)
                             {
-                                bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
-                                if (sendFlag)
+                                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
+                                if (stackerCraneTaskCommand != null)
                                 {
-                                    commonStackerCrane.LastTaskType = task.TaskType;
-                                    task.Dispatchertime = DateTime.Now;
-                                    task.ExceptionMessage = "";
-                                    _taskService.UpdateTask(task, TaskStatusEnum.SC_Executing);
-                                    commonStackerCrane.Communicator.Write("DB1000.20.0", true);
-                                    //寤舵椂1s
-                                    Thread.Sleep(1000);
+                                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
+                                    if (sendFlag)
+                                    {
+                                        commonStackerCrane.LastTaskType = task.TaskType;
+                                        task.Dispatchertime = DateTime.Now;
+                                        task.ExceptionMessage = "";
+                                        _taskService.UpdateTask(task, TaskStatusEnum.SC_Executing);
+                                        commonStackerCrane.Communicator.Write("DB1000.20.0", true);
+                                        //寤舵椂1s
+                                        Thread.Sleep(1000);
+                                    }
                                 }
                             }
                         }
@@ -94,7 +106,7 @@
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
-        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
+        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e)
         {
             CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
             if (commonStackerCrane != null)
@@ -112,19 +124,24 @@
         {
             try
             {
-                Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == taskNum);
+                Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == taskNum && x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt());
                 if (task != null)
                 {
                     if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                     {
-                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StackerCraneStationCode == task.NextAddress && x.StackerCraneCode == deviceCode);
+                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.NextAddress && x.StackerCraneCode == deviceCode);
                         if (stationManger == null)
                         {
                             _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"杈撻�佺嚎鍑哄簱绔欑偣鏈厤缃�,{task.NextAddress}");
                             _taskService.UpdateTaskExceptionMessage(taskNum, $"杈撻�佺嚎鍑哄簱绔欑偣鏈厤缃�,{task.NextAddress}");
                             return WebResponseContent.Instance.Error($"杈撻�佺嚎鍑哄簱绔欑偣鏈厤缃�,{task.NextAddress}");
                         }
-                        Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && x.StartPosi == stationManger.StationCode && x.ChildPosi == stationManger.StationDeviceCode);
+                        bool depth = false;
+                        if (task.Grade>0 && task.TaskType==TaskTypeEnum.PrintYLOutbound.ObjToInt())
+                        {
+                            depth = true;
+                        }
+                        Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && x.StartPosi == stationManger.StationCode && x.ChildPosi == stationManger.StationDeviceCode && (depth ? x.Depth == 2 :  true));
                         if (router == null)
                         {
                             _taskService.UpdateTaskExceptionMessage(taskNum, $"鏈壘鍒拌矾鐢变俊鎭�,{task.NextAddress}");
@@ -132,26 +149,42 @@
                             return WebResponseContent.Instance.Error($"鏈壘鍒拌矾鐢变俊鎭�,{task.NextAddress}");
                         }
                         int oldStatus = task.TaskState;
-                        //todo 鍫嗗灈鏈哄畬鎴愬畬鎴�
-                        _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: router.NextPosi);
+                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
+                        if (device != null)
+                        {
+                            OtherDevice otherDevice = (OtherDevice)device;
+                            otherDevice.SetValue(WR_CLineYLDB.WR_Task, taskNum, stationManger.StationCode);
+                            // 鍫嗗灈鏈哄畬鎴�
+                            _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: router.NextPosi);
+                        }
+                        else
+                        {
+                            WriteError(deviceCode, $"鏈壘鍒板嚭搴撶珯鍙般�恵stationManger.StationDeviceCode}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞彂閫佽緭閫佺嚎鍑哄簱淇″彿");
+                            _taskService.UpdateTaskExceptionMessage(taskNum, $"鏈壘鍒板嚭搴撶珯鍙般�恵stationManger.StationDeviceCode}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞彂閫佽緭閫佺嚎鍑哄簱淇″彿");
+                            return WebResponseContent.Instance.Error($"鏈壘鍒板嚭搴撶珯鍙般�恵stationManger.StationDeviceCode}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞彂閫佽緭閫佺嚎鍑哄簱淇″彿");
+                        }
                         _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"绯荤粺鑷姩娴佺▼,浠诲姟鐘舵�佷粠銆恵oldStatus}銆戣浆鍒般�恵task.TaskState}銆�");
                         WriteInfo(deviceCode, $"鍫嗗灈鏈哄嚭搴撲换鍔″畬鎴�,浠诲姟鍙�:{taskNum}");
                     }
                     else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup || task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
                     {
-                        WriteInfo(deviceCode, $"鍫嗗灈鏈轰换鍔″畬鎴�,浠诲姟鍙�:{taskNum}");
-                        _taskService.TaskCompleted(taskNum);
+                        WebResponseContent responseContent = _taskService.TaskCompleted(taskNum);
+                        if (!responseContent.Status)
+                        {
+                            _taskService.UpdateTaskExceptionMessage(taskNum, $"{responseContent.Message}");
+                        }
+                        WriteInfo(deviceCode, $"鍫嗗灈鏈哄叆搴撲换鍔″畬鎴�,浠诲姟鍙�:{taskNum}");
                     }
                     else
                     {
-                        WriteInfo(deviceCode, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
+                        WriteError(deviceCode, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
                         _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
                         _taskService.UpdateTaskExceptionMessage(taskNum, $"鏈壘鍒拌浠诲姟绫诲瀷鍥炶皟WMS浠诲姟瀹屾垚鎺ュ彛,{task.TaskType}");
                     }
                 }
                 else
                 {
-                    WriteInfo(deviceCode, $"鏈壘鍒颁换鍔′俊鎭�,浠诲姟鍙�:{taskNum}");
+                    WriteError(deviceCode, $"鏈壘鍒颁换鍔′俊鎭�,浠诲姟鍙�:{taskNum}");
                     return WebResponseContent.Instance.Error($"鏈壘鍒颁换鍔′俊鎭�,浠诲姟鍙�:{taskNum}");
                 }
 
@@ -172,7 +205,14 @@
         /// <returns></returns>
         private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
         {
-            Dt_Task task;
+            //鍒ゆ柇鏄惁鏈夋鎵ц鐨勪换鍔★紝濡傛灉鏈夊垯涓嶄笅鍙�
+            Dt_Task? taskOld = _taskRepository.QueryFirst(x => x.DeviceCode == commonStackerCrane.DeviceCode && x.Roadway == commonStackerCrane.DeviceCode && x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt());
+            if (taskOld != null)
+            {
+                return null;
+            }
+
+            Dt_Task? task;
             if (commonStackerCrane.LastTaskType == null)
             {
                 task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
@@ -181,6 +221,7 @@
             {
                 if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                 {
+                    //涓婁竴涓负鍑哄簱鍒欐煡鏄惁瀛樺湪鍏ュ簱浠诲姟 浜ゆ浛鎵ц
                     task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                     if (task == null)
                     {
@@ -189,6 +230,7 @@
                 }
                 else
                 {
+                    //涓婁竴涓负鍏ュ簱鍒欐煡鏄惁瀛樺湪鍑哄簱浠诲姟 浜ゆ浛鎵ц
                     task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                     if (task == null)
                     {
@@ -199,6 +241,7 @@
 
             if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
             {
+                //鍒ゆ柇鍑哄簱绔欏彴鍗犵敤
                 if (OutTaskStationIsOccupied(task) == null)
                 {
                     bool flag = false;
@@ -218,7 +261,11 @@
                     }
                 }
             }
-            task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
+            if (task == null)
+            {
+                task = _taskService.QuertStackerCraneTask(commonStackerCrane.DeviceCode, TaskTypeGroup.RelocationGroup);
+            }
+            
             return task;
         }
 
@@ -238,9 +285,14 @@
                     OtherDevice client = (OtherDevice)device;
                     if (client.GetValue<WR_CLineYLDB, short>(WR_CLineYLDB.WR_Request, stationManger.StationCode) != 99)//鍑哄簱绔欏彴鏈鍗犵敤
                     {
-                        task.NextAddress = stationManger.StackerCraneStationCode;
-                        _taskRepository.UpdateData(task);
+                        //task.NextAddress = stationManger.StackerCraneStationCode;
+                        //_taskRepository.UpdateData(task);
                         return task;
+                    }
+                    else
+                    {
+                        task.ExceptionMessage = "鍑哄簱绔欏彴鏃犳硶涓嬪彂";
+                        _taskRepository.UpdateData(task);
                     }
                 }
                 else
@@ -298,9 +350,10 @@
                     return null;
                 }
             }
-            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
+            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)//鍒ゆ柇鏄惁鏄嚭搴撲换鍔�
             {
-                string[] targetCodes = task.NextAddress.Split("-");
+                Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.NextAddress && x.StackerCraneCode == task.DeviceCode);
+                string[] targetCodes = stationManger.StackerCraneStationCode.Split("-");
                 if (targetCodes.Length == 3)
                 {
                     stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
@@ -327,7 +380,7 @@
                     return null;
                 }
             }
-            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
+            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)//鍒ゆ柇鏄惁鏄Щ搴撲换鍔�
             {
                 string[] targetCodes = task.NextAddress.Split("-");
                 if (targetCodes.Length == 5)

--
Gitblit v1.9.3