刘磊
2025-04-19 bd02cb3fa0fa75ffafb2cf17501929b1b1e0029b
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using HslCommunication;
using Mapster;
using MoYu.Localization;
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_TaskInfoService
{
    public partial class TaskService
    {
        /// <summary>
        /// 根据托盘号、起始地址向WMS请求任务
        /// </summary>
        /// <param name="palletCode">托盘号</param>
        /// <param name="sourceAddress">起始地址</param>
        /// <returns></returns>
        public Dt_Task? OutBeforeCheck(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                var requestTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.TransferCheck)?.ConfigValue;
                if (wmsBase == null || requestTask == null)
                {
                    throw new InvalidOperationException("WMS IP 未配置");
                }
                var wmsIpAddrss = wmsBase + requestTask;
 
                var result = HttpHelper.Post(wmsIpAddrss, new { taskNum = taskNum }.ToJsonString());
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
 
                if (!content.Status)
                {
                    // wms失败
                    throw new Exception();
                }
 
 
                //todo 优化 如未查询到任务则处理
                //if (content.Data != null)
                //{
                var task = JsonConvert.DeserializeObject<Dt_Task>(content.Data.ToString());
 
                Dt_Task? Relocationtask = null;
                if (task.TaskNum != taskNum)
                {
 
                    BaseDal.AddData(task);
                    Relocationtask = BaseDal.QueryFirst(x => x.TaskNum == task.TaskNum);
                }
                //}
 
                if (Relocationtask != null)
                {
                    return task;
                }
                else
                {
                    return null;
                }
 
            }
            catch (Exception ex)
            {
                throw new Exception($"请求异常:{ex.Message}");
            }
        }
 
        #region 根据托盘号、站台向WMS请求任务
        /// <summary>
        /// 根据托盘号、站台向WMS请求任务
        /// </summary>
        /// <param name="palletCode">托盘号</param>
        /// <param name="stationManager">站台</param>
        /// <returns></returns>
        public async Task<WebResponseContent> RequestTask(string palletCode, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                StaticVariable.isLineRun = false;
 
                // 检查当前托盘是否存在任务
                if (await HasTask(palletCode))
                {
                    return content.Error("当前托盘存在任务");
                }
 
                // 发起请求获取入库任务
                content = await RequestInTask(palletCode, stationManager);
                ConsoleHelper.WriteErrorLine($"{stationManager.stationChildCode}站台请求响应信息:{content.ToJsonString()}");
 
                if (!content.Status)
                {
                    return content;
                }
 
                var task = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
                ConsoleHelper.WriteSuccessLine(content.Data.ToString());
                return ReceiveWMSTask(new List<WMSTaskDTO> { task });
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
                StaticVariable.isLineRun = true;
            }
        }
 
        public async Task<WebResponseContent> UpdateCheckTask(string palletCode, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.UpdateCheckTask);
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, Position = stationManager.stationChildCode }.ToJsonString());
                return JsonConvert.DeserializeObject<WebResponseContent>(result);
            }
            catch (Exception ex)
            {
                return content.Error($"{ex.Message}");
            }
        }
 
        /// <summary>
        /// 车轮流向分配
        /// </summary>
        /// <param name="czCode"></param>
        /// <param name="stationManager"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> RequestWheelsFlow(string czCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestWheelsFlow);
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = czCode }.ToJsonString());
                return JsonConvert.DeserializeObject<WebResponseContent>(result);
            }
            catch (Exception ex)
            {
                return content.Error($"{ex.Message}");
            }
        }
 
        // 检查当前托盘是否存在任务
        private async Task<bool> HasTask(string palletCode)
        {
            var hasTask = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode);
            return hasTask != null;
        }
 
        // 获取 WMS 请求地址
        private string GetWmsIpAddress(string configKey)
        {
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
            var requestTask = config.FirstOrDefault(x => x.ConfigKey == configKey)?.ConfigValue;
 
            if (wmsBase == null || requestTask == null)
            {
                throw new InvalidOperationException("WMS IP 未配置");
            }
 
            return wmsBase + requestTask;
        }
 
        // 发起请求获取入库任务
        private async Task<WebResponseContent> RequestInTask(string palletCode, Dt_StationManager stationManager)
        {
            var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestInTask);
            var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode }.ToJsonString());
            return JsonConvert.DeserializeObject<WebResponseContent>(result);
        }
 
        #endregion 根据托盘号、站台向WMS请求任务
 
 
 
        /// <summary>
        /// 接收WMS任务信息
        /// </summary>
        /// <param name="taskDTOs">WMS任务对象集合</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent ReceiveByWMSTask([NotNull] WMSTaskDTO taskDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (BaseDal.QueryFirst(x => x.TaskNum == taskDTO.TaskNum || x.PalletCode == taskDTO.PalletCode) != null)
                {
                    return content.OK();
                }
                Dt_Task task = _mapper.Map<Dt_Task>(taskDTO);
                task.Creater = "WMS";
                task.IsCheck = taskDTO.IsCheck;
                task.CarType = taskDTO.CarType;
                task.wheels_gkcc = taskDTO.wheels_gkcc;
                task.wheels_mttype = taskDTO.wheels_mttype;
                task.WheelsNewOrOld = taskDTO.WheelsNewOrOld;
                task.WheelsLX = taskDTO.WheelsLX;
 
                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    var stationinfo = _stationManagerRepository.QueryFirst(x => x.stationLocation == taskDTO.TargetAddress && x.Roadway == taskDTO.RoadWay);
                    task.TaskState = (int)TaskOutStatusEnum.OutNew;
                    task.CurrentAddress = taskDTO.RoadWay;
                    task.NextAddress = stationinfo.stationChildCode;
                    task.SourceAddress = taskDTO.SourceAddress;
                    task.TargetAddress = taskDTO.TargetAddress;
                }
                else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                {
                    var stationinfo = _stationManagerRepository.QueryFirst(x => x.stationLocation == taskDTO.SourceAddress && x.Roadway == taskDTO.RoadWay);
                    task.TaskState = (int)TaskInStatusEnum.Line_InFinish;
                    task.SourceAddress = taskDTO.SourceAddress;
                    task.CurrentAddress = stationinfo.stationChildCode;
                    task.NextAddress = stationinfo.stationChildCode;
                    task.TargetAddress = taskDTO.TargetAddress;
                }
                else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
                {
                    task.TaskState = (int)TaskStatus.Created;
                    task.SourceAddress = taskDTO.SourceAddress;
                    task.CurrentAddress = taskDTO.SourceAddress;
                    task.NextAddress = taskDTO.TargetAddress;
                    task.TargetAddress = taskDTO.TargetAddress;
                }
 
                BaseDal.AddData(task);
 
                _taskExecuteDetailService.AddTaskExecuteDetail(task.WMSId, "接收WMS任务");
 
                content = WebResponseContent.Instance.OK("成功");
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"任务接收错误,错误信息:{ex.Message}");
            }
            return content;
        }
 
        /// <summary>
        /// 流向请求
        /// </summary>
        /// <param name="taskDTOs">WMS任务对象集合</param>
        /// <returns>返回处理结果</returns>
        public async Task<WebResponseContent> RequestFlow(Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                var requestTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestFlow)?.ConfigValue;
                if (wmsBase == null || requestTask == null)
                {
                    throw new InvalidOperationException("WMS IP 未配置");
                }
                var wmsIpAddrss = wmsBase + requestTask;
 
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode }.ToJsonString());
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
 
                if (!content.Status)
                {
                    return content;
                }
                Dt_StationManager dt_Station = content.Data as Dt_StationManager;
                //dt_Station.
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"任务接收错误,错误信息:{ex.Message}");
            }
            return content;
        }
 
        /// <summary>
        /// 根据托盘号和设备号查询任务
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <param name="currentAddress">当前地址</param>
        /// <returns></returns>
        public Dt_Task QueryTaskByPalletCode(string palletCode, string Roadway)
        {
            return BaseDal.QueryFirst(x => x.PalletCode == palletCode && x.Roadway == Roadway, TaskOrderBy);
        }
 
        /// <summary>
        /// 根据设备编号、当前地址查询输送线未执行的任务
        /// </summary>
        /// <param name="deviceNo">设备编号</param>
        /// <param name="currentAddress">当前地址</param>
        /// <returns></returns>
        public Dt_Task QueryConveyorLineTask(string deviceNo, string currentAddress)
        {
            return BaseDal.QueryFirst(x => (TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskInStatusEnum.InNew || TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskOutStatusEnum.SC_OutFinish) && x.CurrentAddress == currentAddress, TaskOrderBy);
        }
 
        /// 任务完成
        /// </summary>
        /// <param name="taskNum">任务编号</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent StackCraneTaskCompletedByStation(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null) return WebResponseContent.Instance.Error($"未找到该任务信息,任务号:【{taskNum}】");
 
                //if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && task.TaskState == (int)TaskOutStatusEnum.SC_OutExecuting)
                //{
                //    //todo
 
                //}
                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup && task.TaskState == (int)TaskInStatusEnum.SC_InExecuting)
                //{
                //    //todo 同步到WMS
 
                //    BaseDal.DeleteData(task);
 
                //}
                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
                //{
                //    //todo 调用WMS移库完成
                //}
                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OtherGroup)
                //{
                //}
                //else
                //{
                //    throw new Exception($"任务类型错误,未找到该任务类型,任务号:【{taskNum}】,任务类型:【{task.TaskType}】");
                //}
 
                #region WMS同步任务完成
 
                var keys = new Dictionary<string, object>()
                {
                    {"taskNum", taskNum}
                };
                // 获取WMSip地址
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                var completeTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.CompleteTask)?.ConfigValue;
                if (wmsBase == null || completeTask == null)
                {
                    throw new InvalidOperationException("WMS IP 未配置");
                }
                var wmsIpAddress = wmsBase + completeTask;
 
                var result = HttpHelper.GetAsync(wmsIpAddress, keys).Result;
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
 
                #endregion WMS同步任务完成
 
                if (content.Status)
                {
                    task.TaskState = 1;  //任务完成
                    var taskHty = task.Adapt<Dt_Task_Hty>();
                    _taskHtyRepository.AddData(taskHty);
                    BaseDal.DeleteData(task);
                }
 
                #region 更新任务状态
 
                //var updateTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.UpdateTask)?.ConfigValue;
                //if (wmsBase == null || updateTask == null)
                //{
                //    throw new InvalidOperationException("WMS IP 未配置");
                //}
                //wmsIpAddress = wmsBase + updateTask;
 
                //result = HttpHelper.PostAsync(wmsIpAddress, new { TaskNum = task.TaskNum, TaskState = task.TaskState }.ToJsonString()).Result;
                //content = JsonConvert.DeserializeObject<WebResponseContent>(result);
 
                #endregion 更新任务状态
 
                //content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"任务完成异常,任务号:【{taskNum}】");
            }
            return content;
        }
    }
}