Bläddra i källkod

feat(customer): 实现客户编号自动生成机制

- 移除DuplicateKeyException导入依赖
- 添加基于LambdaQueryWrapper的最大客户编号查询逻辑
- 实现客户编号自动递增生成功能
- 移除原有的重试机制相关代码
- 统一两个插入方法中的客户编号生成逻辑
hurx 6 dagar sedan
förälder
incheckning
ff34e8a93a

+ 21 - 3
ruoyi-modules/ruoyi-customer/src/main/java/org/dromara/customer/service/impl/CustomerInfoServiceImpl.java

@@ -34,7 +34,6 @@ import org.dromara.customer.utils.qcc.domain.CompanyInfoResponse;
 import org.dromara.system.api.*;
 import org.dromara.system.api.domain.bo.RemoteUserBo;
 import org.dromara.system.api.domain.vo.RemoteDeptVo;
-import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -570,6 +569,15 @@ public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, Cus
                 // 不存在,继续执行插入
                 entity = MapstructUtils.convert(bo, CustomerInfo.class);
 
+                LambdaQueryWrapper<CustomerInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+                // 按CustomerNo降序排序,取第一条(最大值)
+                lambdaQueryWrapper.select(CustomerInfo::getCustomerNo)
+                    .orderByDesc(CustomerInfo::getCustomerNo)
+                    .last("LIMIT 1");
+                CustomerInfo maxCustomer = baseMapper.selectOne(lambdaQueryWrapper);
+                long l = Long.parseLong(maxCustomer.getCustomerNo()) + 1;
+                entity.setCustomerNo(String.valueOf(l));
+
                 int insertResult = baseMapper.insert(entity);
                 if (insertResult > 0) {
                     bo.setId(entity.getId()); // 回填ID
@@ -979,7 +987,17 @@ public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, Cus
         customerEntity.setStatus("0"); // 正常状态
         customerEntity.setDelFlag("0"); // 未删除
         boolean insertSuccess = false;
-        int maxRetries = 3;
+
+        LambdaQueryWrapper<CustomerInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+        // 按CustomerNo降序排序,取第一条(最大值)
+        lambdaQueryWrapper.select(CustomerInfo::getCustomerNo)
+            .orderByDesc(CustomerInfo::getCustomerNo)
+            .last("LIMIT 1");
+        CustomerInfo maxCustomer = baseMapper.selectOne(lambdaQueryWrapper);
+        long l = Long.parseLong(maxCustomer.getCustomerNo()) + 1;
+        customerEntity.setCustomerNo(String.valueOf(l));
+
+       /* int maxRetries = 3;
 
         // 1. 执行带重试的客户插入逻辑
         for (int i = 0; i < maxRetries; i++) {
@@ -1004,7 +1022,7 @@ public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, Cus
                 log.warn("客户编号 {} 冲突,正在重试第 {} 次", customerEntity.getCustomerNo(), i + 1);
                 // 循环继续,下一次迭代会重新生成 customerNo
             }
-        }
+        }*/
 
         if (!insertSuccess) {
             // 理论上如果没抛异常且没成功,应该到这里,作为兜底