| using Microsoft.AspNetCore.Components.Routing; | 
| using Microsoft.IdentityModel.Tokens; | 
| using Newtonsoft.Json; | 
| using Quartz; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Common; | 
| 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; | 
| using WIDESEAWCS_ITaskInfoService; | 
| using WIDESEAWCS_Model.Models; | 
| using WIDESEAWCS_QuartzJob; | 
| using WIDESEAWCS_QuartzJob.DTO; | 
| using WIDESEAWCS_QuartzJob.Models; | 
| using WIDESEAWCS_QuartzJob.Repository; | 
| using WIDESEAWCS_QuartzJob.Service; | 
| using WIDESEAWCS_Tasks.ConveyorLineJob; | 
|   | 
| namespace WIDESEAWCS_Tasks | 
| { | 
|     [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(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) | 
|         { | 
|             bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); | 
|             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))) | 
|                 { | 
|                     DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|   | 
|                     DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|   | 
|                     if (deviceProRead != null && deviceProWrite != null) | 
|                     { | 
|                         R_ConveyorLineInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress); | 
|   | 
|                         W_ConveyorLineInfo conveyorLineInfoWrite = device.Communicator.ReadCustomer<W_ConveyorLineInfo>(deviceProWrite.DeviceProAddress); | 
|                         if (conveyorLineInfoRead == null || conveyorLineInfoWrite == null) | 
|                         { | 
|                             continue; | 
|                         } | 
|   | 
|                         R_ConveyorLineStatus conveyorLineStatus = conveyorLineInfoRead.Status.ByteToBoolObject<R_ConveyorLineStatus>(); | 
|   | 
|                         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 && !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)//入库 | 
|                             { | 
|                                 WebResponseContent content = _taskService.RequestWMSTaskSimple(conveyorLineInfoRead.Barcode, item.StationCode); | 
|                                 if (content.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_Execute); | 
|                                     } | 
|                                 } | 
|                             } | 
|                             else if (conveyorLineSignalRead.STB /*&& conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm*/ && !conveyorLineSignalWrite.ACK && conveyorLineInfoRead.TaskNum > 0)//出库 | 
|                             { | 
|                                 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 | 
|                     { | 
|                         WriteError(device.DeviceName, $"未找到设备子编号{item.StationCode}的协议信息"); | 
|                     } | 
|                 } | 
|             } | 
|   | 
|             return Task.CompletedTask; | 
|         } | 
|     } | 
| } |