From 225d11e2ddaa55d1d482201cb4d89c9486cdba69 Mon Sep 17 00:00:00 2001
From: wangxinhui <wangxinhui@hnkhzn.com>
Date: 星期五, 20 六月 2025 12:27:52 +0800
Subject: [PATCH] 更新代码

---
 代码管理/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/板材仓/RGVJob_BC.cs |  207 +++++++++++++++++++++++++++++++--------------------
 1 files changed, 125 insertions(+), 82 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/\346\235\277\346\235\220\344\273\223/RGVJob_BC.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/\346\235\277\346\235\220\344\273\223/RGVJob_BC.cs"
index 06d054b..75fb49e 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/\346\235\277\346\235\220\344\273\223/RGVJob_BC.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/\346\235\277\346\235\220\344\273\223/RGVJob_BC.cs"
@@ -1,5 +1,6 @@
 锘縰sing Microsoft.AspNetCore.Components.Routing;
 using Microsoft.IdentityModel.Tokens;
+using Newtonsoft.Json;
 using Quartz;
 using System;
 using System.Collections.Generic;
@@ -10,6 +11,7 @@
 using WIDESEAWCS_Common.Helper;
 using WIDESEAWCS_Common.TaskEnum;
 using WIDESEAWCS_Core;
+using WIDESEAWCS_Core.Caches;
 using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_IBasicInfoRepository;
 using WIDESEAWCS_ITaskInfoRepository;
@@ -27,22 +29,36 @@
     [DisallowConcurrentExecution]
     public class RGVJob_BC : JobBase, IJob
     {
-
+        private readonly ICacheService _cacheService;
         private readonly ITaskService _taskService;
         private readonly ITaskExecuteDetailService _taskExecuteDetailService;
         private readonly ITaskRepository _taskRepository;
         private readonly IStationMangerRepository _stationMangerRepository;
         private readonly IRouterRepository _routerRepository;
         private readonly IRouterService _routerService;
+        private readonly IRouterExtension _routerExtension;
+        private readonly List<Dt_WarehouseDevice> warehouseDevices;
 
-        public RGVJob_BC(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService)
+        public RGVJob_BC(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension)
         {
+            _cacheService = cacheService;
             _taskService = taskService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _taskRepository = taskRepository;
             _stationMangerRepository = stationMangerRepository;
             _routerRepository = routerRepository;
             _routerService = routerService;
+            _routerExtension = routerExtension;
+
+            string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice));
+            if (!string.IsNullOrEmpty(warehouseDevicesStr))
+            {
+                warehouseDevices = JsonConvert.DeserializeObject<List<Dt_WarehouseDevice>>(warehouseDevicesStr) ?? new List<Dt_WarehouseDevice>();
+            }
+            else
+            {
+                warehouseDevices = new List<Dt_WarehouseDevice>();
+            }
         }
 
         public Task Execute(IJobExecutionContext context)
@@ -51,6 +67,14 @@
             if (flag && value != null)
             {
                 OtherDevice device = (OtherDevice)value;
+
+                Dt_WarehouseDevice? warehouseDevice = warehouseDevices.FirstOrDefault(x => x.DeviceCode == device.DeviceCode);
+                if (warehouseDevice == null)
+                {
+                    WriteError(device.DeviceName, $"璇烽厤缃粨搴撹澶囦俊鎭�");
+                    return Task.CompletedTask;
+                }
+
                 List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).ToList();
                 List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
                 foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
@@ -74,102 +98,121 @@
                         ConveyorLineSignal conveyorLineSignalRead = conveyorLineInfoRead.Signal.ByteToBoolObject<ConveyorLineSignal>();
 
                         ConveyorLineSignal conveyorLineSignalWrite = conveyorLineInfoWrite.Signal.ByteToBoolObject<ConveyorLineSignal>();
-
+                        //濡傛灉鏈夐��鏂欐垨鑰呭叆搴撲换鍔¤繘琛岀鎺�
+                        Dt_Task taskInControl = _taskRepository.QueryFirst(x => x.TaskType>=TaskTypeEnum.Inbound.ObjToInt() && x.WarehouseId== warehouseDevice.WarehouseId);
+                        if (item.StationCode=="1511")
+                        {
+                            short isControl = device.GetValue<W_ConveyorLineDB, short>(W_ConveyorLineDB.Spare2, item.StationCode);
+                            if (taskInControl!=null && isControl == 0)
+                            {
+                                device.SetValue(W_ConveyorLineDB.Spare2, 1, item.StationCode);
+                                WriteDebug($"{item.StationCode}绠℃帶", $"绠℃帶鎺у埗");
+                            }
+                            else if (isControl==1 && taskInControl==null)
+                            {
+                                device.SetValue(W_ConveyorLineDB.Spare2, 0, item.StationCode);
+                                WriteDebug($"{item.StationCode}绠℃帶", $"绠℃帶鎭㈠");
+                            }
+                        }
                         if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt())
                         {
-                            if (conveyorLineSignalRead.STB /*&& conveyorLineStatus.Online && conveyorLineStatus.Goods*/ && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode) && conveyorLineInfoRead.TaskNum == 0)//鍏ュ簱
+                            //if (conveyorLineSignalRead.STB && !conveyorLineSignalWrite.ACK && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode) && conveyorLineInfoRead.TaskNum == 0)//鍏ュ簱
+                            //{
+                            //    List<string> stations = _routerExtension.GetEndPoint(item.StationCode, TaskTypeEnum.Inbound.ObjToInt()).Select(x => x.NextPosi).ToList();
+                            //    WebResponseContent responseContent = _taskService.RequestWMSTask(conveyorLineInfoRead.Barcode, item.StationCode,  conveyorLineInfoRead.Spare2, stations);
+                            //    if (responseContent.Status)
+                            //    {
+                            //        Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.Barcode && x.CurrentAddress == item.StationCode && x.DeviceCode == device.DeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt());
+                            //        if (task != null)
+                            //        {
+                            //            device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
+                            //            device.SetValue(W_ConveyorLineDB.StartPos, task.CurrentAddress, item.StationCode);
+                            //            device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
+                            //            device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode);
+                            //            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
+                            //            _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
+                            //        }
+                            //    }
+                            //}
+
+                            if (conveyorLineSignalRead.STB && !conveyorLineSignalWrite.ACK && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode) && conveyorLineInfoRead.TaskNum == 0)//鍏ュ簱
                             {
-                                Dt_Task task = _taskRepository.QueryFirst(x => x.SourceAddress == item.StationDeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt() && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.PalletCode == conveyorLineInfoRead.Barcode && string.IsNullOrEmpty(x.DeviceCode) && string.IsNullOrEmpty(x.NextAddress) && string.IsNullOrEmpty(x.TargetAddress));
-                                if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
+                                WebResponseContent content = _taskService.RequestWMSTaskSimple(conveyorLineInfoRead.Barcode, item.StationCode);
+                                if (content.Status)
                                 {
-                                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.StationCode == item.StationCode);
-                                    if (stationManger == null)
+                                    Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.Barcode && x.CurrentAddress == item.StationCode && x.DeviceCode == device.DeviceCode && x.TaskState == TaskStatusEnum.New.ObjToInt());
+                                    if (task != null)
                                     {
-                                        WriteError(item.StationName, $"鏈壘鍒板搴旂珯鍙颁俊鎭�,璁惧缂栧彿:{item.StationCode},浠诲姟鍙�:{task.TaskNum}");
-                                        continue;
-                                    }
-                                    List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && (task.Roadway == x.ChildPosiDeviceCode || task.Roadway == x.ChildPosi));
-                                    Dt_Router? router = routers.FirstOrDefault();
-                                    if (router == null)
-                                    {
-                                        WriteError(device.DeviceName, $"鏈壘鍒颁换鍔$被鍨媨task.TaskType}瀵瑰簲鐨勮矾鐢变俊鎭�");
-                                    }
-                                    else
-                                    {
-                                        if (routers.Count == 1)
-                                        {
-                                            task.Dispatchertime = DateTime.Now;
-                                            _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing, deviceCode: router.ChildPosi, targetAddress: router.NextPosi, nextAddress: router.StartPosi, currentAddress: item.StationCode);
-
-                                            device.SetValue(W_ConveyorLineDB.StartPos, item.StationCode, item.StationCode);
-                                            device.SetValue(W_ConveyorLineDB.EndPos, router.StartPosi, item.StationCode);
-                                            device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
-                                            device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
-                                            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
-                                        }
-                                        else
-                                        {
-                                            List<string> canInboundStations = new List<string>();
-                                            List<string> stations = routers.Select(x => x.NextPosi).ToList();
-                                            foreach (string station in stations)
-                                            {
-                                                Dt_StationManger stationManger1 = _stationMangerRepository.QueryFirst(x => x.StationCode == station);
-                                                if (stationManger1 != null)
-                                                {
-                                                    IDevice? device1 = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger1.StationDeviceCode);
-                                                    if (device1 != null)
-                                                    {
-                                                        OtherDevice otherDevice = (OtherDevice)device1;
-                                                        //short workType = otherDevice.GetValue<R_ConveyorLineWorkType, short>(R_ConveyorLineWorkType.ConveyorLineWorkType, item.StationCode);
-                                                        //if (workType == ConveyorWorkTypeEnum.Undefined.ObjToInt() || workType == ConveyorWorkTypeEnum.Inbound.ObjToInt())
-                                                        {
-                                                            canInboundStations.Add(stationManger1.Remark);
-                                                        }
-                                                    }
-                                                }
-                                            }
-
-                                            WebResponseContent responseContent = _taskService.RequestWMSAssignRoadway(task.TaskNum, canInboundStations);
-                                            if (responseContent.Status)
-                                            {
-                                                List<Dt_Router> dt_Routers = _routerService.QueryNextRoutes(item.StationCode, responseContent.Data.ToString() ?? "", task.TaskType);
-
-                                                Dt_Router? routerTemp = _routerService.QueryNextRoutes(item.StationCode, responseContent.Data.ToString() ?? "", task.TaskType).FirstOrDefault();
-                                                if (routerTemp != null)
-                                                {
-                                                    task.Dispatchertime = DateTime.Now;
-                                                    task.Roadway = responseContent.Data.ToString() ?? task.Roadway;
-                                                    _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing, deviceCode: routerTemp.ChildPosiDeviceCode, targetAddress: routerTemp.NextPosi, nextAddress: routerTemp.NextPosi, currentAddress: item.StationCode);
-
-                                                    device.SetValue(W_ConveyorLineDB.StartPos, item.StationCode, item.StationCode);
-                                                    device.SetValue(W_ConveyorLineDB.EndPos, routerTemp.NextPosi, item.StationCode);
-                                                    device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
-                                                    device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
-                                                    device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
-                                                }
-                                                else
-                                                {
-                                                    WriteError(device.DeviceName, $"鏈壘鍒颁换鍔$被鍨媨task.TaskType},璧风偣{item.StationCode},缁堢偣{responseContent.Data.ToString()}鐨勮矾鐢变俊鎭�");
-                                                }
-                                            }
-                                            else
-                                            {
-                                                WriteError(device.DeviceName, responseContent.Message);
-                                            }
-                                        }
+                                        device.SetValue(W_ConveyorLineDB.TaskNum, task.TaskNum, item.StationCode);
+                                        device.SetValue(W_ConveyorLineDB.StartPos, task.CurrentAddress, item.StationCode);
+                                        device.SetValue(W_ConveyorLineDB.Spare1, ConveyorWorkTypeEnum.Inbound.ObjToInt(), item.StationCode);
+                                        device.SetValue(W_ConveyorLineDB.EndPos, task.NextAddress, item.StationCode);
+                                        device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
+                                        _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute);
                                     }
                                 }
                             }
-                            else if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !conveyorLineSignalWrite.ACK && conveyorLineInfoRead.TaskNum > 0)//鍑哄簱
+                            else if (conveyorLineSignalRead.STB /*&& conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm*/ && !conveyorLineSignalWrite.ACK && conveyorLineInfoRead.TaskNum > 0)//鍑哄簱
                             {
-                                //_taskService.TaskCompleted(conveyorLineInfoRead.TaskNum);
+                                WriteDebug(device.DeviceName, $"浠诲姟瀹屾垚,浠诲姟鍙�:{conveyorLineInfoRead.TaskNum}");
+                                _taskService.TaskCompleted(conveyorLineInfoRead.TaskNum);
                                 device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                             }
                             else if (!conveyorLineSignalRead.STB && conveyorLineSignalWrite.ACK && !conveyorLineStatus.Alarm)
                             {
+                                Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == conveyorLineInfoRead.Barcode);
+                                if (task!=null && (task.TaskType == TaskTypeEnum.MesPalletLargeReturn.ObjToInt() || task.TaskType == TaskTypeEnum.MesPalletSmallReturn.ObjToInt()))
+                                {
+                                    _taskService.TaskCompleted(task.TaskNum);
+                                }
                                 device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
                             }
                         }
+                        //鍏佽鏀捐涓�鍙峰紑鏂欏彛鍑哄簱浠诲姟
+                        bool outAllowLine1 = device.Communicator.Read<bool>("DB14.4.6");
+                        if (outAllowLine1)
+                        {
+                            Dt_Task taskExcuting = _taskRepository.QueryFirst(x => x.WarehouseId== warehouseDevice.WarehouseId && (x.TargetAddress== "SCUTL-001" || x.TargetAddress == "1101")
+                            && (x.TaskState==TaskStatusEnum.SC_Execute.ObjToInt() || x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt() 
+                            || x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() || x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt()));
+                            Dt_Task? taskExecute = _taskRepository.QueryData(x => x.WarehouseId == warehouseDevice.WarehouseId && x.TargetAddress == "SCUTL-001"
+                            && x.TaskState==TaskStatusEnum.MesPending.ObjToInt()).OrderByDescending(x=>x.Grade).ThenBy(x=>x.TaskNum).ThenBy(x=>x.CreateDate).FirstOrDefault();
+                            if (taskExcuting == null && taskExecute!=null)
+                            {
+                                _taskService.UpdateTask(taskExecute, TaskStatusEnum.SC_Execute);
+                                WriteDebug($"涓�鍙峰紑鏂欏彛鏀捐浠诲姟", $"浠诲姟鍙穥taskExecute.TaskNum}");
+                            }
+                        }
+                        //鍏佽鏀捐浜屽彿寮�鏂欏彛鍑哄簱浠诲姟
+                        bool outAllowLine2 = device.Communicator.Read<bool>("DB14.12.6");
+                        if (outAllowLine2)
+                        {
+                            Dt_Task taskExcuting = _taskRepository.QueryFirst(x => x.WarehouseId == warehouseDevice.WarehouseId && (x.TargetAddress == "SCUTL-002" || x.TargetAddress == "1103")
+                            && (x.TaskState == TaskStatusEnum.SC_Execute.ObjToInt() || x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt()
+                            || x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() || x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt()));
+                            Dt_Task? taskExecute = _taskRepository.QueryData(x => x.WarehouseId == warehouseDevice.WarehouseId && x.TargetAddress == "SCUTL-002"
+                            && x.TaskState == TaskStatusEnum.MesPending.ObjToInt()).OrderByDescending(x => x.Grade).ThenBy(x => x.TaskNum).ThenBy(x => x.CreateDate).FirstOrDefault();
+                            if (taskExcuting == null && taskExecute != null)
+                            {
+                                _taskService.UpdateTask(taskExecute, TaskStatusEnum.SC_Execute);
+                                WriteDebug($"浜屽彿寮�鏂欏彛鏀捐浠诲姟", $"浠诲姟鍙穥taskExecute.TaskNum}");
+                            }
+                        }
+                        //鍏佽鏀捐鎵嬪姩寮�鏂欏彛鍑哄簱浠诲姟
+                        bool outAllowMusual = device.Communicator.Read<bool>("DB14.4.7");
+                        if (outAllowMusual)
+                        {
+                            Dt_Task taskExcuting = _taskRepository.QueryFirst(x => x.WarehouseId == warehouseDevice.WarehouseId && (x.TargetAddress == "SCUTL_04-001" || x.TargetAddress == "1102")
+                            && (x.TaskState == TaskStatusEnum.SC_Execute.ObjToInt() || x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt()
+                            || x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() || x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt()));
+                            Dt_Task? taskExecute = _taskRepository.QueryData(x => x.WarehouseId == warehouseDevice.WarehouseId && x.TargetAddress == "SCUTL_04-001"
+                            && x.TaskState == TaskStatusEnum.MesPending.ObjToInt()).OrderByDescending(x => x.Grade).ThenBy(x => x.TaskNum).ThenBy(x => x.CreateDate).FirstOrDefault();
+                            if (taskExcuting == null && taskExecute != null)
+                            {
+                                _taskService.UpdateTask(taskExecute, TaskStatusEnum.SC_Execute);
+                                WriteDebug($"鎵嬪姩寮�鏂欏彛鏀捐浠诲姟", $"浠诲姟鍙穥taskExecute.TaskNum}");
+                            }
+                        }
                     }
                     else
                     {

--
Gitblit v1.9.3