From 18d2f40bbf40469b033de6f07ca8a8e3bc191132 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期六, 26 四月 2025 19:21:40 +0800
Subject: [PATCH] 优化代码

---
 项目代码/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarExtend/PartialTaskService.cs |   90 ++++++++++++++++++++++++++++-----------------
 1 files changed, 56 insertions(+), 34 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarExtend/PartialTaskService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarExtend/PartialTaskService.cs"
index 315eff5..45ec7ba 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarExtend/PartialTaskService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ShuttleCarJob/ShuttleCarExtend/PartialTaskService.cs"
@@ -19,10 +19,9 @@
 {
     partial class CommonShuttleCarJob
     {
-        private object _CarTaskObject = new object();
         #region 鍫嗗灈鏈虹Щ杞﹀畬鎴�
         /// <summary>
-        /// 鑾峰彇鍫嗗灈鏈虹Щ杞�/绉诲簱瀹屾垚浠诲姟
+        /// 鑾峰彇鍫嗗灈鏈虹Щ杞�/绉诲簱鍏呯數浠诲姟
         /// </summary>
         public void GetSC_CarFinish()
         {
@@ -38,9 +37,6 @@
                 UpdateShuttleCar(dt_Task.ShuttleCarCode, dt_Task.CurrentAddress);
                 SendTask(dt_Task);//涓嬪彂鍏呯數浠诲姟
             }
-            dt_Task = GetTask(TaskRelocationStatusEnum.SC_RelocationFinish.ObjToInt(), TaskRelocationTypeEnum.Relocation.ObjToInt());//绉诲簱浠诲姟
-            if (dt_Task != null)
-                SendTask(dt_Task);
         }
         #endregion
         /// <summary>
@@ -60,27 +56,37 @@
             }
             else
             {
-                var a = Row == 1 ? 2.ToString("000") : 1.ToString("000");
+                //var a = Row == 1 ? 2.ToString("000") : 1.ToString("000");
                 ShuttleCar.ShuttleCarPosition = Address;
-                ShuttleCar.ShuttleCarPosition1 = a + "-" + targetCodes[1] + "-" + targetCodes[2];
+                ShuttleCar.ShuttleCarPosition1 = Address;// a + "-" + targetCodes[1] + "-" + targetCodes[2];
             }
             _shuttleCarService.UpdateData(ShuttleCar);
         }
+        private static readonly SemaphoreSlim _CarTaskObject = new SemaphoreSlim(1, 1);
         #region 鑾峰彇绉昏溅浠诲姟
         /// <summary>
         /// 鑾峰彇绉昏溅浠诲姟/鍑哄叆搴撲换鍔�
         /// </summary>
-        public void Get_CarTask(ShuttleCar shuttleCar, ShuttleCarTaskCommandR command)
+        public async Task Get_CarTask(ShuttleCar shuttleCar, ShuttleCarTaskCommandR command)
         {
-            lock (_CarTaskObject)
+            await _CarTaskObject.WaitAsync();
+            try
             {
+
                 #region 鑾峰彇鏂板缓绉昏溅鍏呯數浠诲姟
                 Dt_Task dt_Task = GetTask((int)CarChargingTaskEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCarCharging);
                 if (dt_Task != null && dt_Task.ShuttleCarCode == shuttleCar.DeviceCode)
                 {
                     if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode))
                     {
-                        SendTask(dt_Task);
+                        Dt_Task? _Task = _taskRepository.QueryData(x => x.ShuttleCarCode == dt_Task.ShuttleCarCode && x.TaskType != TaskOtherTypeEnum.RelocationCarCharging.ObjToInt()).OrderByDescending(x => x.CreateDate).FirstOrDefault();
+                        if (_Task != null && _taskService.TaskOutboundTypes.Contains(_Task.TaskType))
+                        {
+                            if (_Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt())
+                                SendTask(dt_Task);
+                        }
+                        else
+                            SendTask(dt_Task);
                     }
                 }
                 #endregion
@@ -119,7 +125,14 @@
                     //绌挎杞︾Щ杞︿换鍔′笅鍙�
                     if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode) && dt_Task.ShuttleCarCode == shuttleCar.DeviceCode)
                     {
-                        SendTask(dt_Task);
+                        Dt_Task? _Task = _taskRepository.QueryData(x => x.ShuttleCarCode == dt_Task.ShuttleCarCode && x.TaskType != TaskOtherTypeEnum.RelocationCar.ObjToInt()).OrderByDescending(x => x.CreateDate).FirstOrDefault();
+                        if (_Task != null && _taskService.TaskOutboundTypes.Contains(_Task.TaskType))
+                        {
+                            if (_Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt())
+                                SendTask(dt_Task);
+                        }
+                        else
+                            SendTask(dt_Task);
                     }
                 }
                 #endregion
@@ -130,8 +143,10 @@
                     var task = _taskService.ShuttleCarRelocationTask(shuttleCar.DeviceCode);//鑾峰彇绌挎杞︾Щ搴撲换鍔�
                     if (task != null)
                     {
-                        if (!task.SourceIsPickPlace)
+                        if (!task.SourceIsPickPlace && task.ShuttleCarCode == shuttleCar.DeviceCode)
+                        {
                             SendTask(task);
+                        }
                     }
                     else
                     {
@@ -154,6 +169,14 @@
                     }
                 }
                 #endregion
+            }
+            catch (Exception)
+            {
+                throw;
+            }
+            finally
+            {
+                _CarTaskObject.Release();
             }
         }
         #endregion
@@ -189,9 +212,15 @@
                 #region 鍒ゆ柇褰撳墠绌挎杞︽槸鍚﹀瓨鍦ㄤ换鍔�
                 Dt_Task? _Task = _taskRepository.QueryFirst(x =>
                 x.SourceAddress == _ShuttleCar.ShuttleCarPosition ||
-                x.TargetAddress == _ShuttleCar.ShuttleCarPosition ||
-                x.SourceAddress == _ShuttleCar.ShuttleCarPosition1 ||
-                x.TargetAddress == _ShuttleCar.ShuttleCarPosition1);
+                x.TargetAddress == _ShuttleCar.ShuttleCarPosition &&
+                (!_taskService.TaskOutboundTypes.Contains(x.TaskType)));
+                if (_Task != null) return (ok, _ShuttleCar);
+                _Task = _taskRepository.QueryData(x => x.ShuttleCarCode == _ShuttleCar.ShuttleCarCode && _taskService.TaskOutboundTypes.Contains(x.TaskType)).OrderByDescending(x => x.CreateDate).FirstOrDefault();
+                if (_Task != null)
+                {
+                    if (_Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt())
+                        _Task = null;
+                }
                 #endregion
                 ok = _Task == null;
             }
@@ -231,22 +260,9 @@
                 if (command != null && command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && (command.Err_Status == (short)ShuttleCarErr.Normal || command.Err_Status == (short)ShuttleCarErr.LowBattery))
                 {
                     #region 鍒ゆ柇灏忚溅浣嶇疆鏃燗B闈笉涓嬪彂浠诲姟
-                    //short Position = 0;
-                    //if (shuttleCar.GetValue<ShuttleCarDBName, bool>(ShuttleCarDBName.DirectionA)) Position = 1;
-                    //if (shuttleCar.GetValue<ShuttleCarDBName, bool>(ShuttleCarDBName.DirectionB)) Position = 2;
-                    //if (Position != 1 && Position != 2)
-                    //{
-                    //    var Message = $"{shuttleCar.DeviceName}鏃燗B闈�";
-                    //    if (task.ExceptionMessage != Message)
-                    //    {
-                    //        task.ExceptionMessage = Message;
-                    //        _taskService.UpdateData(task);
-                    //    }
-                    //    return;
-                    //}
                     Dt_ShuttleCar _ShuttleCar = QueryCode(task.ShuttleCarCode);
                     var Row = Convert.ToInt32(_ShuttleCar.ShuttleCarPosition.Split("-")[0]);
-                    short Position = Convert.ToInt16(Row < 3 ? 1 : 2);
+                    short Position = Convert.ToInt16(Row);
                     #endregion
                     //浠诲姟杞崲
                     shuttleCar.SetValue(ShuttleCarDBName.Direction, Position);//鍐欏叆杞︿綋鏂瑰悜
@@ -257,15 +273,11 @@
                     {
                         if (task.TaskState == (int)TaskCarStatusEnum.CarNew)
                         {
-                            //if (task.Remark == ShuttleCarTaskType.ExitCharge.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark);
-                            //else
                             shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
                         }
                         else if (task.TaskState == (int)TaskCarStatusEnum.SC_CarFinish)
                         {
                             if (task.Remark == ShuttleCarTaskType.Charging.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark);
-                            //else
-                            //    shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
                         }
                     }
                     else if (task.TaskType == (int)TaskOtherTypeEnum.RelocationCarCharging)
@@ -274,9 +286,14 @@
                             (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction) :
                             (short)GetCarTaskType(ShuttleCarTaskType.Charging.ToString());
                     }
+                    else if (task.TaskType == (int)TaskRelocationTypeEnum.Relocation)
+                    {
+                        shuttleCarTaskCommand.TaskType = task.TaskState == TaskRelocationStatusEnum.RelocationNew.ObjToInt() ?
+                            (short)ShuttleCarTaskType.Out :
+                            (short)ShuttleCarTaskType.In;
+                    }
                     else
                         shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
-                    //shuttleCarTaskCommand.TaskType = string.IsNullOrEmpty(task.Remark) ? (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction) : (short)GetCarTaskType(task.Remark);
                     //浠诲姟涓嬪彂
                     if (shuttleCarTaskCommand.TaskType != null)//0
                     {
@@ -359,6 +376,11 @@
                 case (int)TaskOutboundTypeEnum.PalletOutbound:
                     state = (int)TaskOutStatusEnum.Car_OutExecuting;
                     break;
+                case (int)TaskRelocationTypeEnum.Relocation:
+                    state = state < (int)TaskRelocationStatusEnum.SC_RelocationFinish.ObjToInt() ?
+                        (int)TaskRelocationStatusEnum.CarSource_Executing :
+                        (int)TaskRelocationStatusEnum.CarTarget_Executing;
+                    break;
                 case (int)TaskOtherTypeEnum.RelocationCar:
                     state = (int)TaskCarStatusEnum.ShuttleCar_Executing;
                     break;

--
Gitblit v1.9.3