| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.module.mdm.api.unit.MdmUnitMeasureApi; |
| | | import cn.iocoder.yudao.module.mdm.api.unit.dto.MdmUnitMeasureRespDTO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeBindBatchReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeBindReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeGenerateReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeGenerateRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodePageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeRawGenerateReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeReplaceItemRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeReplaceReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodeReplaceRespVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.mdm.MesMdmItemDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeGenerateTaskDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeReplaceDO; |
| | | 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.MesProBagCodeReplaceMapper; |
| | | 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.redis.MesCodeRedisDAO; |
| | | import cn.iocoder.yudao.module.mes.enums.pro.MesProBagCodeGenerateBizTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.pro.MesProBagCodeStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.pro.MesProBatchStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.pro.MesProPalletStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.framework.bagcode.MesProBagCodeGenerateProperties; |
| | | import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletService; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | |
| | | |
| | | @Resource |
| | | private MesProPalletService palletService; |
| | | @Resource |
| | | private MesMdmItemService mdmItemService; |
| | | @Resource |
| | | private MdmUnitMeasureApi unitMeasureApi; |
| | | |
| | | /** |
| | | * 计量单位类型「吨」(无托盘模式) |
| | | */ |
| | | private static final String UNIT_TYPE_TON = "吨"; |
| | | /** |
| | | * 计量单位类型「N袋」:每托盘装 N 个袋码 |
| | | */ |
| | | private static final Pattern BAG_TYPE_PATTERN = Pattern.compile("^(\\d{1,5})袋$"); |
| | | |
| | | @Resource |
| | | private MesCodeRedisDAO codeRedisDao; |
| | | @Resource |
| | | private cn.iocoder.yudao.module.mes.service.trace.integrity.MesTraceIntegrityService integrityService; |
| | | |
| | | @Resource |
| | | private MesProBagCodeBatchGenerator batchGenerator; |
| | | @Resource |
| | | private MesProBagCodeGenerateCleaner generateCleaner; |
| | | @Resource |
| | | private MesProBagCodeGenerateTaskService generateTaskService; |
| | | @Resource |
| | | private MesProBagCodeGenerateProperties generateProperties; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesProBagCodeGenerateRespVO generateBagCodes(MesProBagCodeGenerateReqVO generateReqVO) { |
| | | if (generateReqVO.getCount() == null || generateReqVO.getCount() <= 0) { |
| | | throw exception(PRO_BAG_CODE_GENERATE_COUNT_INVALID); |
| | | } |
| | | // 1. 校验品种并读取主计量单位类型 |
| | | MesMdmItemDO item = mdmItemService.validateItemExists(generateReqVO.getItemId()); |
| | | String type = getUnitType(item); |
| | | |
| | | // 生成阶段只创建全局唯一袋码,生产批次在后续录入 |
| | | int startNo = bagCodeMapper.selectMaxBagNo() + 1; |
| | | String uuid = IdUtil.fastSimpleUUID(); |
| | | List<MesProBagCodeDO> bagCodes = new ArrayList<>(generateReqVO.getCount()); |
| | | for (int i = 0; i < generateReqVO.getCount(); i++) { |
| | | int bagNo = startNo + i; |
| | | bagCodes.add(new MesProBagCodeDO() |
| | | .setUuid(uuid) |
| | | .setCode("BAG-" + StrUtil.padPre(String.valueOf(bagNo), 8, '0')) |
| | | .setBagNo(bagNo) |
| | | .setStatus(MesProBagCodeStatusEnum.GENERATED.getStatus()) |
| | | .setRemark(generateReqVO.getRemark())); |
| | | // 2. 按类型换算袋码数量:N袋 = 每托 N 袋 × 托盘数;吨 = 直接输入袋码数(无托盘) |
| | | boolean ton = UNIT_TYPE_TON.equals(type); |
| | | int perPallet = ton ? 0 : parseBagsPerPallet(type); |
| | | long totalCount; |
| | | int palletCount = 0; |
| | | if (ton) { |
| | | if (generateReqVO.getBagCount() == null || generateReqVO.getBagCount() <= 0) { |
| | | throw exception(PRO_BAG_CODE_GENERATE_COUNT_INVALID); |
| | | } |
| | | totalCount = generateReqVO.getBagCount(); |
| | | } else { |
| | | if (generateReqVO.getPalletCount() == null || generateReqVO.getPalletCount() <= 0) { |
| | | throw exception(PRO_BAG_CODE_PALLET_COUNT_REQUIRED); |
| | | } |
| | | palletCount = generateReqVO.getPalletCount(); |
| | | // 用 long 计算,避免大托盘数相乘溢出 int |
| | | totalCount = (long) perPallet * palletCount; |
| | | } |
| | | bagCodeMapper.insertBatch(bagCodes); |
| | | return new MesProBagCodeGenerateRespVO().setUuid(uuid); |
| | | assertGenerateCountAllowed(totalCount); |
| | | |
| | | // 3. 生成阶段:袋码直接绑定品种;生产批次在后续录入 |
| | | MesProBagCodeGenerateSpec spec = new MesProBagCodeGenerateSpec( |
| | | MesProBagCodeGenerateBizTypeEnum.PACKAGE, IdUtil.fastSimpleUUID(), |
| | | item.getId(), item.getCode(), item.getName(), type, |
| | | perPallet, palletCount, (int) totalCount, generateReqVO.getRemark()); |
| | | |
| | | // 4. 小批量同步完成,大批量转后台任务 |
| | | if (totalCount <= generateProperties.getSyncThreshold()) { |
| | | MesProBagCodeBatchGenerator.GenerateResult result = runSyncGenerate(spec); |
| | | return new MesProBagCodeGenerateRespVO() |
| | | .setUuid(spec.uuid()) |
| | | .setType(type) |
| | | .setPerPallet(perPallet) |
| | | .setTotalCount(result.finishedCount()) |
| | | .setPalletCount(result.palletIds().size()) |
| | | .setPalletIds(result.palletIds()) |
| | | .setAsync(false); |
| | | } |
| | | MesProBagCodeGenerateTaskDO task = generateTaskService.submit(spec, generateReqVO.getRequestId()); |
| | | return new MesProBagCodeGenerateRespVO() |
| | | .setUuid(task.getUuid()) |
| | | .setType(task.getUnitType()) |
| | | .setPerPallet(task.getPerPallet()) |
| | | .setTotalCount(task.getTotalCount()) |
| | | .setPalletCount(task.getPalletCount()) |
| | | .setAsync(true) |
| | | .setTaskId(task.getId()); |
| | | } |
| | | |
| | | @Override |
| | | public MesProBagCodeGenerateRespVO generateRawBagCodes(MesProBagCodeRawGenerateReqVO generateReqVO) { |
| | | // 原始袋码不依赖计量单位类型与托盘:仅按数量生成,袋码带品种冗余,后续可正常录入批次/绑定托盘 |
| | | MesMdmItemDO item = mdmItemService.validateItemExists(generateReqVO.getItemId()); |
| | | long totalCount = generateReqVO.getCount(); |
| | | assertGenerateCountAllowed(totalCount); |
| | | |
| | | MesProBagCodeGenerateSpec spec = new MesProBagCodeGenerateSpec( |
| | | MesProBagCodeGenerateBizTypeEnum.RAW, IdUtil.fastSimpleUUID(), |
| | | item.getId(), item.getCode(), item.getName(), null, |
| | | 0, 0, (int) totalCount, generateReqVO.getRemark()); |
| | | |
| | | if (totalCount <= generateProperties.getSyncThreshold()) { |
| | | MesProBagCodeBatchGenerator.GenerateResult result = runSyncGenerate(spec); |
| | | return new MesProBagCodeGenerateRespVO() |
| | | .setUuid(spec.uuid()) |
| | | .setTotalCount(result.finishedCount()) |
| | | .setPalletCount(0) |
| | | .setPalletIds(List.of()) |
| | | .setAsync(false); |
| | | } |
| | | MesProBagCodeGenerateTaskDO task = generateTaskService.submit(spec, generateReqVO.getRequestId()); |
| | | return new MesProBagCodeGenerateRespVO() |
| | | .setUuid(task.getUuid()) |
| | | .setTotalCount(task.getTotalCount()) |
| | | .setPalletCount(task.getPalletCount()) |
| | | .setAsync(true) |
| | | .setTaskId(task.getId()); |
| | | } |
| | | |
| | | /** |
| | | * 同步生成:失败时清理已写入的分段,避免留下不完整的数据包 |
| | | */ |
| | | private MesProBagCodeBatchGenerator.GenerateResult runSyncGenerate(MesProBagCodeGenerateSpec spec) { |
| | | try { |
| | | return batchGenerator.generate(spec, null, new AtomicBoolean(false)); |
| | | } catch (RuntimeException e) { |
| | | try { |
| | | generateCleaner.cleanup(spec.uuid()); |
| | | } catch (Exception cleanupException) { |
| | | log.error("[runSyncGenerate][生成组 {} 半成品清理失败]", spec.uuid(), cleanupException); |
| | | } |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验单次生成袋码数量不超过上限 |
| | | */ |
| | | private void assertGenerateCountAllowed(long totalCount) { |
| | | int maxCount = generateProperties.getMaxTaskCount(); |
| | | if (totalCount > maxCount) { |
| | | throw exception(PRO_BAG_CODE_GENERATE_COUNT_TOO_MANY, maxCount); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取品种主计量单位的类型(48袋/100袋/吨),未配置或无法识别时报错 |
| | | */ |
| | | private String getUnitType(MesMdmItemDO item) { |
| | | String type = null; |
| | | if (item.getUnitMeasureId() != null) { |
| | | CommonResult<MdmUnitMeasureRespDTO> result = unitMeasureApi.getUnitMeasure(item.getUnitMeasureId()); |
| | | if (result != null && result.isSuccess() && result.getData() != null) { |
| | | type = StrUtil.trimToNull(result.getData().getType()); |
| | | } |
| | | } |
| | | if (type == null) { |
| | | throw exception(PRO_BAG_CODE_ITEM_TYPE_NOT_CONFIGURED); |
| | | } |
| | | if (!UNIT_TYPE_TON.equals(type) && !BAG_TYPE_PATTERN.matcher(type).matches()) { |
| | | throw exception(PRO_BAG_CODE_ITEM_TYPE_NOT_CONFIGURED); |
| | | } |
| | | return type; |
| | | } |
| | | |
| | | @Override |
| | | public List<MesProBagCodeDO> getUnboundTonBags() { |
| | | Map<Long, Boolean> tonItems = new LinkedHashMap<>(); |
| | | return bagCodeMapper.selectUnboundPalletlessList().stream() |
| | | .filter(bag -> tonItems.computeIfAbsent(bag.getItemId(), id -> { |
| | | MesMdmItemDO item = mdmItemService.getItem(id); |
| | | if (item == null || item.getUnitMeasureId() == null) return false; |
| | | CommonResult<MdmUnitMeasureRespDTO> result = unitMeasureApi.getUnitMeasure(item.getUnitMeasureId()); |
| | | return result != null && result.isSuccess() && result.getData() != null |
| | | && UNIT_TYPE_TON.equals(StrUtil.trim(result.getData().getType())); |
| | | })) |
| | | .toList(); |
| | | } |
| | | |
| | | /** |
| | | * 解析「N袋」类型得出每托盘袋数 |
| | | */ |
| | | private int parseBagsPerPallet(String type) { |
| | | Matcher matcher = BAG_TYPE_PATTERN.matcher(type); |
| | | if (!matcher.matches()) { |
| | | throw exception(PRO_BAG_CODE_ITEM_TYPE_NOT_CONFIGURED); |
| | | } |
| | | return Integer.parseInt(matcher.group(1)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void bindBatch(MesProBagCodeBindBatchReqVO bindReqVO) { |
| | | MesProBatchDO batch = batchMapper.selectByIdForUpdate(bindReqVO.getBatchId()); |
| | | doBindBatch(bindReqVO.getBatchId(), bindReqVO.getIds()); |
| | | } |
| | | |
| | | /** |
| | | * 将袋码录入指定批次,加锁顺序为「批次 → 袋码 → 托盘」。 |
| | | * <p> |
| | | * 「录入批次」与「绑定托盘」共用此实现:绑定托盘时若托盘已属某批次、而所选袋码尚未录入批次, |
| | | * 先按本方法完成录入,从而保证批次准入校验、品种一致性校验、批次汇总回写与完整性记录只有一份。 |
| | | * |
| | | * @param batchId 生产批次编号 |
| | | * @param rawIds 待录入的袋码编号(未去重) |
| | | */ |
| | | private void doBindBatch(Long batchId, List<Long> rawIds) { |
| | | MesProBatchDO batch = batchMapper.selectByIdForUpdate(batchId); |
| | | if (batch == null) { |
| | | throw exception(PRO_BATCH_NOT_EXISTS); |
| | | } |
| | |
| | | if (StrUtil.isEmpty(batch.getCode())) { |
| | | throw exception(PRO_BATCH_NOT_FILLED); |
| | | } |
| | | List<Long> ids = bindReqVO.getIds().stream().distinct().toList(); |
| | | List<Long> ids = rawIds.stream().distinct().toList(); |
| | | List<MesProBagCodeDO> bagCodes = bagCodeMapper.selectListByIdsForUpdate(ids); |
| | | if (bagCodes.size() != ids.size() || bagCodes.stream().anyMatch(code -> |
| | | code.getBatchId() != null || !MesProBagCodeStatusEnum.GENERATED.getStatus().equals(code.getStatus()))) { |
| | | code.getBatchId() != null |
| | | || (!MesProBagCodeStatusEnum.GENERATED.getStatus().equals(code.getStatus()) |
| | | && !MesProBagCodeStatusEnum.PRINTED.getStatus().equals(code.getStatus())))) { |
| | | throw exception(PRO_BAG_CODE_STATUS_NOT_GENERATED); |
| | | } |
| | | // 录入的袋码必须属于同一品种,且与批次已有袋码品种一致(历史空白袋码 item 为空不参与校验) |
| | | Set<Long> itemIds = new LinkedHashSet<>(); |
| | | if (batch.getItemId() != null) itemIds.add(batch.getItemId()); |
| | | bagCodes.stream().map(MesProBagCodeDO::getItemId).filter(Objects::nonNull).forEach(itemIds::add); |
| | | itemIds.addAll(bagCodeMapper.selectDistinctItemIdsByBatchId(batch.getId())); |
| | | for (Long palletId : bagCodes.stream().map(MesProBagCodeDO::getPalletId).filter(Objects::nonNull).distinct().sorted().toList()) { |
| | | MesProPalletDO pallet = palletMapper.selectByIdForUpdate(palletId); |
| | | if (pallet == null) throw exception(PRO_PALLET_NOT_EXISTS); |
| | | if (pallet.getBatchId() != null && !pallet.getBatchId().equals(batch.getId())) |
| | | throw exception(PRO_BAG_CODE_BIND_PALLET_MISMATCH); |
| | | Long palletItem = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.resolve(pallet, bagCodeMapper.selectListByPalletId(palletId)); |
| | | if (palletItem != null) itemIds.add(palletItem); |
| | | } |
| | | if (itemIds.size() > 1) { |
| | | throw exception(PRO_BAG_CODE_BIND_BATCH_ITEM_MISMATCH); |
| | | } |
| | | if (!itemIds.isEmpty()) batch.setItemId(itemIds.iterator().next()); |
| | | if (bagCodeMapper.bindBatchByIds(ids, batch.getId(), batch.getCode(), |
| | | MesProBagCodeStatusEnum.GENERATED.getStatus()) != ids.size()) { |
| | | List.of(MesProBagCodeStatusEnum.GENERATED.getStatus(), |
| | | MesProBagCodeStatusEnum.PRINTED.getStatus())) != ids.size()) { |
| | | throw exception(PRO_BAG_CODE_STATUS_NOT_GENERATED); |
| | | } |
| | | batchMapper.updateById(new MesProBatchDO().setId(batch.getId()) |
| | | batchMapper.updateById(new MesProBatchDO().setId(batch.getId()).setItemId(batch.getItemId()) |
| | | .setBagQuantity(bagCodeMapper.selectEffectiveCountByBatchId(batch.getId()))); |
| | | // 级联:本组袋码绑定的未入批次托盘,分配正式托盘码并绑定批次 |
| | | bindPalletsToBatch(bagCodes, batch); |
| | | integrityService.recordBatchIntegrity(batch.getId()); |
| | | } |
| | | |
| | | /** |
| | | * 袋码录入批次时,将其绑定的临时托盘(未绑定批次)级联绑定到同一批次: |
| | | * 分配正式托盘码(16 位随机数字,与托盘码生成规则一致)、回写袋码上的冗余托盘码、累计批次托盘数 |
| | | */ |
| | | private void bindPalletsToBatch(List<MesProBagCodeDO> bagCodes, MesProBatchDO batch) { |
| | | List<Long> palletIds = bagCodes.stream().map(MesProBagCodeDO::getPalletId) |
| | | .filter(Objects::nonNull).distinct().sorted().toList(); |
| | | if (palletIds.isEmpty()) { |
| | | return; |
| | | } |
| | | int nextPalletNo = palletMapper.selectMaxPalletNo(batch.getId()) + 1; |
| | | int boundCount = 0; |
| | | for (Long palletId : palletIds) { |
| | | MesProPalletDO pallet = palletMapper.selectByIdForUpdate(palletId); |
| | | if (pallet == null || pallet.getBatchId() != null || StrUtil.isNotEmpty(pallet.getCode())) { |
| | | continue; |
| | | } |
| | | String palletCode = codeRedisDao.generate(); |
| | | palletMapper.updateById(new MesProPalletDO().setId(palletId) |
| | | .setItemId(batch.getItemId()) |
| | | .setCode(palletCode) |
| | | .setBatchId(batch.getId()) |
| | | .setBatchCode(batch.getCode()) |
| | | .setPalletNo(nextPalletNo++)); |
| | | // 袋码上的冗余托盘码由临时码更新为正式码 |
| | | bagCodeMapper.updatePalletCodeByPalletId(palletId, palletCode); |
| | | boundCount++; |
| | | } |
| | | if (boundCount > 0) { |
| | | batchMapper.updateById(new MesProBatchDO().setId(batch.getId()) |
| | | .setPalletQuantity((batch.getPalletQuantity() == null ? 0 : batch.getPalletQuantity()) + boundCount)); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | MesProPalletDO pallet = oldBagCode.getPalletId() == null ? null : palletMap.get(oldBagCode.getPalletId()); |
| | | MesProBagCodeDO newBagCode = new MesProBagCodeDO() |
| | | .setUuid(replaceUuid) |
| | | .setCode(batch.getCode() + "-" + StrUtil.padPre(String.valueOf(newBagNo), 4, '0')) |
| | | .setCode(codeRedisDao.generate()) |
| | | .setBusinessCode(codeRedisDao.generate()) |
| | | .setBagNo(newBagNo) |
| | | .setBatchId(batch.getId()) |
| | | .setBatchCode(batch.getCode()) |
| | | .setItemId(oldBagCode.getItemId()) |
| | | .setItemCode(oldBagCode.getItemCode()) |
| | | .setItemName(oldBagCode.getItemName()) |
| | | .setPalletId(pallet == null ? null : pallet.getId()) |
| | | .setPalletCode(pallet == null ? null : pallet.getCode()) |
| | | .setStatus(MesProBagCodeStatusEnum.GENERATED.getStatus()) |
| | |
| | | if (CollUtil.isEmpty(bagCodes)) { |
| | | throw exception(PRO_BAG_CODE_UUID_NOT_EXISTS); |
| | | } |
| | | // 2. 校验均为已生成状态且未绑定托盘 |
| | | // 2. 校验均为已生成状态 |
| | | if (bagCodes.stream().anyMatch(bagCode -> |
| | | !MesProBagCodeStatusEnum.GENERATED.getStatus().equals(bagCode.getStatus()))) { |
| | | throw exception(PRO_BAG_CODE_STATUS_NOT_GENERATED); |
| | | } |
| | | if (bagCodes.stream().anyMatch(bagCode -> bagCode.getPalletId() != null)) { |
| | | throw exception(PRO_BAG_CODE_BIND_PALLET_BOUND); |
| | | // 3. 绑定的托盘仅限"自动生成且未绑定批次"(生成数据包时自动绑定),级联删除;已入批次的托盘不允许随包删除 |
| | | Set<Long> palletIds = new LinkedHashSet<>(); |
| | | bagCodes.stream().map(MesProBagCodeDO::getPalletId).filter(Objects::nonNull).forEach(palletIds::add); |
| | | for (Long palletId : palletIds) { |
| | | MesProPalletDO pallet = palletMapper.selectById(palletId); |
| | | if (pallet != null && (pallet.getBatchId() != null || StrUtil.isNotEmpty(pallet.getCode()))) { |
| | | throw exception(PRO_BAG_CODE_BIND_PALLET_BOUND); |
| | | } |
| | | } |
| | | // 3. 删除生成组 |
| | | // 4. 删除生成组与其自动生成的临时托盘 |
| | | bagCodeMapper.deleteByIds(bagCodes.stream().map(MesProBagCodeDO::getId).toList()); |
| | | // 4. 回减批次袋码数量 |
| | | if (!palletIds.isEmpty()) { |
| | | palletMapper.deleteByIds(palletIds); |
| | | } |
| | | // 5. 回减批次袋码数量 |
| | | MesProBatchDO batch = batchMapper.selectById(bagCodes.get(0).getBatchId()); |
| | | if (batch != null && batch.getBagQuantity() != null) { |
| | | batchMapper.updateById(new MesProBatchDO().setId(batch.getId()) |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void bindPallet(MesProBagCodeBindReqVO bindReqVO) { |
| | | // 1. 锁定托盘,避免与自动归集并发修改袋数 |
| | | // 1. 无锁预读托盘与所选袋码,判断是否需要先为未录入批次的袋码补录批次。 |
| | | // 加锁顺序必须与「录入批次」一致(批次 → 袋码 → 托盘),否则两个入口并发时交叉等待会死锁。 |
| | | List<Long> ids = bindReqVO.getIds().stream().distinct().toList(); |
| | | MesProPalletDO palletSnapshot = palletMapper.selectById(bindReqVO.getPalletId()); |
| | | if (palletSnapshot == null) { |
| | | throw exception(PRO_PALLET_NOT_EXISTS); |
| | | } |
| | | List<MesProBagCodeDO> snapshots = bagCodeMapper.selectListByIds(ids); |
| | | if (snapshots.size() != ids.size()) { |
| | | throw exception(PRO_BAG_CODE_NOT_EXISTS); |
| | | } |
| | | // 2. 托盘未绑定批次时,袋码也必须未录入批次(反向不成立:未录入批次的袋码可绑到任意托盘) |
| | | if (palletSnapshot.getBatchId() == null |
| | | && snapshots.stream().anyMatch(code -> code.getBatchId() != null)) { |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, |
| | | "所选袋码已录入生产批次,请改选该批次下的托盘"); |
| | | } |
| | | // 3. 托盘已绑定批次、且存在未录入批次的袋码时,复用「录入批次」逻辑补录 |
| | | if (palletSnapshot.getBatchId() != null) { |
| | | List<Long> unboundIds = snapshots.stream() |
| | | .filter(code -> code.getBatchId() == null && code.getPalletId() == null |
| | | && !MesProBagCodeStatusEnum.CANCELED.getStatus().equals(code.getStatus())) |
| | | .map(MesProBagCodeDO::getId).toList(); |
| | | if (!unboundIds.isEmpty()) { |
| | | doBindBatch(palletSnapshot.getBatchId(), unboundIds); |
| | | } |
| | | } |
| | | // 4. 锁定托盘,避免与自动归集并发修改袋数 |
| | | MesProPalletDO pallet = palletMapper.selectByIdForUpdate(bindReqVO.getPalletId()); |
| | | if (pallet == null) { |
| | | throw exception(PRO_PALLET_NOT_EXISTS); |
| | | } |
| | | // 2. 锁定并校验袋码 |
| | | List<MesProBagCodeDO> bagCodes = validateBagCodesForBind(bindReqVO.getIds(), pallet, true); |
| | | // 3. 校验不超过托盘可装袋数 |
| | | int currentCount = pallet.getBagCount() == null ? 0 : pallet.getBagCount(); |
| | | if (pallet.getCapacity() != null && pallet.getCapacity() > 0 |
| | | && currentCount + bagCodes.size() > pallet.getCapacity()) { |
| | | throw exception(PRO_BAG_CODE_BIND_PALLET_OVER_CAPACITY); |
| | | // 5. 锁定并校验袋码(录入批次后,袋码与托盘必然同批次) |
| | | List<MesProBagCodeDO> bagCodes = validateBagCodesForBind(ids, pallet, true); |
| | | Long itemId = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.resolve(pallet, |
| | | bagCodeMapper.selectListByPalletId(pallet.getId())); |
| | | for (MesProBagCodeDO bag : bagCodes) { |
| | | if (bag.getItemId() == null) throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "袋码缺少品种"); |
| | | itemId = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.merge(itemId, bag.getItemId()); |
| | | } |
| | | // 4. 绑定袋码并累加托盘已装袋数 |
| | | bagCodeMapper.bindPalletByIds(bindReqVO.getIds(), pallet.getId(), pallet.getCode()); |
| | | palletMapper.updateById(new MesProPalletDO().setId(pallet.getId()).setItemId(itemId)); |
| | | // 6. 绑定袋码并累加托盘已装袋数(托盘无容量上限) |
| | | int currentCount = pallet.getBagCount() == null ? 0 : pallet.getBagCount(); |
| | | bagCodeMapper.bindPalletByIds(ids, pallet.getId(), StrUtil.isNotBlank(pallet.getCode()) ? pallet.getCode() : pallet.getTempCode()); |
| | | palletService.updatePalletBagCount(pallet.getId(), currentCount + bagCodes.size()); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public MesProBagCodeDO getBagCodeByScanCode(String code) { |
| | | return bagCodeMapper.selectByScanCode(code); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int generateBusinessCodes(List<Long> ids) { |
| | | if (CollUtil.isEmpty(ids) || ids.size() > 200 || ids.stream().anyMatch(Objects::isNull)) { |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "请选择 1 至 200 条袋码"); |
| | | } |
| | | List<Long> uniqueIds = ids.stream().distinct().sorted().toList(); |
| | | List<MesProBagCodeDO> bags = bagCodeMapper.selectListByIdsForUpdate(uniqueIds); |
| | | if (bags.size() != uniqueIds.size()) throw exception(PRO_BAG_CODE_NOT_EXISTS); |
| | | if (bags.stream().anyMatch(bag -> MesProBagCodeStatusEnum.CANCELED.getStatus().equals(bag.getStatus()))) { |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "已作废袋码不可补生成业务码"); |
| | | } |
| | | int count = 0; |
| | | for (MesProBagCodeDO bag : bags) { |
| | | if (StrUtil.isNotBlank(bag.getBusinessCode())) continue; |
| | | bagCodeMapper.updateById(new MesProBagCodeDO().setId(bag.getId()).setBusinessCode(codeRedisDao.generate())); |
| | | count++; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | @Override |
| | | public MesProBagCodeReplaceDO getReplacementByOldBagCodeId(Long oldBagCodeId) { |
| | | return replaceMapper.selectByOldBagCodeId(oldBagCodeId); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<MesProBagCodeDO> getPrintableBagCodeListByBatchId(Long batchId) { |
| | | return bagCodeMapper.selectPrintableListByBatchId(batchId); |
| | | } |
| | | |
| | | @Override |
| | | public List<MesProBagCodeDO> getPrintableBagCodeListByUuid(String uuid) { |
| | | return bagCodeMapper.selectPrintableListByUuid(uuid); |
| | | } |
| | | |
| | | @Override |
| | | public Long getPrintableBagCodeCountByUuid(String uuid) { |
| | | return bagCodeMapper.selectPrintableCountByUuid(uuid); |
| | | } |
| | | |
| | | @Override |
| | | public Long getPrintableBagCodeCountByBatchId(Long batchId) { |
| | | return bagCodeMapper.selectPrintableCountByBatchId(batchId); |
| | | } |
| | | |
| | | @Override |
| | | public List<MesProBagCodeDO> getPrintableBagCodePageByUuid(String uuid, int pageNo, int pageSize) { |
| | | return bagCodeMapper.selectPrintablePageByUuid(uuid, pageNo, pageSize); |
| | | } |
| | | |
| | | @Override |
| | | public List<MesProBagCodeDO> getPrintableBagCodePageByBatchId(Long batchId, int pageNo, int pageSize) { |
| | | return bagCodeMapper.selectPrintablePageByBatchId(batchId, pageNo, pageSize); |
| | | } |
| | | |
| | | @Override |
| | | public void markOutbound(Long id, Long salesId) { |
| | | bagCodeMapper.markOutbound(id, salesId, LocalDateTime.now()); |
| | | } |
| | |
| | | throw exception(PRO_BAG_CODE_NOT_EXISTS); |
| | | } |
| | | for (MesProBagCodeDO bagCode : bagCodes) { |
| | | if (!pallet.getBatchId().equals(bagCode.getBatchId())) { |
| | | if (!Objects.equals(pallet.getBatchId(), bagCode.getBatchId())) { |
| | | throw exception(PRO_BAG_CODE_BIND_PALLET_MISMATCH); |
| | | } |
| | | if (bagCode.getPalletId() != null) { |