Browse Source

批量导入

lzzz 4 months ago
parent
commit
70be95e054

+ 48 - 4
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/UserService.java

@@ -190,10 +190,17 @@ public class UserService {
             userVO.setRole(parseRole(item.getRole()));
 
             try {
-                User savedUser = registerUserWithoutPhoneCode(userVO, defaultPassword, false);
-                detail.setUserId(savedUser.getId());
-                detail.setStatus("SUCCESS");
-                detail.setMessage("注册成功");
+                User matchedUser = resolveExistingUser(username, phone);
+                if (matchedUser != null) {
+                    detail.setUserId(matchedUser.getId());
+                    detail.setStatus("EXISTING");
+                    detail.setMessage("账号已存在,直接复用");
+                } else {
+                    User savedUser = registerUserWithoutPhoneCode(userVO, defaultPassword, false);
+                    detail.setUserId(savedUser.getId());
+                    detail.setStatus("SUCCESS");
+                    detail.setMessage("注册成功");
+                }
                 successCount++;
                 batchUsernames.add(username);
                 batchPhones.add(phone);
@@ -215,6 +222,38 @@ public class UserService {
         return result;
     }
 
+    private User resolveExistingUser(String username, String phone) {
+        User userByUsername = userRepository.findByUsername(username).orElse(null);
+        User userByPhone = userRepository.findByPhone(phone).orElse(null);
+
+        if (userByUsername == null && userByPhone == null) {
+            return null;
+        }
+
+        if (userByUsername != null && userByPhone != null
+                && !userByUsername.getId().equals(userByPhone.getId())) {
+            throw new CustomErrorException("学号与手机号分别对应不同门户账号,无法自动处理");
+        }
+
+        User matched = userByUsername != null ? userByUsername : userByPhone;
+
+        if (userByUsername != null && userByPhone == null
+                && matched.getPhone() != null
+                && !matched.getPhone().trim().isEmpty()
+                && !matched.getPhone().equals(phone)) {
+            throw new CustomErrorException("学号已存在但手机号不一致,无法自动处理");
+        }
+
+        if (userByPhone != null && userByUsername == null
+                && matched.getUsername() != null
+                && !matched.getUsername().trim().isEmpty()
+                && !matched.getUsername().equals(username)) {
+            throw new CustomErrorException("手机号已存在但学号不一致,无法自动处理");
+        }
+
+        return matched;
+    }
+
     private User registerUserWithoutPhoneCode(UserVO userVO, String password, boolean syncGitlab) {
         userVO.setUsername(userVO.getUsername());
         userRepository.findByPhone(userVO.getPhone()).ifPresent(user -> {
@@ -398,4 +437,9 @@ public class UserService {
         userRepository.save(user);
         return userMapper.userToUserVO(user);
     }
+
+    public UserVO findByPhoneInternal(String phone) {
+        User user = userRepository.findByPhone(phone).orElseThrow(() -> CustomErrorException.userPhoneNotFound(phone));
+        return userMapper.userToUserVO(user);
+    }
 }