14 小时以前 476d70b2f294f36a9dbd8d0c3cf5d1b75ba34c66
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/pallet/MesProPalletServiceImpl.java
@@ -2,6 +2,7 @@
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletAutoPrintRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletBatchCreateReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletPageReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.batch.MesProBatchDO;
@@ -9,8 +10,13 @@
import cn.iocoder.yudao.module.mes.dal.mysql.pro.bagcode.MesProBagCodeMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.batch.MesProBatchMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.pallet.MesProPalletMapper;
import cn.iocoder.yudao.module.mes.dal.dataobject.mdm.MesMdmItemDO;
import cn.iocoder.yudao.module.mes.enums.pro.MesProBagTypeEnum;
import cn.iocoder.yudao.module.mes.enums.pro.MesProBatchStatusEnum;
import cn.iocoder.yudao.module.mes.enums.pro.MesProPalletStatusEnum;
import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -18,6 +24,7 @@
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -39,6 +46,10 @@
    private MesProBagCodeMapper bagCodeMapper;
    @Resource
    private MesProBatchMapper batchMapper;
    @Resource
    private MesMdmItemService mdmItemService;
    @Resource
    private AdminUserApi adminUserApi;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -69,6 +80,7 @@
                    .setPalletNo(palletNo)
                    .setCapacity(createReqVO.getCapacity())
                    .setBagCount(0)
                    .setAutoGenerated(false)
                    .setStatus(MesProPalletStatusEnum.IN_USE.getStatus())
                    .setRemark(createReqVO.getRemark()));
        }
@@ -79,6 +91,57 @@
                .setPalletQuantity((batch.getPalletQuantity() == null ? 0 : batch.getPalletQuantity())
                        + createReqVO.getCount()));
        return pallets.stream().map(MesProPalletDO::getId).toList();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MesProPalletAutoPrintRespVO> autoCreatePallets(Long batchId) {
        MesProBatchDO batch = batchMapper.selectByIdForUpdate(batchId);
        if (batch == null) {
            throw exception(PRO_BATCH_NOT_EXISTS);
        }
        if (!MesProBatchStatusEnum.PRODUCING.getStatus().equals(batch.getStatus())) {
            throw exception(PRO_BATCH_STATUS_NOT_GENERATE_CODE);
        }
        MesProBagTypeEnum bagType = MesProBagTypeEnum.valueOfType(batch.getBagType());
        if (bagType == null) {
            throw exception(PRO_BATCH_BAG_TYPE_INVALID);
        }
        List<cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeDO> candidates =
                bagCodeMapper.selectUnboundEffectiveListByBatchId(batchId);
        int threshold = bagType.getThreshold();
        int palletCount = candidates.size() / threshold;
        if (palletCount == 0) {
            return Collections.emptyList();
        }
        int startNo = palletMapper.selectMaxPalletNo(batchId) + 1;
        List<MesProPalletDO> pallets = new ArrayList<>(palletCount);
        List<List<Long>> bagIdGroups = new ArrayList<>(palletCount);
        for (int i = 0; i < palletCount; i++) {
            int palletNo = startNo + i;
            MesProPalletDO pallet = new MesProPalletDO()
                    .setCode(batch.getCode() + "-P" + StrUtil.padPre(String.valueOf(palletNo), 2, '0'))
                    .setBatchId(batchId)
                    .setBatchCode(batch.getCode())
                    .setPalletNo(palletNo)
                    .setCapacity(threshold)
                    .setBagCount(threshold)
                    .setAutoGenerated(true)
                    .setStatus(MesProPalletStatusEnum.IN_USE.getStatus())
                    .setRemark("按有效袋码阈值自动生成");
            pallets.add(pallet);
            bagIdGroups.add(candidates.subList(i * threshold, (i + 1) * threshold).stream()
                    .map(cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeDO::getId).toList());
        }
        palletMapper.insertBatch(pallets);
        for (int i = 0; i < pallets.size(); i++) {
            MesProPalletDO pallet = pallets.get(i);
            bagCodeMapper.bindPalletByIds(bagIdGroups.get(i), pallet.getId(), pallet.getCode());
        }
        batchMapper.updateById(new MesProBatchDO().setId(batchId)
                .setPalletQuantity((batch.getPalletQuantity() == null ? 0 : batch.getPalletQuantity()) + pallets.size()));
        return buildAutoPrintVOs(pallets, batch);
    }
    @Override
@@ -100,6 +163,30 @@
        }
    }
    private List<MesProPalletAutoPrintRespVO> buildAutoPrintVOs(List<MesProPalletDO> pallets, MesProBatchDO batch) {
        MesMdmItemDO item = batch.getItemId() == null ? null : mdmItemService.getItem(batch.getItemId());
        AdminUserRespDTO owner = batch.getOwnerUserId() == null ? null : adminUserApi.getUser(batch.getOwnerUserId());
        return pallets.stream().map(pallet -> new MesProPalletAutoPrintRespVO()
                .setId(pallet.getId())
                .setCode(pallet.getCode())
                .setBatchId(pallet.getBatchId())
                .setBatchCode(pallet.getBatchCode())
                .setPalletNo(pallet.getPalletNo())
                .setCopies(3)
                .setCapacity(pallet.getCapacity())
                .setBagCount(pallet.getBagCount())
                .setItemId(batch.getItemId())
                .setItemCode(item == null ? null : item.getCode())
                .setItemName(item == null ? null : item.getName())
                .setItemSpecification(item == null ? null : item.getSpecification())
                .setLineCode(batch.getLineCode())
                .setLineName(batch.getLineName())
                .setProduceDate(batch.getProduceDate())
                .setOwnerUserId(batch.getOwnerUserId())
                .setOwnerUserName(owner == null ? null : owner.getNickname()))
                .toList();
    }
    @Override
    public MesProPalletDO getPallet(Long id) {
        return palletMapper.selectById(id);