| | |
| | | package cn.iocoder.yudao.module.mes.service.pro.pallet; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | 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.MesProPalletBatchCreateReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletBindBatchReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletPreGenerateReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeDO; |
| | | 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.redis.MesCodeRedisDAO; |
| | | 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 jakarta.annotation.Resource; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | |
| | | private MesProBagCodeMapper bagCodeMapper; |
| | | @Resource |
| | | private MesProBatchMapper batchMapper; |
| | | |
| | | @Resource |
| | | private MesCodeRedisDAO codeRedisDao; |
| | | @Resource |
| | | private cn.iocoder.yudao.module.mes.service.trace.integrity.MesTraceIntegrityService integrityService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | 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')) |
| | | .setCode(codeRedisDao.generate()) |
| | | .setBusinessCode(codeRedisDao.generate()) |
| | | .setItemId(batch.getItemId()) |
| | | .setBatchId(batch.getId()) |
| | | .setBatchCode(batch.getCode()) |
| | | .setPalletNo(palletNo) |
| | | .setCapacity(createReqVO.getCapacity()) |
| | | .setBagCount(0) |
| | | .setAutoGenerated(false) |
| | | .setStatus(MesProPalletStatusEnum.IN_USE.getStatus()) |
| | | .setRemark(createReqVO.getRemark())); |
| | | } |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public List<MesProPalletDO> preGeneratePallet(MesProPalletPreGenerateReqVO reqVO) { |
| | | // 1. 通过统一编码生成器分配临时码及业务码,不再解析历史临时码序号。 |
| | | List<MesProPalletDO> list = new ArrayList<>(reqVO.getCount()); |
| | | for (int i = 0; i < reqVO.getCount(); i++) { |
| | | list.add(new MesProPalletDO() |
| | | .setTempCode(codeRedisDao.generate()) |
| | | .setBusinessCode(codeRedisDao.generate()) |
| | | .setBagCount(0) |
| | | .setAutoGenerated(false) |
| | | .setStatus(MesProPalletStatusEnum.IN_USE.getStatus()) |
| | | .setRemark(reqVO.getRemark())); |
| | | } |
| | | // 2. 批量插入(未绑定生产批次,等待绑定) |
| | | palletMapper.insertBatch(list); |
| | | return list; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int bindBatch(MesProPalletBindBatchReqVO bindReqVO) { |
| | | // 1. 锁定目标批次并校验已补录正式信息 + 生产中 |
| | | MesProBatchDO batch = batchMapper.selectByIdForUpdate(bindReqVO.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 (StrUtil.isEmpty(batch.getCode())) { |
| | | throw exception(PRO_BATCH_NOT_FILLED); |
| | | } |
| | | // 2. 锁定托盘,校验均未绑定批次且未生成正式托盘码 |
| | | List<Long> ids = bindReqVO.getIds().stream().distinct().sorted().toList(); |
| | | if (ids.isEmpty()) { |
| | | throw exception(PRO_PALLET_BIND_IDS_EMPTY); |
| | | } |
| | | List<MesProPalletDO> pallets = new ArrayList<>(ids.size()); |
| | | for (Long id : ids) { |
| | | MesProPalletDO pallet = palletMapper.selectByIdForUpdate(id); |
| | | if (pallet == null) { |
| | | throw exception(PRO_PALLET_NOT_EXISTS); |
| | | } |
| | | if (pallet.getBatchId() != null || StrUtil.isNotEmpty(pallet.getCode())) { |
| | | throw exception(PRO_PALLET_ALREADY_BIND_BATCH); |
| | | } |
| | | pallets.add(pallet); |
| | | } |
| | | Long itemId = batch.getItemId(); |
| | | for (Long existing : bagCodeMapper.selectDistinctItemIdsByBatchId(batch.getId())) |
| | | itemId = MesProPalletItemResolver.merge(itemId, existing); |
| | | for (MesProPalletDO pallet : pallets) { |
| | | Long palletItem = MesProPalletItemResolver.resolve(pallet, bagCodeMapper.selectListByPalletId(pallet.getId())); |
| | | if (palletItem == null) throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "托盘缺少品种,请先绑定同品种袋码"); |
| | | itemId = MesProPalletItemResolver.merge(itemId, palletItem); |
| | | } |
| | | // 3. 级联准备:托盘下未绑批次的袋码随托盘一起录入批次,品种须与批次已有袋码一致 |
| | | List<MesProBagCodeDO> bags = bagCodeMapper.selectUnboundListByPalletIds(ids); |
| | | List<Long> bindBagIds = new ArrayList<>(); |
| | | Set<Long> bagItemIds = new LinkedHashSet<>(); |
| | | for (MesProBagCodeDO bag : bags) { |
| | | if (!MesProBagCodeStatusEnum.GENERATED.getStatus().equals(bag.getStatus()) |
| | | && !MesProBagCodeStatusEnum.PRINTED.getStatus().equals(bag.getStatus())) { |
| | | continue; |
| | | } |
| | | bindBagIds.add(bag.getId()); |
| | | if (bag.getItemId() != null) { |
| | | bagItemIds.add(bag.getItemId()); |
| | | } |
| | | } |
| | | if (!bindBagIds.isEmpty()) { |
| | | bagItemIds.addAll(bagCodeMapper.selectDistinctItemIdsByBatchId(batch.getId())); |
| | | if (itemId != null) bagItemIds.add(itemId); |
| | | if (bagItemIds.size() > 1) { |
| | | throw exception(PRO_BAG_CODE_BIND_BATCH_ITEM_MISMATCH); |
| | | } |
| | | } |
| | | // 4. 分配正式托盘码:批次号 + -P + 2 位托盘序号(含逻辑删除记录参与序号,保证不回退、码值唯一) |
| | | int startNo = palletMapper.selectMaxPalletNo(batch.getId()) + 1; |
| | | for (int i = 0; i < pallets.size(); i++) { |
| | | int palletNo = startNo + i; |
| | | String palletCode = codeRedisDao.generate(); |
| | | palletMapper.updateById(new MesProPalletDO() |
| | | .setId(pallets.get(i).getId()) |
| | | .setItemId(itemId) |
| | | .setCode(palletCode) |
| | | .setBatchId(batch.getId()) |
| | | .setBatchCode(batch.getCode()) |
| | | .setPalletNo(palletNo)); |
| | | // 托盘下袋码的冗余托盘码由临时码更新为正式码 |
| | | bagCodeMapper.updatePalletCodeByPalletId(pallets.get(i).getId(), |
| | | palletCode); |
| | | } |
| | | // 5. 托盘下袋码批量绑定本批次 |
| | | if (!bindBagIds.isEmpty() && bagCodeMapper.bindBatchByIds(bindBagIds, batch.getId(), batch.getCode(), |
| | | List.of(MesProBagCodeStatusEnum.GENERATED.getStatus(), |
| | | MesProBagCodeStatusEnum.PRINTED.getStatus())) != bindBagIds.size()) { |
| | | throw exception(PRO_BAG_CODE_STATUS_NOT_GENERATED); |
| | | } |
| | | // 6. 回写批次品种、托盘码数量与有效袋码数量 |
| | | batchMapper.updateById(new MesProBatchDO().setId(batch.getId()).setItemId(itemId) |
| | | .setPalletQuantity((batch.getPalletQuantity() == null ? 0 : batch.getPalletQuantity()) + pallets.size()) |
| | | .setBagQuantity(bagCodeMapper.selectEffectiveCountByBatchId(batch.getId()))); |
| | | integrityService.recordBatchIntegrity(batch.getId()); |
| | | return pallets.size(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deletePallet(Long id) { |
| | | // 1. 校验存在 |
| | | MesProPalletDO pallet = validatePalletExists(id); |
| | |
| | | } |
| | | // 3. 删除托盘码 |
| | | palletMapper.deleteById(id); |
| | | // 4. 回减批次托盘码数量 |
| | | MesProBatchDO batch = batchMapper.selectById(pallet.getBatchId()); |
| | | // 4. 回减批次托盘码数量(临时托盘未绑定批次无需回减) |
| | | MesProBatchDO batch = pallet.getBatchId() == null ? null : batchMapper.selectById(pallet.getBatchId()); |
| | | if (batch != null && batch.getPalletQuantity() != null && batch.getPalletQuantity() > 0) { |
| | | batchMapper.updateById(new MesProBatchDO().setId(batch.getId()) |
| | | .setPalletQuantity(batch.getPalletQuantity() - 1)); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public MesProPalletDO getPalletByScanCode(String code) { |
| | | return palletMapper.selectByScanCode(code); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int generateBusinessCodes(java.util.List<Long> ids) { |
| | | if (ids == null || ids.isEmpty() || ids.size() > 200 || ids.stream().anyMatch(java.util.Objects::isNull)) |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "请选择 1 至 200 条记录"); |
| | | java.util.List<MesProPalletDO> records = new java.util.ArrayList<>(); |
| | | for (Long id : ids.stream().distinct().sorted().toList()) { |
| | | MesProPalletDO record = palletMapper.selectByIdForUpdate(id); |
| | | if (record == null) throw exception(PRO_PALLET_NOT_EXISTS); |
| | | if (MesProPalletStatusEnum.CANCELED.getStatus().equals(record.getStatus())) |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "已作废记录不可补生成业务码"); |
| | | records.add(record); |
| | | } |
| | | int count = 0; |
| | | for (MesProPalletDO record : records) { |
| | | if (StrUtil.isNotBlank(record.getBusinessCode())) continue; |
| | | palletMapper.updateById(new MesProPalletDO().setId(record.getId()).setBusinessCode(codeRedisDao.generate())); |
| | | count++; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | @Override |
| | | public MesProPalletDO getPalletByCode(String code) { |
| | | return palletMapper.selectByCode(code); |
| | | MesProPalletDO pallet = palletMapper.selectByCode(code); |
| | | if (pallet == null) { |
| | | pallet = palletMapper.selectByTempCode(code); |
| | | } |
| | | return pallet; |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<MesProPalletDO> getUnboundPalletList() { |
| | | return palletMapper.selectUnboundList(); |
| | | } |
| | | |
| | | @Override |
| | | public List<MesProPalletDO> getPalletListByIds(Collection<Long> ids) { |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return List.of(); |
| | | } |
| | | return palletMapper.selectBatchIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | public void updatePalletBagCount(Long palletId, Integer bagCount) { |
| | | palletMapper.updateById(new MesProPalletDO().setId(palletId).setBagCount(bagCount)); |
| | | } |
| | | |
| | | @Override |
| | | public void markOutbound(Long id, Long salesId) { |
| | | palletMapper.markOutbound(id, salesId, java.time.LocalDateTime.now()); |
| | | } |
| | | |
| | | @Override |
| | | public void clearOutbound(Long id) { |
| | | palletMapper.clearOutbound(id); |
| | | } |
| | | |
| | | } |