Admin
2026-01-19 4b403ca5072dc74fbebf36f816df7d0c92241a91
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEA_TaskInfoService
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using Microsoft.Extensions.Logging;
using Org.BouncyCastle.Math.EC;
using SixLabors.ImageSharp;
using SqlSugar;
using System.ComponentModel;
using System.Net;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.Log;
using WIDESEA_Common.OtherEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Task;
using WIDESEA_IBasicService;
using WIDESEA_IInboundService;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService : ServiceBase<Dt_Task, IRepository<Dt_Task>>, ITaskService
    {
        private readonly ILogger<TaskService> _logger;
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IStockInfoService _stockInfoService;
        private readonly ILocationInfoService _locationInfoService;
        private readonly IWarehouseService _warehouseService;
        private readonly IRoadWayinfoService _roadWayinfoService;
        private Timer _timer;
        public IRepository<Dt_Task> Repository => BaseDal;
 
        private Dictionary<string, OrderByType> _taskOrderBy = new()
            {
                {nameof(Dt_Task.Grade),OrderByType.Desc },
                {nameof(Dt_Task.CreateDate),OrderByType.Asc},
            };
 
        public List<int> TaskTypes => typeof(TaskTypeEnum).GetEnumIndexList();
 
        public List<int> TaskOutboundTypes => typeof(TaskTypeEnum).GetEnumIndexList();
 
        public TaskService(ILogger<TaskService> logger, IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IStockInfoService stockInfoService, ILocationInfoService locationInfoService,
            IWarehouseService warehouseService, IRoadWayinfoService roadWayinfoService) : base(BaseDal)
        {
            _logger = logger;
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
            _stockInfoService = stockInfoService;
            _locationInfoService = locationInfoService;
            _warehouseService = warehouseService;
            _roadWayinfoService=roadWayinfoService;
 
        }
        public string MES_InReporttask = WIDESEA_Core.Helper.AppSettings.Configuration["MES_InReporttask"]; //入库反馈信息
        public string MES_OutReporttask = WIDESEA_Core.Helper.AppSettings.Configuration["MES_OutReporttask"];   //出库反馈信息
        public string MES_InventoryUpdateApply = WIDESEA_Core.Helper.AppSettings.Configuration["MES_InventoryUpdateApply"];   //储位异动申请
        public string MES_InventoryUpdateCallback = WIDESEA_Core.Helper.AppSettings.Configuration["MES_InventoryUpdateCallback"];   //储位异动申请
        public string MES_TaskErrorInfoCallback = WIDESEA_Core.Helper.AppSettings.Configuration["MES_TaskErrorInfoCallback"];   //任务异常上报
        public string WCS_SendConveyorTask = WIDESEA_Core.Helper.AppSettings.Configuration["WCS_SendConveyorTask"];   //输送线任务下发
        public string WCS_SendStackerTask = WIDESEA_Core.Helper.AppSettings.Configuration["WCS_SendStackerTask"];   //堆垛机任务下发
 
 
        public WebResponseContent CESTEXT()
        {
            WriteLog.Write_Log("接收MES入库任务下发", $"任务接收参数", "成功", $"参数:234jkljsldafjlasdf");
            return WebResponseContent.Instance.OK();
        }
 
 
        /// <summary>
        /// 任务完成接口
        /// </summary>
        /// <param name="wCSTask"></param>
        /// <returns></returns>
        public WebResponseContent TaskCompleted(WCSTaskDTO wCSTask)
        {
            WebResponseContent webResponse= new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskId == wCSTask.TaskNum && x.PalletCode == wCSTask.PalletCode);
                if (task == null)
                    return webResponse.Error("未找到任务信息");
 
                switch ((TaskTypeEnum)task.TaskType)
                {
                    case TaskTypeEnum.Inbound:
                        return HandleInboundTask(task, wCSTask.TaskType);
                    case TaskTypeEnum.Outbound:
                        return HandleOutboundTask(task, wCSTask.TaskType);
                    case TaskTypeEnum.Relocation:
                        return HandleRelocationTask(task, wCSTask.TaskType);
                    default:
                        return webResponse.Error($"任务类型错误,任务号:{task.TaskId},托盘编号:{task.PalletCode},类型:{task.TaskType}");
                }
            }
            catch (Exception ex)
            {
                return webResponse.Error($"WMS任务完成错误:{ex.Message}");
            }
        }
 
        private WebResponseContent HandleInboundTask(Dt_Task task, int wcsTaskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                switch (wcsTaskType)
                {
                    case 1:
 
                        if (string.IsNullOrEmpty(task.TargetAddress))
                        {
                            int loctype = 0;
                            if (task.Roadway == "1" || task.Roadway == "2")
                            {
                                if (task.PalletType == "1") loctype = (int)LocationTypeEnum.SmallPallet;
                                else if (task.PalletType == "2") loctype = (int)LocationTypeEnum.MediumPallet;
                            }
                            else
                            {
                                loctype = (int)LocationTypeEnum.LargePallet;
                            }
 
                            //查找货位,更新任务
                            Dt_LocationInfo dt_Location = _locationInfoService.GetLocation(task.Roadway, loctype);
                            Dt_LocationInfo ShallowCargoHold = _locationInfoService.ShallowGetLocation(dt_Location.RoadwayNo, dt_Location.LocationType, dt_Location.Row, dt_Location.Layer, dt_Location.Column);
                            if (ShallowCargoHold == null) return content.Error($"未找到该货位信息,货位编号:{dt_Location.LocationCode}的浅货位");
                            if (ShallowCargoHold.LocationStatus != (int)LocationStatusEnum.Free) return content.Error($"任务号:{task.TaskId},托盘编号:{task.PalletCode},查找的货位浅货位有货,货位编号:{ShallowCargoHold.LocationCode}");
 
                            if (task.PalletCode[0] == 1 && (dt_Location.RoadwayNo == "1" || dt_Location.RoadwayNo == "2") && dt_Location.Column < 4)
                            {
                                return content.Error($"条码为高托盘,但是查找的货位要入在低托盘下,故障,托盘条码:{task.PalletCode},货位编号:{dt_Location.LocationCode}");
                            }
 
 
                            Dt_roadwayinfo _Roadwayinfo = _roadWayinfoService.QbtainPlatform(task.Roadway);
 
                            task.TargetAddress = dt_Location.LocationCode;
                            task.CurrentAddress = _Roadwayinfo.InSCStationCode;
                            task.NextAddress = dt_Location.LocationCode;
                            task.TaskStatus = (int)InTaskStatusEnum.PLC_InFinish;
 
                            dt_Location.LocationStatus = (int)LocationStatusEnum.Lock;
 
                            Dt_StockInfo dt_StockInfo = new Dt_StockInfo();
                            dt_StockInfo.PalletCode = task.PalletCode;
                            dt_StockInfo.PalletType = task.PalletType;
                            dt_StockInfo.WarehouseId = task.WarehouseId;
                            dt_StockInfo.LocationCode = dt_Location.LocationCode;
                            dt_StockInfo.StockStatus = (int)StockStatusEmun.入库中;
                            dt_StockInfo.Creater = "MWS";
                            dt_StockInfo.CreateDate = DateTime.Now;
 
                            _unitOfWorkManage.BeginTran();
                            _stockInfoService.AddData(dt_StockInfo);
                            _locationInfoService.UpdateData(dt_Location);
                            BaseDal.UpdateData(task);
                            _unitOfWorkManage.CommitTran();
                            return content.OK($"已接收入库输送线完成信息,任务号:{task.TaskId},托盘编号:{task.PalletCode}");
                        }
                        else
                        {
                            //判断是否有该站台
 
                            string Resultplc = MesInTaskStatusEnum.入库完成.GetDescription();
                            MES_parameter mES_PCLParameter = InStoreDocCallback(task.TaskNum, Resultplc, "操作成功", task.PalletCode, task.TargetAddress);
                            if (mES_PCLParameter.Result == "Y")
                            {
                                task.TaskStatus = (int)InTaskStatusEnum.PLC_InFinish;
                                _unitOfWorkManage.BeginTran();
                                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                                _unitOfWorkManage.CommitTran();
                                return content.OK($"输送线任务已反馈至上游,任务号:{task.TaskId},托盘编号:{task.PalletCode}");
                            }
                            else
                            {
                                return content.Error("上传MES失败,原因:" + mES_PCLParameter.ResultMsg);
                            }
                        }
                    case 2:
                        string Resultsc = MesInTaskStatusEnum.入库完成.GetDescription();
                        //上报MES任务完成
                        MES_parameter mES_Parameter = InStoreDocCallback(task.TaskNum, Resultsc, "操作成功", task.PalletCode, task.TargetAddress);
                        if (mES_Parameter.Result != "Y")
                        {
                            Dt_StockInfo dt_Stockowc = _stockInfoService.Repository.QueryData(x => x.PalletCode == task.PalletCode).FirstOrDefault();
                            Dt_LocationInfo dt_LocationInfo=_locationInfoService.Repository.QueryData(x => x.LocationCode == dt_Stockowc.LocationCode).FirstOrDefault();
 
                            dt_Stockowc.StockStatus = (int)StockStatusEmun.已入库;
                            dt_LocationInfo.LocationStatus = (int)LocationStatusEnum.InStock;
                            task.TaskStatus = (int)InTaskStatusEnum.InFinish;
 
                            _unitOfWorkManage.BeginTran();
                            _stockInfoService.UpdateData(dt_Stockowc);
                            _locationInfoService.UpdateData(dt_LocationInfo);
                            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                            _unitOfWorkManage.CommitTran();
                            return content.OK($"已接收入库堆垛机完成信息,任务号:{task.TaskId},托盘编号:{task.PalletCode}");
                        }
                        else
                        {
                            return content.Error("上传MES失败,原因:" + mES_Parameter.ResultMsg);
                        }
                    default:
                        return content.Error($"WCS上报类型错误:{wcsTaskType}");
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content.Error($"WCS任务完成接口故障,原因:{ex.Message}");
            }
        }
 
        private WebResponseContent HandleOutboundTask(Dt_Task task, int wcsTaskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                switch (wcsTaskType)
                {
                    case 1:
                        string Resultplc = MesOutTaskStatusEnum.到达目的地.GetDescription();
                        //上报MES任务完成
                        MES_parameter mES_Parameter = OutStoreDocCallback(task.TaskNum, Resultplc, "操作成功");
                        if (mES_Parameter.Result != "Y")
                        {
                            task.TaskStatus = (int)OutTaskStatusEnum.PLC_OutFinish;
                            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                            return content.OK($"已接收出库输送线完成信息,任务号:{task.TaskId},托盘编号:{task.PalletCode}");
                        }
                        else
                        {
                            return content.Error("上传MES失败,原因:" + mES_Parameter.ResultMsg);
                        }
                    case 2:
                        //获取对应PLC站台信息
                        Dt_roadwayinfo _Roadwayinfo = _roadWayinfoService.QbtainPlatform(task.Roadway);
                        task.CurrentAddress = _Roadwayinfo.OutStationCode;
                        task.NextAddress = task.TargetAddress;
                        task.TaskStatus = (int)OutTaskStatusEnum.SC_OutFinish;
 
                        Dt_StockInfo dt_Stockowc = _stockInfoService.Repository.QueryData(x => x.PalletCode == task.PalletCode).FirstOrDefault();
                        Dt_LocationInfo dt_LocationInfo = _locationInfoService.Repository.QueryData(x => x.LocationCode == dt_Stockowc.LocationCode).FirstOrDefault();
                        dt_LocationInfo.LocationStatus = (int)LocationStatusEnum.Free;
 
                        string Result = MesOutTaskStatusEnum.出库完成.GetDescription();
                        //调取上游系统反馈开始任务
                        MES_parameter mES_SCParameter = OutStoreDocCallback(task.TaskNum, Result, "操作成功");
                        if (mES_SCParameter.Result != "Y")
                        {
                            _unitOfWorkManage.BeginTran();
                            _stockInfoService.DeleteData(dt_Stockowc);
                            _locationInfoService.UpdateData(dt_LocationInfo);
                            BaseDal.UpdateData(task);
                            _unitOfWorkManage.CommitTran();
                            return content.OK($"已接收出库堆垛机完成信息,任务号:{task.TaskId},托盘编号:{task.PalletCode}");
                        }
                        else
                        {
                            return content.Error("上传MES失败,原因:" + mES_SCParameter.ResultMsg);
                        }
                    default:
                        return content.Error($"WCS上报类型错误:{wcsTaskType}");
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content.Error($"WCS任务完成接口故障,原因:{ex.Message}");
            }
        }
 
        private WebResponseContent HandleRelocationTask(Dt_Task task, int wcsTaskType)
        {
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                if (wcsTaskType == 2)
                {
                    //上报MES任务完成
                    MES_parameter mES_Parameter = AbnormalStorageLocation(task.PalletCode, task.SourceAddress, task.TargetAddress);
                    if (mES_Parameter.Result == "Y")
                    {
                        Dt_LocationInfo OriginalLocation = _locationInfoService.Repository.QueryData(x => x.LocationCode == task.SourceAddress).FirstOrDefault();
                        Dt_LocationInfo NewLocation = _locationInfoService.Repository.QueryData(x => x.LocationCode == task.TargetAddress).FirstOrDefault();
                        Dt_StockInfo dt_StockInfo = _stockInfoService.Repository.QueryData(x => x.LocationCode == task.TargetAddress).FirstOrDefault();
 
                        List<Dt_LocationInfo> dt_Locations = new List<Dt_LocationInfo>();
 
                        OriginalLocation.LocationStatus = (int)LocationStatusEnum.Free;
                        NewLocation.LocationStatus = (int)LocationStatusEnum.InStock;
                        dt_StockInfo.LocationCode = NewLocation.LocationCode;
                        dt_StockInfo.StockStatus = (int)StockStatusEmun.已入库;
                        task.TaskStatus = (int)RelocationTaskStatusEnum.RelocationFinish;
 
                        dt_Locations.Add(OriginalLocation);
                        dt_Locations.Add(NewLocation);
 
                        _unitOfWorkManage.BeginTran();
                        _locationInfoService.UpdateData(dt_Locations);
                        _stockInfoService.UpdateData(dt_StockInfo);
                        BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                        _unitOfWorkManage.CommitTran();
 
                        return WebResponseContent.Instance.OK($"已接收移库堆垛机完成信息,任务号:{task.TaskId},托盘编号:{task.PalletCode}");
                    }
                    else
                    {
                        return responseContent.Error("上传MES失败,原因:" + mES_Parameter.ResultMsg);
                    }
                }
                else
                {
                    return WebResponseContent.Instance.Error($"WCS上报类型错误:{wcsTaskType}");
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error($"WMS系统错误,原因:{ex.Message}");
            }
            
        }
 
 
        
 
    }
}