huangxiaoqiang
2 天以前 843cc2ea1b104ecdf9da61318a4136a5d4096411
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using AutoMapper;
using Masuit.Tools;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.Const;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.WMS;
using WIDESEA_IServices;
using WIDESEA_IStorageTaskRepository;
using WIDESEA_IStorageTaskServices;
using WIDESEA_Model.Models;
using WIDESEA_StorageTaskRepository;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEA_Tasks
{
    public class OutNGTask : IJob
    {
        private readonly IDt_TaskRepository _taskRepository;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly ISys_ConfigService _configService;
        private readonly IDt_TaskService _taskService;
        private readonly IDt_Task_HtyRepository _task_HtyRepository;
        private readonly IMapper _mapper;
        public OutNGTask(IDt_TaskRepository taskRepository, IDt_StationManagerRepository stationManagerRepository, ISys_ConfigService configService, IDt_TaskService taskService, IDt_Task_HtyRepository task_HtyRepository, IMapper mapper)
        {
            _taskRepository = taskRepository;
            _stationManagerRepository = stationManagerRepository;
            _configService = configService;
            _taskService = taskService;
            _task_HtyRepository = task_HtyRepository;
            _mapper = mapper;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var task = _taskRepository.QueryFirst(x => x.TaskType == (int)TaskInboundTypeEnum.Inbound && x.TaskState == (int)TaskInStatusEnum.SC_InExecuting && x.ErrorMessage != null && (x.Roadway.Contains("GW") || x.Roadway.Contains("CW")));
 
                if (task != null)
                {
                    Dt_StationManager stationManager = null;
                    string position = null;
                    if (task.Roadway.Contains("CW"))
                    {
                        stationManager = _stationManagerRepository.QueryFirst(x => (x.stationPLC == "1017" || x.stationPLC == "1024") && x.stationType == 10 && x.Roadway == task.Roadway);
                        position = task.Roadway switch
                        {
                            var s when s.StartsWith("CWSC") =>
                            int.TryParse(s.Substring(4), out var num)
                            ? num switch
                            {
                                >= 1 and <= 9 => "001-035-001",
                                >= 11 and <= 17 => "001-036-001"
 
                            }
                            : throw new Exception("未找到该巷道信息"),
                            _ => throw new Exception("未找到该巷道信息")
                        };
                    }
                    else
                    {
                        stationManager = _stationManagerRepository.QueryFirst(x => x.stationType == 1 && (x.stationPLC == "1015" || x.stationPLC == "2025") && x.remark == task.Roadway);
                        position = "002-000-002";
                    }
 
                    var taskNew = CreateTask(task, stationManager, position);
                    WMSTaskDTO taskDTO = CreateTaskDTO(taskNew);
 
                    var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                    var wmsBase = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
                    var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveByWMSNGTask)?.ConfigValue;
                    if (wmsBase == null || ipAddress == null)
                    {
                        throw new InvalidOperationException("WMS IP 未配置");
                    }
                    var wmsIpAddress = wmsBase + ipAddress;
 
                    var result = HttpHelper.PostAsync(wmsIpAddress, taskDTO.ToJsonString()).Result;
                    var content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                    if (content.Status)
                    {
                        var taskHtyNG = CreateHistoricalTask(task);
                        _task_HtyRepository.AddData(taskHtyNG);
                        _taskRepository.DeleteData(task);
                        _taskRepository.AddData(taskNew);
                    }
                    return Task.CompletedTask;
                }
 
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteWarningLine($"执行 NG 任务出库时发生异常: {ex.Message}{ex.StackTrace}");
            }
            return Task.CompletedTask;
        }
        private Dt_Task CreateTask(Dt_Task task,Dt_StationManager stationManager,string position)
        {
            return new Dt_Task
            {
                Grade = 1,
                Roadway = task.Roadway,
                TargetAddress = stationManager.stationChildCode,
                Dispatchertime = DateTime.Now,
                MaterialNo = "",
                NextAddress = position,
                OrderNo = null,
                PalletCode = task.PalletCode,
                SourceAddress = task.TargetAddress,
                CurrentAddress = task.TargetAddress,
                TaskState = (int)TaskOutStatusEnum.OutNew,
                TaskType = (int)TaskOutboundTypeEnum.OutNG,
                TaskNum = _taskRepository.GetTaskNo().Result,
                Creater = "System",
                CreateDate = DateTime.Now,
                TaskId = 0,
                ProductionLine = task.ProductionLine,
                ProcessCode = task.ProcessCode,
                ErrorMessage = task.ErrorMessage,
            };
        }
        private WMSTaskDTO CreateTaskDTO(Dt_Task task)
        {
            return new WMSTaskDTO
            {
                TaskNum = task.TaskNum.Value,
                Grade = 1,
                PalletCode = task.PalletCode,
                RoadWay = task.Roadway,
                SourceAddress = task.SourceAddress,
                TargetAddress = task.TargetAddress,
                NextAddress = task.NextAddress,
                TaskState = task.TaskState.Value,
                Id = 0,
                TaskType = task.TaskType,
                ProductionLine = task.ProductionLine,
            };
        }
        private Dt_Task_Hty CreateHistoricalTask(Dt_Task task, bool isHand = false)
        {
            // 更新任务状态
            task.TaskState = task.TaskType > 199 ? (int)TaskInStatusEnum.InFinish : (int)TaskOutStatusEnum.OutFinish;
            task.CurrentAddress = task.NextAddress;
 
            // 创建历史任务
            var taskHty = _mapper.Map<Dt_Task_Hty>(task);
            taskHty.FinishTime = DateTime.Now;
            taskHty.TaskId = 0;
            taskHty.OperateType = (int)OperateTypeEnum.NG任务删除;
            taskHty.SourceId = task.TaskId;
            if (isHand)
            {
                taskHty.Creater = App.User.UserName != null ? App.User.UserName : "System";
            }
            return taskHty;
        }
    }
}