1
hutongqing
2025-01-11 e110e0884f1a1e10b9fddb669ca0f06c89c5faf6
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
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Task;
using WIDESEA_Model.Models;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService
    {
        /// <summary>
        /// 申请入库任务(PDA使用,仅托盘绑定入库站台,不分配货位)
        /// </summary>
        /// <param name="palletCode">托盘号</param>
        /// <param name="stationCode">站台号</param>
        /// <returns></returns>
        public WebResponseContent RequestInboundTask(SaveModel saveModel)
        {
            try
            {
                var palletCode = saveModel.MainData["barcode"].ToString();
                var warehouseId = saveModel.MainData["warehouseId"].ObjToInt();
                var stationCode = saveModel.MainData["startPoint"].ToString();
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == warehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到库区");
                }
                Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode && x.WarehouseId == warehouseId);
                if (task != null)
                {
                    return WebResponseContent.Instance.Error($"该托盘已生成任务");
                }
 
                if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
                {
                    return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
                }
 
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
                if (stockInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到组盘信息");
                }
                if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.手动组盘暂存.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.出库完成.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.拣选完成.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
                }
                if (!string.IsNullOrEmpty(stockInfo.LocationCode))
                {
                    return WebResponseContent.Instance.Error($"该托盘已绑定货位");
                }
                if (warehouseId != stockInfo.WarehouseId)
                {
                    return WebResponseContent.Instance.Error($"仓库不正确");
                }
                if (warehouse.WarehouseCode == WarehouseEnum.HA60.ToString())
                {
                    Dt_LocationInfo locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.WarehouseId == warehouseId);
                    if (locationInfo == null) return WebResponseContent.Instance.Error($"未找到当前库区货位信息");
                    return DeviceRequestInboundTask(stationCode, locationInfo.RoadwayNo, palletCode);
                }
                else
                {
                    Dt_Task newTask = new Dt_Task()
                    {
                        CurrentAddress = stationCode,
                        Grade = 0,
                        NextAddress = "",
                        PalletCode = palletCode,
                        Roadway = "",
                        SourceAddress = stationCode,
                        TargetAddress = "",
                        TaskType = TaskTypeEnum.Inbound.ObjToInt(),
                        TaskStatus = TaskStatusEnum.New.ObjToInt(),
                        WarehouseId = stockInfo.WarehouseId,
                        PalletType = stockInfo.PalletType
                    };
 
                    if (stockInfo.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
                    {
                        stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
                    }
                    else
                    {
                        stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
                    }
                    _unitOfWorkManage.BeginTran();
                    int taskId = BaseDal.AddData(newTask);
                    newTask.TaskId = taskId;
                    _stockRepository.StockInfoRepository.UpdateData(stockInfo);
                    _unitOfWorkManage.CommitTran();
                    PushTasksToWCS(new List<Dt_Task> { newTask });
                    return WebResponseContent.Instance.OK(data: newTask);
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 入空箱
        /// </summary>
        /// <returns></returns>
        public WebResponseContent InEmpty(string barcode, string address, int WarehouseId)
        {
            try
            {
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == WarehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到库区");
                }
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == barcode && x.StockStatus != StockStatusEmun.出库完成.ObjToInt());
                if (stockInfo != null) throw new Exception($"托盘号已存在");
                stockInfo = new Dt_StockInfo()
                {
                    PalletCode = barcode,
                    StockStatus = StockStatusEmun.入库确认.ObjToInt(),
                    WarehouseId = WarehouseId,
                    PalletType = PalletTypeEnum.Empty.ObjToInt(),
                    Details = new List<Dt_StockInfoDetail>()
                };
                Dt_LocationInfo locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.WarehouseId == WarehouseId);
                if (locationInfo == null) return WebResponseContent.Instance.Error($"未找到当前库区货位信息");
                locationInfo = _basicService.LocationInfoService.AssignLocation(locationInfo.RoadwayNo, ((PalletTypeEnum)stockInfo.PalletType).ObjToInt(), stockInfo.WarehouseId);
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                }
 
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = address,
                    Grade = 0,
                    NextAddress = locationInfo.LocationCode,
                    PalletCode = barcode,
                    Roadway = locationInfo.RoadwayNo,
                    SourceAddress = address,
                    TargetAddress = locationInfo.LocationCode,
                    TaskType = TaskTypeEnum.InEmpty.ObjToInt(),
                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
                    WarehouseId = stockInfo.WarehouseId,
                    PalletType = stockInfo.PalletType
                };
                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                _basicRepository.LocationInfoRepository.UpdateData(locationInfo);
                _stockRepository.StockInfoRepository.AddData(stockInfo);
                _unitOfWorkManage.CommitTran();
                PushTasksToWCS(new List<Dt_Task> { newTask });
                PutFinish(address);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent OutEmpty(int qty, string address, int WarehouseId, string barcode)
        {
            try
            {
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == WarehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到库区");
                }
                List<Dt_StockInfo> stockInfos = null;
                if (string.IsNullOrEmpty(barcode))
                {
                    stockInfos = _stockRepository.StockInfoRepository.QueryData(x => x.WarehouseId == WarehouseId && x.PalletType == PalletTypeEnum.Empty.ObjToInt() && x.StockStatus == StockStatusEmun.入库完成.ObjToInt(), qty, nameof(Dt_StockInfo.CreateDate));
                }
                else
                {
                    stockInfos = _stockRepository.StockInfoRepository.QueryData(x => x.WarehouseId == WarehouseId && x.PalletType == PalletTypeEnum.Empty.ObjToInt() && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.PalletCode == barcode);
                    if (stockInfos.Count == 0) return WebResponseContent.Instance.Error($"{warehouse.WarehouseName}未找到空箱【{barcode}】");
                }
 
                if (stockInfos.Count < qty) return WebResponseContent.Instance.Error($"{warehouse.WarehouseName}空箱库存不足,库存数【{stockInfos.Count}】");
                List<Dt_Task> tasks = GetTasks(stockInfos, TaskTypeEnum.OutEmpty);
                stockInfos.ForEach(x =>
                {
                    x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                });
                tasks.ForEach(x =>
                {
                    x.TargetAddress = address;
                    x.NextAddress = address;
                });
                _unitOfWorkManage.BeginTran();
                BaseDal.AddData(tasks);
                _stockRepository.StockInfoRepository.UpdateData(stockInfos);
                _unitOfWorkManage.CommitTran();
                PushTasksToWCS(tasks);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent DeviceRequestInboundTask(string stationCode, string roadwayNo, string palletCode)
        {
            try
            {
                Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
                if (task != null)
                {
                    return WebResponseContent.Instance.OK($"该托盘已生成任务", _mapper.Map<WMSTaskDTO>(task));
                }
 
                if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
                {
                    return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
                }
 
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
                if (stockInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到组盘信息");
                }
                if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.手动组盘暂存.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.出库完成.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.拣选完成.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
                }
                if (!string.IsNullOrEmpty(stockInfo.LocationCode))
                {
                    return WebResponseContent.Instance.Error($"该托盘已绑定货位");
                }
 
                Dt_LocationInfo? locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayNo, stockInfo.PalletType, stockInfo.WarehouseId);
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                }
 
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = stationCode,
                    Grade = 0,
                    NextAddress = locationInfo.LocationCode,
                    PalletCode = palletCode,
                    Roadway = roadwayNo,
                    SourceAddress = stationCode,
                    TargetAddress = locationInfo.LocationCode,
                    TaskType = TaskTypeEnum.Inbound.ObjToInt(),
                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
                    WarehouseId = stockInfo.WarehouseId,
                    PalletType = stockInfo.PalletType
                };
 
                if (stockInfo.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
                {
                    stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
                }
                else
                {
                    stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
                }
                LocationStatusEnum lastStatus = (LocationStatusEnum)locationInfo.LocationStatus;
                _unitOfWorkManage.BeginTran();
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, lastStatus, LocationStatusEnum.Lock, LocationChangeType.InboundAssignLocation);
                _basicService.LocationInfoService.UpdateLocationStatus(locationInfo, newTask.PalletType, LocationStatusEnum.Lock, newTask.WarehouseId);
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                _stockRepository.StockInfoRepository.UpdateData(stockInfo);
                _unitOfWorkManage.CommitTran();
                WMSTaskDTO wMSTaskDTO = _mapper.Map<WMSTaskDTO>(newTask);
 
                PushTasksToWCS(new List<Dt_Task> { newTask });
                if (newTask.WarehouseId == 5) PutFinish(stationCode);
                return WebResponseContent.Instance.OK(data: wMSTaskDTO);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// 入库任务申请分配货位
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <param name="roadwayNo">巷道号</param>
        /// <returns></returns>
        public WebResponseContent AssignInboundTaskLocation(int taskNum, string roadwayNo)
        {
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该入库任务");
                }
 
                if (_basicRepository.LocationInfoRepository.QueryFirst(x => x.LocationCode == task.TargetAddress) != null)
                {
                    return WebResponseContent.Instance.OK(data: task.TargetAddress);
                }
 
                Dt_LocationInfo? locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayNo, task.PalletType, task.WarehouseId);
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                }
 
                task.Roadway = roadwayNo;
                task.TargetAddress = locationInfo.LocationCode;
                task.TaskStatus = TaskStatusEnum.SC_Execute.ObjToInt();
 
                LocationStatusEnum lastStatus = (LocationStatusEnum)locationInfo.LocationStatus;
 
                _unitOfWorkManage.BeginTran();
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, lastStatus, LocationStatusEnum.Lock, LocationChangeType.InboundAssignLocation);
                _basicService.LocationInfoService.UpdateLocationStatus(locationInfo, task.PalletType, LocationStatusEnum.Lock, task.WarehouseId);
                BaseDal.UpdateData(task);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK(data: locationInfo.LocationCode);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}