1
wankeda
2026-03-05 a03ff104df699229eff31f31890544ccc6a7c8da
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEA_TaskInfoService
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using MailKit.Search;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Database;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Org.BouncyCastle.Asn1.Tsp;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_BasicRepository;
using WIDESEA_Common;
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_Core.TaskEnum;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Stock;
using WIDESEA_DTO.Task;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IInboundService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
using WIDESEA_StockRepository;
using WIDESEA_TaskInfoRepository;
using static WIDESEA_ITaskInfoService.ITaskService;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService : ServiceBase<Dt_Task, ITaskRepository>, ITaskService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IStockRepository _stockRepository;
        private readonly IBasicService _basicService;
        private readonly IBasicRepository _basicRepository;
        private readonly IOutboundService _outboundService;
        private readonly IInboundService _inboundService;
        private readonly IInboundOrderDetailService _inboundOrderDetailService;
        private readonly IRecordService _recordService;
        private readonly IStockService _stockService;
        private readonly ITask_HtyService _taskHtyService;
        private readonly ILocationInfoService _locationInfoService;
        private readonly ITaskService _taskService;
        private readonly IMaterielInfoService _materielInfoService;
        private readonly IInboundOrder_HtyService _inboundOrder_HtyService;
        private readonly IInboundOrderDetail_HtyService _inboundOrderDetail_HtyService;
        private readonly IOutboundOrder_HtyService _outboundOrder_HtyService;
        private readonly IOutboundOrderDetail_HtyService _outboundOrderDetail_HtyService;
        private readonly IPalletTypeInfoRepository _palletTypeInfoRepository;
        private readonly IOutboundOrderDetailRepository _outboundOrderDetailRepository;
        private readonly IStockInfoDetailRepository _stockInfoDetailRepository;
        public ITaskRepository Repository => BaseDal;
 
        public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IMaterielInfoService materielInfoService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService, IOutboundOrder_HtyService outboundOrder_HtyService, IOutboundOrderDetail_HtyService outboundOrderDetail_HtyService, IInboundOrder_HtyService inboundOrder_HtyService, IStockRepository stockRepository, IInboundOrderDetailService inboundOrderDetailService, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, ITask_HtyService taskHtyService, ILocationInfoService locationInfoService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IBasicRepository basicRepository, IStockInfoDetailRepository stockInfoDetailRepository, IPalletTypeInfoRepository palletTypeInfoRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _stockRepository = stockRepository;
            _unitOfWorkManage = unitOfWorkManage;
            _basicService = basicService;
            _outboundService = outboundService;
            _inboundService = inboundService;
            _recordService = recordService;
            _stockService = stockService;
            _taskHtyService = taskHtyService;
            _locationInfoService = locationInfoService;
            _inboundOrderDetailService = inboundOrderDetailService;
            _materielInfoService = materielInfoService;
            _inboundOrder_HtyService = inboundOrder_HtyService;
            _inboundOrderDetail_HtyService = inboundOrderDetail_HtyService;
            _outboundOrder_HtyService = outboundOrder_HtyService;
            _outboundOrderDetail_HtyService = outboundOrderDetail_HtyService;
            _outboundOrderDetailRepository = outboundOrderDetailRepository;
            _basicRepository = basicRepository;
            _stockInfoDetailRepository = stockInfoDetailRepository;
            _palletTypeInfoRepository = palletTypeInfoRepository;
        }
 
        public string ReceiveWMSTaskin = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTaskin"];
        public string ReceiveWMSTaskout = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTaskout"];
        public string ReceiveWMSTaskAUT = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTaskAUT"];
        public string ReceiveWMSTaskAllocatein = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTaskAllocatein"];
 
 
        /// <summary>
        /// 任务信息推送至WCS
        /// </summary>
        /// <returns></returns>
        public WebResponseContent PushTasksToWCS(List<Dt_Task> tasks, string agvDescription = "")
        {
            try
            {
                List<WMSTaskDTO> taskDTOs = _mapper.Map<List<WMSTaskDTO>>(tasks);
                taskDTOs.ForEach(x =>
                {
                    x.AGVArea = agvDescription;
                });
                string url = AppSettings.app("WCS");
                if (string.IsNullOrEmpty(url))
                {
                    return WebResponseContent.Instance.Error($"未找到WCSApi地址,请检查配置文件");
                }
                string response = HttpHelper.Post($"{url}/api/Task/ReceiveTask", taskDTOs.Serialize());
 
                return JsonConvert.DeserializeObject<WebResponseContent>(response) ?? WebResponseContent.Instance.Error("返回错误");
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 放货完成
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public WebResponseContent PutFinish(string code)
        {
            try
            {
                string url = AppSettings.app("WCS");
                if (string.IsNullOrEmpty(url))
                {
                    return WebResponseContent.Instance.Error($"未找到WCSAApi地址,请检查配置文件");
                }
                string response = HttpHelper.Post($"{url}/api/CTU_AGV/PutFinish?code=" + code);
 
                return JsonConvert.DeserializeObject<WebResponseContent>(response) ?? WebResponseContent.Instance.Error("返回错误");
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <returns>返回处理结果</returns>
        public async Task<WebResponseContent> TaskCompleted(int taskNum)
        {
            try
            {
                Dt_Task task = await Repository.QueryFirstAsync(x => x.TaskNum == taskNum);
                if (task == null)
                {
                    return await Task.FromResult(WebResponseContent.Instance.Error($"未找到任务信息"));
                }
                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                {
                    return await Task.FromResult(InboundTaskCompleted(taskNum));
                }
                else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    return await Task.FromResult(OutboundTaskCompleted(taskNum));
                }
                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
                //{
                //    return await Task.FromResult(RelocationTaskCompleted(task));
                //}
                else
                {
                    return await Task.FromResult(WebResponseContent.Instance.Error($"未找到该类型任务,任务类型:{task.TaskType}"));
                }
            }
            catch (Exception ex)
            {
                return await Task.FromResult(WebResponseContent.Instance.Error(ex.Message));
            }
        }
 
 
 
        /// <summary>
        /// 修改任务状态
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public WebResponseContent UpdateTaskInfo(WCSTaskDTO task)
        {
            try
            {
                Dt_Task wmsTask = BaseDal.QueryFirst(x => x.TaskNum == task.TaskNum);
                if (wmsTask != null)
                {
                    wmsTask.TaskStatus = task.TaskState;
                    wmsTask.CurrentAddress = task.CurrentAddress;
                    wmsTask.NextAddress = task.NextAddress;
                    wmsTask.Dispatchertime = task.Dispatchertime;
                    BaseDal.UpdateData(wmsTask);
                }
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        public WebResponseContent UpdateTaskStatus(int taskNum, int tasktype)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum && x.TaskType == tasktype);
                if (task == null) return WebResponseContent.Instance.Error("未找到任务信息");
                if (TaskEnumHelper.GetTaskTypeGroup(task.TaskType) == TaskTypeGroup.InboundGroup)
                {
                    task.TaskStatus = TaskInStatusEnum.InFinish.ObjToInt();
                    content = UpdateTaskStatusInFinish(task);
                }
            }
            catch (Exception)
            {
 
                throw;
            }
            return content;
        }
 
        public WebResponseContent UpdateTaskStatusInFinish(Dt_Task task)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);//组盘库存
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);//货位
                var result = CheckCompleted(stockInfo, locationInfo);
                if (!result.Item1)
                {
                    throw new Exception(result.Item2);
                }
 
                if (stockInfo.StockStatus != StockStatusEmun.入库中.ObjToInt()) throw new Exception($"托盘[{task.PalletCode}],该组盘状态不可入库");
                Dt_StockInfoDetail stockInfoDetail = stockInfo.Details.FirstOrDefault(x => x.StockId == stockInfo.Id);
 
                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.Repository.LocationCodesGetStockInfos(new List<string> { stockInfo.LocationCode }).Where(x => x.StockStatus == (int)StockStatusEmun.已入库.ObjToInt()).ToList();
                if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt())
                {
                    stockInfo.SerialNumber = stockInfos.Count + 1;
                    stockInfo.InDate = DateTime.Now;
                    stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
                    stockInfoDetail.Status = StockStatusEmun.已入库.ObjToInt();
                    locationInfo.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
                    task.CurrentAddress = task.NextAddress;
                    task.NextAddress = string.Empty;
                    Db.Ado.BeginTran();
                    BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                    _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                    _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                    _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
                    Db.Ado.CommitTran();
                    return content;
                }
                #region 入库单
                Dt_InboundOrder inboundOrder = _inboundService.InbounOrderService.GetInboundOrder(stockInfoDetail.OrderNo);
                if (inboundOrder != null || inboundOrder.Details.Count < 1) throw new Exception($"未找到托盘[{task.PalletCode}]的入库单明细信息");
                Dt_InboundOrderDetail inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfoDetail.BatchNo && x.MaterielCode == stockInfoDetail.MaterielCode);
                inboundOrderDetail.OrderDetailStatus = inboundOrderDetail.OverInQuantity == inboundOrderDetail.OrderQuantity ? OrderDetailStatusEnum.Over.ObjToInt() : OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                if (inboundOrder.Details.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null)
                {
                    inboundOrder.OrderStatus = InboundStatusEnum.入库完成.ObjToInt();
                }
                else if (inboundOrder.OrderStatus == InboundStatusEnum.未开始.ObjToInt())
                {
                    inboundOrder.OrderStatus = InboundStatusEnum.入库中.ObjToInt();
                }
                stockInfo.SerialNumber = stockInfos.Count + 1;
                stockInfo.InDate = DateTime.Now;
                stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
                stockInfoDetail.Status = StockStatusEmun.已入库.ObjToInt();
                locationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                task.CurrentAddress = task.NextAddress;
                task.NextAddress = string.Empty;
                Db.Ado.BeginTran();
                #region 任务和入库单
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                if (inboundOrder.OrderStatus != InboundStatusEnum.入库完成.ObjToInt())
                {
                    _inboundService.InbounOrderService.Repository.UpdateData(inboundOrder);
                    _inboundService.InboundOrderDetailService.Repository.UpdateData(inboundOrderDetail);
                }
                else
                {
                    List<Dt_InboundOrderDetail> orderDetails = inboundOrder.Details;
                    inboundOrder.Details = null;
                    _inboundService.InbounOrderService.Repository.DeleteAndMoveIntoHty(inboundOrder, OperateType.自动完成);
                    _inboundService.InboundOrderDetailService.Repository.DeleteAndMoveIntoHty(orderDetails, OperateType.自动完成);
                }
                #endregion
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
                //_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                Db.Ado.CommitTran();
                #endregion
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 验证数据
        /// </summary>
        /// <param name="stockInfo"></param>
        /// <param name="locationInfo"></param>
        /// <returns></returns>
        private (bool, string) CheckCompleted(Dt_StockInfo stockInfo, Dt_LocationInfo locationInfo, bool isCheckStockDetail = true)
        {
            if (stockInfo == null)
            {
                return (false, "未找到组盘信息");
            }
 
            if (locationInfo == null)
            {
                return (false, "未找到货位信息");
            }
 
            if (isCheckStockDetail && (stockInfo.Details == null || stockInfo.Details.Count == 0))
            {
                return (false, "未找到组盘明细信息");
            }
 
            return (true, "成功");
        }
 
       #region 入库任务完成
        public WebResponseContent InboundTaskCompleted(int taskNum)
        {
            return null;
        }
        #endregion
 
        #region//出库任务完成
        public WebResponseContent OutboundTaskCompleted(int taskNum)
        {
            return null;
        }
        #endregion
    }
}