hurx 1 개월 전
부모
커밋
648605ee74
1개의 변경된 파일25개의 추가작업 그리고 13개의 파일을 삭제
  1. 25 13
      ruoyi-modules/ruoyi-customer/src/main/java/org/dromara/customer/service/impl/CustomerInfoServiceImpl.java

+ 25 - 13
ruoyi-modules/ruoyi-customer/src/main/java/org/dromara/customer/service/impl/CustomerInfoServiceImpl.java

@@ -537,27 +537,39 @@ public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, Cus
                 return false;
             }
             String platform = PlatformContext.getPlatform();
+            // 1. 提前校验名称是否存在 (核心修改点)
+            String customerName = bo.getCustomerName();
+            if (customerName == null || customerName.trim().isEmpty()) {
+                throw new IllegalArgumentException("客户名称不能为空");
+            }
 
-            boolean isNew = bo.getId() == null || bo.getId() <= 0;
+            // 构造查询条件
+            LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper.eq(CustomerInfo::getCustomerName, customerName.trim());
 
-            // 1. 处理主表 CustomerInfo
-            CustomerInfo entity = MapstructUtils.convert(bo, CustomerInfo.class);
-            validEntityBeforeSave(entity);
+            // 执行查询
+            CustomerInfo existingCustomer = baseMapper.selectOne(queryWrapper);
 
-            boolean success;
+            boolean isNew = bo.getId() == null || bo.getId() <= 0;
+            CustomerInfo entity = null;
             if (isNew) {
-                success = baseMapper.insert(entity) > 0;
-                if (success) {
+                // --- 新增场景 ---
+                if (existingCustomer != null) {
+                    // 数据库中已存在同名用户
+                    throw new IllegalArgumentException("客户名称不能为空");
+                }
+                // 不存在,继续执行插入
+                entity = MapstructUtils.convert(bo, CustomerInfo.class);
+
+                int insertResult = baseMapper.insert(entity);
+                if (insertResult > 0) {
                     bo.setId(entity.getId()); // 回填ID
+                    return true;
+                } else {
+                    return false;
                 }
-            } else {
-                success = baseMapper.updateById(entity) > 0;
-            }
 
-            if (!success) {
-                return false;
             }
-
             Long customerId = entity.getId();
 
             /*新增客户时给每个客户创建自己的组织架构*/