Blame view

src/main/java/com/huaheng/pc/task/taskHeader/service/TaskHeaderServiceImpl.java 38.6 KB
mahuandong authored
1
2
package com.huaheng.pc.task.taskHeader.service;
pengcheng authored
3
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
wangyanxiong authored
4
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
pengcheng authored
5
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
mahuandong authored
6
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
pengcheng authored
7
import com.huaheng.api.wcs.service.taskAssignService.TaskAssignService;
pengcheng authored
8
import com.huaheng.common.constant.QuantityConstant;
wangyanxiong authored
9
import com.huaheng.common.exception.service.ServiceException;
游杰 authored
10
import com.huaheng.common.support.Convert;
wangyanxiong authored
11
import com.huaheng.common.utils.StringUtils;
pengcheng authored
12
13
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
14
import com.huaheng.pc.config.configWarning.service.ConfigWarningService;
xqs authored
15
import com.huaheng.pc.config.container.domain.Container;
wangyanxiong authored
16
import com.huaheng.pc.config.container.service.ContainerService;
17
import com.huaheng.pc.config.containerCapacity.service.ContainerCapacityService;
18
19
import com.huaheng.pc.config.cycleCountPreference.domain.CycleCountPreference;
import com.huaheng.pc.config.cycleCountPreference.service.CycleCountPreferenceService;
pengcheng authored
20
21
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
xqs authored
22
23
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
24
import com.huaheng.pc.inventory.cycleCountHeader.service.CycleCountHeaderService;
pengcheng authored
25
26
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
27
28
29
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
xqs authored
30
31
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
mahuandong authored
32
33
34
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
xqs authored
35
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
游杰 authored
36
import com.huaheng.pc.receipt.receiving.service.ReceivingService;
pengcheng authored
37
38
39
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
40
41
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
pengcheng authored
42
43
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
游杰 authored
44
import com.huaheng.pc.task.taskHeader.domain.MobileTask;
mahuandong authored
45
46
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.mapper.TaskHeaderMapper;
pengcheng authored
47
import org.springframework.beans.factory.annotation.Autowired;
mahuandong authored
48
import org.springframework.stereotype.Service;
mahuandong authored
49
50
51
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
pengcheng authored
52
import java.math.BigDecimal;
53
import java.util.*;
xqs authored
54
import java.util.concurrent.CopyOnWriteArrayList;
游杰 authored
55
import java.util.stream.Collectors;
xqs authored
56
mahuandong authored
57
@Service
wangyanxiong authored
58
public class TaskHeaderServiceImpl extends ServiceImpl<TaskHeaderMapper, TaskHeader> implements TaskHeaderService {
mahuandong authored
59
60
    @Resource
pengcheng authored
61
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
62
    @Resource
pengcheng authored
63
    private LocationService locationService;
64
    @Resource
xqs authored
65
66
    private ContainerService containerService;
    @Resource
pengcheng authored
67
    private TaskDetailService taskDetailService;
68
    @Resource
wangyanxiong authored
69
    private TaskHeaderService taskHeaderService;
70
    @Resource
71
    private InventoryHeaderService inventoryHeaderService;
72
73
    @Resource
    private InventoryDetailService inventoryDetailService;
xqs authored
74
    @Resource
xqs authored
75
    private ReceiptContainerDetailService receiptContainerDetailService;
76
    @Resource
xqs authored
77
    private CycleCountDetailService cycleCountDetailService;
mahuandong authored
78
    @Resource
79
80
81
    private CycleCountPreferenceService cycleCountPreferenceService;
    @Resource
    private CycleCountTaskService cycleCountTaskService;
82
    @Resource
mahuandong authored
83
    private ReceiptContainerHeaderService receiptContainerHeaderService;
xqs authored
84
    @Resource
85
    private TaskHeaderMapper taskHeaderMapper;
游杰 authored
86
    @Autowired
87
    private TaskAssignService taskAssignService;
mahuandong authored
88
    @Resource
89
    private ReceiptTaskService receiptTaskService;
90
91
    @Resource
    private ShipmentTaskService shipmentTaskService;
92
93
    @Resource
    private WorkTaskService workTaskService;
游杰 authored
94
95
    @Resource
    private TransferTaskService transferTaskService;
mahuandong authored
96
97
98
xqs authored
99
100
101
102
103
104
    @Override
    public List<TaskHeader> getTasksStatus(Short lastStatus) {
        return taskHeaderMapper.getTasksStatus(lastStatus);
    }
105
pengcheng authored
106
    /**
107
     * 查询容器有无任务
mahuandong authored
108
109
     */
    @Override
110
111
    public Integer UncompleteCount(String ContainerCode) {
        return taskHeaderMapper.UncompleteCount(ContainerCode, ShiroUtils.getWarehouseCode());
mahuandong authored
112
113
114
115
    }


    /**
116
117
118
     * 盘点任务首选项
     *
     * @param taskHeaderList
mahuandong authored
119
120
121
     * @return
     */
    @Override
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
    public List<TaskHeader> preferenceRealize(List<TaskHeader> taskHeaderList) {
        //盘点任务头,默认不显示库位,容器。
        List<TaskHeader> taskHeaders = new ArrayList<>();
        for (TaskHeader item:taskHeaderList){
            //盘点明细
            CycleCountDetail cycleCountDetailtemp = new CycleCountDetail();
            cycleCountDetailtemp.setWarehouseCode(item.getWarehouseCode());
            cycleCountDetailtemp.setCycleCountHeadCode(item.getReferenceCode());
            cycleCountDetailtemp.setId(item.getAllocationHeadId());  //明细ID
            LambdaQueryWrapper<CycleCountDetail> detailLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountDetailtemp);
            CycleCountDetail cycleCountDetail = cycleCountDetailService.getOne(detailLambdaQueryWrapper);
            if(StringUtils.isNull(cycleCountDetail)){
                throw new SecurityException("没有找到子任务对应的盘点明细!");
            }
            //找盘点首选项
            CycleCountPreference cycleCountPreferenceTemp = new CycleCountPreference();
            cycleCountPreferenceTemp.setCode(cycleCountDetail.getPreferenceCode());
            cycleCountPreferenceTemp.setWarehouseCode(cycleCountDetail.getWarehouseCode());
            LambdaQueryWrapper<CycleCountPreference> preferenceLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountPreferenceTemp);
            CycleCountPreference cycleCountPreference = cycleCountPreferenceService.getOne(preferenceLambdaQueryWrapper);
            if(StringUtils.isNull(cycleCountPreference)){
                throw new SecurityException("没有找到盘点首选项!");
            }
            //根据首选项判断数据
            if(!cycleCountPreference.getPromptLocation()){
                //库位
                item.setFromLocation("");
                item.setToLocation("");
            }
            if(!cycleCountPreference.getPromptLpn()){
                //容器
                item.setContainerCode("");
            }
            taskHeaders.add(item);
mahuandong authored
156
        }
157
        return taskHeaders;
xqs authored
158
    }
wangyanxiong authored
159
160
161
    /**
     * 取消任务
162
     */
163
    @Override
xqs authored
164
    @Transactional(rollbackFor = Exception.class)
165
    public AjaxResult cancelTask(Integer[] taskIds) {
166
        for (int taskId : taskIds) {
167
            TaskHeader taskHeader = taskHeaderService.getById(taskId);
168
169
            if (taskHeader == null) {
                return AjaxResult.error("任务" + taskId + "未找到,操作中止");
170
            }
171
172
            if (taskHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
                return AjaxResult.error("存在任务" + taskHeader.getId() + "已下发或执行,操作中止");
173
            }
xqs authored
174
175
176
177
178
179
180
181
            //查出任务明细
            TaskDetail taskDetail1 = new TaskDetail();
            taskDetail1.setTaskId(taskHeader.getId());
            taskDetail1.setWarehouseCode(taskHeader.getWarehouseCode());
            taskDetail1.setCompanyCode(taskHeader.getCompanyCode());
            LambdaQueryWrapper<TaskDetail> td = Wrappers.lambdaQuery(taskDetail1);
            List<TaskDetail> taskDetailList = taskDetailService.list(td);
            TaskDetail taskDetail = taskDetailList.get(0);
182
            if (taskDetail == null) {
xqs authored
183
184
                throw new ServiceException("任务明细条目错误");
            }
185
186
            //删除子任务
            LambdaQueryWrapper<TaskDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
187
            lambdaQueryWrapper.eq(TaskDetail::getTaskId, taskHeader.getId());
188
189
190
191
            taskDetailService.remove(lambdaQueryWrapper);
            //删除主任务
            taskHeaderService.removeById(taskHeader.getId());
            // 更改库位状态(整盘入库任务除外)
mahuandong authored
192
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF)) {
193
                receiptContainerHeaderService.cancelReceipt(taskHeader.getAllocationHeadId());
194
195
196
            }
            //根据任务类型来更新货箱状态
            //修改关联的货箱状态
mahuandong authored
197
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_PICKING)) {
198
199
                ShipmentContainerHeader shipmentContainerHeader = new ShipmentContainerHeader();
                shipmentContainerHeader.setId(taskHeader.getAllocationHeadId());
pengcheng authored
200
                shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
201
202
                shipmentContainerHeaderService.updateById(shipmentContainerHeader);
            }
xqs authored
203
            //盘点取消任务,恢复明细状态为1
xqs authored
204
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_CYCLECOUNT)) {
xqs authored
205
206
207
208
209
210
211
212
213
214

                CycleCountDetail cycleCountDetail = new CycleCountDetail();
                cycleCountDetail.setCompanyCode(taskDetail.getCompanyCode());
                cycleCountDetail.setWarehouseCode(taskDetail.getWarehouseCode());
                cycleCountDetail.setLocationCode(taskDetail.getFromLocation());
                cycleCountDetail.setContainerCode(taskDetail.getContainerCode());
                cycleCountDetail.setCycleCountHeadCode(taskDetail.getBillCode());//盘点单Code
                cycleCountDetail.setId(taskDetail.getBillDetailId());//盘点细单ID
                LambdaQueryWrapper<CycleCountDetail> lam = Wrappers.lambdaQuery(cycleCountDetail);
                List<CycleCountDetail> cycleCountDetailList = cycleCountDetailService.list(lam);//
215
                for (CycleCountDetail item : cycleCountDetailList) {
xqs authored
216
217
218
219
220
221
222
223
224
                    item.setTaskHeaderId(null);
                    item.setTaskHeaderId(null);
                    item.setLastUpdated(new Date());
                    item.setLastUpdatedBy(ShiroUtils.getLoginName());
                    item.setEnableStatus(1);
                    cycleCountDetailService.saveOrUpdate(item);
                }

            }
225
226
227
228
229
230
231
232
233
234
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_TRANSFER)) {
                if (taskHeader.getFromLocation() != null) {
                    //更新托盘、库位状态
                    locationService.updateStatus(taskHeader.getFromLocation(), "empty");
                }
                if (taskHeader.getToLocation() != null) {
                    //更新托盘、库位状态
                    locationService.updateStatus(taskHeader.getToLocation(), "empty");
                }
            }
xqs authored
235
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF) || taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_PICKING)) {
236
                if (taskHeader.getToLocation() != null) {
237
238
239
                    //更新托盘、库位状态
                    locationService.updateStatus(taskHeader.getToLocation(), "empty");
                }
游杰 authored
240
241
242
                LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, taskHeader.getContainerCode());
                InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
243
244
                Container container = new Container();
                container.setStatus("empty");
游杰 authored
245
                if(inventoryHeader != null) {
246
                    if("some".equals(inventoryHeader.getContainerStatus())) {
游杰 authored
247
248
249
                        container.setStatus("some");
                    }
                }
250
251
252
                LambdaUpdateWrapper<Container> containerUpdateWrapper = Wrappers.lambdaUpdate();
                containerUpdateWrapper.eq(Container::getCode, taskHeader.getContainerCode());
                containerService.update(container, containerUpdateWrapper);
253
254
255
256
257
258
259
260
261
            }
//            if(task.getType()==900){
//                //出库查看任务没有关联的货箱,不做处理
//            }
        }

        return AjaxResult.success("取消任务成功!");
    }
mahuandong authored
262
    /**
263
     * 下发WCS执行任务
wangyanxiong authored
264
     * 执行任务
265
     */
wangyanxiong authored
266
    @Override
xqs authored
267
    @Transactional(rollbackFor = Exception.class)
wangyanxiong authored
268
    public AjaxResult<TaskHeader> sendTaskToWcs(Integer[] taskIds) {
wangyanxiong authored
269
270
        TaskHeader task = null;
        for (Integer taskId : taskIds) {
wangyanxiong authored
271
            task = taskHeaderService.getById(taskId);
272
pengcheng authored
273
            if (task.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
wangyanxiong authored
274
275
                return AjaxResult.error("任务" + taskId + "已经下发,请不要重复下发,操作中止");
            }
276
pengcheng authored
277
            // 给wcs传递任务
游杰 authored
278
279
280
281
282

             AjaxResult ajaxResult = taskAssignService.wcsTaskAssign(task);
             if(ajaxResult != null && ajaxResult.hasErr()) {
                 return AjaxResult.error(ajaxResult.getMsg());
             }
283
pengcheng authored
284
wangyanxiong authored
285
            //修改任务头表
286
            task.setId(taskId);
pengcheng authored
287
            task.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
wangyanxiong authored
288
289
290
            task.setStartPickDateTime(new Date());   //生成时间
            task.setLastUpdated(new Date());
            task.setLastUpdatedBy(ShiroUtils.getLoginName());
291
            taskHeaderService.saveOrUpdate(task);
wangyanxiong authored
292
            //修改任务明细状态
293
294
295
296
            LambdaQueryWrapper<TaskDetail> lambdaWrapper = Wrappers.lambdaQuery();
            lambdaWrapper.eq(TaskDetail::getTaskId, task.getId());
            List<TaskDetail> taskDetailList = taskDetailService.list(lambdaWrapper);
            List<TaskDetail> taskDetails = new ArrayList<>();
297
            for (TaskDetail item : taskDetailList) {
pengcheng authored
298
                item.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
299
300
301
302
303
                item.setLastUpdated(new Date());
                item.setLastUpdatedBy(ShiroUtils.getLoginName());
                item.setProcessStamp("100");
                taskDetails.add(item);
                //盘点执行修改盘点单据状态为10
mahuandong authored
304
                if (task.getTaskType().equals(QuantityConstant.TASK_TYPE_CYCLECOUNT)) {
305
                    cycleCountDetailService.updataDetailStatus(item.getBillDetailId(), QuantityConstant.CYCLECOUNT_STATUS_EXECUTING);
306
                }
307
            }
游杰 authored
308
309
310
311
            if(taskDetails != null && taskDetails.size() > 0) {
                if (!taskDetailService.saveOrUpdateBatch(taskDetails)) {
                    throw new ServiceException("更新任务明细失败");
                }
312
            }
313
游杰 authored
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
//            //修改入库明细
//            if (task.getInternalTaskType()==100){
//                ReceiptDetail receiptDetail = receiptDetailService.queryflow(receiptDetailService.getById(record.getId()));
//                if (!receiptDetailService.updateById(receiptDetail)){
//                    throw new ServiceException("更新状态失败");
//                }
//                receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
//
//                //修改组盘表状态为20
//                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
//                receiptContainerDetail.setStatus(20);
//                receiptContainerDetail.setLastUpdated(new Date());
//                receiptContainerDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
//                LambdaUpdateWrapper<ReceiptContainerDetail> receiptContainerDetailLambdaUpdateWrapper = Wrappers.lambdaUpdate();
//                receiptContainerDetailLambdaUpdateWrapper.eq(ReceiptContainerDetail::getReceiptId,receiptDetail.getReceiptId());
//                if (! receiptContainerDetailService.update(receiptContainerDetail, receiptContainerDetailLambdaUpdateWrapper))
//                    throw new ServiceException("更新组盘状态失败");
//            }
//
//            //修改出库单状态
//            if (task.getInternalTaskType()==200){
//                LambdaQueryWrapper<TaskDetail> taskDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
//                taskDetailLambdaQueryWrapper.eq(TaskDetail::getTaskId,task.getId());
//               List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLambdaQueryWrapper);
//
//                for (TaskDetail taskDeatails: taskDetailList) {
//                    LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
//                    shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getId,taskDeatails.getBillDetailId());
//
//                    ShipmentHeader shipmentHeader =new ShipmentHeader();
//                    shipmentHeader.setId(shipmentDetailService.getOne(shipmentDetailLambdaQueryWrapper).getShipmentId());
//                    shipmentHeader.setFirstStatus(100);
//                    shipmentHeader.setLastStatus(100);
//                    shipmentHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
//                    shipmentHeader.setLastUpdated(new Date());
//                    shipmentHeaderService.updateById(shipmentHeader);
//                }
//            }
wangyanxiong authored
353
        }
xqs authored
354
        return AjaxResult.success("执行下发任务成功", task);
wangyanxiong authored
355
356
    }
357
358
359
    /**
     * WMS完成任务
     */
wangyanxiong authored
360
    @Override
361
    @Transactional(rollbackFor = Exception.class)
xqs authored
362
    public AjaxResult completeTaskByWMS(Integer[] taskIds, String[] weightConvert) {
mahuandong authored
363
364
        for (int i = 0;i<taskIds.length;i++) {
            TaskHeader task = taskHeaderService.getById(taskIds[i]);
xqs authored
365
wangyanxiong authored
366
            if (task == null) {
mahuandong authored
367
                throw new ServiceException("任务" + taskIds[i] + "未找到,执行中止");
wangyanxiong authored
368
            }
xqs authored
369
370
371
372
373
374
            //任务写入重量
            String weight = "";
            if (StringUtils.isNotNull(weightConvert)) {
                weight = weightConvert[i];
            }
            task.setWeight(weight);
wangyanxiong authored
375
            //如果已完成则不管
mahuandong authored
376
377
378
            if (task.getStatus().equals(QuantityConstant.TASK_STATUS_COMPLETED)) {
                return AjaxResult.success("任务(" + taskIds[i] + ")任务已经是完成的!");
wangyanxiong authored
379
            }
380
381
            //整盘入库,空托入库库位自动分配
            //整盘入库手动完成分配库位,已分配则略过
游杰 authored
382
            if(task.getTaskType().equals(QuantityConstant.TASK_TYPE_WHOLERECEIPT) || task.getTaskType().equals(QuantityConstant.TASK_TYPE_EMPTYRECEIPT)){
383
384
                if(StringUtils.isEmpty(task.getToLocation())){
                    //自动分配库位
385
386
                    //  AjaxResult ajaxResult = this.setLocationCode(task.getId(),0 );
                    //   task.setToLocation((String)ajaxResult.getData());
387
388
                }
            }
wangyanxiong authored
389
            //如果没有库位不能完成
390
391
            if (StringUtils.isEmpty(task.getFromLocation()) && StringUtils.isEmpty(task.getToLocation())) {
                throw new ServiceException("任务" + task.getId() + "没有库位,执行中止");
mahuandong authored
392
            }
393
mahuandong authored
394
            this.completeTask(task);
wangyanxiong authored
395
396
397
398
399
400
401
402
403
        }
        return AjaxResult.success("完成任务成功!");
    }

    /**
     * 完成任务
     *
     * @param task
     */
xqs authored
404
    public void completeTask(TaskHeader task) {
wangyanxiong authored
405
        //区分任务类型
mahuandong authored
406
        if (task.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF) ) {
wangyanxiong authored
407
            //入库任务
408
            receiptTaskService.completeReceiptTask(task);
409
           // combineInventory(task);
wangyanxiong authored
410
        }
mahuandong authored
411
412
413
        if (task.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_PICKING) &&
                (task.getTaskType().equals(QuantityConstant.TASK_TYPE_WHOLESHIPMENT) || task.getTaskType().equals(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT))) {
            //出库任务
414
            shipmentTaskService.completeShipmentTask(task);
wangyanxiong authored
415
        }
416
        // 900 出库查看,空托出库查看
mahuandong authored
417
        if (task.getTaskType().equals( QuantityConstant.TASK_TYPE_VIEW)) {
418
            workTaskService.completeCheckOutTask(task);
wangyanxiong authored
419
        }
420
        // 700 盘点
mahuandong authored
421
        if (task.getTaskType().equals(QuantityConstant.TASK_TYPE_CYCLECOUNT)) {
422
            cycleCountTaskService.completeCycleCountTask(task);
423
        }
mahuandong authored
424
        if (task.getTaskType().equals( QuantityConstant.TASK_TYPE_TRANSFER)) {
xqs authored
425
//          //移库
426
            transferTaskService.completeTransferTask(task);
wangyanxiong authored
427
        }
mahuandong authored
428
        if (task.getTaskType().equals( QuantityConstant.TASK_TYPE_EMPTYRECEIPT)) {
xqs authored
429
            //空托盘入库
430
            workTaskService.completeEmptyIn(task);
wangyanxiong authored
431
        }
mahuandong authored
432
        if (task.getTaskType().equals(QuantityConstant.TASK_TYPE_EMPTYSHIPMENT) ) {
433
            //空托盘出库
434
            workTaskService.completeEmptyOut(task);
wangyanxiong authored
435
436
437
        }
    }
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    /**
     * 根据库位完成任务
     *
     * @param locationCode
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult completeTask(String locationCode) {
        LambdaQueryWrapper<TaskHeader> taskQueryWrapper = Wrappers.lambdaQuery();
        taskQueryWrapper.eq(TaskHeader::getToLocation, locationCode);
        List<TaskHeader> list = this.list(taskQueryWrapper);
        if (list.isEmpty()) {
            throw new ServiceException("库位(" + locationCode + ")没有任务!");
        } else if(list.get(0).getStatus().equals(QuantityConstant.TASK_STATUS_COMPLETED)) {
            //如果已完成则不管
            throw new ServiceException("库位(" + locationCode + ")任务已经完成!");
        }
        if(list.get(0).getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF)){
            //入库任务
            return receiptTaskService.completeReceiptTask((list.get(0)));
        } else {
            throw new ServiceException("库位(" + locationCode + ")的任务不是上架,不能完成!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createReplenishTask(String containerCode, Integer taskStatus) {
        LambdaQueryWrapper<ReceiptContainerHeader> headerQueryWrapper = Wrappers.lambdaQuery();
        headerQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                .eq(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_BUILD);

        List<ReceiptContainerHeader> receiptContainerHeaderList = receiptContainerHeaderService.list(headerQueryWrapper);
        if (receiptContainerHeaderList.isEmpty()) {
            throw new ServiceException("该容器没有任务!");
        }
        if (receiptContainerHeaderList.size() > 1){
            throw new ServiceException("该容器存在多条任务");
        }
        if (receiptContainerHeaderList.get(0).getWarehouseCode() != ShiroUtils.getWarehouseCode()) {
            throw new ServiceException("容器不在当前仓库!");
        }
        LambdaQueryWrapper<ReceiptContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ReceiptContainerDetail::getReceiptContainerId, receiptContainerHeaderList.get(0).getId());
        List<ReceiptContainerDetail> containerDetailList = receiptContainerDetailService.list(lambdaQueryWrapper);
        ReceiptContainerHeader containerHeader = receiptContainerHeaderList.get(0);
        if (containerDetailList.isEmpty()){
            throw new ServiceException("没有组盘明细,请先组盘!");
        }
        if (containerHeader.getStatus().equals(QuantityConstant.TASK_STATUS_BUILD)) {
            if (containerHeader.getStatus().intValue() != taskStatus.intValue()) {
                containerHeader.setStatus(taskStatus);
                receiptContainerHeaderService.updateById(containerHeader);
            }
        }
        List<Integer> ids = new ArrayList<>();
        ids.add(containerHeader.getId());
        receiptTaskService.createReceiptTask(ids);
        return AjaxResult.success("成功!");
    }

    /**
     * 移动端根据容器编码获取盘点任务
     * @param containerCode 容器编码
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MobileTask> mobileFindTransferTask(String containerCode) {
        /* 查询该容器未完成的盘点任务明细*/
        LambdaQueryWrapper<TaskDetail> detailLambdaQueryWrapper = Wrappers.lambdaQuery();
        detailLambdaQueryWrapper.eq(TaskDetail::getContainerCode, containerCode)
                .eq(TaskDetail::getTaskType, QuantityConstant.TASK_TYPE_CYCLECOUNT)
                .ne(TaskDetail::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetailList = taskDetailService.list(detailLambdaQueryWrapper);

        /* 获取任务明细中的任务头id*/
        List<Integer> headerIdList = taskDetailList.stream().map(TaskDetail::getTaskId).collect(Collectors.toList());
        List<MobileTask> mobileTasks = new ArrayList<>();

        /* 遍历id查询出盘点任务*/
        for (Integer id : headerIdList) {
            TaskHeader taskHeader = taskHeaderService.getById(id);
            MobileTask mobileTask = new MobileTask();
            mobileTask.setTaskHeader(taskHeader);
            LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(TaskDetail::getTaskId, id)
                    .eq(TaskDetail::getContainerCode, containerCode);
            List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
            mobileTask.setTaskDetail(taskDetails);
            mobileTasks.add(mobileTask);
        }
        return mobileTasks;
    }

    /**
     * 容器收货上架
     *
     * @param containerCode
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createQuickTask(String containerCode) {
        LambdaQueryWrapper<ReceiptContainerHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                .eq(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_BUILD)
                .eq(ReceiptContainerHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(queryWrapper);
        if (receiptContainerHeader == null) {
            throw new ServiceException("该容器没有任务!");
        }

        LambdaQueryWrapper<ReceiptContainerDetail> containerDetailQueryWrapper = Wrappers.lambdaQuery();
        containerDetailQueryWrapper.eq(ReceiptContainerDetail::getReceiptContainerId, receiptContainerHeader.getId())
                .eq(ReceiptContainerDetail::getStatus, QuantityConstant.RECEIPT_CONTAINER_BUILD)
                .eq(ReceiptContainerDetail::getContainerCode, containerCode);
        List<ReceiptContainerDetail> containerDetailList = receiptContainerDetailService.list(containerDetailQueryWrapper);
        if (containerDetailList.isEmpty()) {
            throw new SecurityException("没有组盘明细,请先组盘");
        }
        if (receiptContainerHeader.getStatus() == QuantityConstant.RECEIPT_HEADER_BUILD.shortValue()) {
            String ids = String.valueOf(receiptContainerHeader.getId());
            if (StringUtils.isEmpty(ids)){
                return AjaxResult.error("id不能为空");
            }
            List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
            if (StringUtils.isEmpty(ids)){
                return AjaxResult.error("id不能为空");
            }

            Integer maxId = idList.stream().max(Comparator.comparing(Integer::intValue)).orElse(null);

            LambdaQueryWrapper<ReceiptContainerHeader> lambda = Wrappers.lambdaQuery();
            lambda.select(ReceiptContainerHeader::getId)
                    .lt(ReceiptContainerHeader::getStatus, 10)
                    .le(ReceiptContainerHeader::getId, maxId);
            return receiptTaskService.createReceiptTask(idList);
        }
        return AjaxResult.error("生成入库任务失败");
    }
游杰 authored
584
585
586
587
588
589
590
591
592
593
594

    @Transactional(rollbackFor = Exception.class)
    public void combineInventory(TaskHeader task) {
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderErapper = Wrappers.lambdaQuery();
        inventoryHeaderErapper.eq(InventoryHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(InventoryHeader::getLocationCode, task.getToLocation())
                .eq(InventoryHeader::getContainerCode, task.getContainerCode());
        InventoryHeader header = inventoryHeaderService.getOne(inventoryHeaderErapper);
        if(header != null) {
            LambdaQueryWrapper<InventoryDetail> inventory = Wrappers.lambdaQuery();
            inventory.eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
游杰 authored
595
                    .eq(InventoryDetail::getLocationCode, task.getToLocation())
游杰 authored
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
                    .eq(InventoryDetail::getContainerCode, task.getContainerCode());
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventory);
            for(int i=0; i < inventoryDetailList.size() -1; i++) {
                for(int j = inventoryDetailList.size() - 1; j > i; j--) {
                    InventoryDetail inventoryDetail = inventoryDetailList.get(i);
                    InventoryDetail inventoryDetail2 = inventoryDetailList.get(j);
                    if(inventoryDetail.getMaterialCode().equals(inventoryDetail2.getMaterialCode())
                            && inventoryDetail.getBatch().equals(inventoryDetail2.getBatch())
                            && inventoryDetail.getLot().equals(inventoryDetail2.getLot())
                            && inventoryDetail.getProjectNo().equals(inventoryDetail2.getProjectNo())) {
                        BigDecimal totalQty = inventoryDetailList.get(i).getQty().add(inventoryDetailList.get(j).getQty());
                        inventoryDetailList.get(i).setQty(totalQty);
                        LambdaQueryWrapper<InventoryDetail> wrapper = Wrappers.lambdaQuery();
                        wrapper.eq(InventoryDetail::getId, inventoryDetailList.get(i).getId());
                        inventoryDetailService.update(inventoryDetailList.get(i), wrapper);
                        LambdaQueryWrapper<InventoryDetail> wrapper2 = Wrappers.lambdaQuery();
                        wrapper2.eq(InventoryDetail::getId, inventoryDetailList.get(j).getId());
                        inventoryDetailService.remove(wrapper2);
                        inventoryDetailList.remove(j);
                    }
                }
            }
            BigDecimal totalQty = new BigDecimal(0);
            int totalLines = 0;
            for(InventoryDetail inventoryDetail : inventoryDetailList) {
                totalQty = totalQty.add(inventoryDetail.getQty());
                totalLines++;
            }
            header.setTotalQty(totalQty);
            header.setTotalLines(totalLines);
            header.setContainerStatus("some");
            LambdaQueryWrapper<InventoryHeader> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(InventoryHeader::getId, header.getId());
            inventoryHeaderService.update(header, wrapper);
        }
    }
mahuandong authored
633
634
635
636
637
638
639
640
    /**
     * 移动端创建入库任务
     *
     * @param containerCode  容器编码
     * @param sourceLocation 目的库位
     * @return
     */
    @Override
xqs authored
641
    @Transactional(rollbackFor = Exception.class)
mahuandong authored
642
643
644
645
646
647
648
649
650
651
652
653
654
    public AjaxResult mobileCreateReceiptTask(String containerCode, String sourceLocation) {
        LambdaQueryWrapper<ReceiptContainerHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                .eq(ReceiptContainerHeader::getStatus, 0);
        ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(queryWrapper);
        if (receiptContainerHeader == null) {
            throw new ServiceException("该容器没有任务!");
        }
        if (receiptContainerHeader.getWarehouseCode() != ShiroUtils.getWarehouseCode()) {
            throw new ServiceException("容器不在当前仓库!");
        }
        List<Integer> idList = new ArrayList<>();
        idList.add(receiptContainerHeader.getId());
655
        receiptTaskService.createReceiptTask(idList);
mahuandong authored
656
657
658
659

        return AjaxResult.success(receiptContainerHeader);
    }
xqs authored
660
    /**
661
662
     * 自动空托出库
     * */
663
    @Override
xqs authored
664
    @Transactional(rollbackFor = Exception.class)
665
666
667
668
669
670
    public AjaxResult createAutoEmptyOut(String warehouseCode){
        /*先查询容器表,选中空托盘,再查询库存核实*/
        //该容器中有库位信息,并且状态是空
        List<Location> locationsList = locationService.selectContainerEmpty(warehouseCode);
        if(locationsList.size() == 0){
            throw new ServiceException("立库中没有空容器!");
671
        }
672
673
674
675
676
677
678
679
680
681
        //根据入库顺序正序排列取第一个
        Location location = locationsList.get(0);
        //反查库位
        Container containerTmp = new Container();
        containerTmp.setWarehouseCode(location.getWarehouseCode());
        containerTmp.setCode(location.getContainerCode());
        LambdaQueryWrapper<Container> containerLambda = Wrappers.lambdaQuery(containerTmp);
        Container container = containerService.getOne(containerLambda);
        if(container == null){
            throw new ServiceException("选中的库位容器错误,请确认库位和容器!");
682
        }
683
684
685
        //生成空容器出库任务
        inventoryHeaderService.createEmptyOut(container.getCode(), location.getCode());
        return AjaxResult.success("空容器出库任务已下发!",container.getCode());
686
687
688
    }

    /**
游杰 authored
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
     * 自动分配库位
     * @param taskId  任务号
     * @param high 1是高库位,否则低库位
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult setLocationCode(Integer taskId, Integer high)    {
        TaskHeader taskHeader = this.getById(taskId);
        if (taskHeader == null) {
            throw new ServiceException("找不到id为"+ taskId +"的任务!");
        }
        if (StringUtils.isNotEmpty(taskHeader.getToLocation())) {
            return AjaxResult.success("任务已经有目的库位,不需要再分配");
        }
游杰 authored
704
705
706
707
708
709
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getWarehouseCode,ShiroUtils.getWarehouseCode())
                .eq(Location::getContainerCode,"")
                .eq(Location::getStatus,"empty")
                .eq(Location::getDeleted,false);
游杰 authored
710
711
712
713
        List<Location> locations = locationService.list(locationLambdaQueryWrapper);
        if (locations.size() == 0) {
            throw new ServiceException("没有空闲库位!");
游杰 authored
714
        }
715
716
717
        //随机取库位
        Random rand = new Random();
        Location location = locations.get(rand.nextInt(locations.size()));
游杰 authored
718
        if (location == null) {
719
            throw new ServiceException("库位分配错误!");
游杰 authored
720
721
        }
        //锁定库位
722
        locationService.updateStatus(location.getCode(),"lock");
游杰 authored
723
        //给组盘分配库位
游杰 authored
724
游杰 authored
725
726
727
728
729
730
731
732
733
734
735
736
        int internalTaskType = taskHeader.getInternalTaskType();
        if(internalTaskType == QuantityConstant.TASK_TYPE_WHOLERECEIPT.intValue() || internalTaskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT.intValue() ) {
            ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
            receiptContainerHeader.setId(taskHeader.getAllocationHeadId());
            receiptContainerHeader.setToLocation(location.getCode());
            LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderlLambdaQueryWrapper = Wrappers.lambdaQuery(receiptContainerHeader);
            receiptContainerHeaderService.update(receiptContainerHeader, receiptContainerHeaderlLambdaQueryWrapper);
            //给子任务分配库位
            TaskDetail condition = new TaskDetail();
            condition.setWarehouseCode(ShiroUtils.getWarehouseCode());
            condition.setTaskId(taskHeader.getId());
            LambdaQueryWrapper<TaskDetail> taskDetailLambdaQueryWrapper = Wrappers.lambdaQuery(condition);
737
738
739
740
741
742
743
744
            List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLambdaQueryWrapper);
            if(taskDetailList != null && taskDetailList.size() > 0) {
                for(TaskDetail taskDetail : taskDetailList) {
                    if (taskDetail != null) {
                        taskDetail.setToLocation(location.getCode());
                        taskDetailService.update(taskDetail, taskDetailLambdaQueryWrapper);
                    }
                }
游杰 authored
745
746
747
748
749
750
751
            }
        }
        //给任务分配库位
        taskHeader.setToLocation(location.getCode());
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery(taskHeader);
        this.update(taskHeader, taskHeaderLambdaQueryWrapper);
        return AjaxResult.success("生成库位成功",location.getCode());
752
游杰 authored
753
    }
754
755
756
757
    @Override
    public int getUncompleteTaskInNear(Location location) {
        Location location1 = locationService.getNear(location);
758
759
760
        if(location1 == null) {
            return  0;
        }
761
762
763
        String locationCode = location1.getCode();
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getToLocation, locationCode)
764
                                    .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
765
        List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
游杰 authored
766
767
768
769
770
771
772
773
        if(taskHeaderList == null || taskHeaderList.size() == 0) {
            taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getFromLocation, locationCode)
                    .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
            if(taskHeaderList == null || taskHeaderList.size() == 0) {
                return 0;
            }
774
775
776
        }
        return taskHeaderList.size();
    }
游杰 authored
777
778
779
}