wangxinhui
2 天以前 875e462062d827b109a86f6a1bccbfc853156f30
代码管理/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/阻焊仓/StackerCraneJob_ZH.cs
@@ -21,6 +21,12 @@
using WIDESEAWCS_Tasks.StackerCraneJob;
using WIDESEAWCS_Tasks;
using WIDESEAWCS_Core;
using Newtonsoft.Json;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_Core.Caches;
using AutoMapper;
namespace WIDESEAWCS_Tasks
{
@@ -32,14 +38,32 @@
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private readonly IStationMangerRepository _stationMangerRepository;
        private readonly ICacheService _cacheService;
        private readonly IMapper _mapper;
        private List<Dt_ApiInfo> apiInfos;
        public StackerCraneJob_ZH(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository)
        public StackerCraneJob_ZH(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository,ICacheService cacheService, IMapper mapper)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _stationMangerRepository = stationMangerRepository;
            _cacheService = cacheService;
            _mapper = mapper;
            string? apiInfoStr = _cacheService.Get("apiInfos");
            if (!string.IsNullOrEmpty(apiInfoStr))
            {
                List<Dt_ApiInfo>? infos = JsonConvert.DeserializeObject<List<Dt_ApiInfo>>(apiInfoStr);
                if (infos == null || infos.Count == 0)
                {
                    apiInfos = new List<Dt_ApiInfo>();
                }
                else
                {
                    apiInfos = infos;
                }
            }
        }
        public Task Execute(IJobExecutionContext context)
@@ -183,22 +207,89 @@
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                if (OutTaskStationIsOccupied(task) != null || true)
                if (OutTaskStationIsOccupied(task) == null)
                {
                    return task;
                }
                else
                {
                    bool flag = false;
                    List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress, task.TaskType).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;
                            flag = true;
                            break;
                        }
                    }
                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    if (!flag)
                    {
                        task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    }
                }
            }
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                string? url = apiInfos.FirstOrDefault(x => x.ApiCode == APIEnum.WMSIsReloaction.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(url))
                {
                    _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"鏈壘鍒癢MS绉诲簱鍒ゆ柇鎺ュ彛");
                    WriteError(commonStackerCrane.DeviceCode, $"鏈壘鍒癢MS绉诲簱鍒ゆ柇鎺ュ彛");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒癢MS绉诲簱鍒ゆ柇鎺ュ彛");
                    return null;
                }
                string response = HttpHelper.Post($"{url}?taskNum={task.TaskNum}&locationCode={task.CurrentAddress}", "");//todo 璋冪敤WMS浠诲姟瀹屾垚鏂规硶
                if (string.IsNullOrEmpty(response))
                {
                    _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    WriteError(commonStackerCrane.DeviceCode, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    return null;
                }
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(response);
                if (responseContent == null || !responseContent.Status)
                {
                    _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    WriteError(commonStackerCrane.DeviceCode, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    return null;
                }
                WMSTaskDTO? taskDTO = JsonConvert.DeserializeObject<WMSTaskDTO>(responseContent.Data.Serialize());
                if (taskDTO == null)
                {
                    _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    WriteError(commonStackerCrane.DeviceCode, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"绉诲簱鎺ュ彛璋冪敤閿欒");
                    return null;
                }
                if (task.TaskNum == taskDTO.TaskNum)
                {
                    return task;
                }
                else
                {
                    Dt_Task reloTask = _mapper.Map<Dt_Task>(taskDTO);
                    //鍒ゆ柇绉诲簱璐т綅浠诲姟鏄惁宸插瓨鍦紝濡傚瓨鍦ㄥ厛鎵ц
                    Dt_Task existTask = _taskService.QueryStackerExistTask(reloTask.PalletCode, reloTask.SourceAddress);
                    if (existTask != null && existTask.TaskState == (int)TaskStatusEnum.SC_Execute)
                    {
                        return existTask;
                    }
                    else if (existTask != null && existTask.TaskState != (int)TaskStatusEnum.SC_Execute)
                    {
                        return null;
                    }
                    else
                    {
                        reloTask.TaskState = TaskStatusEnum.SC_Execute.ObjToInt();
                        reloTask.CurrentAddress = taskDTO.SourceAddress;
                        reloTask.NextAddress = taskDTO.TargetAddress;
                        reloTask.DeviceCode = task.DeviceCode;
                        reloTask.TaskType = TaskTypeEnum.Relocation.ObjToInt();
                        int taskId = _taskRepository.AddData(reloTask);
                        reloTask.TaskId = taskId;
                    }
                    return reloTask;
                }
            }