| | |
| | | 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.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 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; |
| | | |
| | |
| | | */ |
| | | private static final String UNIT_TYPE_TON = "吨"; |
| | | /** |
| | | * 计量单位类型「N千克」:每托盘装 N 个袋码 |
| | | * 计量单位类型「N袋」:每托盘装 N 个袋码 |
| | | */ |
| | | private static final Pattern KG_TYPE_PATTERN = Pattern.compile("^(\\d{1,5})千克$"); |
| | | 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) { |
| | | // 1. 校验品种并读取主计量单位类型 |
| | | MesMdmItemDO item = mdmItemService.validateItemExists(generateReqVO.getItemId()); |
| | | String type = getUnitType(item); |
| | | |
| | | // 2. 按类型换算袋码数量:N千克 = 每托 N 袋 × 托盘数;吨 = 直接输入袋码数(无托盘) |
| | | // 2. 按类型换算袋码数量:N袋 = 每托 N 袋 × 托盘数;吨 = 直接输入袋码数(无托盘) |
| | | boolean ton = UNIT_TYPE_TON.equals(type); |
| | | int perPallet = ton ? 0 : parseKgPerPallet(type); |
| | | int totalCount; |
| | | 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_PALLET_COUNT_REQUIRED); |
| | | } |
| | | palletCount = generateReqVO.getPalletCount(); |
| | | totalCount = perPallet * palletCount; |
| | | // 用 long 计算,避免大托盘数相乘溢出 int |
| | | totalCount = (long) perPallet * palletCount; |
| | | } |
| | | assertGenerateCountAllowed(totalCount); |
| | | |
| | | // 3. 生成阶段:袋码直接绑定品种;生产批次在后续录入 |
| | | String uuid = IdUtil.fastSimpleUUID(); |
| | | List<MesProPalletDO> pallets = new ArrayList<>(palletCount); |
| | | if (!ton) { |
| | | for (int i = 0; i < palletCount; i++) { |
| | | pallets.add(new MesProPalletDO() |
| | | .setTempCode(codeRedisDao.generate()) |
| | | .setBusinessCode(codeRedisDao.generate()) |
| | | .setBagCount(perPallet) |
| | | .setAutoGenerated(true) |
| | | .setItemId(item.getId()) |
| | | .setStatus(MesProPalletStatusEnum.IN_USE.getStatus())); |
| | | } |
| | | palletMapper.insertBatch(pallets); |
| | | 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); |
| | | } |
| | | // 袋序号原子分配:先取库内最大序号作基准,再由 Redis 原子自增,避免并发生成产生重复袋序号 |
| | | int startNo = codeRedisDao.allocateBagNoStart(totalCount, bagCodeMapper.selectMaxBagNo()); |
| | | List<MesProBagCodeDO> bagCodes = new ArrayList<>(totalCount); |
| | | for (int i = 0; i < totalCount; i++) { |
| | | int bagNo = startNo + i; |
| | | MesProPalletDO pallet = ton ? null : pallets.get(i / perPallet); |
| | | bagCodes.add(new MesProBagCodeDO() |
| | | .setUuid(uuid) |
| | | .setCode(codeRedisDao.generate()) |
| | | .setBusinessCode(codeRedisDao.generate()) |
| | | .setBagNo(bagNo) |
| | | .setItemId(item.getId()) |
| | | .setItemCode(item.getCode()) |
| | | .setItemName(item.getName()) |
| | | .setPalletId(pallet == null ? null : pallet.getId()) |
| | | .setPalletCode(pallet == null ? null : pallet.getTempCode()) |
| | | .setStatus(MesProBagCodeStatusEnum.GENERATED.getStatus()) |
| | | .setRemark(generateReqVO.getRemark())); |
| | | } |
| | | bagCodeMapper.insertBatch(bagCodes); |
| | | MesProBagCodeGenerateTaskDO task = generateTaskService.submit(spec, generateReqVO.getRequestId()); |
| | | return new MesProBagCodeGenerateRespVO() |
| | | .setUuid(uuid) |
| | | .setType(type) |
| | | .setPerPallet(perPallet) |
| | | .setTotalCount(totalCount) |
| | | .setPalletIds(pallets.stream().map(MesProPalletDO::getId).toList()); |
| | | .setUuid(task.getUuid()) |
| | | .setType(task.getUnitType()) |
| | | .setPerPallet(task.getPerPallet()) |
| | | .setTotalCount(task.getTotalCount()) |
| | | .setPalletCount(task.getPalletCount()) |
| | | .setAsync(true) |
| | | .setTaskId(task.getId()); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesProBagCodeGenerateRespVO generateRawBagCodes(MesProBagCodeRawGenerateReqVO generateReqVO) { |
| | | // 原始袋码不依赖计量单位类型与托盘:仅按数量生成,袋码带品种冗余,后续可正常录入批次/绑定托盘 |
| | | MesMdmItemDO item = mdmItemService.validateItemExists(generateReqVO.getItemId()); |
| | | String uuid = IdUtil.fastSimpleUUID(); |
| | | // 袋序号原子分配:先取库内最大序号作基准,再由 Redis 原子自增,避免并发生成产生重复袋序号 |
| | | int startNo = codeRedisDao.allocateBagNoStart(generateReqVO.getCount(), bagCodeMapper.selectMaxBagNo()); |
| | | 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(codeRedisDao.generate()) |
| | | .setBusinessCode(codeRedisDao.generate()) |
| | | .setBagNo(bagNo) |
| | | .setItemId(item.getId()) |
| | | .setItemCode(item.getCode()) |
| | | .setItemName(item.getName()) |
| | | .setStatus(MesProBagCodeStatusEnum.GENERATED.getStatus()) |
| | | .setRemark(generateReqVO.getRemark())); |
| | | 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); |
| | | } |
| | | bagCodeMapper.insertBatch(bagCodes); |
| | | MesProBagCodeGenerateTaskDO task = generateTaskService.submit(spec, generateReqVO.getRequestId()); |
| | | return new MesProBagCodeGenerateRespVO() |
| | | .setUuid(uuid) |
| | | .setTotalCount(bagCodes.size()) |
| | | .setPalletIds(List.of()); |
| | | .setUuid(task.getUuid()) |
| | | .setTotalCount(task.getTotalCount()) |
| | | .setPalletCount(task.getPalletCount()) |
| | | .setAsync(true) |
| | | .setTaskId(task.getId()); |
| | | } |
| | | |
| | | /** |
| | | * 读取品种主计量单位的类型(48千克/100千克/吨),未配置或无法识别时报错 |
| | | * 同步生成:失败时清理已写入的分段,避免留下不完整的数据包 |
| | | */ |
| | | 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 (type == null) { |
| | | throw exception(PRO_BAG_CODE_ITEM_TYPE_NOT_CONFIGURED); |
| | | } |
| | | if (!UNIT_TYPE_TON.equals(type) && !KG_TYPE_PATTERN.matcher(type).matches()) { |
| | | 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千克」类型得出每托盘袋数 |
| | | * 解析「N袋」类型得出每托盘袋数 |
| | | */ |
| | | private int parseKgPerPallet(String type) { |
| | | Matcher matcher = KG_TYPE_PATTERN.matcher(type); |
| | | private int parseBagsPerPallet(String type) { |
| | | Matcher matcher = BAG_TYPE_PATTERN.matcher(type); |
| | | if (!matcher.matches()) { |
| | | throw exception(PRO_BAG_CODE_ITEM_TYPE_NOT_CONFIGURED); |
| | | } |
| | |
| | | @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 |
| | |
| | | @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); |
| | | // 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) { |
| | |
| | | itemId = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.merge(itemId, bag.getItemId()); |
| | | } |
| | | palletMapper.updateById(new MesProPalletDO().setId(pallet.getId()).setItemId(itemId)); |
| | | // 3. 绑定袋码并累加托盘已装袋数(托盘无容量上限) |
| | | // 6. 绑定袋码并累加托盘已装袋数(托盘无容量上限) |
| | | int currentCount = pallet.getBagCount() == null ? 0 : pallet.getBagCount(); |
| | | bagCodeMapper.bindPalletByIds(bindReqVO.getIds(), pallet.getId(), StrUtil.isNotBlank(pallet.getCode()) ? pallet.getCode() : pallet.getTempCode()); |
| | | bagCodeMapper.bindPalletByIds(ids, pallet.getId(), StrUtil.isNotBlank(pallet.getCode()) ? pallet.getCode() : pallet.getTempCode()); |
| | | palletService.updatePalletBagCount(pallet.getId(), currentCount + bagCodes.size()); |
| | | } |
| | | |