From 704265308b1227dbada5a36b2027b588c062ded2 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期一, 12 十月 2026 09:14:37 +0800
Subject: [PATCH] feat(mes): 新增业务码支持并优化追溯查询功能

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/pallet/MesProPalletServiceImpl.java |   55 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/pallet/MesProPalletServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/pallet/MesProPalletServiceImpl.java
index fd5557a..ec1de18 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/pallet/MesProPalletServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/pallet/MesProPalletServiceImpl.java
@@ -51,6 +51,8 @@
 
     @Resource
     private MesCodeRedisDAO codeRedisDao;
+    @Resource
+    private cn.iocoder.yudao.module.mes.service.trace.integrity.MesTraceIntegrityService integrityService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -76,6 +78,8 @@
             int palletNo = startNo + i;
             pallets.add(new MesProPalletDO()
                     .setCode(codeRedisDao.generate())
+                    .setBusinessCode(codeRedisDao.generate())
+                    .setItemId(batch.getItemId())
                     .setBatchId(batch.getId())
                     .setBatchCode(batch.getCode())
                     .setPalletNo(palletNo)
@@ -96,12 +100,12 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public List<MesProPalletDO> preGeneratePallet(MesProPalletPreGenerateReqVO reqVO) {
-        // 1. 鐢熸垚涓存椂鎵樼洏鐮侊紙鍏ㄥ眬 8 浣嶅簭鍙凤紝鏍煎紡 PL-00000001锛�
-        int startSeq = palletMapper.selectMaxTempCodeSeq() + 1;
+        // 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())
@@ -142,6 +146,14 @@
             }
             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<>();
@@ -158,6 +170,7 @@
         }
         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);
             }
@@ -166,15 +179,17 @@
         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())
-                    .setCode(codeRedisDao.generate())
+                    .setItemId(itemId)
+                    .setCode(palletCode)
                     .setBatchId(batch.getId())
                     .setBatchCode(batch.getCode())
                     .setPalletNo(palletNo));
             // 鎵樼洏涓嬭鐮佺殑鍐椾綑鎵樼洏鐮佺敱涓存椂鐮佹洿鏂颁负姝e紡鐮�
             bagCodeMapper.updatePalletCodeByPalletId(pallets.get(i).getId(),
-                    batch.getCode() + "-P" + StrUtil.padPre(String.valueOf(palletNo), 2, '0'));
+                    palletCode);
         }
         // 5. 鎵樼洏涓嬭鐮佹壒閲忕粦瀹氭湰鎵规
         if (!bindBagIds.isEmpty() && bagCodeMapper.bindBatchByIds(bindBagIds, batch.getId(), batch.getCode(),
@@ -182,10 +197,11 @@
                         MesProBagCodeStatusEnum.PRINTED.getStatus())) != bindBagIds.size()) {
             throw exception(PRO_BAG_CODE_STATUS_NOT_GENERATED);
         }
-        // 6. 鍥炲啓鎵规鎵樼洏鐮佹暟閲忎笌鏈夋晥琚嬬爜鏁伴噺
-        batchMapper.updateById(new MesProBatchDO().setId(batch.getId())
+        // 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();
     }
 
@@ -214,6 +230,33 @@
     }
 
     @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) {
         MesProPalletDO pallet = palletMapper.selectByCode(code);
         if (pallet == null) {

--
Gitblit v1.9.3