From 7c926c1e3ade5158e6b1b0b805cd1b9c142e4b6c Mon Sep 17 00:00:00 2001
From: 肖洋 <cathay_xy@163.com>
Date: 星期六, 21 十二月 2024 09:45:33 +0800
Subject: [PATCH] 更新代码以使用SqlSugarScope替代SqlSugarClient,并添加生产产线属性

---
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs |  228 +++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 158 insertions(+), 70 deletions(-)

diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
index 2d8259a..7889386 100644
--- a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
@@ -1,12 +1,18 @@
 锘縰sing Autofac.Core;
 using HslCommunication;
+using Mapster;
 using Newtonsoft.Json;
 using Quartz;
 using System.Diagnostics.CodeAnalysis;
+using System.Threading.Tasks;
+using WIDESEAWCS_BasicInfoRepository;
 using WIDESEAWCS_Common.TaskEnum;
+using WIDESEAWCS_Core.Caches;
 using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_Core.HttpContextUser;
 using WIDESEAWCS_DTO.MOM;
 using WIDESEAWCS_IProcessRepository;
+using WIDESEAWCS_ITaskInfo_HtyRepository;
 using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model;
@@ -16,6 +22,8 @@
 using WIDESEAWCS_QuartzJob.Models;
 using WIDESEAWCS_QuartzJob.Service;
 using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
+using WIDESEAWCS_SignalR;
+using WIDESEAWCS_Tasks.ConveyorLineJob;
 using WIDESEAWCS_Tasks.StackerCraneJob;
 
 namespace WIDESEAWCS_Tasks
@@ -28,14 +36,22 @@
         private readonly ITaskRepository _taskRepository;
         private readonly IRouterService _routerService;
         private readonly IProcessRepository _processRepository;
+        private readonly ICacheService _cacheService;
+        private readonly INoticeService _noticeService;
+        private readonly IDt_StationManagerRepository _stationManagerRepository;
+        private readonly ITask_HtyRepository _htyRepository;
 
-        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IProcessRepository processRepository)
+        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IProcessRepository processRepository, ICacheService cacheService, INoticeService noticeService, IDt_StationManagerRepository stationManagerRepository, ITask_HtyRepository htyRepository)
         {
             _taskService = taskService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _taskRepository = taskRepository;
             _routerService = routerService;
             _processRepository = processRepository;
+            _cacheService = cacheService;
+            _noticeService = noticeService;
+            _stationManagerRepository = stationManagerRepository;
+            _htyRepository = htyRepository;
         }
 
         public Task Execute(IJobExecutionContext context)
@@ -78,14 +94,41 @@
                             }
                         }
                     }
+
+                    #region 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇
+
+                    var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
+                    if (tokenInfos == null || !tokenInfos.Any())
+                    {
+                        //throw new Exception(commonStackerCrane.DeviceName + "缂撳瓨涓湭鎵惧埌Token缂撳瓨");
+                        return Task.CompletedTask;
+                    }
+                    var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
+                    var userIds = tokenInfos?.Select(x => x.UserId).ToList();
+
+                    object obj = new
+                    {
+                        commonStackerCrane.StackerCraneStatusDes,
+                        commonStackerCrane.StackerCraneAutoStatusDes,
+                        commonStackerCrane.StackerCraneWorkStatusDes,
+                        commonStackerCrane.DeviceCode,
+                        commonStackerCrane.DeviceName,
+                        commonStackerCrane.CurrentTaskNum,
+                        commonStackerCrane.LastTaskNum,
+                    };
+                    _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonStackerCrane.DeviceName, data = obj });
+
+                    #endregion 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇
+
                 }
             }
             catch (Exception ex)
             {
                 WriteError("CommonConveyorLineJob", "test", ex);
+                ConsoleHelper.WriteErrorLine($"{ex.Message}");
                 //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
             }
-            WriteDebug("CommonConveyorLineJob", "test");
+            //WriteDebug("CommonConveyorLineJob", "test");
             return Task.CompletedTask;
         }
 
@@ -99,10 +142,49 @@
             CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
             if (commonStackerCrane != null)
             {
-                if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
+                //var x = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType);
+                //if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                 {
-                    Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
+                    //Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
+                    ConsoleHelper.WriteColorLine($"銆恵commonStackerCrane.DeviceName}銆戜换鍔″畬鎴�,浠诲姟鍙凤細銆恵e.TaskNum}銆�", ConsoleColor.Blue);
+                    var task = _taskRepository.QueryFirst(x => x.TaskNum == e.TaskNum);
+
+                    if (task == null) commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
+
+                    if (commonStackerCrane.DeviceCode.Contains("GW") && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
+                    {
+                        var station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.TargetAddress);
+
+                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == station.stationPLC);
+                        if (device != null)
+                        {
+                            CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device;
+                            var isResult = conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, task.PalletCode, task.TargetAddress);
+                            if (!isResult)
+                            {
+                                var result = conveyorLine.GetValue<ConveyorLineDBName_After, string>(ConveyorLineDBName_After.ConveyorLineBarcode, task.TargetAddress);
+                                if (result != task.PalletCode)
+                                {
+                                    conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, task.PalletCode, task.TargetAddress);
+                                }
+                            }
+                        }
+                        else
+                            return;
+                    }
+
                     _taskService.StackCraneTaskCompleted(e.TaskNum);
+                    if (commonStackerCrane.DeviceCode.Contains("CH") && task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
+                    {
+                        task = _taskRepository.QueryFirst(x => x.TaskNum == e.TaskNum);
+                        Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
+                    }
+                    if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && task.TargetAddress == "002-021-001")
+                    {
+                        var TASKHTY = task.Adapt<Dt_Task_Hty>();
+                        _taskRepository.DeleteData(task);
+                        _htyRepository.AddData(TASKHTY);
+                    }
                     commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                 }
             }
@@ -122,7 +204,8 @@
             }
             else
             {
-                if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
+                var lastTaskTypeGroup = commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup();
+                if (lastTaskTypeGroup == TaskTypeGroup.OutbondGroup)
                 {
                     task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                     if (task == null)
@@ -138,23 +221,18 @@
 
             if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
             {
-                if (OutTaskStationIsOccupied(task) != null)
+                // 妫�鏌ュ綋鍓嶅嚭搴撲换鍔$珯鍙版槸鍚﹀厑璁告斁璐�
+                var occupiedStation = OutTaskStationIsOccupied(task);
+                if (occupiedStation == null)
+                {
+                    // 濡傛灉褰撳墠鍑哄簱浠诲姟绔欏彴涓嶅厑璁告斁璐э紝鎺掗櫎褰撳墠浠诲姟锛屾煡鎵惧叾浠栧嚭搴撲换鍔�
+                    ConsoleHelper.WriteErrorLine($"浠诲姟鍙凤細銆恵task.TaskNum}銆戝嚭搴撳湴鍧�锛氥�恵task.NextAddress}銆戜笉鍏佽鏀捐揣");
+                    task = FindAnotherOutboundTask(commonStackerCrane.DeviceCode, task.TaskId);
+                }
+                else
                 {
                     return task;
                 }
-                //else
-                //{
-                //    List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress).Select(x => x.ChildPosi).ToList();
-                //    List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes);
-                //    foreach (var item in tasks)
-                //    {
-                //        if (OutTaskStationIsOccupied(task) != null)
-                //        {
-                //            return task;
-                //        }
-                //    }
-                //    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
-                //}
             }
             else if (task == null)
             {
@@ -176,34 +254,44 @@
             {
                 if (router != null)
                 {
-
                     IDevice? device = null;
                     if (task.Roadway.Contains("CW"))
                     {
                         device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode);
-                    }
-
-                    else
-                    {
-                        device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1015");
-                    }
-
-                    if (device != null)
-                    {
-                        CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device;
-                        if (conveyorLine.IsOccupied(task.TargetAddress))//鍑哄簱绔欏彴鏈鍗犵敤
+                        if (device != null)
                         {
-                            return task;
+                            CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device;
+                            if (conveyorLine.IsOccupied(task.NextAddress))//鍑哄簱绔欏彴鏈鍗犵敤
+                            {
+                                return task;
+                            }
                         }
-                    }
-                    else
-                    {
-                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒板嚭搴撶珯鍙般�恵task.TargetAddress}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞垽鏂嚭搴撶珯鍙版槸鍚﹁鍗犵敤");
+                        else
+                        {
+                            _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒板嚭搴撶珯鍙般�恵task.NextAddress}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞垽鏂嚭搴撶珯鍙版槸鍚﹁鍗犵敤");
+                        }
                     }
                 }
                 else
                 {
-                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒扮珯鍙般�恵task.NextAddress}銆戜俊鎭紝鏃犳硶鏍¢獙绔欏彴");
+                    IDevice? device = null;
+                    if (task.Roadway.Contains("GW"))
+                    {
+                        device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1015");
+                        if (device != null)
+                        {
+                            CommonConveyorLine_GW conveyorLine = (CommonConveyorLine_GW)device;
+                            if (conveyorLine.IsOccupied(task.TargetAddress))//鍑哄簱绔欏彴鏈鍗犵敤
+                            {
+                                return task;
+                            }
+                        }
+                        else
+                        {
+                            _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒板嚭搴撶珯鍙般�恵task.TargetAddress}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞垽鏂嚭搴撶珯鍙版槸鍚﹁鍗犵敤");
+                        }
+                    }
+                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒扮珯鍙般�恵task.TargetAddress}銆戜俊鎭紝鏃犳硶鏍¢獙绔欏彴");
                 }
             }
             else
@@ -215,7 +303,7 @@
                     if (device != null)
                     {
                         CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
-                        if (conveyorLine.IsOccupied(router.ChildPosi))//鍑哄簱绔欏彴鏈鍗犵敤
+                        //if (conveyorLine.IsOccupied(router.ChildPosi))//鍑哄簱绔欏彴鏈鍗犵敤
                         {
                             return task;
                         }
@@ -230,6 +318,38 @@
                     _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒扮珯鍙般�恵task.NextAddress}銆戜俊鎭紝鏃犳硶鏍¢獙绔欏彴");
                 }
             }
+            return null;
+        }
+
+
+        /// <summary>
+        /// 鏌ユ壘鍏朵粬鍑哄簱浠诲姟鐨勮緟鍔╂柟娉曪紙鎺掗櫎鎸囧畾浠诲姟ID鐨勪换鍔★級
+        /// </summary>
+        /// <param name="deviceCode">璁惧浠g爜</param>
+        /// <param name="excludedTaskId">瑕佹帓闄ょ殑浠诲姟ID</param>
+        /// <returns></returns>
+        private Dt_Task? FindAnotherOutboundTask(string deviceCode, int excludedTaskId)
+        {
+            // 鍏堣幏鍙栨墍鏈夌鍚堟潯浠讹紙鎺掗櫎鎸囧畾浠诲姟ID锛夌殑鍑哄簱浠诲姟鍒楄〃
+            var allOutboundTasks = _taskService.QueryAllOutboundTasks(deviceCode);
+            var availableTasks = allOutboundTasks?.Where(t => excludedTaskId != t.TaskId && t.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup).ToList();
+
+            if (availableTasks == null || availableTasks.Count == 0)
+            {
+                return null;
+            }
+
+            // 閬嶅巻鍙敤浠诲姟鍒楄〃锛屾鏌ヤ换鍔$珯鍙版槸鍚﹀厑璁告斁璐э紝鎵惧埌绗竴涓厑璁告斁璐х殑浠诲姟灏辫繑鍥�
+            foreach (var candidateTask in availableTasks)
+            {
+                var occupiedStation = OutTaskStationIsOccupied(candidateTask);
+                if (occupiedStation != null)
+                {
+                    return candidateTask;
+                }
+                ConsoleHelper.WriteErrorLine($"浠诲姟鍙凤細銆恵occupiedStation.TaskNum}銆戝嚭搴撳湴鍧�锛氥�恵occupiedStation.NextAddress}銆戜笉鍏佽鏀捐揣");
+            }
+
             return null;
         }
 
@@ -350,38 +470,6 @@
                 }
             }
             return stackerCraneTaskCommand;
-        }
-
-
-        /// <summary>
-        /// 璁惧蹇冭烦
-        /// </summary>
-        /// <param name="commonStackerCrane"></param>
-        public async void EqptAlive(CommonStackerCrane commonStackerCrane)
-        {
-            if (commonStackerCrane.StackerOnline)
-            {
-                BasicDto dto = new BasicDto
-                {
-                    EmployeeNo = "T00001",
-                    EquipmentCode = commonStackerCrane.DeviceCode,
-                    RequestTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
-                    SessionId = Guid.NewGuid().ToString(),
-                    Software = commonStackerCrane.DeviceName,
-                };
-                var respone = await HttpHelper.PostAsync("http://ts-momapp01:12020/api/MachineIntegration/EqptAlive", dto.ToJsonString());
-                if (respone != null)
-                {
-                    var result = JsonConvert.DeserializeObject<ResponseEqptAliveDto>(respone);
-                    if (result != null && result.Success)
-                    {
-                        if (result.KeyFlag == "99")
-                            commonStackerCrane.StackerOnline = true;
-                        else if (result.KeyFlag == "98")
-                            commonStackerCrane.StackerOnline = false;
-                    }
-                }
-            }
         }
 
     }

--
Gitblit v1.9.3