Blame view

src/main/java/com/huaheng/pc/task/taskHeader/service/TaskHeaderServiceImpl.java 38.8 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;
pengcheng authored
18
19
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
xqs authored
20
21
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
22
import com.huaheng.pc.inventory.cycleCountHeader.service.CycleCountHeaderService;
pengcheng authored
23
24
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
25
26
27
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
28
29
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
mahuandong authored
30
31
32
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
33
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
游杰 authored
34
import com.huaheng.pc.receipt.receiving.service.ReceivingService;
pengcheng authored
35
36
37
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
38
39
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
pengcheng authored
40
41
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
游杰 authored
42
import com.huaheng.pc.task.taskHeader.domain.MobileTask;
mahuandong authored
43
44
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.mapper.TaskHeaderMapper;
pengcheng authored
45
import org.springframework.beans.factory.annotation.Autowired;
mahuandong authored
46
import org.springframework.stereotype.Service;
mahuandong authored
47
48
49
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
pengcheng authored
50
import java.math.BigDecimal;
51
import java.util.*;
xqs authored
52
import java.util.concurrent.CopyOnWriteArrayList;
游杰 authored
53
import java.util.stream.Collectors;
xqs authored
54
mahuandong authored
55
@Service
wangyanxiong authored
56
public class TaskHeaderServiceImpl extends ServiceImpl<TaskHeaderMapper, TaskHeader> implements TaskHeaderService {
mahuandong authored
57
58
    @Resource
pengcheng authored
59
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
60
    @Resource
游杰 authored
61
62
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @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
游杰 authored
75
76
77
78
79
    private InventoryTransactionService inventoryTransactionService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
80
    @Resource
xqs authored
81
    private ReceiptContainerDetailService receiptContainerDetailService;
82
    @Resource
游杰 authored
83
84
85
86
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
xqs authored
87
    private CycleCountDetailService cycleCountDetailService;
mahuandong authored
88
    @Resource
89
90
    private CycleCountHeaderService cycleCountHeaderService;
    @Resource
mahuandong authored
91
    private ReceiptContainerHeaderService receiptContainerHeaderService;
xqs authored
92
    @Resource
93
    private TaskHeaderMapper taskHeaderMapper;
游杰 authored
94
    @Autowired
95
    private TaskAssignService taskAssignService;
mahuandong authored
96
    @Resource
游杰 authored
97
98
99
100
101
102
    private ContainerCapacityService containerCapacityService;
    @Resource
    private ConfigWarningService configWarningService;
    @Resource
    private ReceivingService receivingService;
    @Resource
mahuandong authored
103
    private CreateTaskMessage createTaskMessage;
104
105
    @Resource
    private ReceiptTaskService receiptTaskService;
106
107
    @Resource
    private ShipmentTaskService shipmentTaskService;
108
109
    @Resource
    private WorkTaskService workTaskService;
游杰 authored
110
111
    @Resource
    private TransferTaskService transferTaskService;
mahuandong authored
112
113
114
    /**
     * 盘点任务首选项
115
     *
116
117
118
119
120
     * @param taskHeaderList
     * @return
     */
    @Override
    public List<TaskHeader> preferenceRealize(List<TaskHeader> taskHeaderList) {
121
        //盘点任务头,默认不显示库位,容器。
122
        List<TaskHeader> taskHeaders = new ArrayList<>();
123
        for (TaskHeader item : taskHeaderList) {
124
125
126
127
128
129
130
            item.setFromLocation("");
            item.setToLocation("");
            item.setContainerCode("");
            taskHeaders.add(item);
        }
        return taskHeaders;
    }
xqs authored
131
132
133
134
135
136
    @Override
    public List<TaskHeader> getTasksStatus(Short lastStatus) {
        return taskHeaderMapper.getTasksStatus(lastStatus);
    }
pengcheng authored
137
    /**
mahuandong authored
138
139
140
141
142
143
144
     * 根据库位完成任务
     *
     * @param locationCode
     * @return
     * @throws Exception
     */
    @Override
xqs authored
145
146
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult completeTask(String locationCode) {
mahuandong authored
147
148
149
        LambdaQueryWrapper<TaskHeader> taskQueryWrapper = Wrappers.lambdaQuery();
        taskQueryWrapper.eq(TaskHeader::getToLocation, locationCode);
        List<TaskHeader> list = this.list(taskQueryWrapper);
150
        if (list.isEmpty()) {
mahuandong authored
151
            throw new ServiceException("库位(" + locationCode + ")没有任务!");
152
        } else if(list.get(0).getStatus().equals(QuantityConstant.TASK_STATUS_COMPLETED)) {
mahuandong authored
153
154
155
            //如果已完成则不管
            throw new ServiceException("库位(" + locationCode + ")任务已经完成!");
        }
156
        if(list.get(0).getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF)){
mahuandong authored
157
            //入库任务
158
            return receiptTaskService.completeReceiptTask((list.get(0)));
mahuandong authored
159
160
161
162
163
164
        } else {
            throw new ServiceException("库位(" + locationCode + ")的任务不是上架,不能完成!");
        }
    }

    @Override
xqs authored
165
    @Transactional(rollbackFor = Exception.class)
mahuandong authored
166
    public AjaxResult createReplenishTask(String containerCode, Integer taskStatus) {
mahuandong authored
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
        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());
196
        receiptTaskService.createReceiptTask(ids);
xqs authored
197
        return AjaxResult.success("成功!");
mahuandong authored
198
199
200
201
202
203
204
205
    }

    /**
     * 移动端根据容器编码获取盘点任务
     * @param containerCode 容器编码
     * @return
     */
    @Override
xqs authored
206
    @Transactional(rollbackFor = Exception.class)
mahuandong authored
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
    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;
    }

    /**
xqs authored
235
     * 查询容器有无任务
xqs authored
236
     */
xqs authored
237
238
239
240
    @Override
    public Integer UncompleteCount(String ContainerCode) {
        return taskHeaderMapper.UncompleteCount(ContainerCode, ShiroUtils.getWarehouseCode());
    }
wangyanxiong authored
241
242
243
    /**
     * 取消任务
244
     */
245
    @Override
xqs authored
246
    @Transactional(rollbackFor = Exception.class)
247
    public AjaxResult cancelTask(Integer[] taskIds) {
248
        for (int taskId : taskIds) {
249
            TaskHeader taskHeader = taskHeaderService.getById(taskId);
250
251
            if (taskHeader == null) {
                return AjaxResult.error("任务" + taskId + "未找到,操作中止");
252
            }
253
254
            if (taskHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
                return AjaxResult.error("存在任务" + taskHeader.getId() + "已下发或执行,操作中止");
255
            }
xqs authored
256
257
258
259
260
261
262
263
            //查出任务明细
            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);
264
            if (taskDetail == null) {
xqs authored
265
266
                throw new ServiceException("任务明细条目错误");
            }
267
268
            //删除子任务
            LambdaQueryWrapper<TaskDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
269
            lambdaQueryWrapper.eq(TaskDetail::getTaskId, taskHeader.getId());
270
271
272
273
            taskDetailService.remove(lambdaQueryWrapper);
            //删除主任务
            taskHeaderService.removeById(taskHeader.getId());
            // 更改库位状态(整盘入库任务除外)
mahuandong authored
274
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF)) {
275
                receiptContainerHeaderService.cancelReceipt(taskHeader.getAllocationHeadId());
276
277
278
            }
            //根据任务类型来更新货箱状态
            //修改关联的货箱状态
mahuandong authored
279
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_PICKING)) {
280
281
                ShipmentContainerHeader shipmentContainerHeader = new ShipmentContainerHeader();
                shipmentContainerHeader.setId(taskHeader.getAllocationHeadId());
pengcheng authored
282
                shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
283
284
                shipmentContainerHeaderService.updateById(shipmentContainerHeader);
            }
xqs authored
285
            //盘点取消任务,恢复明细状态为1
xqs authored
286
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_CYCLECOUNT)) {
xqs authored
287
288
289
290
291
292
293
294
295
296

                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);//
297
                for (CycleCountDetail item : cycleCountDetailList) {
xqs authored
298
299
300
301
302
303
304
305
306
                    item.setTaskHeaderId(null);
                    item.setTaskHeaderId(null);
                    item.setLastUpdated(new Date());
                    item.setLastUpdatedBy(ShiroUtils.getLoginName());
                    item.setEnableStatus(1);
                    cycleCountDetailService.saveOrUpdate(item);
                }

            }
307
308
309
310
311
312
313
314
315
316
            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
317
            if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF) || taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_PICKING)) {
318
                if (taskHeader.getToLocation() != null) {
319
320
321
                    //更新托盘、库位状态
                    locationService.updateStatus(taskHeader.getToLocation(), "empty");
                }
游杰 authored
322
323
324
                LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, taskHeader.getContainerCode());
                InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
325
326
                Container container = new Container();
                container.setStatus("empty");
游杰 authored
327
                if(inventoryHeader != null) {
328
                    if("some".equals(inventoryHeader.getContainerStatus())) {
游杰 authored
329
330
331
                        container.setStatus("some");
                    }
                }
332
333
334
                LambdaUpdateWrapper<Container> containerUpdateWrapper = Wrappers.lambdaUpdate();
                containerUpdateWrapper.eq(Container::getCode, taskHeader.getContainerCode());
                containerService.update(container, containerUpdateWrapper);
335
336
337
338
339
340
341
342
343
            }
//            if(task.getType()==900){
//                //出库查看任务没有关联的货箱,不做处理
//            }
        }

        return AjaxResult.success("取消任务成功!");
    }
游杰 authored
344
pengcheng authored
345
    /**
mahuandong authored
346
347
348
349
350
351
     * 容器收货上架
     *
     * @param containerCode
     * @return
     */
    @Override
xqs authored
352
    @Transactional(rollbackFor = Exception.class)
mahuandong authored
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
    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("没有组盘明细,请先组盘");
        }
游杰 authored
371
        if (receiptContainerHeader.getStatus() == QuantityConstant.RECEIPT_HEADER_BUILD.shortValue()) {
mahuandong authored
372
373
374
375
376
377
378
379
            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不能为空");
            }
380
mahuandong authored
381
382
383
384
385
386
            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);
387
            return receiptTaskService.createReceiptTask(idList);
mahuandong authored
388
        }
游杰 authored
389
        return AjaxResult.error("生成入库任务失败");
mahuandong authored
390
391
392
    }

    /**
393
     * 下发WCS执行任务
wangyanxiong authored
394
     * 执行任务
395
     */
wangyanxiong authored
396
    @Override
xqs authored
397
    @Transactional(rollbackFor = Exception.class)
wangyanxiong authored
398
    public AjaxResult<TaskHeader> sendTaskToWcs(Integer[] taskIds) {
wangyanxiong authored
399
400
        TaskHeader task = null;
        for (Integer taskId : taskIds) {
wangyanxiong authored
401
            task = taskHeaderService.getById(taskId);
402
pengcheng authored
403
            if (task.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
wangyanxiong authored
404
405
                return AjaxResult.error("任务" + taskId + "已经下发,请不要重复下发,操作中止");
            }
406
pengcheng authored
407
            // 给wcs传递任务
408
409
            taskAssignService.wcsTaskAssign(task);
pengcheng authored
410
wangyanxiong authored
411
            //修改任务头表
412
            task.setId(taskId);
pengcheng authored
413
            task.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
wangyanxiong authored
414
415
416
            task.setStartPickDateTime(new Date());   //生成时间
            task.setLastUpdated(new Date());
            task.setLastUpdatedBy(ShiroUtils.getLoginName());
417
            taskHeaderService.saveOrUpdate(task);
wangyanxiong authored
418
            //修改任务明细状态
419
420
421
422
            LambdaQueryWrapper<TaskDetail> lambdaWrapper = Wrappers.lambdaQuery();
            lambdaWrapper.eq(TaskDetail::getTaskId, task.getId());
            List<TaskDetail> taskDetailList = taskDetailService.list(lambdaWrapper);
            List<TaskDetail> taskDetails = new ArrayList<>();
423
            for (TaskDetail item : taskDetailList) {
pengcheng authored
424
                item.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
425
426
427
428
429
                item.setLastUpdated(new Date());
                item.setLastUpdatedBy(ShiroUtils.getLoginName());
                item.setProcessStamp("100");
                taskDetails.add(item);
                //盘点执行修改盘点单据状态为10
mahuandong authored
430
                if (task.getTaskType().equals(QuantityConstant.TASK_TYPE_CYCLECOUNT)) {
431
                    cycleCountDetailService.updataDetailStatus(item.getBillDetailId(), QuantityConstant.CYCLECOUNT_STATUS_EXECUTING);
432
                }
433
            }
游杰 authored
434
435
436
437
            if(taskDetails != null && taskDetails.size() > 0) {
                if (!taskDetailService.saveOrUpdateBatch(taskDetails)) {
                    throw new ServiceException("更新任务明细失败");
                }
438
            }
439
游杰 authored
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
//            //修改入库明细
//            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
479
        }
xqs authored
480
        return AjaxResult.success("执行下发任务成功", task);
wangyanxiong authored
481
482
483
    }

    @Override
484
    @Transactional(rollbackFor = Exception.class)
xqs authored
485
    public AjaxResult completeTaskByWMS(Integer[] taskIds, String[] weightConvert) {
mahuandong authored
486
487
        for (int i = 0;i<taskIds.length;i++) {
            TaskHeader task = taskHeaderService.getById(taskIds[i]);
xqs authored
488
wangyanxiong authored
489
            if (task == null) {
mahuandong authored
490
                throw new ServiceException("任务" + taskIds[i] + "未找到,执行中止");
wangyanxiong authored
491
            }
xqs authored
492
493
494
495
496
497
            //任务写入重量
            String weight = "";
            if (StringUtils.isNotNull(weightConvert)) {
                weight = weightConvert[i];
            }
            task.setWeight(weight);
wangyanxiong authored
498
            //如果已完成则不管
mahuandong authored
499
500
501
            if (task.getStatus().equals(QuantityConstant.TASK_STATUS_COMPLETED)) {
                return AjaxResult.success("任务(" + taskIds[i] + ")任务已经是完成的!");
wangyanxiong authored
502
            }
503
504
            //整盘入库,空托入库库位自动分配
            //整盘入库手动完成分配库位,已分配则略过
游杰 authored
505
            if(task.getTaskType().equals(QuantityConstant.TASK_TYPE_WHOLERECEIPT) || task.getTaskType().equals(QuantityConstant.TASK_TYPE_EMPTYRECEIPT)){
506
507
                if(StringUtils.isEmpty(task.getToLocation())){
                    //自动分配库位
508
509
                 //  AjaxResult ajaxResult = this.setLocationCode(task.getId(),0 );
                //   task.setToLocation((String)ajaxResult.getData());
510
511
                }
            }
wangyanxiong authored
512
            //如果没有库位不能完成
513
514
            if (StringUtils.isEmpty(task.getFromLocation()) && StringUtils.isEmpty(task.getToLocation())) {
                throw new ServiceException("任务" + task.getId() + "没有库位,执行中止");
mahuandong authored
515
            }
516
mahuandong authored
517
            this.completeTask(task);
wangyanxiong authored
518
519
520
521
522
523
524
525
526
        }
        return AjaxResult.success("完成任务成功!");
    }

    /**
     * 完成任务
     *
     * @param task
     */
xqs authored
527
    public void completeTask(TaskHeader task) {
wangyanxiong authored
528
        //区分任务类型
mahuandong authored
529
        if (task.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_SHELF) ) {
wangyanxiong authored
530
            //入库任务
531
            receiptTaskService.completeReceiptTask(task);
532
            combineInventory(task);
wangyanxiong authored
533
        }
mahuandong authored
534
535
536
        if (task.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_PICKING) &&
                (task.getTaskType().equals(QuantityConstant.TASK_TYPE_WHOLESHIPMENT) || task.getTaskType().equals(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT))) {
            //出库任务
537
            shipmentTaskService.completeShipmentTask(task);
wangyanxiong authored
538
        }
539
        // 900 出库查看,空托出库查看
mahuandong authored
540
        if (task.getTaskType().equals( QuantityConstant.TASK_TYPE_VIEW)) {
541
            workTaskService.completeCheckOutTask(task);
wangyanxiong authored
542
        }
543
        // 700 盘点
mahuandong authored
544
545
        if (task.getTaskType().equals(QuantityConstant.TASK_TYPE_CYCLECOUNT)) {
            this.completeCycleCountTask(task);
546
        }
mahuandong authored
547
        if (task.getTaskType().equals( QuantityConstant.TASK_TYPE_TRANSFER)) {
xqs authored
548
//          //移库
549
            transferTaskService.completeTransferTask(task);
wangyanxiong authored
550
        }
mahuandong authored
551
        if (task.getTaskType().equals( QuantityConstant.TASK_TYPE_EMPTYRECEIPT)) {
xqs authored
552
            //空托盘入库
553
            workTaskService.completeEmptyIn(task);
wangyanxiong authored
554
        }
mahuandong authored
555
        if (task.getTaskType().equals(QuantityConstant.TASK_TYPE_EMPTYSHIPMENT) ) {
556
            //空托盘出库
557
            workTaskService.completeEmptyOut(task);
wangyanxiong authored
558
559
560
        }
    }
游杰 authored
561
562
563
564
565
566
567
568
569
570
571

    @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
572
                    .eq(InventoryDetail::getLocationCode, task.getToLocation())
游杰 authored
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
                    .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
610
611
612
613
614
615
616
617
    /**
     * 移动端创建入库任务
     *
     * @param containerCode  容器编码
     * @param sourceLocation 目的库位
     * @return
     */
    @Override
xqs authored
618
    @Transactional(rollbackFor = Exception.class)
mahuandong authored
619
620
621
622
623
624
625
626
627
628
629
630
631
    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());
632
        receiptTaskService.createReceiptTask(idList);
mahuandong authored
633
634
635
636

        return AjaxResult.success(receiptContainerHeader);
    }
xqs authored
637
    /**
638
     * 盘点完成
639
     *
640
641
642
643
     * @param taskHeader
     * @return
     */
    @Override
xqs authored
644
    @Transactional(rollbackFor = Exception.class)
645
    public AjaxResult completeCycleCountTask(TaskHeader taskHeader) {
646
647
648
        /*盘点完成,传入任务主单,查出任务明细,通过任务明细查找盘点的明细单,
        完成任务同时,修改盘点细单和主单的状态,完成后库存锁复位*/
        //修改任务主单状态
pengcheng authored
649
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
650
651
652
653
654
655
656
657
658
659
660
661
        taskHeader.setLastUpdatedBy(ShiroUtils.getLoginName()); //更新用户
        taskHeader.setLastUpdated(new Date());   //更新时间
        //task更新明细单状态
        TaskDetail taskDetail = new TaskDetail();
        taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
        taskDetail.setTaskType(taskHeader.getTaskType());
        taskDetail.setTaskId(taskHeader.getId());
        LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery(taskDetail);
        List<TaskDetail> taskDetailList = taskDetailService.list(lambdaQueryWrapper);//查询子单
        List<TaskDetail> list = new CopyOnWriteArrayList<>();
        //修改任务明细状态的同时查找到盘点明细的条目并修改状态,最后修改主单状态
        List<CycleCountDetail> cycleCountDetailList = new CopyOnWriteArrayList<>();
662
        for (TaskDetail item : taskDetailList) {
pengcheng authored
663
            item.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
664
665
666
            item.setLastUpdatedBy(ShiroUtils.getLoginName()); //更新用户
            item.setLastUpdated(new Date());   //更新时间
            list.add(item);
667
668
            CycleCountDetail cycleCountDetail = cycleCountDetailService.getById(item.getBillDetailId());//盘点明细
pengcheng authored
669
            cycleCountDetail.setEnableStatus(QuantityConstant.CYCLECOUNT_STATUS_COMPLETED);
670
671
672
673
674
675
676
677
            cycleCountDetail.setCompletedBy(ShiroUtils.getLoginName());
            cycleCountDetail.setCompletedAt(new Date());
            cycleCountDetailList.add(cycleCountDetail);
            //取消库存盘点锁
            InventoryDetail inventoryDetail = inventoryDetailService.getById(cycleCountDetail.getInventoryDetailId());
            inventoryDetail.setLockCode("");
            inventoryDetailService.saveOrUpdate(inventoryDetail);
        }
678
        if (taskHeaderService.saveOrUpdate(taskHeader) == false ||
679
680
681
682
683
684
685
686
687
                taskDetailService.saveOrUpdateBatch(list) == false ||
                cycleCountDetailService.saveOrUpdateBatch(cycleCountDetailList) == false) {
            throw new ServiceException("盘点任务单据状态更新失败!");
        }
        //更新主单状态
        cycleCountHeaderService.updataHeaderStatus(cycleCountDetailList.get(0).getCycleCountHeadCode());
        //释放库位
        locationService.updateStatus(cycleCountDetailList.get(0).getLocationCode(), "empty");
        return AjaxResult.success("完成盘点任务");
688
689
690
691

    }

    /**
游杰 authored
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
     * 自动分配库位
     * @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
707
708
709
710
711
712
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getWarehouseCode,ShiroUtils.getWarehouseCode())
                .eq(Location::getContainerCode,"")
                .eq(Location::getStatus,"empty")
                .eq(Location::getDeleted,false);
游杰 authored
713
714
715
716
        List<Location> locations = locationService.list(locationLambdaQueryWrapper);
        if (locations.size() == 0) {
            throw new ServiceException("没有空闲库位!");
游杰 authored
717
        }
718
719
720
        //随机取库位
        Random rand = new Random();
        Location location = locations.get(rand.nextInt(locations.size()));
游杰 authored
721
        if (location == null) {
722
            throw new ServiceException("库位分配错误!");
游杰 authored
723
724
        }
        //锁定库位
725
        locationService.updateStatus(location.getCode(),"lock");
游杰 authored
726
        //给组盘分配库位
游杰 authored
727
游杰 authored
728
729
730
731
732
733
734
735
736
737
738
739
        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);
740
741
742
743
744
745
746
747
            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
748
749
750
751
752
753
754
            }
        }
        //给任务分配库位
        taskHeader.setToLocation(location.getCode());
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery(taskHeader);
        this.update(taskHeader, taskHeaderLambdaQueryWrapper);
        return AjaxResult.success("生成库位成功",location.getCode());
755
游杰 authored
756
    }
757
758
759
760
    @Override
    public int getUncompleteTaskInNear(Location location) {
        Location location1 = locationService.getNear(location);
761
762
763
        if(location1 == null) {
            return  0;
        }
764
765
766
        String locationCode = location1.getCode();
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getToLocation, locationCode)
767
                                    .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
768
        List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
游杰 authored
769
770
771
772
773
774
775
776
        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;
            }
777
778
779
        }
        return taskHeaderList.size();
    }
游杰 authored
780
781
782
}