Blame view

src/main/java/com/huaheng/pc/shipment/shipmentContainerHeader/service/ShipmentContainerHeaderServiceImpl.java 49.8 KB
1
2
package com.huaheng.pc.shipment.shipmentContainerHeader.service;
pengcheng authored
3
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
pengcheng authored
5
import com.huaheng.common.constant.QuantityConstant;
pengcheng authored
6
import com.huaheng.common.exception.service.ServiceException;
7
import com.huaheng.common.utils.StringUtils;
8
import com.huaheng.common.utils.Wrappers;
pengcheng authored
9
10
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
11
import com.huaheng.framework.web.domain.RetCode;
12
13
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
14
15
16
17
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
18
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
pengcheng authored
19
20
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
周鸿 authored
21
import com.huaheng.pc.monitor.job.task.RyTaskKS;
22
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
周鸿 authored
23
import com.huaheng.pc.shipment.lockingWorkOrder.service.LockingWorkOrderService;
pengcheng authored
24
25
26
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentCombinationModel;
27
28
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.mapper.ShipmentContainerHeaderMapper;
pengcheng authored
29
30
31
32
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
pengcheng authored
33
import com.huaheng.pc.shipment.shippingCombination.service.ShippingCombinationService;
pengcheng authored
34
35
import com.huaheng.pc.shipment.wave.domain.Wave;
import com.huaheng.pc.shipment.wave.service.WaveService;
周鸿 authored
36
import com.huaheng.pc.system.dict.service.IDictDataService;
37
38
import com.huaheng.pc.system.role.domain.Role;
import com.huaheng.pc.system.role.service.IRoleService;
39
40
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
41
import com.huaheng.pc.task.taskHeader.domain.ShipmentTaskCreateModel;
42
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
43
import com.huaheng.pc.task.taskHeader.service.ShipmentTaskService;
44
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
周鸿 authored
45
46
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
pengcheng authored
47
import org.springframework.beans.factory.annotation.Autowired;
48
import org.springframework.stereotype.Service;
49
50
import org.springframework.transaction.annotation.Transactional;
51
import javax.annotation.Resource;
pengcheng authored
52
import java.math.BigDecimal;
pengcheng authored
53
import java.util.ArrayList;
54
import java.util.Collections;
55
import java.util.List;
pengcheng authored
56
import java.util.Map;
57
import java.util.stream.Collectors;
pengcheng authored
58
59
@Service
60
public class ShipmentContainerHeaderServiceImpl extends ServiceImpl<ShipmentContainerHeaderMapper, ShipmentContainerHeader> implements ShipmentContainerHeaderService {
周鸿 authored
61
    private static final Logger logger = LoggerFactory.getLogger(ShipmentContainerHeaderServiceImpl.class);
62
pengcheng authored
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    @Autowired
    private ShipmentDetailService shipmentDetailService;
    @Autowired
    private InventoryDetailService inventoryDetailService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentContainerHeaderMapper shipmentContainerHeaderMapper;
    @Autowired
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Autowired
    private MaterialService materialService;
pengcheng authored
77
78
    @Autowired
    private ShippingCombinationService shippingCombinationService;
79
80
    @Autowired
    private TaskHeaderService taskHeaderService;
81
82
    @Autowired
    private TaskDetailService taskDetailService;
pengcheng authored
83
84
    @Autowired
    private WaveService waveService;
85
86
    @Autowired
    private ShipmentPreferenceService shipmentPreferenceService;
87
88
    @Autowired
    private ContainerService containerService;
89
90
    @Resource
    private ShipmentTaskService shipmentTaskService;
91
92
    @Autowired
    private ReceiptContainerHeaderService receiptContainerHeaderService;
周鸿 authored
93
94
    @Autowired
    private LockingWorkOrderService lockingWorkOrderService;
95
96
    @Autowired
    private IRoleService roleService;
周鸿 authored
97
98
    @Autowired
    private IDictDataService dictDataService;
pengcheng authored
99
100
101
102
103
104
105
106
107


    @Override
    public Map<String, Integer> getShipmentContainerMaxAndMinStatusByShipmentID(int shipmentId) {
        return shipmentContainerHeaderMapper.getShipmentContainerMaxAndMinStatusByShipmentID(shipmentId);
    }


    /**
108
109
110
111
112
113
114
     * 1.检查基本属性
     * 2.更改库存明细
     * 3.更新单据明细的已出库数量
     * 4.自动判定出库任务状态,根据库存数量减去预定库存相等就是整盘出--预计任务状态
     * 5.增加出库组盘头
     * 6.增加出库组盘明细
     * 7.更新单据状态
115
     * <p>
pengcheng authored
116
     * 出库组盘单条保存
117
     *
pengcheng authored
118
119
120
     * @param shipmentCombinationModel
     * @return
     */
周鸿 authored
121
    @Transactional(rollbackFor = Exception.class)
pengcheng authored
122
    @Override
pengcheng authored
123
    public ShipmentContainerHeader combination(ShipmentCombinationModel shipmentCombinationModel) {
pengcheng authored
124
125
        //1.检查基本属性
pengcheng authored
126
        //校验
127
        if (shipmentCombinationModel.getShipQty().compareTo(new BigDecimal("0")) <= 0) {
pengcheng authored
128
            throw new ServiceException("出库数量必须大于0");
pengcheng authored
129
130
        }
        ShipmentDetail shipmentDetail = shipmentDetailService.getById(shipmentCombinationModel.getShipmentDetailId());
131
        if (shipmentDetail == null) {
pengcheng authored
132
            throw new ServiceException("出库明细未找到");
pengcheng authored
133
134
        }
        InventoryDetail inventoryDetail = inventoryDetailService.getById((shipmentCombinationModel.getInventoryDetailId()));
135
        if (inventoryDetail == null) {
pengcheng authored
136
            throw new ServiceException("库存未找到");
pengcheng authored
137
        }
138
139
        String containerCode = inventoryDetail.getContainerCode();
        int unCompleteCombineNumber = receiptContainerHeaderService.getUnCompleteCombineNumber(containerCode);
140
        if (unCompleteCombineNumber > 0) {
141
142
            throw new ServiceException("托盘:" + containerCode + " 已经用于入库组盘");
        }
143
        Container container = containerService.getContainerByCode(containerCode);
144
145
        if (container != null) {
            if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
xumiao authored
146
                //throw new ServiceException("托盘已经锁定,不能再组盘");
147
148
            }
        }
pengcheng authored
149
        //校验数量是否超出
150
        if ((shipmentDetail.getQty().subtract(shipmentDetail.getTaskQty())).compareTo(shipmentCombinationModel.getShipQty()) < 0) {
pengcheng authored
151
            throw new ServiceException("录入数量超出明细待出数量");
pengcheng authored
152
153
        }
        //校验库存可用数量
154
        if (inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty()).compareTo(shipmentCombinationModel.getShipQty()) < 0) {
pengcheng authored
155
            throw new ServiceException("录入数量超出可出数量");
pengcheng authored
156
        }
157
        if (!shipmentDetail.getMaterialCode().equals(inventoryDetail.getMaterialCode())) {
pengcheng authored
158
            throw new ServiceException("配盘物料不一致");
pengcheng authored
159
160
        }
        //库位
161
162
163
        LambdaQueryWrapper<Location> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Location::getWarehouseCode, inventoryDetail.getWarehouseCode())
                .eq(Location::getCode, inventoryDetail.getLocationCode());
pengcheng authored
164
        Location location = locationService.getOne(lambdaQueryWrapper);
165
166
167
        if (location == null) {
            throw new ServiceException("库位 " + inventoryDetail.getLocationCode() + " 不存在");
        }
168
169
170
171
172
173
174
175
176
        LambdaQueryWrapper<TaskHeader> taskWrapper = Wrappers.lambdaQuery();
        taskWrapper.eq(TaskHeader::getWarehouseCode, inventoryDetail.getWarehouseCode())
                .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_VIEW, QuantityConstant.TASK_TYPE_TRANSFER)
                .apply("(fromLocation='" + inventoryDetail.getLocationCode() + "' or toLocation='" + inventoryDetail.getLocationCode() + "')");
        List<TaskHeader> taskHeaders = taskHeaderService.list(taskWrapper);
        if (taskHeaders.size() > 0) {
            throw new ServiceException("库位 " + inventoryDetail.getLocationCode() + " 已经有移库或出库查看任务,不能组盘");
        }
177
178
179
180
181
182
183
184
185
186
187
        if (container.getZoneCode().equals("LK")) {
            LambdaQueryWrapper<TaskHeader> taskWrapper1 = Wrappers.lambdaQuery();
            taskWrapper1.eq(TaskHeader::getWarehouseCode, inventoryDetail.getWarehouseCode())
                    .eq(TaskHeader::getContainerCode, containerCode)
                    .ge(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_RELEASE)
                    .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            List<TaskHeader> taskHeaders1 = taskHeaderService.list(taskWrapper1);
            if (taskHeaders1.size() > 0) {
                throw new ServiceException("容器 " + containerCode + " 任务已经下发,不能组盘");
            }
        }
188
        //3.更新单据明细的已出库数量
肖超群 authored
189
190
        shipmentDetail.setTaskQty(shipmentDetail.getTaskQty().add(shipmentCombinationModel.getShipQty()));
        int i = shipmentDetail.getQty().compareTo(shipmentDetail.getTaskQty());
191
        if (i > 0) {
pengcheng authored
192
            shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);
193
        } else if (i == 0) {
pengcheng authored
194
            shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
195
        } else if (i < 0) {
pengcheng authored
196
197
            throw new ServiceException("出库数量不能大于单据数量!");
        }
198
周鸿 authored
199
pengcheng authored
200
        //自动判定出库任务状态,根据库存数量减去预定库存相等就是整盘出--预计任务状态
周鸿 authored
201
202
203
204
205
206
207
208
209
210
211
//        BigDecimal inventoryQty = inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty());
//        if (inventoryQty.compareTo(new BigDecimal(0)) <= 0) {
//            shipmentCombinationModel.setTaskType(QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
//        } else {
//            shipmentCombinationModel.setTaskType(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT);
//        }
//        Container container1 = containerService.getContainerByCode(inventoryDetail.getContainerCode());
//        String containerType = container1.getContainerType();
//        if ("S".equals(containerType)) {
//            shipmentCombinationModel.setTaskType(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT);
//        }
212
213
214
        //5.增加出库组盘头
        ShipmentContainerHeader shipmentContainerHeader = ShipmentContainerHeaderAdd(location, shipmentDetail);
        //6.增加出库组盘明细
pengcheng authored
215
        ShipmentContainerDetail shipmentContainerDetail = ShipmentContainerDetailAdd(shipmentDetail, shipmentContainerHeader, shipmentCombinationModel);
216
周鸿 authored
217
218
219
        if (!shipmentDetailService.saveOrUpdate(shipmentDetail)) {
            throw new ServiceException("新增组盘明细失败,sql错误");
        }
220
        //7.更新单据状态
周鸿 authored
221
222
223
224
        AjaxResult ajaxResult = shipmentHeaderService.updateShipmentStatus(shipmentDetail.getShipmentId());
        if (ajaxResult != null && ajaxResult.hasErr()) {
            throw new ServiceException("组盘:" + ajaxResult.getMsg());
        }
周鸿 authored
225
226
//2.更新库存分配数
        inventoryDetail.setTaskQty(inventoryDetail.getTaskQty().add(shipmentCombinationModel.getShipQty()));
pengcheng authored
227
周鸿 authored
228
229
230
        if (!inventoryDetailService.saveOrUpdate(inventoryDetail)) {
            throw new ServiceException("修改库存明细失败,sql错误");
        }
231
        return shipmentContainerHeader;
pengcheng authored
232
233
234
    }

    /**
235
236
237
238
     * 组盘头
     * 1.查看是否有状态小于等于20的组盘头,有就需新建组盘头,没有就新建
     * 2.没有符合条件的组盘头,新建组盘头
     *
pengcheng authored
239
     * @param location
240
     * @param shipmentDetail
pengcheng authored
241
242
243
     * @return
     */
    private ShipmentContainerHeader ShipmentContainerHeaderAdd(Location location,
244
245
                                                               ShipmentDetail shipmentDetail) {
        //1.查看是否有状态小于等于20的组盘头,有就需新建组盘头,没有就新建
246
247
248
249
250
        LambdaQueryWrapper<ShipmentContainerHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentContainerHeader::getLocationCode, location.getCode())
                .eq(ShipmentContainerHeader::getContainerCode, location.getContainerCode())
                .eq(ShipmentContainerHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .lt(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
pengcheng authored
251
        ShipmentContainerHeader shipmentContainerHeader = this.getOne(lambdaQueryWrapper);
252
253
254
        if (shipmentContainerHeader != null) {
            if (shipmentDetail.getWarehouseCode().equals("KS0001") && !ShiroUtils.getUserName().equals(shipmentContainerHeader.getCreatedBy())) {
                throw new ServiceException(shipmentDetail.getMaterialName() + "物料匹配托盘" + location.getContainerCode() + "已被" + shipmentContainerHeader.getCreatedBy() + "组盘,暂时不能组盘");
255
            }
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
            if (shipmentDetail.getWarehouseCode().equals("CS0001") && !ShiroUtils.getUserName().equals(shipmentContainerHeader.getCreatedBy())) {
                if(location.getContainerCode().equals("X00001")){
                    throw new ServiceException(shipmentDetail.getMaterialName() + "物料匹配托盘" + location.getContainerCode() + "已被" + shipmentContainerHeader.getCreatedBy() + "组盘,暂时不能组盘");
                }
            }
            //下发后不能再配盘
            LambdaQueryWrapper<TaskHeader> taskWrapper = Wrappers.lambdaQuery();
            taskWrapper.eq(TaskHeader::getWarehouseCode,ShiroUtils.getWarehouseCode())
                    .eq(TaskHeader::getAllocationHeadId, shipmentContainerHeader.getId())
                    .gt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_BUILD);
            taskWrapper.last("limit 1");
            TaskHeader taskHeader = taskHeaderService.getOne(taskWrapper);
            if(taskHeader != null){
                throw new ServiceException("托盘" + location.getContainerCode() + "已有任务下发,暂时不能组盘");
            }
271
            return shipmentContainerHeader;
272
        } else {
273
274
            // 2.没有符合条件的组盘头,新建组盘头
            //找到容器类型
275
276
277
            LambdaQueryWrapper<Container> containerLam = Wrappers.lambdaQuery();
            containerLam.eq(Container::getCode, location.getContainerCode())
                    .eq(Container::getWarehouseCode, ShiroUtils.getWarehouseCode());
278
            Container container = containerService.getOne(containerLam);
279
            if (container == null) {
280
281
                throw new ServiceException("系统没有此容器编码");
            }
282
283
            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, container.getCode());
284
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
285
            int taskType = QuantityConstant.TASK_TYPE_WHOLESHIPMENT;
286
287
288
            if (inventoryDetailList != null && inventoryDetailList.size() > 0) {
                for (InventoryDetail inventoryDetail : inventoryDetailList) {
                    if (inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty()).
289
290
291
                            compareTo(BigDecimal.ZERO) > 0) {
                        taskType = QuantityConstant.TASK_TYPE_SORTINGSHIPMENT;
                    }
292
                }
pengcheng authored
293
            }
tongzhonghao authored
294
295
            if ("S".equals(container.getContainerType())) {
tongzhonghao authored
296
297
                taskType = QuantityConstant.TASK_TYPE_SORTINGSHIPMENT;
            }
298
            ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentDetail.getShipmentId());
299
300
301
            shipmentContainerHeader = new ShipmentContainerHeader();
            shipmentContainerHeader.setContainerCode(location.getContainerCode());
            shipmentContainerHeader.setLocationCode(location.getCode());
302
            shipmentContainerHeader.setZoneCode(location.getZoneCode());
303
            shipmentContainerHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
周鸿 authored
304
            shipmentContainerHeader.setUWarehouseCode(shipmentHeader.getUWarehouseCode());
305
306
307
308
309
310
311
312
313
314
315
316
317
            shipmentContainerHeader.setCompanyCode(shipmentDetail.getCompanyCode());
            shipmentContainerHeader.setPort(shipmentDetail.getPort());
            shipmentContainerHeader.setContainerType(container.getContainerType());
            shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
            shipmentContainerHeader.setTaskType(taskType);
            shipmentContainerHeader.setTaskCreated(0);
            shipmentContainerHeader.setCreatedBy(ShiroUtils.getLoginName());
            Boolean flag = this.save(shipmentContainerHeader);
            if (flag == false) {
                throw new ServiceException("新建组盘头失败,sql错误");
            }
            return shipmentContainerHeader;
        }
pengcheng authored
318
319
320
    }

    /**
321
322
323
324
325
326
     * 组盘明细
     * 1.查看是否有同一出库明细的物料需要出库
     * 2.有时修改阻盘明细的出库数量就行
     * 3.没有就新建明细
     * 4。查看组盘头状态,如果状态在1020,则生成任务明细或修改任务明细的数量
     *
pengcheng authored
327
328
329
330
331
332
333
334
     * @param shipmentDetail
     * @param shipmentContainerHeader
     * @param shipmentCombinationModel
     * @return
     */
    private ShipmentContainerDetail ShipmentContainerDetailAdd(ShipmentDetail shipmentDetail,
                                                               ShipmentContainerHeader shipmentContainerHeader,
                                                               ShipmentCombinationModel shipmentCombinationModel) {
335
336
        Boolean flag = true;
337
        //1.查看是否有同一出库明细的物料需要出库
338
        LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
339
340
341
342
343
344
        lambdaQueryWrapper.eq(ShipmentContainerDetail::getShippingContainerId, shipmentContainerHeader.getId())
                .eq(ShipmentContainerDetail::getLocationCode, shipmentContainerHeader.getLocationCode())
                .eq(ShipmentContainerDetail::getContainerCode, shipmentContainerHeader.getContainerCode())
                .eq(ShipmentContainerDetail::getShipmentDetailId, shipmentCombinationModel.getShipmentDetailId())
                .eq(ShipmentContainerDetail::getInventoryId, shipmentCombinationModel.getInventoryDetailId())
                .eq(ShipmentContainerDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
pengcheng authored
345
        ShipmentContainerDetail shipmentContainerDetail = shipmentContainerDetailService.getOne(lambdaQueryWrapper);
346
347
        ShipmentContainerDetail shipmentContainerDetaill = new ShipmentContainerDetail();
348
        Container container = containerService.getContainerByCode(shipmentContainerHeader.getContainerCode());
周鸿 authored
349
350
        //2.有时修改阻盘明细的出库数量就行
351
        if (shipmentContainerDetail != null) {
pengcheng authored
352
            shipmentContainerDetail.setQty(shipmentContainerDetail.getQty().add(shipmentCombinationModel.getShipQty()));
353
            shipmentContainerDetaill = shipmentContainerDetail;
周鸿 authored
354
            shipmentContainerDetaill.setZoneCode(container.getZoneCode());
355
356
            flag = shipmentContainerDetailService.saveOrUpdate(shipmentContainerDetail);
            if (flag == false) {
357
358
                throw new ServiceException("修改组盘明细失败,sql错误");
            }
359
        } else {
pengcheng authored
360
361
            //3.没有就新建明细
362
363
364
            LambdaQueryWrapper<Material> lam = Wrappers.lambdaQuery();
            lam.eq(Material::getCode, shipmentDetail.getMaterialCode())
                    .eq(Material::getWarehouseCode, ShiroUtils.getWarehouseCode());
pengcheng authored
365
            Material material = materialService.getOne(lam);
366
367
368
            if (material == null) {
                throw new ServiceException("出库单(" + shipmentDetail.getShipmentCode() + ")的物料(" + shipmentDetail.getMaterialCode() + ")不存在!");
            }
周鸿 authored
369
370
371
            shipmentContainerDetaill.setWarehouseCode(shipmentContainerHeader.getWarehouseCode());
周鸿 authored
372
            shipmentContainerDetaill.setUWarehouseCode(shipmentContainerHeader.getUWarehouseCode());
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
            shipmentContainerDetaill.setCompanyCode(shipmentContainerHeader.getCompanyCode());
            shipmentContainerDetaill.setContainerCode(shipmentContainerHeader.getContainerCode());
            shipmentContainerDetaill.setLocationCode(shipmentContainerHeader.getLocationCode());
            shipmentContainerDetaill.setInventoryId(shipmentCombinationModel.getInventoryDetailId());
            shipmentContainerDetaill.setInventorySts(shipmentDetail.getInventorySts());
            shipmentContainerDetaill.setShippingContainerId(shipmentContainerHeader.getId());
            shipmentContainerDetaill.setShipmentCode(shipmentDetail.getShipmentCode());
            shipmentContainerDetaill.setShipmentId(shipmentDetail.getShipmentId());
            shipmentContainerDetaill.setShipmentDetailId(shipmentDetail.getId());
            shipmentContainerDetaill.setMaterialCode(shipmentDetail.getMaterialCode());
            shipmentContainerDetaill.setMaterialName(shipmentDetail.getMaterialName());
            shipmentContainerDetaill.setMaterialSpec(shipmentDetail.getMaterialSpec());
            shipmentContainerDetaill.setMaterialUnit(shipmentDetail.getMaterialUnit());
            shipmentContainerDetaill.setQty(shipmentCombinationModel.getShipQty());
            shipmentContainerDetaill.setWaveId(shipmentDetail.getWaveId());
            shipmentContainerDetaill.setTaskCreated(0);
pengcheng authored
389
            shipmentContainerDetaill.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
390
391
392
393
            shipmentContainerDetaill.setBatch(shipmentDetail.getBatch());
            shipmentContainerDetaill.setLot(shipmentDetail.getLot());
            shipmentContainerDetaill.setProjectNo(shipmentDetail.getProjectNo());
            shipmentContainerDetaill.setCreatedBy(ShiroUtils.getLoginName());
周鸿 authored
394
            shipmentContainerDetaill.setZoneCode(container.getZoneCode());
pengcheng authored
395
            flag = shipmentContainerDetailService.save(shipmentContainerDetaill);
396
            if (flag == false) {
397
398
                throw new ServiceException("新建组盘明细失败,sql错误");
            }
pengcheng authored
399
        }
400
401
        //4.查看组盘头状态,如果状态在10到30,则生成任务明细或修改任务明细的数量
402
        //查看任务头
403
        int containerHeaderStatus = shipmentContainerHeader.getStatus().intValue();
404
405
406
407
        if (QuantityConstant.SHIPMENT_CONTAINER_TASK <= containerHeaderStatus &&
                containerHeaderStatus < QuantityConstant.SHIPMENT_CONTAINER_FINISHED) {
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getWarehouseCode, shipmentContainerHeader.getWarehouseCode())
游杰 authored
408
                    .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT, QuantityConstant.TASK_TYPE_WHOLESHIPMENT)
409
410
411
                    .eq(TaskHeader::getAllocationHeadId, shipmentContainerHeader.getId());
            TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
            if (taskHeader == null) {
pengcheng authored
412
                throw new ServiceException("有相同的组盘头,但找不到相应的任务头");
413
            }
周鸿 authored
414
415
416
417
418
419
420
421
422
423

            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, shipmentContainerHeader.getContainerCode())
                    .eq(InventoryDetail::getWarehouseCode, shipmentContainerHeader.getWarehouseCode());
            List<InventoryDetail> inventories = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
            BigDecimal inventoryTotal = new BigDecimal("0");
            for (InventoryDetail item : inventories) {
                BigDecimal lastQty = item.getQty().subtract(item.getTaskQty());
                inventoryTotal = inventoryTotal.add(lastQty);
            }
424
425
426
427
428
            InventoryDetail inventoryDetail=inventoryDetailService.getById(shipmentCombinationModel.getInventoryDetailId());
            if(inventoryDetail==null){
                throw new ServiceException("找不到对应的库存明细");
            }
            inventoryTotal = inventoryTotal.subtract(shipmentCombinationModel.getShipQty());
周鸿 authored
429
430
431
432
433
434
435
436
437
438
            String value = dictDataService.getDictValueByLabel(QuantityConstant.RULE_SHIPMENT_TASK, taskHeader.getZoneCode(), ShiroUtils.getWarehouseCode());
            int shipmentTaskRule = Integer.parseInt(value);
//            if (inventoryTotal.compareTo(new BigDecimal(0)) <= 0 && !container.getContainerType().equals(QuantityConstant.AGV_CONTAINER_TYPE)) {
            if (inventoryTotal.compareTo(new BigDecimal(0)) <= 0){
            //整盘出库
                if(shipmentTaskRule == QuantityConstant.RULE_TASK_WHOLE_SHIPMENT) {
                    taskHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
                    taskHeaderService.updateById(taskHeader);
                }
周鸿 authored
439
            }
周鸿 authored
440
            if (shipmentContainerDetaill == null || shipmentContainerDetaill.getId() == null) {
441
442
443
                //shipmentContainerDetaill为空时,说明组盘明细不是新建,so修改任务明细
                //查找任务明细
                LambdaQueryWrapper<TaskDetail> taskDetailLam = Wrappers.lambdaQuery();
444
445
446
                taskDetailLam.eq(TaskDetail::getWarehouseCode, shipmentContainerDetail.getWarehouseCode())
                        .eq(TaskDetail::getAllocationId, shipmentContainerDetail.getId())
                        .eq(TaskDetail::getTaskId, taskHeader.getId())
游杰 authored
447
                        .in(TaskDetail::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT, QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
448
                TaskDetail taskDetail = taskDetailService.getOne(taskDetailLam);
449
                if (taskDetail == null) {
450
451
452
                    throw new ServiceException("找不到对应的任务明细");
                }
                taskDetail.setQty(taskDetail.getQty().add(shipmentCombinationModel.getShipQty()));
453
454
                flag = taskDetailService.saveOrUpdate(taskDetail);
                if (flag == false) {
455
456
                    throw new ServiceException("修改任务明细失败,sql错误");
                }
457
458
459
460
                shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_TASK);
                if (!shipmentContainerDetailService.updateById(shipmentContainerDetail)) {
                    throw new ServiceException("修改组盘明细状态失败");
                }
461
            } else {
462
                //shipmentContainerDetaill不为空时,说明组盘明细是新建,so新建任务明细
周鸿 authored
463
                LambdaQueryWrapper<TaskDetail> taskDetailLam = Wrappers.lambdaQuery();
周鸿 authored
464
465
                taskDetailLam.eq(TaskDetail::getWarehouseCode, shipmentContainerDetaill.getWarehouseCode())
                        .eq(TaskDetail::getAllocationId, shipmentContainerDetaill.getId())
周鸿 authored
466
467
468
469
470
471
472
                        .eq(TaskDetail::getTaskId, taskHeader.getId())
                        .in(TaskDetail::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT, QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
                TaskDetail taskDetail = taskDetailService.getOne(taskDetailLam);
                if (taskDetail != null) {
                } else {
                    taskDetail = new TaskDetail();
                }
473
474
475
476
477
478
479
480
481
482
483
484
485
                taskDetail.setTaskId(taskHeader.getId());
                taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
                taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
                taskDetail.setCompanyCode(taskHeader.getCompanyCode());
                taskDetail.setTaskType(taskHeader.getTaskType());
                taskDetail.setAllocationId(shipmentContainerDetaill.getId());
                taskDetail.setBillCode(shipmentContainerDetaill.getShipmentCode());
                taskDetail.setBillDetailId(shipmentContainerDetaill.getShipmentDetailId());
                taskDetail.setMaterialCode(shipmentContainerDetaill.getMaterialCode());
                taskDetail.setMaterialName(shipmentContainerDetaill.getMaterialName());
                taskDetail.setMaterialSpec(shipmentContainerDetaill.getMaterialSpec());
                taskDetail.setMaterialUnit(shipmentContainerDetaill.getMaterialUnit());
                taskDetail.setFromInventoryId(shipmentContainerDetaill.getInventoryId());
486
487
                taskDetail.setToInventoryId(shipmentContainerDetaill.getInventoryId());
                taskDetail.setInventorySts(shipmentContainerDetaill.getInventorySts());
488
489
490
491
492
493
494
                taskDetail.setQty(shipmentContainerDetaill.getQty());
                taskDetail.setContainerCode(taskHeader.getContainerCode());
                taskDetail.setFromLocation(taskHeader.getFromLocation());
                taskDetail.setToLocation(taskHeader.getToLocation());
                taskDetail.setLot(shipmentContainerDetaill.getLot());
                taskDetail.setWaveId(shipmentContainerDetaill.getWaveId());
                taskDetail.setBatch(shipmentContainerDetaill.getBatch());
周峰 authored
495
                taskDetail.setProjectNo(shipmentContainerDetaill.getProjectNo());
496
                taskDetail.setReferenceCode(shipmentDetail.getReferCode());
497
                taskDetail.setGoodsShelfNo(taskHeader.getGoodsShelfNo());
xqs authored
498
                if (shipmentContainerHeader.getStatus().equals(QuantityConstant.SHIPMENT_CONTAINER_TASK)) {
499
500
//                    taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
                    taskDetail.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
501
                } else {
pengcheng authored
502
                    taskDetail.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
503
504
505
                }
                taskDetail.setCreatedBy(ShiroUtils.getLoginName());
                taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
周鸿 authored
506
                flag = taskDetailService.saveOrUpdate(taskDetail);
507
                if (flag == false) {
508
509
                    throw new ServiceException("新建任务明细失败,sql错误");
                }
510
511
                shipmentContainerDetaill.setStatus(QuantityConstant.SHIPMENT_CONTAINER_TASK);
                if (!shipmentContainerDetailService.updateById(shipmentContainerDetaill)) {
512
513
                    throw new ServiceException("修改组盘明细状态失败");
                }
514
515
            }
        }
pengcheng authored
516
517
518
        return shipmentContainerDetail;
    }
pengcheng authored
519
520
521

    /**
     * 取消组盘的一组明细
522
     *
pengcheng authored
523
524
525
526
     * @param detailIds
     * @return
     */
    @Override
527
    public AjaxResult cancelCombinationDetail(List<Integer> detailIds) {
pengcheng authored
528
        List<ShipmentContainerDetail> shipmentContainerDetails = new ArrayList<>();
529
        for (int detailId : detailIds) {
pengcheng authored
530
531
532
533
534
535
536
537
            ShipmentContainerDetail shipmentContainerDetail = shipmentContainerDetailService.getById(detailId);
            shipmentContainerDetails.add(shipmentContainerDetail);
        }
        return cancelCombinationDetailInner(shipmentContainerDetails);
    }

    /**
     * 取消组盘的一组明细
538
     *
pengcheng authored
539
540
541
542
     * @param shipmentContainerDetails
     * @return
     */
    @Override
周鸿 authored
543
    @Transactional(rollbackFor = Exception.class)
544
545
    public AjaxResult cancelCombinationDetailInner(List<ShipmentContainerDetail> shipmentContainerDetails) {
        for (ShipmentContainerDetail shipmentContainerDetail : shipmentContainerDetails) {
pengcheng authored
546
547
            //获取头
            ShipmentContainerHeader shipmentContainerHeader = this.getById(shipmentContainerDetail.getShippingContainerId());
548
549
            if (shipmentContainerHeader.getStatus() >= QuantityConstant.SHIPMENT_CONTAINER_TASK) {
                return AjaxResult.error("容器" + shipmentContainerHeader.getContainerCode() + "非新建状态,不允许取消明细");
pengcheng authored
550
551
552
            }
            //恢复占用库存
            InventoryDetail inventoryDetail = inventoryDetailService.getById(shipmentContainerDetail.getInventoryId());
553
            if (inventoryDetail != null) {
周鸿 authored
554
555
556
557
                InventoryDetail inventoryDetail1 = new InventoryDetail();
                inventoryDetail1.setId(inventoryDetail.getId());
                inventoryDetail1.setTaskQty(inventoryDetail.getTaskQty().subtract(shipmentContainerDetail.getQty()));
                inventoryDetailService.updateById(inventoryDetail1);
558
                logger.info("扣减库存::" + inventoryDetail.getId() + "容器:" + inventoryDetail.getContainerCode() + "物料code:" + inventoryDetail.getMaterialCode() + "数量:" + inventoryDetail.getTaskQty() + "组盘数量:" + shipmentContainerDetail.getQty());
周鸿 authored
559
            }
pengcheng authored
560
561
            //恢复单据发货数量
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(shipmentContainerDetail.getShipmentDetailId());
xumiao authored
562
            shipmentDetail.setTaskQty(shipmentDetail.getTaskQty().subtract(shipmentContainerDetail.getQty()));
563
            if (shipmentDetail.getTaskQty().compareTo(BigDecimal.ZERO) != 0) {
pengcheng authored
564
                shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);//明细状态恢复,如果删除后还有以出数量就是波次
565
566
            } else {
                shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_POOL);//明细状态
pengcheng authored
567
568
569
570
571
572
573
574
            }
            shipmentDetailService.saveOrUpdate(shipmentDetail);
            //删除这个配盘明细
            shipmentContainerDetailService.removeById(shipmentContainerDetail.getId());

            //查询头表下还有没有明细,如果没有,则删了这个头表
            ShipmentContainerDetail condition = new ShipmentContainerDetail();
            condition.setShippingContainerId(shipmentContainerDetail.getShippingContainerId());
575
576
            List<Map<String, Object>> list = shipmentContainerDetailService.selectListMapByEqual("id", condition);
            if (list == null || list.size() == 0) {
pengcheng authored
577
                this.removeById(shipmentContainerHeader.getId());
游杰 authored
578
579
                locationService.updateStatus(shipmentContainerHeader.getLocationCode(),
                        QuantityConstant.STATUS_LOCATION_EMPTY);
pengcheng authored
580
581
582
583
584
585
586
            }
            //更新单据状态
            shipmentHeaderService.updateShipmentStatus(shipmentDetail.getShipmentId());
        }
        return AjaxResult.success("");
    }
587
588
    /**
     * 取消组盘
589
     *
590
591
592
593
     * @param shipmentContainerIds
     * @return
     */
    @Override
594
    public AjaxResult cancelCombination(List<Integer> shipmentContainerIds) {
595
596

        AjaxResult result = checkForCancelCombination(shipmentContainerIds);
597
        if (result.getCode() != RetCode.SUCCESS.getValue()) {
598
599
            return result;
        }
600
601
602
        for (int id : shipmentContainerIds) {
            LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ShipmentContainerDetail::getShippingContainerId, id);
603
604
            List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(lambdaQueryWrapper);
            result = cancelCombinationDetailInner(shipmentContainerDetails);
605
            if (result.hasErr()) {
606
607
608
609
610
611
                return result;
            }
        }

        return AjaxResult.success("");
    }
pengcheng authored
612
613

    /**
614
615
     * 出库自动组盘
     *
pengcheng authored
616
617
618
619
     * @param shipmentCode
     * @return
     */
    @Override
周鸿 authored
620
    @Transactional(rollbackFor = Exception.class)
pengcheng authored
621
    public AjaxResult autoCombination(String shipmentCode) {
622
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLam = Wrappers.lambdaQuery();
623
624
        shipmentHeaderLam.eq(ShipmentHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(ShipmentHeader::getCode, shipmentCode);
625
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentHeaderLam);
626
        if (shipmentHeader == null) {
627
628
            throw new ServiceException("系统没有此单据");
        }
629
        shipmentPreferenceService.checkShipmentProcess(shipmentHeader.getId().toString(), 100, shipmentCode);
630
pengcheng authored
631
632
633
        LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentCode)
                .eq(ShipmentDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
pengcheng authored
634
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(lambdaQueryWrapper);
635
        if (shipmentDetailList.isEmpty()) {
周鸿 authored
636
            throw new ServiceException("找不到子单链");
pengcheng authored
637
        }
638
        for (ShipmentDetail shipmentDetail : shipmentDetailList) {
639
           /* if(shipmentDetail.getWaveId() != 0){
640
                return AjaxResult.error(shipmentCode+"单号已加入波次"+shipmentDetail.getWaveId()+",无法自动组盘");
641
            }*/
642
        }
pengcheng authored
643
644
645
646
647
        return this.autoCombination(shipmentDetailList);
    }


    //出库自动组盘
xqs authored
648
    @Override
649
    public AjaxResult autoCombination(List<ShipmentDetail> shipmentDetailList) {
pengcheng authored
650
        int num = 0;
651
        List<Integer> list = new ArrayList<>();
652
653
        lable1:
        for (ShipmentDetail item : shipmentDetailList) {
pengcheng authored
654
            //获取需要出库数量
肖超群 authored
655
            BigDecimal shipmentQty = item.getQty().subtract(item.getTaskQty());
656
657
658
659
            String userDef1 = item.getUserDef1();
            if (StringUtils.isNotEmpty(userDef1)) {
                shipmentQty = new BigDecimal(userDef1);
            }
pengcheng authored
660
661
            //判断是否还有需要出库的物料,如果没有就跳过该物料
            if (shipmentQty.compareTo(BigDecimal.ZERO) <= 0) {
662
                continue lable1;
pengcheng authored
663
            }
xumiao authored
664
            List<InventoryDetail> inventoryList = null;
665
            if (item.getWarehouseCode().equals("CS0001") || item.getWarehouseCode().equals("XZ0001")) {
666
                inventoryList = shippingCombinationService.getInventorys(item);
xumiao authored
667
            }
668
            if (item.getWarehouseCode().equals("KS0001")) {
xumiao authored
669
670
                inventoryList = shippingCombinationService.getInventoryAutoKS(item);
            }
671
            if (inventoryList == null) {
672
//                throw new ServiceException("没有可用库存");
673
                continue lable1;
xumiao authored
674
            }
675
            inventoryList = combCondit(inventoryList);
676
            if (inventoryList.isEmpty()) {
677
                continue lable1;
678
//                throw new ServiceException("没有可用库存");
679
            }
lector authored
680
            //去除已锁的库存
681
            ArrayList<InventoryDetail> removeInventoryList = new ArrayList<>();
682
683
684
//            List<TaskHeader> taskHeaderList = taskHeaderService.getUnCompleteTaskList();
//            List<ReceiptContainerHeader> receiptContainerHeaderList =
//                    receiptContainerHeaderService.getUnCompleteCombineList();
lector authored
685
686
            boolean lock = false;
            for (InventoryDetail inventoryDetail : inventoryList) {
周鸿 authored
687
688
689
690
691
692
693
694
695
696
697
698
                // 校验锁定数量之前,先查询锁定工作令数据是否存在
//                LockingWorkOrder lockingWorkOrder = lockingWorkOrderService.selectLockingWorkOrder(item.getMoCode(), item.getMaterialCode(), item.getId().toString());
//                if (lockingWorkOrder == null){
//                    //校验库存是否有被锁定的数量
//                    LockingWorkOrder lockingWorkOrder1=new LockingWorkOrder();
//                    lockingWorkOrder1.setInventoryId(inventoryDetail.getId().toString());
//                    List<LockingWorkOrder> lockingWorkOrderList = lockingWorkOrderService.getListByDoamin(lockingWorkOrder1);
//                    BigDecimal qty = inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty());
//                    if (lockingWorkOrderService.inventoryCheck(lockingWorkOrderList, qty, shipmentQty)){
//                        continue;
//                    }
//                }
xumiao authored
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//                LambdaQueryWrapper<Container> containerLambdaQueryWrapper  = Wrappers.lambdaQuery();
//                containerLambdaQueryWrapper.eq(Container::getCode, inventoryDetail.getContainerCode());
//                Container container = containerService.getOne(containerLambdaQueryWrapper);
//                if (QuantityConstant.STATUS_CONTAINER_LOCK.equals(container.getStatus())) {
//                    removeInventoryList.add(inventoryDetail);
//                    lock = true;
//                }
//                if(taskHeaderList != null) {
//                    for(TaskHeader taskHeader : taskHeaderList) {
//                        if(taskHeader.getContainerCode().equals(container.getCode())) {
//                            removeInventoryList.add(inventoryDetail);
//                        }
//                    }
//                }
//                if(receiptContainerHeaderList != null) {
//                    for(ReceiptContainerHeader receiptContainerHeader : receiptContainerHeaderList) {
//                        if(receiptContainerHeader.getContainerCode().equals(container.getCode())) {
//                            removeInventoryList.add(inventoryDetail);
//                        }
//                    }
//                }
周鸿 authored
720
721
722
723
                // 判断锁定工作令数据是否需要修改
//                if (lockingWorkOrder != null){
//                    lockingWorkOrderService.checkLockingWorkOrder(lockingWorkOrder, shipmentQty);
//                }
lector authored
724
            }
725
            inventoryList.removeAll(removeInventoryList);
726
            if (inventoryList.size() < 1 && lock) {
727
//                return AjaxResult.error("'"+item.getMaterialName()+"' 所在托盘全部处于锁定状态,请完成或取消任务后再进行组盘");
728
                continue lable1;
lector authored
729
730
            }
731
            if (inventoryList.size() < 1 && item.getWaveId() != 0) {
pengcheng authored
732
                Wave wave = waveService.getById(item.getWaveId());
pengcheng authored
733
734
735
                wave.setStatus(QuantityConstant.WAVE_STATUS_ERROR);
                wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_ERROR);
                wave.setLastWaveStep(QuantityConstant.WAVE_STEP_BUILD);
pengcheng authored
736
                waveService.updateById(wave);
737
                throw new ServiceException("主单为" + item.getShipmentCode() + "子单id为" + item.getId() + "的单据没有库存,波次失败");
pengcheng authored
738
            }
pengcheng authored
739
740
741
            if (inventoryList.size() < 1) {
                num = num + 1;
            } else {
742
                //遍历扣减库存,知道扣完为止
743
744
                lable2:
                for (InventoryDetail inventory : inventoryList) {
pengcheng authored
745
746
                    //判断是否组盘完毕,完毕就退出内循环
                    if (shipmentQty.compareTo(BigDecimal.ZERO) <= 0) {
747
                        break lable2;
pengcheng authored
748
749
                    }
750
pengcheng authored
751
752
753
754
                    BigDecimal inventoryQty = inventory.getQty().subtract(inventory.getTaskQty());
                    ShipmentCombinationModel shipmentCombination = new ShipmentCombinationModel();
                    shipmentCombination.setInventoryDetailId(inventory.getId());
                    shipmentCombination.setShipmentDetailId(item.getId());
游杰 authored
755
                    shipmentCombination.setType((short) QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
pengcheng authored
756
757
758
759
                    if (inventoryQty.compareTo(BigDecimal.ZERO) <= 0) {
                        continue lable2;
                    }
pengcheng authored
760
761
762
763
                    //如果库存数 >= 待出库数,组盘数就是待出库数并且结束组盘,否则组盘数就是库存数
                    if (inventoryQty.compareTo(shipmentQty) > -1) {
                        shipmentCombination.setShipQty(shipmentQty);
                    } else {
xumiao authored
764
                        shipmentCombination.setShipQty(inventoryQty);
pengcheng authored
765
                    }
766
767
                    try {
                        ShipmentContainerHeader shipmentContainerHeader = this.combination(shipmentCombination);
768
769
                        list.add(shipmentContainerHeader.getId());
                        shipmentQty = shipmentQty.subtract(shipmentCombination.getShipQty());
770
                    } catch (Exception e) {
771
772
                        break lable2;
                    }
773
pengcheng authored
774
775
776
                }
            }
        }
777
778
        if (num == shipmentDetailList.size()) {
易文鹏 authored
779
780
            //throw new ServiceException("单据物料的库存分配规则在此库区没有库存,无法出库");
            throw new ServiceException("此库区没有库存了,无法出库");
pengcheng authored
781
        }
782
        return AjaxResult.success("成功", list);
pengcheng authored
783
784
    }
785
786
    /**
     * 检查是否可以进行取消配盘
787
     *
788
789
790
791
792
     * @param shipmentContainerIds
     * @return
     */
    @Override
    public AjaxResult checkForCancelCombination(List<Integer> shipmentContainerIds) {
793
        for (int id : shipmentContainerIds) {
794
            ShipmentContainerHeader shipmentContainerHeader = this.getById(id);
795
            if (shipmentContainerHeader == null) {
796
797
                return AjaxResult.error("找不到组盘信息");
            }
798
            if (shipmentContainerHeader.getStatus() >= QuantityConstant.SHIPMENT_CONTAINER_TASK) {
799
800
801
802
803
804
805
806
807
                return AjaxResult.error("存在已生成任务的记录,请先取消任务再取消组盘");
            }
        }
        return AjaxResult.success("");
    }

    /* 生成任务 */
    @Override
    @Transactional
808
    public AjaxResult createTask(List<Integer> idList) {
809
        ShipmentTaskCreateModel shipmentTask = new ShipmentTaskCreateModel();
810
        for (Integer id : idList) {
811
            shipmentTask.setShipmentContainerHeaderIds(id);
812
            AjaxResult ajaxResult = shipmentTaskService.createTaskFromShipmentContainers(shipmentTask);
813
            if (ajaxResult.hasErr()) {
pengcheng authored
814
815
                return ajaxResult;
            }
816
817
818
819
        }
        return AjaxResult.success("全部生成完毕");
    }
lector authored
820
821
822
823
    /**
     * 取消出库任务
     */
    @Override
周鸿 authored
824
    @Transactional(rollbackFor = Exception.class)
lector authored
825
826
827
828
829
    public boolean cancelShipment(Integer combineHeaderId) {
        // 回滚组盘主表状态
        ShipmentContainerHeader shipmentContainerHeader = new ShipmentContainerHeader();
        shipmentContainerHeader.setId(combineHeaderId);
        shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
830
        if (!updateById(shipmentContainerHeader)) {
lector authored
831
832
833
834
            throw new ServiceException("更新出库组盘头表失败");
        }
        // 回滚组盘明细状态
        LambdaQueryWrapper<ShipmentContainerDetail> queryWrapper = Wrappers.lambdaQuery();
835
        queryWrapper.eq(ShipmentContainerDetail::getShippingContainerId, combineHeaderId);
lector authored
836
837
838
839
        List<ShipmentContainerDetail> shipmentContainerDetailList = shipmentContainerDetailService.list(queryWrapper);
        for (ShipmentContainerDetail shipmentContainerDetail : shipmentContainerDetailList) {
            shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        }
840
        if (!shipmentContainerDetailService.updateBatchById(shipmentContainerDetailList)) {
lector authored
841
842
843
844
845
            throw new ServiceException("更新出库组盘明细失败");
        }
        return true;
    }
846
847
848
849
    @Override
    public AjaxResult getShipmentInfoByCode(String code) {
        LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentContainerDetail::getShipmentCode, code)
850
                .le(ShiroUtils.getWarehouseCode().equals("KS0001"), ShipmentContainerDetail::getStatus, QuantityConstant.SHIPMENT_CONTAINER_TASK)
851
852
                .eq(ShipmentContainerDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .orderByAsc(ShipmentContainerDetail::getStatus);
853
854
855
856
857

        List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(lambdaQueryWrapper);
        return AjaxResult.success(shipmentContainerDetails);
    }
858
859
860
861
862
863
864
    @Override
    public int getUnCompleteCombineNumber(String containerCode) {
        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getContainerCode, containerCode)
                .lt(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        int number = 0;
        List<ShipmentContainerHeader> shipmentContainerHeaderList = list(shipmentContainerHeaderLambdaQueryWrapper);
865
        if (shipmentContainerHeaderList != null && shipmentContainerHeaderList.size() > 0) {
866
867
868
869
870
871
872
873
874
875
876
877
            number = shipmentContainerHeaderList.size();
        }
        return number;
    }

    @Override
    public List<ShipmentContainerHeader> getUnCompleteCombineList() {
        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.lt(ShipmentContainerHeader::getStatus,
                QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        List<ShipmentContainerHeader> shipmentContainerHeaderList =
                list(shipmentContainerHeaderLambdaQueryWrapper);
878
        return shipmentContainerHeaderList;
879
880
    }
881
    private List<InventoryDetail> combCondit(List<InventoryDetail> inventoryDetails) {
882
883
884
885
        List<Role> roles = roleService.selectRolesByUserId(ShiroUtils.getUserId(), ShiroUtils.getWarehouseCode());
        List<Integer> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
        int role = QuantityConstant.ROLE_XC;
        int roleNum = 0;
886
        if (roleIds.contains(role)) {
887
            roleNum = 1;
888
        } else {
889
890
891
892
893
            roleNum = 2;
        }
        String XC = QuantityConstant.ZONECODE_XC;
        List<InventoryDetail> returnList = Collections.EMPTY_LIST;
        for (InventoryDetail inventoryDetail : inventoryDetails) {
894
895
896
            if (roleNum == 1 && !XC.equals(inventoryDetail.getZoneCode())) {
                return returnList;
            } else if (roleNum == 2 && XC.equals(inventoryDetail.getZoneCode())) {
897
898
899
900
901
902
                return returnList;
            }
        }
        return inventoryDetails;
    }
903
}