chen.lin
16 小时以前 82065a03737fa1370eb9f4f01ab5332933baf08a
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java
@@ -12,10 +12,12 @@
import com.vincent.rsf.server.manager.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.common.utils.QuantityUtils;
import com.vincent.rsf.server.system.constant.GlobalConfigCode;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.entity.Config;
import com.vincent.rsf.server.system.service.ConfigService;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -45,6 +47,8 @@
    private TaskItemService taskItemService;
    @Autowired
    private MatnrMapper matnrMapper;
    @Autowired
    private ConfigService configService;
    /**
@@ -67,8 +71,8 @@
        WaitPakin pakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
                .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
        // 如果料箱码已经组托过,提示已组托,请更换料箱码
        if (!Objects.isNull(pakin)) {
        // 半箱时允许同一料箱码继续组托追加;非半箱时料箱码已组托则不可再用
        if (!Objects.isNull(pakin) && !Boolean.TRUE.equals(waitPakin.getIsHalf())) {
            throw new CoolException("已组托,请更换料箱码");
        }
@@ -85,14 +89,17 @@
        Double sum = QuantityUtils.roundToScale(waitPakin.getItems().stream().mapToDouble(PakinItem::getReceiptQty).sum());
        WaitPakin waitPakin1 = new WaitPakin();
        Config directPakinConfig = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, GlobalConfigCode.DIRECT_WAIT_PAKIN));
        boolean directWaitPakin = directPakinConfig != null && Boolean.parseBoolean(directPakinConfig.getVal());
        WaitPakin waitPakin1;
        if (Objects.isNull(pakin)) {
            waitPakin1 = new WaitPakin();
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_WAIT_PAKIN_CODE, null);
            if (StringUtils.isBlank(ruleCode)) {
                throw new CoolException("编码规则错误: 编码规则「SYS_WAIT_PAKIN_CODE」规则是不存在");
            }
            waitPakin1.setCode(ruleCode)
                    //状态修改为入库中
                    .setIoStatus(PakinIOStatus.PAKIN_IO_STATUS_DONE.val)
                    .setAnfme(sum)
                    .setUpdateBy(userId)
@@ -102,14 +109,94 @@
                throw new CoolException("主单保存失败!!");
            }
        } else {
            BeanUtils.copyProperties(pakin, waitPakin1);
            // 半箱追加:沿用已有组托单,只追加明细
            waitPakin1 = pakin;
        }
        List<WaitPakinItem> items = new ArrayList<>();
        if (directWaitPakin) {
            // DirectWaitPakin 启用:组托来自收货区,param 中 id 为 WarehouseAreasItem.id
            for (PakinItem pakinItem1 : waitPakin.getItems()) {
                WarehouseAreasItem areaItem = warehouseAreasItemService.getById(pakinItem1.getId());
                if (areaItem == null) {
                    throw new CoolException("物料未送至收货区!!");
                }
                WaitPakinItem pakinItem = new WaitPakinItem();
                pakinItem.setPakinId(waitPakin1.getId())
                        .setSource(areaItem.getId())
                        .setAsnId(areaItem.getAsnId())
                        .setAsnCode(areaItem.getAsnCode())
                        .setAsnItemId(areaItem.getAsnItemId())
                        .setIsptResult(areaItem.getIsptResult())
                        .setPlatItemId(areaItem.getPlatItemId())
                        .setPlatOrderCode(areaItem.getPlatOrderCode())
                        .setPlatWorkCode(areaItem.getPlatWorkCode())
                        .setProjectCode(areaItem.getProjectCode())
                        .setBatch(areaItem.getSplrBatch())
                        .setUnit(areaItem.getStockUnit())
                        .setFieldsIndex(areaItem.getFieldsIndex())
                        .setMatnrId(areaItem.getMatnrId())
                        .setMaktx(areaItem.getMaktx())
                        .setUpdateBy(userId)
                        .setCreateBy(userId)
                        .setMatnrCode(areaItem.getMatnrCode());
                WkOrder order = asnOrderService.getById(areaItem.getAsnId());
                if (order != null) {
                    pakinItem.setType(order.getType()).setWkType(StringUtils.isNotBlank(order.getWkType()) ? Short.parseShort(order.getWkType()) : null);
                }
                if (pakinItem1.getReceiptQty() == null || pakinItem1.getReceiptQty().compareTo(0.0) <= 0) {
                    throw new CoolException("组托数量不能小于等于零!!");
                }
                if (QuantityUtils.compare(pakinItem1.getReceiptQty(), areaItem.getAnfme()) > 0) {
                    throw new CoolException("组托数量不能大于收货数量!!");
                }
                pakinItem.setAnfme(QuantityUtils.roundToScale(pakinItem1.getReceiptQty())).setTrackCode(pakinItem1.getTrackCode());
                items.add(pakinItem);
            }
            if (!waitPakinItemService.saveBatch(items)) {
                throw new CoolException("组托明细保存失败!!");
            }
            for (WaitPakinItem pakinItem : items) {
                WarehouseAreasItem one = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>().eq(WarehouseAreasItem::getId, pakinItem.getSource()));
                if (one == null) {
                    throw new CoolException("收货区数据错误!!");
                }
                Double workQty = QuantityUtils.roundToScale(QuantityUtils.add(one.getWorkQty() != null ? one.getWorkQty() : 0.0, pakinItem.getAnfme()));
                Double qty = QuantityUtils.roundToScale(QuantityUtils.add(workQty, one.getQty() != null ? one.getQty() : 0.0));
                one.setWorkQty(workQty);
                if (QuantityUtils.compare(qty, one.getAnfme() != null ? one.getAnfme() : 0.0) > 0) {
                    throw new CoolException("组托数量不能大于收货数量!!");
                }
                if (!warehouseAreasItemService.saveOrUpdate(one)) {
                    throw new CoolException("收货区执行数量修改失败!!");
                }
            }
            // 半箱追加时需按该组托单下全部明细重算总数量
            List<WaitPakinItem> allItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, waitPakin1.getId()));
            waitPakin1.setAnfme(QuantityUtils.roundToScale(allItems.stream().mapToDouble(w -> w.getAnfme() != null ? w.getAnfme() : 0.0).sum()));
            if (!this.updateById(waitPakin1)) {
                throw new CoolException("组托数量修改失败!!");
            }
            return waitPakin1;
        }
        // 有ASN时:按订单明细汇总已组托数量,校验 已组托+本次组盘 ≤ 计划数量
        Map<Long, Double> alreadyPalletizedByItemId = new java.util.HashMap<>();
        for (PakinItem p : waitPakin.getItems()) {
            if (StringUtils.isNotBlank(p.getAsnCode()) && p.getId() != null) {
                alreadyPalletizedByItemId.putIfAbsent(p.getId(), null);
            }
        }
        if (!alreadyPalletizedByItemId.isEmpty()) {
            for (Long asnItemId : alreadyPalletizedByItemId.keySet()) {
                double alreadySum = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getAsnItemId, asnItemId).eq(WaitPakinItem::getDeleted, 0))
                        .stream().mapToDouble(w -> w.getAnfme() != null ? w.getAnfme() : 0.0).sum();
                alreadyPalletizedByItemId.put(asnItemId, QuantityUtils.roundToScale(alreadySum));
            }
        }
        for (PakinItem pakinItem1 : waitPakin.getItems()) {
            WaitPakinItem pakinItem = new WaitPakinItem();
            // 如果ASN单号为空,从物料信息表获取物料信息,不查询收货区
            if (StringUtils.isBlank(pakinItem1.getAsnCode())) {
                if (Objects.isNull(pakinItem1.getMatnrId())) {
                    throw new CoolException("物料ID不能为空!!");
@@ -184,8 +271,10 @@
                        if (waitPakinItem.getReceiptQty() == null || waitPakinItem.getReceiptQty().compareTo(0.0) <= 0) {
                            throw new CoolException("组托数量不能小于等于零!!");
                        }
                        if (QuantityUtils.compare(waitPakinItem.getReceiptQty(), orderItem.getAnfme()) > 0) {
                            throw new CoolException("组托数量不能大于计划数量!!");
                        Double anfme = orderItem.getAnfme() != null ? orderItem.getAnfme() : 0.0;
                        Double already = alreadyPalletizedByItemId.getOrDefault(orderItem.getId(), 0.0);
                        if (QuantityUtils.compare(QuantityUtils.add(already, waitPakinItem.getReceiptQty()), anfme) > 0) {
                            throw new CoolException("组托数量不能超过可组盘数量(计划" + anfme + ",已组托" + already + ",本次最多" + QuantityUtils.subtract(anfme, already) + ")!!");
                        }
                        pakinItem.setAnfme(QuantityUtils.roundToScale(waitPakinItem.getReceiptQty()))
                                .setTrackCode(waitPakinItem.getTrackCode());
@@ -284,16 +373,19 @@
                                throw new CoolException("组托明细删除失败!!");
                            }
                        }
                        // 收货区已停用,不再回写收货区
                        // for (int i = 0; i < warehouseAreasItems.size(); i++) {
                        //     if (warehouseAreasItems.get(i).getId().equals(pakinItems.get(i1).getSource())) {
                        //         double v = Math.round((warehouseAreasItems.get(i).getWorkQty() - item.getReceiptQty()) * 1000000) / 1000000.0;
                        //         warehouseAreasItems.get(i).setWorkQty(v);
                        //         if (!warehouseAreasItemService.updateById(warehouseAreasItems.get(i))) {
                        //             throw new CoolException("收货区数量修改失败!!");
                        //         }
                        //     }
                        // }
                        // DirectWaitPakin 启用时组托来自收货区,解绑需回写收货区 workQty
                        if (Objects.nonNull(pakinItems.get(i1).getSource())) {
                            for (WarehouseAreasItem areaItem : warehouseAreasItems) {
                                if (areaItem.getId().equals(pakinItems.get(i1).getSource())) {
                                    Double v = QuantityUtils.roundToScale(QuantityUtils.subtract(areaItem.getWorkQty() != null ? areaItem.getWorkQty() : 0.0, item.getReceiptQty()));
                                    areaItem.setWorkQty(v);
                                    if (!warehouseAreasItemService.updateById(areaItem)) {
                                        throw new CoolException("收货区数量修改失败!!");
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }