Blame view

src/main/java/com/huaheng/pc/task/taskHeader/service/TransferTaskService.java 40.9 KB
1
2
package com.huaheng.pc.task.taskHeader.service;
3
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
xumiao authored
5
import com.google.common.collect.Lists;
周鸿 authored
6
import com.huaheng.api.wcs.service.taskAssignService.TaskAssignService;
xumiao authored
7
8
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationService;
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationServiceImpl;
周鸿 authored
9
import com.huaheng.common.constant.CodeConstans;
周鸿 authored
10
import com.huaheng.common.utils.Wrappers;
11
12
13
14
15
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
16
17
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
18
import com.huaheng.pc.config.container.domain.Container;
19
20
21
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
周鸿 authored
22
23
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
周鸿 authored
39
import java.util.Date;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.List;
import java.util.stream.Collectors;

/**
 * 移库任务
 */
@Service
public class TransferTaskService {

    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private ContainerService containerService;
    @Resource
    private LocationService locationService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;
周鸿 authored
63
64
65
66
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private TaskAssignService taskAssignService;
67
68
    @Resource
    private CompanyService companyService;
xumiao authored
69
70
    @Resource
    private LocationAllocationServiceImpl locationAllocationService;
周鸿 authored
71
72
73
74
75
76
77
78

    /**
     * 创建立库移库任务
     * @param sourceLocationCode 源库位
     * @param desLocationCode 目的库位
     * @return
     */
79
    @Transactional(rollbackFor = Exception.class)
80
81
82
    public AjaxResult createTransferTask(String sourceLocationCode, String desLocationCode, String warehouseCode) {
        Location sourceLocation = locationService.getLocationByCode(sourceLocationCode, warehouseCode);
        Location desLocation = locationService.getLocationByCode(desLocationCode, warehouseCode);
83
        Integer preTaskNo = 0;
84
        if (StringUtils.isNull(sourceLocation)) {
85
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "未找到");
86
        }
游杰 authored
87
        if (!QuantityConstant.STATUS_LOCATION_EMPTY.equals(sourceLocation.getStatus())) {
88
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "状态非空闲");
89
90
        }
        if (StringUtils.isEmpty(sourceLocation.getContainerCode())) {
91
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "不存在托盘");
92
93
94
        }
        //这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
        //校验入库组盘
95
        if (inventoryHeaderService.getUncompleteReceiptContainer(sourceLocationCode, warehouseCode) > 0) {
96
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "存在入库组盘,不能移库");
97
        }
98
        if (inventoryHeaderService.getUncompleteShipmentContainer(sourceLocationCode, warehouseCode) > 0) {
99
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "存在出库组盘,不能移库");
100
101
        }
        if (StringUtils.isNull(desLocation)) {
102
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "未找到");
103
        }
游杰 authored
104
        if (!QuantityConstant.STATUS_LOCATION_EMPTY.equals(desLocation.getStatus())) {
105
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "状态非空闲");
106
107
        }
        if (StringUtils.isNotEmpty(desLocation.getContainerCode())) {
108
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "存在托盘");
109
        }
110
周鸿 authored
111
        if (taskHeaderService.getUncompleteTaskInNear(desLocation) > 0) {
112
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "旁边存在任务,请完成任务以后再分配");
周鸿 authored
113
        }
114
        if(!sourceLocation.getRoadway().equals(desLocation.getRoadway())) {
115
            return AjaxResult.error("移入(目标)库位和移出(源)库位不在同一个巷道");
116
        }
117
xumiao authored
118
        if(sourceLocation.getHigh()>desLocation.getHigh()) {
119
//            return AjaxResult.error("移入(目标)库位和移出(源)库位高度不一样");
游杰 authored
120
121
        }
xumiao authored
122
123
124
//        if(!sourceLocation.getLocationType().equals(desLocation.getLocationType())) {
//            return AjaxResult.error("移入(目标)库位和移出(源)库位库位类型不一样");
//        }
游杰 authored
125
游杰 authored
126
        if(!sourceLocation.getArea().equals(desLocation.getArea())) {
127
            return AjaxResult.error("移入(目标)库位和移出(源)库位不在同一个区域");
游杰 authored
128
129
        }
130
        //创建移库任务前,记录托盘的状态
131
        Container container = containerService.getContainerByCode(sourceLocation.getContainerCode(),warehouseCode);
132
133
134
        container.setLastStatus(container.getStatus());
        containerService.updateById(container);
游杰 authored
135
        if(sourceLocation.getRowFlag() == QuantityConstant.ROW_OUT) {
136
137
138
139
140
            Location location1 = locationService.getNear(sourceLocation);  //内侧库位
            String locationCode = location1.getCode();
            if(StringUtils.isNotEmpty(location1.getContainerCode())) {
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getFromLocation, locationCode)
141
                        .eq(TaskHeader::getWarehouseCode, warehouseCode)
142
143
144
145
146
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
                if(taskHeader != null) {
                    preTaskNo = taskHeader.getId();
                } else {
147
                    return AjaxResult.error("移出(源)库位:" + sourceLocationCode + "旁边库位有托盘无法移库");
148
149
150
151
                }
            } else {
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getToLocation, locationCode)
152
                        .eq(TaskHeader::getWarehouseCode, warehouseCode)
153
154
155
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
                if(taskHeader != null) {
156
                    return AjaxResult.error("移出(源)库位:" + sourceLocationCode + "旁边库位有任务无法移库");
157
158
159
160
                }
            }
        }
161
162
163
        List<Company> warehouseCode1 = companyService.findByWarehouseCode(warehouseCode);
        Company company = warehouseCode1.get(0);
164
        TaskHeader taskHeader = new TaskHeader();
165
        taskHeader.setCompanyCode(company.getCode());
166
        taskHeader.setWarehouseCode(warehouseCode);
167
        taskHeader.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
游杰 authored
168
        taskHeader.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_WORK);
169
        taskHeader.setZoneCode(sourceLocation.getZoneCode());
170
        taskHeader.setContainerCode(sourceLocation.getContainerCode());
171
        taskHeader.setRoadway(sourceLocation.getRoadway());
172
173
174
        taskHeader.setFromLocation(sourceLocationCode);
        taskHeader.setToLocation(desLocationCode);
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_BUILD);
175
        taskHeader.setPreTaskNo(preTaskNo);
176
177
178
179
180
181
182
183
184
185


        LambdaUpdateWrapper<InventoryDetail> detailLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        detailLambdaUpdateWrapper.eq(InventoryDetail::getWarehouseCode, warehouseCode);
        detailLambdaUpdateWrapper.eq(InventoryDetail::getLocationCode, sourceLocationCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(detailLambdaUpdateWrapper);
        if(inventoryDetailList!=null&&inventoryDetailList.size()>0){
            taskHeader.setCompanyCode(inventoryDetailList.get(0).getCompanyCode());
        }
186
        if (!taskHeaderService.save(taskHeader)) {
187
188
            throw new ServiceException("创建任务失败");
        }
189
190
191
192
193
        locationService.updateStatus(sourceLocationCode, QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        locationService.updateStatus(desLocationCode, QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        containerService.updateLocationCodeAndStatus(sourceLocation.getContainerCode(),
                sourceLocationCode, QuantityConstant.STATUS_CONTAINER_LOCK, warehouseCode);
194
195
196
197
198
199
200
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(taskHeader.getId());
            taskDetail.setTaskType(taskHeader.getTaskType());
            taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
            taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());;
201
            taskDetail.setCompanyCode(inventoryDetail.getCompanyCode());
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
            taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            taskDetail.setMaterialName(inventoryDetail.getMaterialName());
            taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
            taskDetail.setQty(inventoryDetail.getQty());
            taskDetail.setFromLocation(inventoryDetail.getLocationCode());
            taskDetail.setToLocation(desLocationCode);
            taskDetail.setContainerCode(inventoryDetail.getContainerCode());
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
            taskDetail.setReferenceCode(inventoryDetail.getReferCode());
            taskDetail.setAttribute1(inventoryDetail.getAttribute1());
            taskDetail.setAttribute2(inventoryDetail.getAttribute2());
            taskDetail.setAttribute3(inventoryDetail.getAttribute3());
            taskDetail.setBatch(inventoryDetail.getBatch());
            taskDetail.setLot(inventoryDetail.getLot());
            taskDetail.setProjectNo(inventoryDetail.getProjectNo());
            taskDetail.setManufactureDate(inventoryDetail.getManufactureDate());
            taskDetail.setExpirationDate(inventoryDetail.getExpirationDate());
            taskDetail.setAgingDate(inventoryDetail.getAgingDate());
            taskDetail.setInventorySts(inventoryDetail.getInventorySts());
            taskDetail.setFromInventoryId(inventoryDetail.getId());
            taskDetail.setToInventoryId(inventoryDetail.getId());
            if (!taskDetailService.save(taskDetail) || !inventoryDetailService.updateById(inventoryDetail)) {
                throw new ServiceException("创建任务失败");
            }
        }
        return AjaxResult.success(taskHeader.getId());
    }

    /**
     * 完成移库任务
     * @param taskHeader
     */
235
    public AjaxResult completeTransferTask(TaskHeader taskHeader) {
236
237
238
239
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetailList = taskDetailService.findByTaskId(taskHeader.getId());
        /* 库存头表id*/
        List<Integer> inventoryHeadIdList = new ArrayList<>();
游杰 authored
240
241
        String warehouseCode = taskHeader.getWarehouseCode();
        InventoryDetail inventoryDetail = null;
242
243
244

        Container container1 = containerService.getContainerByCode(
                taskHeader.getContainerCode(), warehouseCode);
tongzhonghao authored
245
        container1.setLocationCode(taskHeader.getToLocation());
246
247
248
        container1.setMovementCount(container1.getMovementCount() + 1);
        containerService.updateById(container1);
249
250
        for (TaskDetail taskDetail : taskDetailList) {
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
游杰 authored
251
            inventoryDetail = inventoryDetailService.getById(taskDetail.getToInventoryId());
252
253
254
255
256
257
            inventoryHeadIdList.add(inventoryDetail.getInventoryHeaderId());
            InventoryTransaction inventoryTransaction = new InventoryTransaction();
            inventoryTransaction.setWarehouseCode(taskDetail.getWarehouseCode());
            inventoryTransaction.setCompanyCode(taskDetail.getCompanyCode());
            inventoryTransaction.setLocationCode(taskDetail.getToLocation());
            inventoryTransaction.setContainerCode(taskDetail.getContainerCode());
258
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFERINTO);
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
            inventoryTransaction.setMaterialCode(taskDetail.getMaterialCode());
            inventoryTransaction.setMaterialName(taskDetail.getMaterialName());
            inventoryTransaction.setMaterialSpec(taskDetail.getMaterialSpec());
            inventoryTransaction.setMaterialUnit(taskDetail.getMaterialUnit());
            inventoryTransaction.setTaskQty(taskDetail.getQty());
            inventoryTransaction.setInventorySts(taskDetail.getInventorySts());
            inventoryTransaction.setReferCode(taskDetail.getReferenceCode());
            inventoryTransaction.setBatch(taskDetail.getBatch());
            inventoryTransaction.setLot(taskDetail.getLot());
            inventoryTransaction.setProjectNo(taskDetail.getProjectNo());
            inventoryTransaction.setQcCheck(taskDetail.getQcCheck());
            inventoryTransaction.setManufactureDate(taskDetail.getManufactureDate());
            inventoryTransaction.setExpirationDate(taskDetail.getExpirationDate());
            inventoryTransaction.setAgingDate(taskDetail.getAgingDate());
            inventoryTransaction.setAttributeId(String.valueOf(taskDetail.getAttributeId()));
            inventoryTransaction.setAttribute1(taskDetail.getAttribute1());
            inventoryTransaction.setAttribute2(taskDetail.getAttribute2());
            inventoryTransaction.setAttribute3(taskDetail.getAttribute3());
            inventoryTransactionService.save(inventoryTransaction);
278
279
280
            inventoryTransaction.setLocationCode(taskDetail.getFromLocation());
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFEROUT);
            inventoryTransactionService.save(inventoryTransaction);
281
        }
282
283
284
285
        if (taskDetailList.size() != 0 ){
            taskDetailService.saveOrUpdateBatch(taskDetailList);
        }
        if (!taskHeaderService.saveOrUpdate(taskHeader)) {
286
287
            throw new ServiceException("任务单据状态更新失败!");
        }
游杰 authored
288
        if(inventoryDetail != null) {
游杰 authored
289
290
            containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(),
                    taskHeader.getToLocation(), QuantityConstant.STATUS_CONTAINER_SOME, warehouseCode);
游杰 authored
291
        } else {
292
            Container container = containerService.getContainerByCode(taskHeader.getContainerCode(),taskHeader.getWarehouseCode());
xumiao authored
293
            if(StringUtils.isNotEmpty(container.getLastStatus())&&container.getLastStatus().equalsIgnoreCase(QuantityConstant.STATUS_CONTAINER_MANY)) {
294
295
296
297
298
299
300
301
302
303
                //恢复托盘组的状态
                containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(),
                        taskHeader.getToLocation(), QuantityConstant.STATUS_CONTAINER_MANY, warehouseCode);
                container = containerService.getContainerByCode(taskHeader.getContainerCode());
                container.setLastStatus(QuantityConstant.EMPTY_STRING);
                containerService.updateById(container);
            }else {
                containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(),
                        taskHeader.getToLocation(), QuantityConstant.STATUS_CONTAINER_EMPTY, warehouseCode);
            }
游杰 authored
304
        }
游杰 authored
305
306
307
308
        locationService.updateContainerCodeAndStatus(taskHeader.getFromLocation(), "",
                QuantityConstant.STATUS_LOCATION_EMPTY, warehouseCode);
        locationService.updateContainerCodeAndStatus(taskHeader.getToLocation(), taskHeader.getContainerCode(),
                QuantityConstant.STATUS_LOCATION_EMPTY, warehouseCode);
309
310
311
312
313
314
315
        inventoryHeadIdList = inventoryHeadIdList.stream().distinct().collect(Collectors.toList());
        LambdaUpdateWrapper<InventoryHeader> headerUpdateWrapper = Wrappers.lambdaUpdate();
        headerUpdateWrapper.set(InventoryHeader::getLocationCode, taskHeader.getToLocation())
                .in(InventoryHeader::getId, inventoryHeadIdList);
        LambdaUpdateWrapper<InventoryDetail> detailUpdateWrapper = Wrappers.lambdaUpdate();
        detailUpdateWrapper.set(InventoryDetail::getLocationCode, taskHeader.getToLocation())
                .in(InventoryDetail::getInventoryHeaderId, inventoryHeadIdList);
316
317
318
319
320
        if (inventoryHeadIdList.size() != 0) {
            if (!inventoryHeaderService.update(headerUpdateWrapper) ||
                    !inventoryDetailService.update(detailUpdateWrapper)) {
                throw new ServiceException("完成任务失败");
            }
321
        }
322
        return AjaxResult.success("完成移库任务成功");
323
    }
周鸿 authored
324
325
326
327
328
329
330
331

    //判断是否需要移库
    public void doubletransfer(TaskHeader task) {
        if(task.getTaskType() == 300 || task.getTaskType() == 600){
            task.setToLocation(task.getFromLocation());
        }
        //判断任务的库位不为空
xumiao authored
332
333
334
335
        Location location = new Location();
        location.setCode(task.getFromLocation());
        location.setWarehouseCode(task.getWarehouseCode());
        location =locationService.selectFirstEntity(location);
周鸿 authored
336
xumiao authored
337
338
339
        if(location == null){
            throw new ServiceException("任务的库位错误,系统没有此库位");
        }
周鸿 authored
340
xumiao authored
341
342
343
        //判断库位是否为为内部库位,并且外部库位锁定
        if(location.getIRow().intValue() == CodeConstans.INTERNELLOCATION.intValue()){
            Location loca = new Location();
周鸿 authored
344
            loca.setZoneCode("LK");
xumiao authored
345
346
347
348
349
350
351
352
353
354
355
356
357
            loca.setIRow(CodeConstans.EXTERNALLOCATION);
            loca.setIColumn(location.getIColumn());
            loca.setILayer(location.getILayer());
            loca.setIGrid(location.getIGrid());
            loca.setWarehouseCode(location.getWarehouseCode());
            loca = locationService.selectFirstEntity(loca);

            if(loca == null){
                throw new ServiceException("外侧库位在系统不存在");
            }
            if(loca.getStatus().equals("lock")){
                throw new ServiceException("外侧库位已有任务被锁住");
            }
周鸿 authored
358
xumiao authored
359
360
361
362
363
364
365
366
367
368
369
            if(loca.getStatus().equals("lock") && StringUtils.isNotEmpty(loca.getUserDef1()) && loca.getUserDef1().equals("1")){
                throw new ServiceException(task.getContainerCode() + "此托盘外部库位有进库任务,请等待下发");
            }
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getFromLocation, loca.getCode())
                    .eq(TaskHeader::getWarehouseCode, loca.getWarehouseCode());
            taskHeaderLambdaQueryWrapper.last("limit 1");
            TaskHeader task1 = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
            if(task1 !=null && task1.getFirstStatus() == 1){
                throw new ServiceException("先下发库位号为"+loca.getCode()+"的任务");
            }
周鸿 authored
370
371
xumiao authored
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
            //外部库位绑定了容器时,需要移库任务
            if(loca !=null && StringUtils.isNotEmpty(loca.getContainerCode()) && loca.getStatus().equals("empty")){
                //查看库位类型
                LambdaQueryWrapper<LocationType> lambda = Wrappers.lambdaQuery();
                lambda.eq(LocationType::getCode, loca.getLocationType())
                        .eq(LocationType::getWarehouseCode, loca.getWarehouseCode());
                LocationType locationType=locationTypeService.getOne(lambda);
                if(locationType ==null){
                    throw new ServiceException("该库位的库位类型不存在");
                }


                //查找不可移库的空库位
                List<String> locationss=new ArrayList<>();
                //查出外侧有货,内测无货的库位,将其排除
                List<String> locationlist =locationService.selectlistEmpty(loca.getWarehouseCode());
                if(!locationlist.isEmpty()){
                    locationss.addAll(locationlist);
                }
                //查有任务的库位
                List<Location> locationList =new ArrayList<>();
                List<Location> locations = locationService.selectCode(loca.getWarehouseCode());
                if(locations!=null && locations.size()>0) {
                    //筛选内部库位
                    for(Location item :locations){
                        if(item.getIRow().intValue()== CodeConstans.INTERNELLOCATION.intValue()){
                            locationList.add(item);
周鸿 authored
399
400
                        }
                    }
xumiao authored
401
402
403
404
405
406
407
408
409
                    //筛选外部库位
                    if(locationList!=null && locationList.size()>0) {
                        for (Location item : locationList) {
                            Location condition = new Location();
                            condition.setIRow(2);
                            condition.setIColumn(item.getIColumn());
                            condition.setIGrid(item.getIGrid());
                            condition.setILayer(item.getILayer());
                            condition.setZoneCode("LK");
周鸿 authored
410
                            condition.setWarehouseCode(location.getWarehouseCode());
xumiao authored
411
412
                            condition = locationService.selectFirstEntity(condition);
                            locationss.add(condition.getCode());
周鸿 authored
413
                        }
xumiao authored
414
415
                    }
                }
周鸿 authored
416
xumiao authored
417
                Container container =containerService.getContainerByCode(loca.getContainerCode());
xumiao authored
418
419
420
421
422
//                Location loc = locationTypeService.checkTypeV1(Double.valueOf(locationType.getLength()), Double.valueOf(locationType.getWidth()),
//                        Double.valueOf(container.getUserDef1()), null, ShiroUtils.getWarehouseCode(), locationss, container);
                List<String> list = Lists.newArrayList("1");
                LambdaQueryWrapper<LocationType> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(LocationType::getZoneCode, "LK")
周鸿 authored
423
                        .eq(LocationType::getWarehouseCode, location.getWarehouseCode());
xumiao authored
424
425
426
                List<LocationType> locationTypeList = locationTypeService.list(lambdaQueryWrapper);
                List<String> locationTypeCodeList = locationTypeList.stream().
                        map(t -> t.getCode()).collect(Collectors.toList());
427
428
                //立库area为5,移库用托盘高度匹配
                String loc = locationAllocationService.allocation(1, locationTypeCodeList, container.getHeight(), "5",list,location.getWarehouseCode(), loca.getContainerCode(), null);
xumiao authored
429
430
                List<Integer> taskIds = new ArrayList<>();
                if(loc !=null){
xumiao authored
431
                   // this.createTransferTask2(loca.getCode(),task.getCompanyCode());
周鸿 authored
432
                    AjaxResult ajaxResult = createTransferTask(loca.getCode(), loc, location.getWarehouseCode());
xumiao authored
433
434
435
436
                    if(!ajaxResult.hasErr()){
                        String taskId = ajaxResult.getData().toString();
                        taskIds.add(Integer.valueOf(taskId));
                    }else {
437
                        throw new ServiceException("创建移库任务失败:"+ajaxResult.getMsg());
周鸿 authored
438
                    }
xumiao authored
439
440
441
442
443
444
445
446
447
//                    TaskHeader tas = new TaskHeader();
//                    tas.setZoneCode("LK");
//                    tas.setStatus(1);
//                    tas.setFromLocation(loca.getCode());
//                    tas.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
//                    tas = selectFirstEntity(tas);
//                    if(tas !=null){
//                        taskIds.add(tas.getId());
//                    }
xumiao authored
448
449
450
451
452
453
454
455
456
457
458
459
                }else {
                    //移库任务
                    List<String> locatinCodes = new ArrayList<>();
                    locatinCodes.add(loca.getCode());
                    taskIds = this.createOutPutTask(locatinCodes);
                }
                if(!taskIds.isEmpty()){
                    TaskHeader task2 =taskHeaderService.getById(taskIds.get(0));

                    AjaxResult ajaxResult=taskAssignService.wcsTaskAssign(task2);
                    if(ajaxResult.hasErr()){
                        throw new ServiceException(ajaxResult.getMsg());
周鸿 authored
460
                    }
xumiao authored
461
462
                    task2.setStatus(10);
                    taskHeaderService.updateById(task2);
周鸿 authored
463
464
465
                }
            }
        }
xumiao authored
466
周鸿 authored
467
468
469
470
471
472
    }


    public TaskHeader selectFirstEntity(TaskHeader tas){
        LambdaQueryWrapper<TaskHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(StringUtils.isNotEmpty(tas.getContainerCode()),TaskHeader::getContainerCode, tas.getContainerCode())
473
                .eq(StringUtils.isNotNull(tas.getZoneCode()) && StringUtils.isNotEmpty(tas.getZoneCode()),TaskHeader::getZoneCode, tas.getZoneCode())
周鸿 authored
474
                .eq(StringUtils.isNotNull(tas.getStatus()),TaskHeader::getStatus, tas.getStatus())
475
476
477
                .eq(StringUtils.isNotNull(tas.getFromLocation()) && StringUtils.isNotEmpty(tas.getFromLocation()),TaskHeader::getFromLocation, tas.getFromLocation())
                .eq(StringUtils.isNotNull(tas.getTaskType()) ,TaskHeader::getTaskType, tas.getTaskType());
        lambda.last("limit 1");
周鸿 authored
478
479
480
481
482
483
484
        return taskHeaderService.getOne(lambda);
    }
    public List<Integer> createOutPutTask(List<String> locations){
        List<Integer> taskIds  = new ArrayList<>();
        for(String code: locations){
            Location temp = new Location();
            temp.setCode(code);
周鸿 authored
485
            temp.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
            Location loc = locationService.selectFirstEntity(temp);

            if(loc.getStatus().equals("lock")){
                throw new ServiceException("该库位已锁定,有其他任务");
            }

            Container container =containerService.getContainerByCode(loc.getContainerCode());


            if(loc.getZoneCode().equals("LK") || loc.getZoneCode().equals("XC")){
                if(StringUtils.isEmpty(loc.getContainerCode())){
                    throw new ServiceException("该库位没有托盘");
                }

                TaskHeader task =new TaskHeader();
                task.setContainerCode(loc.getContainerCode());
周鸿 authored
502
                task.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
503
504
505
506
507
508
509
                task.setStatus(10);
                task =this.selectFirstEntity(task);
                if(task!=null){
                    throw new ServiceException("该容器已有任务");
                }
                TaskHeader task1 =new TaskHeader();
                task1.setContainerCode(loc.getContainerCode());
周鸿 authored
510
                task.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
                task1.setStatus(1);
                task1 =this.selectFirstEntity(task1);
                if(task1!=null){
                    throw new ServiceException("该容器已有任务");
                }
            }

            if(loc.getLocationType().equals("DM") || loc.getLocationType().equals("XN")){
                continue;
            }
            if(!loc.getStatus().equals("empty")){
                throw new ServiceException(code+"状态非空闲,操作失败");
            }
            if(StringUtils.isEmpty(loc.getContainerCode())){
                throw new ServiceException(code+"没有托盘,操作失败");
            }
            TaskHeader task = new TaskHeader();
            task.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
529
530

            task.setCompanyCode("kshhhanjieweId");
周鸿 authored
531
532
533
            //这里默认一个0
            task.setPriority(0);
            task.setZoneCode(loc.getZoneCode());
周鸿 authored
534
            task.setTaskType(900);
周鸿 authored
535
            task.setPort("P1100");
周鸿 authored
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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
            task.setZoneCode(loc.getZoneCode());
            task.setRoadway(loc.getRoadway());
            task.setContainerCode(loc.getContainerCode());
            task.setStatus(1);
            task.setFromLocation(loc.getCode());
            task.setToLocation(loc.getCode());
            task.setCreated(new Date());
            task.setCreatedBy(ShiroUtils.getLoginName());
            task.setCreated(new Date());
            taskHeaderService.save(task);
            taskIds.add(task.getId());
            //更新库位状态
            loc.setStatus("lock");
            locationService.updateById(loc);
        }
        return taskIds;
    }
    @Transactional
    public AjaxResult createTransferTask2(String sourceLocation, String companyCode) {
        Location temp1 = new Location();
        temp1.setCode(sourceLocation);
        Location location = locationService.selectFirstEntity(temp1);
        if(location==null){
            return AjaxResult.error("源库位:"+sourceLocation+"未找到");
        }
        if(!location.getStatus().equals("empty")){
            return AjaxResult.error("源库位:"+sourceLocation+"状态非空闲");
        }
        if(StringUtils.isEmpty(location.getContainerCode())){
            return AjaxResult.error("源库位:"+sourceLocation+"不存在托盘");
        }
        if(!location.getZoneCode().equals("LK") && !location.getZoneCode().equals("XC")){
            return AjaxResult.error("该库位:"+sourceLocation+"不是立库库位");
        }

        if(location.getZoneCode().equals("LK") || location.getZoneCode().equals("XC")){
            TaskHeader task =new TaskHeader();
            task.setContainerCode(location.getContainerCode());
            task.setStatus(10);
            task =this.selectFirstEntity(task);
            if(task!=null){
                return AjaxResult.error(location.getContainerCode()+"该容器已有任务");
            }
        }
        //这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
        //校验入库组盘
        int count1 = inventoryHeaderService.getUncompleteReceiptContainer(sourceLocation,ShiroUtils.getWarehouseCode());
        if(count1>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在入库组盘,不能移库");
        }
        int count2 = inventoryHeaderService.getUncompleteShipmentContainer(sourceLocation,ShiroUtils.getWarehouseCode());
        if(count2>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在出库组盘,不能移库");
        }

        //判断是否为内测库位
        if(location.getIRow().intValue() == CodeConstans.INTERNELLOCATION.intValue()) {
            Location loca = new Location();
            loca.setIRow(CodeConstans.EXTERNALLOCATION);
            loca.setIColumn(location.getIColumn());
            loca.setILayer(location.getILayer());
            loca.setIGrid(location.getIGrid());
            loca.setWarehouseCode(location.getWarehouseCode());
            loca = locationService.selectFirstEntity(loca);

            if (loca == null) {
                throw new ServiceException("外侧库位在系统不存在");
            }
            //外部库位绑定了容器时,需要移库任务
            if (loca != null && StringUtils.isNotEmpty(loca.getContainerCode()) && loca.getStatus().equals("empty")) {
                //查看库位类型
                LocationType locationTypee = new LocationType();
                locationTypee.setWarehouseCode(loca.getWarehouseCode());
                locationTypee.setCode(loca.getLocationType());
                locationTypee = locationTypeService.getLocationTypeByDoamin(locationTypee);

                if (locationTypee == null) {
                    throw new ServiceException("该库位的库位类型不存在");
                }

                Container containerl = containerService.getContainerByCode(loca.getContainerCode(),loca.getWarehouseCode());

                Location local = locationTypeService.checkType(Double.valueOf(locationTypee.getLength()),Double.valueOf(locationTypee.getWidth()),
                        Double.valueOf(containerl.getUserDef1()),Double.valueOf(locationTypee.getMaxWeight()), ShiroUtils.getWarehouseCode(),loca.getCode(),containerl);


                List<String> locationss=new ArrayList<>();
xumiao authored
623
                List<String> locationlist =locationService.selectlistEmpty(loca.getWarehouseCode());
周鸿 authored
624
625
626
627
628
629
                if(!locationlist.isEmpty()){
                    locationss.addAll(locationlist);
                }

                //如果此库位为外部库位,则查找所有内部库位是否有任务,且其内部库位是否是绑定状态
                List<Location> locationList =new ArrayList<>();
xumiao authored
630
                List<Location> locations = locationService.selectCode(loca.getWarehouseCode());
周鸿 authored
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
                if(locations!=null && locations.size()>0) {

                    //筛选内部库位
                    for(Location item :locations){
                        if(item.getIRow().intValue()== CodeConstans.INTERNELLOCATION.intValue()){
                            locationList.add(item);
                        }
                    }

                    //筛选外部库位
                    if(locationList!=null && locationList.size()>0) {
                        for (Location item : locationList) {
                            Location condition = new Location();
                            condition.setIRow(2);
                            condition.setIColumn(item.getIColumn());
                            condition.setIGrid(item.getIGrid());
                            condition.setILayer(item.getILayer());
                            condition.setZoneCode("LK");
                            condition = locationService.selectFirstEntity(condition);
                            locationss.add(condition.getCode());
                        }
                    }
                }


                if(!locationss.isEmpty()) {
                    local = locationTypeService.checkTypeV1(Double.valueOf(locationTypee.getLength()),Double.valueOf(locationTypee.getWidth()),
                            Double.valueOf(containerl.getUserDef1()),Double.valueOf(locationTypee.getMaxWeight()), ShiroUtils.getWarehouseCode(),locationss,containerl);
                }
                if(local == null){
                    throw new ServiceException("没有空库位");
                }

                TaskHeader task = new TaskHeader();
                task.setWarehouseCode(ShiroUtils.getWarehouseCode());
                task.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
                //这里默认一个0
                task.setPriority(0);
669
                task.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
周鸿 authored
670
671
                //对移库来说,这个没啥用
                task.setPort(null);
672
                task.setRoadway(loca.getRoadway());
周鸿 authored
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
                task.setZoneCode(loca.getZoneCode());
                task.setContainerCode(loca.getContainerCode());
                task.setStatus(1);
                task.setCreated(new Date());
                task.setFromLocation(loca.getCode());
                task.setToLocation(local.getCode());
                task.setCreated(new Date());
                task.setCreatedBy(ShiroUtils.getLoginName());
                taskHeaderService.save(task);
                //更新货位状态为预定
                local.setStatus("lock");
                local.setUserDef1("1");
                loca.setStatus("lock");
                locationService.updateById(loca);
                locationService.updateById(local);
            }
        }


        Container container =containerService.getContainerByCode(location.getContainerCode(),location.getWarehouseCode());

        LocationType locationType =new LocationType();
        locationType.setWarehouseCode(location.getWarehouseCode());
        locationType.setCode(location.getLocationType());
        locationType = locationTypeService.getLocationTypeByDoamin(locationType);
        //查找可移入的库位
        //通过限制条件查找库位类型列表
        Location locat = new Location();
701
702
703
704
705
706
707
        if(container.getContainerType().equals(QuantityConstant.CONTAINER_TYPEC)){
            locat = locationService.getLocationOfRule();
        }else {
            locat = locationTypeService.checkType(Double.valueOf(locationType.getLength()), Double.valueOf(locationType.getWidth()),
                    Double.valueOf(container.getUserDef1()), Double.valueOf(locationType.getMaxWeight()), ShiroUtils.getWarehouseCode(), location.getCode(), container);

            List<String> locationsss=new ArrayList<>();
xumiao authored
708
            List<String> locationlists =locationService.selectlistEmpty(locat.getWarehouseCode());
709
710
711
712
713
            if(!locationlists.isEmpty()){
                locationsss.addAll(locationlists);
            }
            //如果此库位为外部库位,则查找所有内部库位是否有任务,且其内部库位是否是绑定状态
            List<Location> locationList =new ArrayList<>();
xumiao authored
714
            List<Location> locations = locationService.selectCode(locat.getWarehouseCode());
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
            if(locations!=null && locations.size()>0) {

                //筛选内部库位
                for(Location item :locations){
                    if(item.getIRow().intValue()== CodeConstans.INTERNELLOCATION.intValue()){
                        locationList.add(item);
                    }
                }

                //筛选外部库位
                if(locationList!=null && locationList.size()>0) {
                    for (Location item : locationList) {
                        Location condition = new Location();
                        condition.setIRow(2);
                        condition.setIColumn(item.getIColumn());
                        condition.setIGrid(item.getIGrid());
                        condition.setILayer(item.getILayer());
                        condition.setZoneCode("LK");
                        condition = locationService.selectFirstEntity(condition);
                        locationsss.add(condition.getCode());
                    }

                    locat = locationTypeService.checkTypeV1(Double.valueOf(locationType.getLength()),Double.valueOf(locationType.getWidth()),
                            Double.valueOf(container.getUserDef1()),Double.valueOf(locationType.getMaxWeight()), ShiroUtils.getWarehouseCode(),locationsss,container);
                }
            }


            if(!locationsss.isEmpty()) {
                locat = locationTypeService.checkTypeV1(Double.valueOf(locationType.getLength()),Double.valueOf(locationType.getWidth()),
                        Double.valueOf(container.getUserDef1()),Double.valueOf(locationType.getMaxWeight()), ShiroUtils.getWarehouseCode(),locationsss,container);
            }
        }


        if(locat == null){
            throw new ServiceException("没有空库位");
        }

        TaskHeader task = new TaskHeader();
周鸿 authored
755
756
757
758
        task.setWarehouseCode(ShiroUtils.getWarehouseCode());
        task.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
        //这里默认一个0
        task.setPriority(0);
759
        task.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
周鸿 authored
760
        //对移库来说,这个没啥用
761
        task.setPort(null);
762
        task.setRoadway(location.getRoadway());
周鸿 authored
763
764
        task.setZoneCode(location.getZoneCode());
        task.setContainerCode(location.getContainerCode());
765
766
767
        task.setStatus(1);
        task.setFromLocation(sourceLocation);
        task.setToLocation(locat.getCode());
周鸿 authored
768
769
        task.setCreated(new Date());
        task.setCreatedBy(ShiroUtils.getLoginName());
770
        taskHeaderService.save(task);
周鸿 authored
771
772
773
774
        //更新货位状态为预定
        location.setStatus("lock");
        locat.setStatus("lock");
        locat.setUserDef1("0");
775
776
777
778
        locationService.updateById(location);
        locationService.updateById(locat);
        return AjaxResult.success(task);
//        return AjaxResult.success("");
周鸿 authored
779
780
    }
781
}