package cn.iocoder.yudao.module.mes.service.pro.pallet;
|
|
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;
|
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.pallet.MesProPalletDO;
|
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;
|
import org.springframework.transaction.annotation.Transactional;
|
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;
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
|
|
/**
|
* MES 托盘码 Service 实现类
|
*
|
* @author 超级管理员
|
*/
|
@Service
|
@Validated
|
@Slf4j
|
public class MesProPalletServiceImpl implements MesProPalletService {
|
|
@Resource
|
private MesProPalletMapper palletMapper;
|
@Resource
|
private MesProBagCodeMapper bagCodeMapper;
|
@Resource
|
private MesProBatchMapper batchMapper;
|
@Resource
|
private MesMdmItemService mdmItemService;
|
@Resource
|
private AdminUserApi adminUserApi;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public List<Long> batchCreatePallet(MesProPalletBatchCreateReqVO createReqVO) {
|
// 1. 校验批次存在且处于生产中状态
|
MesProBatchDO batch = batchMapper.selectById(createReqVO.getBatchId());
|
if (batch == null) {
|
throw exception(PRO_BATCH_NOT_EXISTS);
|
}
|
if (!MesProBatchStatusEnum.PRODUCING.getStatus().equals(batch.getStatus())) {
|
throw exception(PRO_BATCH_STATUS_NOT_GENERATE_CODE);
|
}
|
if (createReqVO.getCount() == null || createReqVO.getCount() <= 0) {
|
throw exception(PRO_PALLET_GENERATE_COUNT_INVALID);
|
}
|
|
// 2. 计算起始序号(包含已逻辑删除记录,保证序号不回退、码值唯一)
|
int startNo = palletMapper.selectMaxPalletNo(batch.getId()) + 1;
|
|
// 3. 批量生成托盘码:批次号 + -P + 2 位托盘序号
|
List<MesProPalletDO> pallets = new ArrayList<>(createReqVO.getCount());
|
for (int i = 0; i < createReqVO.getCount(); i++) {
|
int palletNo = startNo + i;
|
pallets.add(new MesProPalletDO()
|
.setCode(batch.getCode() + "-P" + StrUtil.padPre(String.valueOf(palletNo), 2, '0'))
|
.setBatchId(batch.getId())
|
.setBatchCode(batch.getCode())
|
.setPalletNo(palletNo)
|
.setCapacity(createReqVO.getCapacity())
|
.setBagCount(0)
|
.setAutoGenerated(false)
|
.setStatus(MesProPalletStatusEnum.IN_USE.getStatus())
|
.setRemark(createReqVO.getRemark()));
|
}
|
palletMapper.insertBatch(pallets);
|
|
// 4. 回写批次托盘码数量
|
batchMapper.updateById(new MesProBatchDO().setId(batch.getId())
|
.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
|
@Transactional(rollbackFor = Exception.class)
|
public void deletePallet(Long id) {
|
// 1. 校验存在
|
MesProPalletDO pallet = validatePalletExists(id);
|
// 2. 校验未绑定袋码
|
if (bagCodeMapper.selectCountByPalletId(id) > 0) {
|
throw exception(PRO_PALLET_HAS_BAG_NOT_DELETE);
|
}
|
// 3. 删除托盘码
|
palletMapper.deleteById(id);
|
// 4. 回减批次托盘码数量
|
MesProBatchDO batch = batchMapper.selectById(pallet.getBatchId());
|
if (batch != null && batch.getPalletQuantity() != null && batch.getPalletQuantity() > 0) {
|
batchMapper.updateById(new MesProBatchDO().setId(batch.getId())
|
.setPalletQuantity(batch.getPalletQuantity() - 1));
|
}
|
}
|
|
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);
|
}
|
|
@Override
|
public MesProPalletDO getPalletByCode(String code) {
|
return palletMapper.selectByCode(code);
|
}
|
|
@Override
|
public MesProPalletDO validatePalletExists(Long id) {
|
MesProPalletDO pallet = palletMapper.selectById(id);
|
if (pallet == null) {
|
throw exception(PRO_PALLET_NOT_EXISTS);
|
}
|
return pallet;
|
}
|
|
@Override
|
public PageResult<MesProPalletDO> getPalletPage(MesProPalletPageReqVO pageReqVO) {
|
return palletMapper.selectPage(pageReqVO);
|
}
|
|
@Override
|
public List<MesProPalletDO> getPalletListByBatchId(Long batchId) {
|
return palletMapper.selectListByBatchId(batchId);
|
}
|
|
@Override
|
public void updatePalletBagCount(Long palletId, Integer bagCount) {
|
palletMapper.updateById(new MesProPalletDO().setId(palletId).setBagCount(bagCount));
|
}
|
|
}
|