From 87733eb3623593da247c051593d64b8ff9e1d3a4 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期五, 06 十一月 2026 19:00:39 +0800
Subject: [PATCH] feat(crm,mes): 扩展客户字段与重构批次号生成逻辑

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/batch/MesProBatchServiceImpl.java |   83 +++++++++++++++++++++++++++++------------
 1 files changed, 59 insertions(+), 24 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 084b5a0..43946d8 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
@@ -20,9 +20,7 @@
 import cn.iocoder.yudao.module.mes.dal.mysql.pro.batch.MesProBatchWorkerMapper;
 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.md.autocode.MesMdAutoCodeRuleCodeEnum;
 import cn.iocoder.yudao.module.mes.enums.pro.MesProBatchStatusEnum;
-import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService;
 import cn.iocoder.yudao.module.mes.service.pro.line.MesProLineService;
 import cn.iocoder.yudao.module.mes.service.pro.bagcode.MesProBagCodeService;
 import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService;
@@ -35,12 +33,14 @@
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 
+import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@@ -55,6 +55,12 @@
 @Validated
 @Slf4j
 public class MesProBatchServiceImpl implements MesProBatchService {
+
+    /**
+     * 鎵规鍙锋牸寮忥細4 浣嶅勾浠� + 3 浣嶆祦姘达紙濡� 2026001锛�
+     */
+    private static final Pattern BATCH_CODE_PATTERN = Pattern.compile("^[0-9]{7}$");
+    private static final int BATCH_CODE_MAX_SERIAL = 999;
 
     @Resource
     private MesProBatchMapper batchMapper;
@@ -78,8 +84,6 @@
     private MesCodeRedisDAO codeRedisDao;
 
     @Resource
-    private MesMdAutoCodeRecordService autoCodeRecordService;
-    @Resource
     private AdminUserApi adminUserApi;
     @Resource
     private MesProLineService lineService;
@@ -94,8 +98,8 @@
         validateRelations(createReqVO);
         createReqVO.setItemId(resolveBindingItem(null, createReqVO));
 
-        // 2. 鐢熸垚鎵规鍙凤紙缂栫爜瑙勫垯锛氬墠缂� + 鏃ユ湡 + 浜х嚎缂栫爜 + 娴佹按鍙凤級
-        String code = generateBatchCode(line.getCode());
+        // 2. 纭畾鎵规鍙凤紙浜哄伐杈撳叆浼樺厛锛屾湭杈撳叆鍒欐寜銆屽勾浠�+3浣嶆祦姘淬�嶈嚜鍔ㄧ敓鎴愶級
+        String code = resolveBatchCode(createReqVO.getCode(), null);
 
         // 3. 鎻掑叆鎵规锛堜骇绾跨紪鐮�/鍚嶇О鍐椾綑鍥炲~锛涘嬀閫夌粦瀹氭墭鐩樻椂鎵规鐩存帴杩涘叆鐢熶骇涓級
         boolean hasBindings = hasBindings(createReqVO);
@@ -152,11 +156,11 @@
         if (StrUtil.isNotEmpty(batch.getCode())) {
             throw exception(PRO_BATCH_ALREADY_FILLED);
         }
-        // 2. 鏍¢獙鍏宠仈鏁版嵁骞剁敓鎴愭寮忔壒娆″彿
+        // 2. 鏍¢獙鍏宠仈鏁版嵁骞剁‘瀹氭寮忔壒娆″彿锛堜汉宸ヨ緭鍏ヤ紭鍏堬紝鏈緭鍏ュ垯鑷姩鐢熸垚锛�
         MesProLineDO line = lineService.validateLineExistsAndEnable(fillReqVO.getLineId());
         validateRelations(fillReqVO);
         fillReqVO.setItemId(resolveBindingItem(batch, fillReqVO));
-        String code = generateBatchCode(line.getCode());
+        String code = resolveBatchCode(fillReqVO.getCode(), null);
         // 3. 钀藉簱涓氬姟淇℃伅锛堜繚鐣欎复鏃舵壒娆$爜鐢ㄤ簬鍘嗗彶鎵爜杩芥函锛涗骇绾跨紪鐮�/鍚嶇О鍐椾綑鍥炲~锛涘嬀閫夌粦瀹氭墭鐩樻椂鎵规鐩存帴杩涘叆鐢熶骇涓級
         boolean hasBindings = hasBindings(fillReqVO);
         MesProBatchDO updateObj = BeanUtils.toBean(fillReqVO, MesProBatchDO.class)
@@ -187,17 +191,14 @@
         validateRelations(updateReqVO);
         updateReqVO.setItemId(resolveBindingItem(oldBatch, updateReqVO));
 
-        // 3. 鏈敓鎴愭寮忔壒娆″彿鎴栦骇绾垮彉鍖栨椂閲嶆柊鐢熸垚鎵规鍙凤紙鎵规鍙峰寘鍚骇绾跨紪鐮佽绱狅級
+        // 3. 鎵规鍙蜂粎鍚勾浠戒笌娴佹按鍙凤紝鐢熸垚鍚庝笉鍐嶉殢浜х嚎鍙樺寲锛涙湭鐢熸垚姝e紡鎵规鍙锋椂琛ョ敓鎴�
         boolean codeEmpty = StrUtil.isEmpty(oldBatch.getCode());
         boolean lineChanged = !Objects.equals(oldBatch.getLineId(), updateReqVO.getLineId());
-        String code = oldBatch.getCode();
+        String code = codeEmpty ? resolveBatchCode(updateReqVO.getCode(), oldBatch.getId()) : oldBatch.getCode();
         MesProBatchDO updateObj = BeanUtils.toBean(updateReqVO, MesProBatchDO.class);
-        if (codeEmpty || lineChanged) {
-            // 鍙樻洿浜х嚎鏃讹紝鐩爣浜х嚎蹇呴』鍚敤锛涙湭鍙樻洿鍒欎粎鏍¢獙瀛樺湪锛堥伩鍏嶅仠鐢ㄤ骇绾跨殑鍘嗗彶鎵规鏃犳硶缂栬緫鍏朵粬瀛楁锛�
-            MesProLineDO line = (lineChanged || codeEmpty)
-                    ? lineService.validateLineExistsAndEnable(updateReqVO.getLineId())
-                    : lineService.validateLineExists(updateReqVO.getLineId());
-            code = generateBatchCode(line.getCode());
+        if (lineChanged || codeEmpty) {
+            // 鍙樻洿浜х嚎鏃讹紝鐩爣浜х嚎蹇呴』鍚敤
+            MesProLineDO line = lineService.validateLineExistsAndEnable(updateReqVO.getLineId());
             updateObj.setLineCode(line.getCode()).setLineName(line.getName());
         }
         // 4. 鏇存柊鎵规锛堝嬀閫夌粦瀹氭墭鐩樻椂鎵规鐩存帴杩涘叆鐢熶骇涓紱鍙栨秷鍕鹃�変笉鍥為��锛屽凡缁戝畾鎵樼洏涓嶅彲瑙g粦锛�
@@ -448,16 +449,50 @@
     }
 
     /**
-     * 鐢熸垚鎵规鍙�
-     * <p>
-     * 缂栫爜瑙勫垯锛氬浐瀹氬墠缂�(PC) + 鏃ユ湡(yyyyMMdd) + 杈撳叆瀛楃(浜х嚎缂栫爜) + 娴佹按鍙�(鎸夋棩寰幆)
+     * 纭畾鎵规鍙凤細浜哄伐杈撳叆浼樺厛锛屾湭杈撳叆鍒欐寜銆屽綋鍓嶅勾浠� + 3 浣嶆祦姘淬�嶈嚜鍔ㄧ敓鎴�
      *
-     * @param lineCode 浜х嚎缂栫爜
-     * @return 鎵规鍙�
+     * @param requestedCode 浜哄伐杈撳叆鐨勬壒娆″彿锛屽彲涓虹┖
+     * @param selfId        褰撳墠鎵规缂栧彿锛堟洿鏂版椂鎺掗櫎鑷韩锛屾柊澧炰紶 null锛�
+     * @return 鏈�缁堟壒娆″彿
      */
-    private String generateBatchCode(String lineCode) {
-        String inputChar = StrUtil.emptyToDefault(lineCode, "");
-        return autoCodeRecordService.generateAutoCode(MesMdAutoCodeRuleCodeEnum.PRO_BATCH_CODE.getCode(), inputChar);
+    private String resolveBatchCode(String requestedCode, Long selfId) {
+        if (StrUtil.isBlank(requestedCode)) {
+            return generateNextBatchCode();
+        }
+        String code = StrUtil.trim(requestedCode);
+        validateBatchCode(code, selfId);
+        return code;
+    }
+
+    /**
+     * 鏍¢獙鎵规鍙锋牸寮忥紙4 浣嶅勾浠� + 3 浣嶆祦姘达紝濡� 2026001锛夊強鍞竴鎬�
+     */
+    private void validateBatchCode(String code, Long selfId) {
+        if (!BATCH_CODE_PATTERN.matcher(code).matches()) {
+            throw exception(PRO_BATCH_CODE_INVALID);
+        }
+        int year = Integer.parseInt(code.substring(0, 4));
+        int serial = Integer.parseInt(code.substring(4));
+        if (year < 2000 || year > 2100 || serial < 1) {
+            throw exception(PRO_BATCH_CODE_INVALID);
+        }
+        MesProBatchDO existing = batchMapper.selectByCodeIgnoreDeleted(code);
+        if (existing != null && !Objects.equals(existing.getId(), selfId)) {
+            throw exception(PRO_BATCH_CODE_DUPLICATE, code);
+        }
+    }
+
+    /**
+     * 鐢熸垚褰撳墠骞翠唤鐨勪笅涓�涓壒娆″彿
+     */
+    private String generateNextBatchCode() {
+        String year = String.valueOf(LocalDate.now().getYear());
+        Integer maxSerial = batchMapper.selectMaxCodeSerialByYear(year);
+        int serial = codeRedisDao.allocateBatchNo(year, maxSerial);
+        if (serial > BATCH_CODE_MAX_SERIAL) {
+            throw exception(PRO_BATCH_CODE_EXHAUSTED);
+        }
+        return year + String.format("%03d", serial);
     }
 
     /**

--
Gitblit v1.9.3