yanjinhui
2026-03-31 0128fce75a5e7cc9afc4bd89ce1966eb2231b69f
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Core;
using SqlSugar.Extensions;
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Common.LocationEnum;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_DTO;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_QuartzJob;
 
namespace WIDESEAWCS_TaskInfoService
{
    public partial class TaskService
    {
        #region 凯乐士任务创建
        /// <summary>
        /// 创建凯乐士AGV入库任务
        /// </summary>
        /// <param name="taskDTO"></param>
        /// <returns></returns>
        public WebResponseContent CreateKLSNewInTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_KLSLocationInfo? kLSLocationInfo = null;
            try
            {
                int containerType = taskDTO.containerCode.Contains("LLM") ? (int)LocationTypeEnum.LargePallet : (int)LocationTypeEnum.SmallPallet;
                #region 点到点
                if (!string.IsNullOrEmpty(taskDTO.toLocationCode))
                {
                    kLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点货位【{taskDTO.toLocationCode}】");
                    if (kLSLocationInfo.LocationStatus != (int)LocationStatusEnum.Free) throw new Exception($"终点货位【{taskDTO.toLocationCode}】货位状态不为空货位");
                }
                #endregion
                //获取货位信息
                else
                    kLSLocationInfo = _kLSLocationInfoService.GetFreeLocationInfo(taskDTO.toAreaCode, containerType) ?? throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!");
                Dt_Task dt_Task = new()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    Roadway = kLSLocationInfo.RoadwayNo,
                    TaskState = (int)TaskStatusEnum.New,
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = kLSLocationInfo.LocationCode,
                    TargetAddress = kLSLocationInfo.LocationCode,
                    Creater = "WMS",
                };
                kLSLocationInfo.LocationStatus = (int)LocationStatusEnum.InLock;
                #region 下发凯乐士任务
                content = SendKLSTask(dt_Task);
                if (!content.Status) throw new Exception(content.Message);
                dt_Task.TaskState = (int)TaskStatusEnum.Execut;
                dt_Task.Dispatchertime = DateTime.Now;
                #endregion
                try
                {
                    Db.Ado.BeginTran();
                    BaseDal.AddData(dt_Task);
                    _kLSLocationInfoService.Repository.UpdateData(kLSLocationInfo);
                    Db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    Db.Ado.RollbackTran();
                    throw new Exception(ex.Message);
                }
 
                return content.OK(data: new
                {
                    taskCode = taskDTO.taskCode,
                    Message = "成功!"
                });
            }
            catch (Exception ex)
            {
                content.Data = new
                {
                    taskCode = taskDTO.taskCode,
                    Message = $"失败!{ex.Message}"
                };
                return content.Error(ex.Message);
            }
        }
        /// <summary>
        /// 创建凯乐士AGV出库任务
        /// </summary>
        /// <param name="taskDTO"></param>
        /// <param name="taskType"></param>
        /// <returns></returns>
        public WebResponseContent CreateKLSNewOutTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_KLSLocationInfo? kLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
                if (kLSLocationInfo.LocationStatus != (int)LocationStatusEnum.InStock) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】当前库位状态不可出库!");
                if (kLSLocationInfo.PalletCode != taskDTO.containerCode) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】绑定料箱号【{kLSLocationInfo.PalletCode}】与任务料箱号【{taskDTO.containerCode}】不匹配!");
                Dt_Task dt_Task = new()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    Roadway = kLSLocationInfo.RoadwayNo,
                    TaskState = (int)TaskStatusEnum.New,
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = taskDTO.toLocationCode,
                    TargetAddress = taskDTO.toLocationCode,
                    Creater = "WMS",
                };
                kLSLocationInfo.LocationStatus = (int)LocationStatusEnum.OutLock;
                #region 下发凯乐士任务
                content = SendKLSTask(dt_Task);
                if (!content.Status) throw new Exception(content.Message);
                dt_Task.TaskState = (int)TaskStatusEnum.Execut;
                dt_Task.Dispatchertime = DateTime.Now;
                #endregion
                try
                {
                    Db.Ado.BeginTran();
                    BaseDal.AddData(dt_Task);
                    _kLSLocationInfoService.Repository.UpdateData(kLSLocationInfo);
                    Db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    Db.Ado.RollbackTran();
                    throw new Exception(ex.Message);
                }
                return content.OK(data: new
                {
                    taskCode = taskDTO.taskCode,
                    Message = "成功!"
                });
            }
            catch (Exception ex)
            {
                content.Data = new
                {
                    taskCode = taskDTO.taskCode,
                    Message = $"失败!{ex.Message}"
                };
                return content.Error(ex.Message);
            }
        }
        #endregion
 
        #region 下发凯乐士任务
        public WebResponseContent SendKLSTask(Dt_Task task)
        {
            WebResponseContent content = new WebResponseContent();
            GALAXISTaskInfo gALAXISTaskInfo = new();
            GALAXISReturn agvContent = new();
            try
            {
                gALAXISTaskInfo.groupId = DateTime.Now.ToString("yyMMddHHmmss") + task.TaskNum;
                gALAXISTaskInfo.msgTime = DateTime.Now.ToString();
                GALAXISTask gALAXISTask = new GALAXISTask()
                {
                    taskId = task.WMSTaskNum,
                    taskType = task.TaskType == (int)TaskTypeEnum.MLInbound ? 0 : 1,
                    barCode = task.PalletCode,
                    endNode = task.TargetAddress,
                    startNode = task.SourceAddress,
                    priorityCode = task.Grade
                };
                gALAXISTaskInfo.tasks.Add(gALAXISTask);
                Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(GALAXISTaskInfo)) ?? throw new Exception("未找到凯乐士AGV任务下发接口配置信息!请检查接口配置");
                string response = HttpHelper.Post(apiInfo.ApiAddress, gALAXISTaskInfo.Serialize());
                agvContent = response.DeserializeObject<GALAXISReturn>();
                content.OK(data: agvContent);
                if (agvContent.success)
                {
                    if (agvContent.data.returnStatus != 0) throw new Exception(agvContent.data.returnInfo);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            finally
            {
                _trackloginfoService.AddTrackLog(gALAXISTaskInfo, content, "下发凯乐士AGV任务", "", agvContent.data.returnInfo);
            }
            return content;
        }
        #endregion
 
        #region 5 跨楼层运输任务创建 (都是点到点)
        public WebResponseContent CarryTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_HKLocationInfo? hKLocationInfo = null;
                Dt_StationInfo? stationInfo = null;
                Dt_Task dt_Task = null;
                //入库 到一楼三个站台的位置
                if (taskType == (int)TaskTypeEnum.Q3RK)
                {
                    //读取光电信号
                    var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "F1") as OtherDevice;
                    if (device == null) throw new Exception("未找到1楼质检门设备信息");
                    if (!device.IsConnected) throw new Exception("PLC1楼质检门设备连接失败");
                    bool value = device.GetValue<QualityInspectionCommandEnum, bool>(QualityInspectionCommandEnum.R_StockAvailableSymbol, taskDTO.toLocationCode);
                    if (value)
                    {
                        content.Error($"{taskDTO.toLocationCode}站台有货,无法入库");
                    }
                    dt_Task = new Dt_Task()
                    {
                        TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                        WMSTaskNum = taskDTO.taskCode,
                        Grade = taskDTO.taskPriority,
                        PalletCode = taskDTO.containerCode,
                        Roadway = hKLocationInfo.RoadwayNo,
                        TaskState = (int)TaskStatusEnum.New,
                        TaskType = taskType,
                        SourceAddress = taskDTO.fromLocationCode,
                        CurrentAddress = taskDTO.fromLocationCode,
                        NextAddress = taskDTO.toLocationCode,
                        TargetAddress = taskDTO.toLocationCode,
                        Creater = "WMS",
                    };
 
                }
                //出库
                if (taskType == (int)TaskTypeEnum.Q3CK)
                {
                     stationInfo = _stationInfo.Repository.QueryFirst(x => x.StationName == taskDTO.toLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.toLocationCode}】!");
                    if (stationInfo.StationName != LocationStatusEnum.InStock.ToString()) throw new Exception($"起点库位【{taskDTO.toLocationCode}】当前库位状态不可出库!");
                    if (stationInfo.PalletCode != taskDTO.containerCode) throw new Exception($"起点库位【{taskDTO.toLocationCode}】绑定料箱号【{stationInfo.PalletCode}】与任务料箱号【{taskDTO.containerCode}】不匹配!");
                        dt_Task = new Dt_Task()
                    {
                        TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                        WMSTaskNum = taskDTO.taskCode,
                        //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                        Grade = taskDTO.taskPriority,
                        PalletCode = taskDTO.containerCode,
                        Roadway = hKLocationInfo.RoadwayNo,
                        TaskState = (int)TaskStatusEnum.New,
                        TaskType = taskType,
                        SourceAddress = taskDTO.fromLocationCode,
                        CurrentAddress = taskDTO.fromLocationCode,
                        NextAddress = taskDTO.toLocationCode,
                        TargetAddress = taskDTO.toLocationCode,
                        Creater = "WMS",
                    };
                }
                try
                {
                    Db.Ado.BeginTran();
                    BaseDal.AddData(dt_Task);
                    _hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
                    _stationInfo.Repository.UpdateData(stationInfo);
                    Db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    Db.Ado.RollbackTran();
                    throw new Exception(ex.Message);
                }
                return content.OK(data: new
                {
                    taskCode = taskDTO.taskCode,
                    Message = "成功!"
                });
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
 
 
        #region 6 裁剪送货
        public WebResponseContent CJCarryTaske(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task dt_Task = new Dt_Task()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    Roadway = "",
                    TaskState = (int)TaskStatusEnum.New,
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = taskDTO.toLocationCode,
                    TargetAddress = taskDTO.toLocationCode,
                    Creater = "WMS",
                };
                content = SendHIKROBOTTask(dt_Task);
                if (!content.Status) throw new Exception(content.Message);
                dt_Task.TaskState = (int)TaskStatusEnum.Execut;
                dt_Task.Dispatchertime = DateTime.Now;
                BaseDal.AddData(dt_Task);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
    }
}