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/batch/MesProBatchServiceImpl.java |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/batch/MesProBatchServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/batch/MesProBatchServiceImpl.java
index a5d4fbe..bae3140 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/batch/MesProBatchServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/batch/MesProBatchServiceImpl.java
@@ -80,6 +80,7 @@
         // 1. 鏍¢獙鍏宠仈鏁版嵁锛堜骇绾裤�佺敓浜т汉鍛橈級
         MesProLineDO line = lineService.validateLineExistsAndEnable(createReqVO.getLineId());
         validateRelations(createReqVO);
+        createReqVO.setItemId(resolveBindingItem(null, createReqVO));
 
         // 2. 鐢熸垚鎵规鍙凤紙缂栫爜瑙勫垯锛氬墠缂� + 鏃ユ湡 + 浜х嚎缂栫爜 + 娴佹按鍙凤級
         String code = generateBatchCode(line.getCode());
@@ -88,6 +89,7 @@
         boolean bindPallets = CollUtil.isNotEmpty(createReqVO.getPalletIds());
         MesProBatchDO batch = BeanUtils.toBean(createReqVO, MesProBatchDO.class)
                 .setCode(code)
+                .setBusinessCode(codeRedisDao.generate())
                 .setLineCode(line.getCode())
                 .setLineName(line.getName())
                 .setBagQuantity(0)
@@ -108,12 +110,12 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public List<MesProBatchDO> preGenerateBatch(MesProBatchPreGenerateReqVO reqVO) {
-        // 1. 鐢熸垚涓存椂鎵规鐮侊紙鍏ㄥ眬 8 浣嶅簭鍙凤紝鏍煎紡 PB-00000001锛�
-        int startSeq = batchMapper.selectMaxTempCodeSeq() + 1;
+        // 1. 閫氳繃缁熶竴缂栫爜鐢熸垚鍣ㄥ垎閰嶄复鏃剁爜鍙婁笟鍔$爜锛屼笉鍐嶈В鏋愬巻鍙蹭复鏃剁爜搴忓彿銆�
         List<MesProBatchDO> list = new ArrayList<>(reqVO.getCount());
         for (int i = 0; i < reqVO.getCount(); i++) {
             list.add(new MesProBatchDO()
                     .setTempCode(codeRedisDao.generate())
+                    .setBusinessCode(codeRedisDao.generate())
                     .setBagQuantity(0)
                     .setPalletQuantity(0)
                     .setStatus(MesProBatchStatusEnum.DRAFT.getStatus())
@@ -140,6 +142,7 @@
         // 2. 鏍¢獙鍏宠仈鏁版嵁骞剁敓鎴愭寮忔壒娆″彿
         MesProLineDO line = lineService.validateLineExistsAndEnable(fillReqVO.getLineId());
         validateRelations(fillReqVO);
+        fillReqVO.setItemId(resolveBindingItem(batch, fillReqVO));
         String code = generateBatchCode(line.getCode());
         // 3. 钀藉簱涓氬姟淇℃伅锛堜繚鐣欎复鏃舵壒娆$爜鐢ㄤ簬鍘嗗彶鎵爜杩芥函锛涗骇绾跨紪鐮�/鍚嶇О鍐椾綑鍥炲~锛涘嬀閫夌粦瀹氭墭鐩樻椂鎵规鐩存帴杩涘叆鐢熶骇涓級
         boolean bindPallets = CollUtil.isNotEmpty(fillReqVO.getPalletIds());
@@ -163,10 +166,12 @@
     @Transactional(rollbackFor = Exception.class)
     public void updateBatch(MesProBatchSaveReqVO updateReqVO) {
         // 1. 鏍¢獙瀛樺湪 + 鐘舵�侊紙鑽夌/鐢熶骇涓墠鍏佽淇敼锛�
-        MesProBatchDO oldBatch = validateBatchExists(updateReqVO.getId());
+        MesProBatchDO oldBatch = batchMapper.selectByIdForUpdate(updateReqVO.getId());
+        if (oldBatch == null) throw exception(PRO_BATCH_NOT_EXISTS);
         validateBatchEditable(oldBatch);
         // 2. 鏍¢獙鍏宠仈鏁版嵁锛堢敓浜т汉鍛橈級
         validateRelations(updateReqVO);
+        updateReqVO.setItemId(resolveBindingItem(oldBatch, updateReqVO));
 
         // 3. 鏈敓鎴愭寮忔壒娆″彿鎴栦骇绾垮彉鍖栨椂閲嶆柊鐢熸垚鎵规鍙凤紙鎵规鍙峰寘鍚骇绾跨紪鐮佽绱狅級
         boolean codeEmpty = StrUtil.isEmpty(oldBatch.getCode());
@@ -250,6 +255,33 @@
     }
 
     @Override
+    public MesProBatchDO getBatchByScanCode(String code) {
+        return batchMapper.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<MesProBatchDO> records = new java.util.ArrayList<>();
+        for (Long id : ids.stream().distinct().sorted().toList()) {
+            MesProBatchDO record = batchMapper.selectByIdForUpdate(id);
+            if (record == null) throw exception(PRO_BATCH_NOT_EXISTS);
+            if (MesProBatchStatusEnum.CANCELED.getStatus().equals(record.getStatus()))
+                throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "宸蹭綔搴熻褰曚笉鍙ˉ鐢熸垚涓氬姟鐮�");
+            records.add(record);
+        }
+        int count = 0;
+        for (MesProBatchDO record : records) {
+            if (StrUtil.isNotBlank(record.getBusinessCode())) continue;
+            batchMapper.updateById(new MesProBatchDO().setId(record.getId()).setBusinessCode(codeRedisDao.generate()));
+            count++;
+        }
+        return count;
+    }
+
+    @Override
     public MesProBatchDO getBatchByCode(String code) {
         MesProBatchDO batch = batchMapper.selectByCode(code);
         if (batch == null) {
@@ -311,6 +343,28 @@
      * 鎵规缁戝畾鎵樼洏锛堝彲閫夛級锛氬悓浜嬪姟鍐呭鐢ㄦ墭鐩樼粦瀹氭壒娆¢�昏緫锛�
      * 鐢熸垚姝e紡鎵樼洏鐮佸苟绾ц仈褰曞叆鎵樼洏涓嬫湭缁戞壒娆$殑琚嬬爜锛屽け璐ユ暣浣撳洖婊�
      */
+    private Long resolveBindingItem(MesProBatchDO batch, MesProBatchSaveReqVO req) {
+        Long itemId = batch == null ? null : batch.getItemId();
+        if (batch != null) {
+            for (Long existing : bagCodeMapper.selectDistinctItemIdsByBatchId(batch.getId()))
+                itemId = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.merge(itemId, existing);
+            for (var pallet : palletMapper.selectListByBatchId(batch.getId()))
+                itemId = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.merge(itemId,
+                        cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.resolve(pallet, bagCodeMapper.selectListByPalletId(pallet.getId())));
+        }
+        if (CollUtil.isNotEmpty(req.getPalletIds())) for (Long id : req.getPalletIds().stream().distinct().sorted().toList()) {
+            var pallet = palletMapper.selectByIdForUpdate(id);
+            if (pallet == null) throw exception(PRO_PALLET_NOT_EXISTS);
+            Long candidate = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.resolve(pallet, bagCodeMapper.selectListByPalletId(id));
+            if (candidate == null) throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "鎵樼洏缂哄皯鍝佺锛岃鍏堢粦瀹氬悓鍝佺琚嬬爜");
+            itemId = cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletItemResolver.merge(itemId, candidate);
+        }
+        // 璇锋眰閲岀殑鍝佺鍙兘涓庡凡缁戝畾/寰呯粦瀹氭墭鐩樹竴鑷达紝涓嶈兘閫氳繃缂栬緫鏀瑰彉鎵规鍝佺銆�
+        if (req.getItemId() != null && !Objects.equals(req.getItemId(), itemId))
+            throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "鍝佺鐢辩粦瀹氭墭鐩樺甫鍏ワ紝涓嶅彲淇敼涓哄叾浠栧搧绉�");
+        return itemId;
+    }
+
     private void bindPalletsToBatch(Long batchId, List<Long> palletIds) {
         if (CollUtil.isEmpty(palletIds)) {
             return;

--
Gitblit v1.9.3