Blame view

src/main/java/com/huaheng/api/general/service/ShipmentApiService.java 32.7 KB
pengcheng authored
1
2
package com.huaheng.api.general.service;
易文鹏 authored
3
4
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
pengcheng authored
5
6
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
pengcheng authored
7
import com.huaheng.api.general.domain.ShipmentDomain;
易文鹏 authored
8
import com.huaheng.common.constant.HttpConstant;
lector authored
9
import com.huaheng.common.constant.QuantityConstant;
pengcheng authored
10
11
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
易文鹏 authored
12
import com.huaheng.common.utils.restful.RestUtil;
pengcheng authored
13
import com.huaheng.common.utils.security.ShiroUtils;
pengcheng authored
14
import com.huaheng.framework.web.domain.AjaxResult;
易文鹏 authored
15
import com.huaheng.pc.config.address.service.AddressService;
pengcheng authored
16
import com.huaheng.pc.config.company.service.CompanyService;
17
import com.huaheng.pc.config.container.domain.Container;
易文鹏 authored
18
import com.huaheng.pc.config.container.service.ContainerService;
pengcheng authored
19
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
易文鹏 authored
20
import com.huaheng.pc.config.location.service.LocationService;
pengcheng authored
21
22
23
24
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.shipmentType.domain.ShipmentType;
import com.huaheng.pc.config.shipmentType.service.ShipmentTypeService;
易文鹏 authored
25
26
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
pengcheng authored
27
import com.huaheng.pc.config.warehouse.service.WarehouseService;
28
29
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
30
31
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
易文鹏 authored
32
import com.huaheng.pc.monitor.apilog.service.IApiLogService;
33
import com.huaheng.pc.rgv.service.RgvService;
易文鹏 authored
34
35
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
pengcheng authored
36
37
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
lector authored
38
39
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentDetailHistory.service.ShipmentDetailHistoryService;
pengcheng authored
40
41
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
lector authored
42
43
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
易文鹏 authored
44
import com.huaheng.pc.task.taskHeader.domain.ShipmentTaskCreateModel;
易文鹏 authored
45
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
易文鹏 authored
46
import com.huaheng.pc.task.taskHeader.service.ShipmentTaskService;
易文鹏 authored
47
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
易文鹏 authored
48
import org.springframework.beans.BeanUtils;
pengcheng authored
49
import org.springframework.beans.factory.annotation.Autowired;
易文鹏 authored
50
import org.springframework.http.ResponseEntity;
pengcheng authored
51
52
53
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
lector authored
54
import javax.annotation.Resource;
pengcheng authored
55
import java.math.BigDecimal;
56
import java.util.*;
57
import java.util.stream.Collectors;
pengcheng authored
58
59
60

/**
 * @author ricard
易文鹏 authored
61
 * @time 19/11/11
pengcheng authored
62
63
64
 */
pengcheng authored
65
66
67
68
@Component
@Transactional
public class ShipmentApiService {
易文鹏 authored
69
70
71
72
73
74
75
76
    @Resource
    private StationService stationService;
    @Resource
    private AddressService addressService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private ContainerService containerService;
pengcheng authored
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    @Autowired
    private ShipmentHeaderService shipmentHeaderService;

    @Autowired
    private ShipmentDetailService shipmentDetailService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CustomerServiceImpl customerService;

    @Autowired
    private ShipmentTypeService shipmentTypeService;

    @Autowired
    private MaterialService materialService;
lector authored
98
99
100
101
102
    @Resource
    private ShipmentHeaderHistoryService shipmentHeaderHistoryService;

    @Resource
    private ShipmentDetailHistoryService shipmentDetailHistoryService;
易文鹏 authored
103
104
105
106
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    ShipmentTaskService shipmentTaskService;
lector authored
107
108
109
    @Resource
    private InventoryDetailService inventoryDetailService;
易文鹏 authored
110
111
    @Resource
    private TaskHeaderService taskHeaderService;
112
113
114
    @Resource
    private InventoryTransactionService inventoryTransactionService;
易文鹏 authored
115
116
117
118
119
120
121
122
    @Resource
    private ShipmentApiService shipmentApiService;

    @Resource
    private IApiLogService apiLogService;

    @Resource
    private RgvService rgvService;
123
pengcheng authored
124
    /**
易文鹏 authored
125
     * 出库单下发
pengcheng authored
126
127
128
129
130
     *
     * @param shipmentDomain
     * @return
     */
    @Transactional
易文鹏 authored
131
    public AjaxResult shipment(ShipmentDomain shipmentDomain) {
pengcheng authored
132
pengcheng authored
133
134
135
        //1、判断出库主单和子单列是否为空
        ShipmentHeader shipmentHeader = shipmentDomain.getShipmentHeader();
        List<ShipmentDetail> shipmentDetails = shipmentDomain.getShipmentDetails();
易文鹏 authored
136
        if (shipmentHeader == null) {
pengcheng authored
137
138
            return AjaxResult.error("出库主单为空");
        }
139
        if (shipmentDetails == null || shipmentDetails.size() < 1) {
pengcheng authored
140
141
142
143
            return AjaxResult.error("出库子单为空");
        }

        //2、检查出库主单的合法性
144
145
146
147
        AjaxResult ajaxResult = checkShipmentHeader(shipmentHeader);
        if (ajaxResult.hasErr()) {
            return AjaxResult.error(ajaxResult.getMsg());
        }
易文鹏 authored
148
        //3、检查出库明细的合法性
149
150
151
152
        AjaxResult result = checkShipmentDetail(shipmentDetails);
        if (result.hasErr()) {
            return AjaxResult.error(result.getMsg());
        }
pengcheng authored
153
154
155

        //4、保存出库主表
        BigDecimal totalQty = new BigDecimal(0);
易文鹏 authored
156
157
        for (ShipmentDetail item : shipmentDetails) {
            totalQty = totalQty.add(item.getQty());
pengcheng authored
158
        }
159
pengcheng authored
160
161
        shipmentHeader.setTotalLines(shipmentDetails.size());
        shipmentHeader.setTotalQty(totalQty);
162
        shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
163
164
165
        if (StringUtils.isNotEmpty(shipmentHeader.getShipmentNote())) {
            shipmentHeader.setShipmentNote(shipmentHeader.getShipmentNote());
        }
166
167
        if (StringUtils.isNotEmpty(shipmentHeader.getLine())) {
            shipmentHeader.setLine(shipmentHeader.getLine());
168
        }
易文鹏 authored
169
170
171
172
        String stack = shipmentHeader.getStack();
        List<String> stackList = Arrays.asList(stack.split(","));
        if (StringUtils.isEmpty(stack)) {
            return AjaxResult.error("出库栈口不能为空");
173
        }
易文鹏 authored
174
175
176
177
178
179
180
181
        //匹配出没有执行任务的栈口
        List<TaskHeader> taskHeaderList = taskHeaderService.list(new LambdaQueryWrapper<TaskHeader>()
                .isNotNull(TaskHeader::getPort)
                .ne(TaskHeader::getStatus, 100));
        List<String> taskPorts = taskHeaderList.stream().map(TaskHeader::getPort).distinct().collect(Collectors.toList());
        List<Station> stationList = stationService.list(new LambdaQueryWrapper<Station>()
                .notIn(StringUtils.isNotEmpty(taskPorts), Station::getCode, taskPorts).in(Station::getCode, stackList));
        List<String> ports = stationList.stream().map(Station::getCode).collect(Collectors.toList());
易文鹏 authored
182
        if (ports.isEmpty()) {
易文鹏 authored
183
184
            return AjaxResult.error("没有空闲出库口");
        }
185
        String port = ports.get(0);
186
187
188
189
190
191
192
193
194
195
196
197
        //玻布仓分配栈口
        if (StringUtils.isNotEmpty(shipmentHeader.getLineNumber())) {
            //匹配出库口
            port = matchPort(ports, shipmentHeader.getLineNumber());
            if (StringUtils.isBlank(port)) {
                //如果有多个栈口,排除已经执行过的栈口
                AjaxResult result1 = rgvService.excludeStack(ports, port, "shipment");
                if (result1.hasErr()) {
                    return AjaxResult.error(result1.getMsg());
                }
                port = result1.getMsg();
            }
198
易文鹏 authored
199
        }
易文鹏 authored
200
        shipmentHeader.setStack(port);
201
202
203
204
205
206
        shipmentHeader.setWarehouseCode("CS0001");
        shipmentHeader.setCompanyCode("BHF");
        //String shipmentCode = shipmentHeaderService.createCode(shipmentHeader.getShipmentType());
        shipmentHeader.setCode(shipmentHeader.getReferCode());
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);//新建
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);//新建
pengcheng authored
207
易文鹏 authored
208
        if (!shipmentHeaderService.save(shipmentHeader)) {
pengcheng authored
209
210
211
            throw new ServiceException("保存出库主表失败");
        }
易文鹏 authored
212
pengcheng authored
213
        //5、保存出库子表
易文鹏 authored
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        addDetail(shipmentHeader, shipmentDetails, port);


        //自动组盘
        AjaxResult result2 = shipmentContainerHeaderService.autoCombination(shipmentHeader.getCode());
        if (result2.hasErr()) {
            throw new ServiceException("MES下发出库组盘时失败:" + result2.getMsg());
        }

        //生成任务
        List<ShipmentContainerHeader> shipmentContainerHeaders = shipmentContainerHeaderService.list(new LambdaQueryWrapper<ShipmentContainerHeader>()
                .eq(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_BUILD));

        for (ShipmentContainerHeader item : shipmentContainerHeaders) {
            ShipmentTaskCreateModel shipmentTask = new ShipmentTaskCreateModel();
            shipmentTask.setShipmentContainerHeaderIds(item.getId());
            AjaxResult ajaxResults = shipmentTaskService.createTaskFromShipmentContainers(shipmentTask);
            if (ajaxResults.hasErr()) {
                throw new ServiceException("任务生成失败");
            }
        }

        //点位不等于空,调用MES的RGV接口
        if (StringUtils.isNotEmpty(shipmentHeader.getLine())) {
238
            AjaxResult result3 = rgvService.sendRgv(shipmentHeader.getLine(), port, "RGV出库,发送点位", "shipment", "", shipmentHeader.getCode());
易文鹏 authored
239
240
241
242
243
            if (result3.hasErr()) {
                throw new ServiceException("发送RGV接口失败:" + result3.getMsg());
            }
        }
244
245
246
247
        //出库单明细的出库数量必须等于预出库数量
        List<ShipmentDetail> list = shipmentDetailService.list(new LambdaQueryWrapper<ShipmentDetail>().eq(ShipmentDetail::getShipmentCode, shipmentHeader.getCode()));
        for (ShipmentDetail shipmentDetail : list) {
            if (!shipmentDetail.getQty().equals(shipmentDetail.getTaskQty())) {
248
                throw new ServiceException(shipmentDetail.getMaterialCode() + "," + shipmentDetail.getMaterialName() + ",库存不够,任务要出:" + shipmentDetail.getQty());
249
250
251
            }
        }
易文鹏 authored
252
253
254
255
256
257
258
        /* 6.step回传出库单和出库明细单 */
//        ShipmentDomain shipment = new ShipmentDomain();
//        shipment.setShipmentHeader(shipmentHeader);
//        shipment.setShipmentDetails(shipmentDetailList);
        return AjaxResult.success();
    }
259
260

    //匹配出库口
261
262
263
264
265
266
267
268
    private String matchPort(List<String> ports, String lineNumber) {
        String port = ports.get(0);
        if (ports.contains("P1014") && ports.contains("P1011")) {
            if (ports.size() > 1 && StringUtils.isNotEmpty(lineNumber)) {
                if (lineNumber.equals("SJ01") || lineNumber.equals("SJ02") || lineNumber.equals("SJ03")) {
                    port = "P1014";
                } else if (lineNumber.equals("SJ04") || lineNumber.equals("SJ05")) {
                    port = "P1011";
269
270
                } else {
                    port = "";
271
272
273
274
275
276
                }
            }
        }
        return port;
    }
易文鹏 authored
277
    private void addDetail(ShipmentHeader shipmentHeader, List<ShipmentDetail> shipmentDetails, String port) {
易文鹏 authored
278
        int count = 0;
279
        shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getCode, shipmentHeader.getCode()));
280
        //List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
易文鹏 authored
281
        for (ShipmentDetail shipmentDetail : shipmentDetails) {
pengcheng authored
282
283
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
284
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
易文鹏 authored
285
            shipmentDetail.setPort(port);
286
287
288
289
            if (StringUtils.isNotEmpty((shipmentDetail.getBatch()))) {
                shipmentDetail.setBatch(shipmentDetail.getBatch());
            }
            if (StringUtils.isNotEmpty((shipmentDetail.getLabelCode()))) {
290
                shipmentDetail.setLabelCode(shipmentDetail.getLabelCode());
pengcheng authored
291
            }
292
293
294
            if (StringUtils.isNotEmpty((shipmentDetail.getCustomerCode()))) {
                shipmentDetail.setCustomerCode(shipmentDetail.getCustomerCode());
            }
295
296
297
298
299
300
            if (StringUtils.isNotEmpty((shipmentDetail.getPo()))) {
                shipmentDetail.setPo(shipmentDetail.getPo());
            }
            if (shipmentDetail.getProductionDate() != null) {
                shipmentDetail.setProductionDate(shipmentDetail.getProductionDate());
            }
301
302
            shipmentDetail.setDoubleSided(shipmentDetail.getDoubleSided());
            shipmentDetail.setManage(shipmentDetail.getManage());
303
304
            shipmentDetail.setWarehouseCode("CS0001");
            shipmentDetail.setCompanyCode("BHF");
易文鹏 authored
305
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode, shipmentDetail.getMaterialCode()));
306
307
308
309
310
311
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
            shipmentDetail.setMaterialCode(material.getCode());
            if (!shipmentDetailService.save(shipmentDetail)) {
                throw new ServiceException("保存入库明细失败");
pengcheng authored
312
            }
313
            //shipmentDetailList.add(shipmentDetail);
易文鹏 authored
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335


            //双面,分配库存的标签给他们两个,指定出库
            if (shipmentDetail.getDoubleSided() == 1) {
                count++;
                if (count > 1) {
                    throw new ServiceException("双面只能有一个");
                }
                List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>()
                        .eq(InventoryDetail::getMaterialCode, shipmentDetail.getMaterialCode()));
                shipmentDetail.setLabelCode(inventoryDetailList.get(0).getLabelCode());
                shipmentDetailService.updateById(shipmentDetail);

                ShipmentDetail newShipmentDetail = new ShipmentDetail();
                BeanUtils.copyProperties(shipmentDetail, newShipmentDetail, "id");
                newShipmentDetail.setQty(new BigDecimal(1));
                newShipmentDetail.setIsNew("双面出库新增出库明细");
                newShipmentDetail.setLabelCode(inventoryDetailList.get(1).getLabelCode());
                if (!shipmentDetailService.save(newShipmentDetail)) {
                    throw new ServiceException("保存双面入库明细失败");
                }
            }
pengcheng authored
336
337
338
339
340
341
        }
    }


    /**
     * 检查出库主单的合法性
易文鹏 authored
342
     *
pengcheng authored
343
344
345
     * @param shipmentHeader
     * @return
     */
易文鹏 authored
346
347
    public AjaxResult checkShipmentHeader(ShipmentHeader shipmentHeader) {
        if (StringUtils.isEmpty(shipmentHeader.getReferCode()) || StringUtils.isEmpty(shipmentHeader.getShipmentType())) {
348
            return AjaxResult.error("出库单号或出库单类型为空");
pengcheng authored
349
350
351
        }

        //2、判断出库类型
352
        if (shipmentTypeService.getOne(new LambdaQueryWrapper<ShipmentType>().eq(ShipmentType::getCode, shipmentHeader.getShipmentType())) == null) {
pengcheng authored
353
354
355
356
            return AjaxResult.error("wms没有此出库类型");
        }

        //3、检查上游单号是否存在
易文鹏 authored
357
        if (shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getReferCode, shipmentHeader.getReferCode())) != null) {
358
            return AjaxResult.error("该单据已存在:" + shipmentHeader.getReferCode());
pengcheng authored
359
360
361
362
363
364
365
        }
        return AjaxResult.success(shipmentHeader);
    }


    /**
     * 检查出库子单的合法性
易文鹏 authored
366
     *
pengcheng authored
367
368
369
     * @param shipmentDetails
     * @return
     */
易文鹏 authored
370
371
    public AjaxResult checkShipmentDetail(List<ShipmentDetail> shipmentDetails) {
        for (ShipmentDetail shipmentDetail : shipmentDetails) {
易文鹏 authored
372
            //判断必填字段是否为空且总数量不能为0
易文鹏 authored
373
374
            if (shipmentDetail.getMaterialCode() == null || new BigDecimal(0).compareTo(shipmentDetail.getQty() != null ?
                    shipmentDetail.getQty() : new BigDecimal(0)) == 0) {
375
376
377
378
                return AjaxResult.error("出库明细字段有误,MaterialCode或qty");
            }
            //检查物料
            String materialCode = shipmentDetail.getMaterialCode();
易文鹏 authored
379
            if (StringUtils.isEmpty(materialCode)) {
易文鹏 authored
380
                return AjaxResult.error("物料为空");
pengcheng authored
381
            }
易文鹏 authored
382
            if (materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode, materialCode)) == null) {
易文鹏 authored
383
                return AjaxResult.error("WMS不存在此物料:", materialCode);
pengcheng authored
384
            }
385
易文鹏 authored
386
            //如果带标签号,判断标签号相同的库存是否足够
易文鹏 authored
387
            if (StringUtils.isNotEmpty(shipmentDetail.getLabelCode())) {
易文鹏 authored
388
389
390
                AjaxResult result1 = judgingLabelInventory(materialCode, shipmentDetail);
                if (result1.hasErr()) {
                    return AjaxResult.error(result1.getMsg());
易文鹏 authored
391
392
393
                }
            }
易文鹏 authored
394
395
396
397
            //判断库存是否足够
            AjaxResult result2 = judgingInventory(materialCode, shipmentDetail);
            if (result2.hasErr()) {
                return AjaxResult.error(result2.getMsg());
398
            }
易文鹏 authored
399
        }
400
易文鹏 authored
401
402
403
404
405
406
407
        //物料号相同的出库单合并qty
        List<ShipmentDetail> newShipmentDetailList = mergeShipmentDetails(shipmentDetails);
        for (ShipmentDetail shipmentDetail : newShipmentDetailList) {
            //判断库存是否足够
            AjaxResult result2 = judgingInventory(shipmentDetail.getMaterialCode(), shipmentDetail);
            if (result2.hasErr()) {
                return AjaxResult.error(result2.getMsg());
易文鹏 authored
408
            }
易文鹏 authored
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
        }

        return AjaxResult.success();
    }


    //物料号相同的出库单合并qty
    public static List<ShipmentDetail> mergeShipmentDetails(List<ShipmentDetail> shipmentDetails) {
        Map<String, BigDecimal> materialCodeQtyMap = new HashMap<>();
        List<ShipmentDetail> mergedShipmentDetails = new ArrayList<>();

        // 统计相同MaterialCode的qty总和
        for (ShipmentDetail shipmentDetail : shipmentDetails) {
            String materialCode = shipmentDetail.getMaterialCode();
            BigDecimal qty = shipmentDetail.getQty();

            BigDecimal oldQty = materialCodeQtyMap.get(materialCode);
            if (oldQty != null) {
                qty = qty.add(oldQty);
易文鹏 authored
428
            }
易文鹏 authored
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
            materialCodeQtyMap.put(materialCode, qty);
        }

        // 合并ShipmentDetail并添加到结果集中
        for (Map.Entry<String, BigDecimal> entry : materialCodeQtyMap.entrySet()) {
            ShipmentDetail mergedShipmentDetail = new ShipmentDetail();
            mergedShipmentDetail.setMaterialCode(entry.getKey());
            mergedShipmentDetail.setQty(entry.getValue());
            mergedShipmentDetails.add(mergedShipmentDetail);
        }

        return mergedShipmentDetails;
    }

    //判断标签库存
    private AjaxResult judgingLabelInventory(String materialCode, ShipmentDetail shipmentDetail) {
        List<InventoryDetail> labelCodeinventoryDetailList = inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>()
446
                .eq(InventoryDetail::getLabelCode, shipmentDetail.getLabelCode()));
易文鹏 authored
447
448
449
450
451
452
453
454
455
456
457
        if (labelCodeinventoryDetailList.isEmpty()) {
            return AjaxResult.error("WMS库存中没有此标签条码:", shipmentDetail.getLabelCode());
        }
        //排除掉有任务的库存
        List<InventoryDetail> newInventoryDetailList = excludeLock(labelCodeinventoryDetailList);

        for (InventoryDetail inventoryDetail : newInventoryDetailList) {
            BigDecimal inventoryQty = inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty());
            if (inventoryQty.compareTo(shipmentDetail.getQty()) < 0) {
                return AjaxResult.error("该标签库存不够" + shipmentDetail.getLabelCode() + ":需求为:" + shipmentDetail.getQty()
                        + ",库存剩余" + inventoryQty
易文鹏 authored
458
459
                        + ",物料名称:" + shipmentDetail.getMaterialName()
                        + ",物料编码:" + shipmentDetail.getMaterialCode());
460
            }
pengcheng authored
461
        }
易文鹏 authored
462
463
464
465
466
467
468
469
470
471
472
473
474
        return AjaxResult.success();
    }


    //判断库存
    public AjaxResult judgingInventory(String materialCode, ShipmentDetail shipmentDetail) {
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>()
                .eq(InventoryDetail::getMaterialCode, materialCode));
        if (inventoryDetailList.isEmpty()) {
            return AjaxResult.error("WMS库存中没有此物料库存:", materialCode);
        }
        //排除掉有任务的库存
        List<InventoryDetail> newInventoryDetailList2 = excludeLock(inventoryDetailList);
475
易文鹏 authored
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
        //判断双面库存
        if (shipmentDetail.getDoubleSided() == 1) {
            if (newInventoryDetailList2.size() < 2) {
                return AjaxResult.error("双面库存不足:", materialCode);
            }
        }
        //和出库单明细物料相同的库存
        BigDecimal totalQty = new BigDecimal(0);
        for (InventoryDetail inventoryDetail : newInventoryDetailList2) {
            BigDecimal inventoryQty = inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty());
            totalQty = totalQty.add(inventoryQty);
        }
        if (totalQty.compareTo(shipmentDetail.getQty()) < 0) {
            return AjaxResult.error("该物料库存不足或该托盘已有任务:需求为:" + shipmentDetail.getQty()
                    + ",库存剩余" + totalQty
                    + ",物料名称:" + shipmentDetail.getMaterialName()
                    + ",物料编码:" + shipmentDetail.getMaterialCode());
        }
易文鹏 authored
494
        return AjaxResult.success();
pengcheng authored
495
    }
lector authored
496
易文鹏 authored
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
    private List<InventoryDetail> exclude(List<InventoryDetail> inventoryDetailList) {
        List<InventoryDetail> removeList = new ArrayList<>();
        ArrayList<String> lockContainerCode = new ArrayList<>();

        List<String> containerCodes = inventoryDetailList.stream().map(InventoryDetail::getContainerCode).distinct().collect(Collectors.toList());
        for (String containerCode : containerCodes) {
            Container container = containerService.getOne(new LambdaQueryWrapper<Container>()
                    .eq(Container::getStatus, QuantityConstant.STATUS_LOCATION_LOCK)
                    .eq(Container::getCode, containerCode));
            if (container != null) {
                lockContainerCode.add(containerCode);
            }
        }
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            for (String str : lockContainerCode) {
                if (inventoryDetail.getContainerCode().contains(str)) {
                    removeList.add(inventoryDetail);
                }
            }
        }
        inventoryDetailList.removeAll(removeList);
        return inventoryDetailList;
    }


    private List<InventoryDetail> excludeLock(List<InventoryDetail> inventoryDetailList) {
        List<String> lockContainerCodes = containerService.list(new LambdaQueryWrapper<Container>()
                        .eq(Container::getStatus, QuantityConstant.STATUS_LOCATION_LOCK))
                .stream().map(Container::getCode).collect(Collectors.toList());
        List<InventoryDetail> remainingList = new ArrayList<>();
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            if (!lockContainerCodes.contains(inventoryDetail.getContainerCode())) {
                remainingList.add(inventoryDetail);
            }
        }
        return remainingList;
    }
lector authored
536
537
538
539
540
    @Transactional
    public AjaxResult remove(List<String> shipmentCodeList) {
        LambdaQueryWrapper<ShipmentHeader> headerQueryWrapper;
        for (String shipmentCode : shipmentCodeList) {
            headerQueryWrapper = Wrappers.lambdaQuery();
易文鹏 authored
541
            headerQueryWrapper.eq(ShipmentHeader::getCode, shipmentCode);
lector authored
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerQueryWrapper);
            if (shipmentHeader == null) {
                return AjaxResult.success("");
            }
            if ((shipmentHeader.getFirstStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN && shipmentHeader.getLastStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN) ||
                    (shipmentHeader.getFirstStatus() < QuantityConstant.SHIPMENT_HEADER_POOL & shipmentHeader.getLastStatus() < QuantityConstant.SHIPMENT_HEADER_POOL)) {
                ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
                List<ShipmentDetailHistory> shipmentDetailHistoryList = new ArrayList<>();
                //查询入库单明细
                LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentCode);
                List<ShipmentDetail> list = shipmentDetailService.list(lambdaQueryWrapper);

                //复制到入库历史实体
                com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentHeaderHistory, shipmentHeader);
                for (ShipmentDetail shipmentDetail : list) {
                    ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                    com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentDetailHistory, shipmentDetail);
                    shipmentDetailHistoryList.add(shipmentDetailHistory);
                }
564
                shipmentHeaderHistory.setLastUpdatedBy(ShiroUtils.getName());
lector authored
565
566
567
568
569
570
571
572
573
574
575
                if (!shipmentHeaderService.removeById(shipmentHeader.getId())) {
                    throw new ServiceException("删除头表失败");
                }
                if (!shipmentHeaderHistoryService.save(shipmentHeaderHistory)) {
                    throw new ServiceException("新增历史出库单失败");
                }
                // 当存在明细时删除
                if (list.size() != 0) {
                    //删除入库明细
                    List<Integer> shipmentDetailIds = new ArrayList<>();
                    for (int i = 0; i < shipmentDetailHistoryList.size(); i++) {
576
                        shipmentDetailHistoryList.get(i).setLastUpdatedBy(ShiroUtils.getName());
lector authored
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
                        shipmentDetailHistoryList.get(i).setShipmentId(shipmentHeaderHistory.getId());
                        shipmentDetailIds.add(shipmentDetailHistoryList.get(i).getId());
                    }
                    if (!shipmentDetailService.removeByIds(shipmentDetailIds)) {
                        throw new ServiceException("删除明细表失败");
                    }
                    if (!shipmentDetailHistoryService.saveBatch(shipmentDetailHistoryList)) {
                        throw new ServiceException("新增明细失败");
                    }
                }
            } else {
                return AjaxResult.success("出库单没有完成,无法删除");
            }
        }
        return AjaxResult.success("删除成功");
    }
易文鹏 authored
594
595
    public AjaxResult search(String shipmentCode, String companyCode, String warehouseCode) {
        if (companyCode == null) {
lector authored
596
597
            return AjaxResult.error("货主编码不能为空");
        }
易文鹏 authored
598
        if (warehouseCode == null) {
lector authored
599
600
601
602
603
            return AjaxResult.error("仓库编码不能为空");
        }
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        LambdaQueryWrapper<ShipmentHeader> headerLambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<ShipmentDetail> detailLambdaQuery = Wrappers.lambdaQuery();
易文鹏 authored
604
605
606
607
608
609
        headerLambdaQuery.eq(ShipmentHeader::getWarehouseCode, warehouseCode)
                .eq(ShipmentHeader::getCompanyCode, companyCode)
                .eq(ShipmentHeader::getCode, shipmentCode);
        detailLambdaQuery.eq(ShipmentDetail::getWarehouseCode, warehouseCode)
                .eq(ShipmentDetail::getCompanyCode, companyCode)
                .eq(ShipmentDetail::getShipmentCode, shipmentCode);
lector authored
610
611
612
613
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerLambdaQuery);
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQuery);
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetailList);
易文鹏 authored
614
        return AjaxResult.success("查询成功", shipmentDomain);
lector authored
615
    }
易文鹏 authored
616
617
618
619
620


    public AjaxResult shipmentBack(ShipmentHeader shipmentHeader) {
        String warehouseCode = "CS0001";
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_MES_SHIPMENT);
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
        //List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(new LambdaQueryWrapper<ShipmentDetail>().eq(ShipmentDetail::getShipmentId, shipmentHeader.getId()));
        List<InventoryTransaction> inventoryTransactionList = inventoryTransactionService.list(new LambdaQueryWrapper<InventoryTransaction>()
                .eq(InventoryTransaction::getTransactionType, 20)
                .eq(InventoryTransaction::getBillCode, shipmentHeader.getCode()));

        //处理库存交易
        List<InventoryTransaction> inventoryTransactions = processInventoryTransactionList(inventoryTransactionList);
        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
        for (InventoryTransaction it : inventoryTransactions) {
            ShipmentDetail sd = new ShipmentDetail();
            sd.setId(it.getBillDetailId());
            sd.setBatch(it.getBatch());
            sd.setDoubleSided(it.getDoubleSided());
            sd.setLabelCode(it.getLabelCode());
            sd.setManage(it.getManage());
            sd.setMaterialName(it.getMaterialName());
            sd.setMaterialUnit(it.getMaterialUnit());
            sd.setMaterialCode(it.getMaterialCode());
            sd.setMaterialSpec(it.getMaterialSpec());
            sd.setQty(it.getTaskQty());
            sd.setShipmentCode(shipmentHeader.getCode());
            sd.setShipmentId(shipmentHeader.getId());
643
            sd.setContainerCode(it.getContainerCode());
644
645
646
            shipmentDetailList.add(sd);
        }
        //替换出库明细部分值
易文鹏 authored
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetailList);
        String jsonParam = JSON.toJSONString(shipmentDomain);
        ResponseEntity<JSONObject> result = RestUtil.request_post(url, warehouseCode, jsonParam, "出库回传MES");
        if (result != null && result.getBody() != null) {
            String code = result.getBody().getString("RequestStatus");
            String msg = result.getBody().getString("Msg");
            if (!HttpConstant.isMesSuccess(Integer.parseInt(code))) {
                return AjaxResult.error(msg);
            }
        } else {
            throw new ServiceException("接口地址错误或返回为空");
        }

        return AjaxResult.success();
    }
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
    public List<InventoryTransaction> processInventoryTransactionList(List<InventoryTransaction> inventoryTransactionList) {
        // 新建一个HashMap用于存储labelCode相同的库存交易对象
        Map<String, InventoryTransaction> itMap = new HashMap<>();

        // 循环遍历 库存交易List
        for (InventoryTransaction inventoryTransaction : inventoryTransactionList) {
            // 如果taskQty为负数,则改为正数
            if (inventoryTransaction.getTaskQty().compareTo(BigDecimal.ZERO) < 0) {
                inventoryTransaction.setTaskQty(inventoryTransaction.getTaskQty().abs());
            }

            // 如果itMap中已存在标签条码相同的,库存交易对象,则将taskQty相加
            if (itMap.containsKey(inventoryTransaction.getLabelCode())) {
                InventoryTransaction existingInventoryTransaction = itMap.get(inventoryTransaction.getLabelCode());
                existingInventoryTransaction.setTaskQty(existingInventoryTransaction.getTaskQty().add(inventoryTransaction.getTaskQty()));
            } else {
                // 如果itMap中不存在该labelCode的库存交易对象,则将该对象存入map中
                itMap.put(inventoryTransaction.getLabelCode(), inventoryTransaction);
            }
        }

        // 将map中的所有库存交易对象转为List并返回
        return new ArrayList<>(itMap.values());
    }
pengcheng authored
690
}