From 825a575deb96cce822468bb9d9ac3e4eea0f91d1 Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期四, 17 九月 2026 08:58:14 +0800
Subject: [PATCH] update

---
 backend/src/main/java/com/tourguide/modules/user/service/GuideUserService.java |   42 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/backend/src/main/java/com/tourguide/modules/user/service/GuideUserService.java b/backend/src/main/java/com/tourguide/modules/user/service/GuideUserService.java
index 8b4039c..205c7d4 100644
--- a/backend/src/main/java/com/tourguide/modules/user/service/GuideUserService.java
+++ b/backend/src/main/java/com/tourguide/modules/user/service/GuideUserService.java
@@ -41,6 +41,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -128,6 +129,7 @@
     public GuideUser findByPhone(String phone) {
         return userMapper.selectOne(new LambdaQueryWrapper<GuideUser>()
                 .and(query -> query.eq(GuideUser::getUsername, phone).or().eq(GuideUser::getMobile, phone))
+                .orderByDesc(GuideUser::getId)
                 .last("LIMIT 1"));
     }
 
@@ -282,6 +284,10 @@
         }
         String username = application.getMobile().trim();
         GuideUser existingUser = findByPhone(username);
+        if (existingUser != null && existingUser.getStatus() == STATUS_CANCELLED) {
+            anonymizeCancelledGuide(existingUser);
+            existingUser = null;
+        }
         if (existingUser != null && (existingUser.getStatus() == null || existingUser.getStatus() != 2)) {
             throw new BusinessException(409, "该手机号已存在导游账号");
         }
@@ -305,6 +311,10 @@
     @Transactional
     public GuideUserResponse createPendingFromApplication(GuideApplication application) {
         GuideUser user = findByPhone(application.getMobile());
+        if (user != null && user.getStatus() == STATUS_CANCELLED) {
+            anonymizeCancelledGuide(user);
+            user = null;
+        }
         if (user == null) {
             user = new GuideUser();
             user.setUsername(application.getMobile().trim());
@@ -316,35 +326,51 @@
             throw new BusinessException(409, "该手机号已存在导游账号");
         }
         ensureUniqueGuideIdentity(application, user);
-        if (user.getStatus() == STATUS_CANCELLED) {
-            user.setStatus(STATUS_PENDING);
-            user.setAutoDisableWhitelisted(0);
-            user.setPremiumEnabled(0);
-            user.setAutoDisabled(0);
-            user.setDeleted(0);
-        }
         copyApplicationProfile(user, application);
         if (user.getId() == null) userMapper.insert(user); else userMapper.updateById(user);
         return GuideUserResponse.from(user);
     }
 
+    private void anonymizeCancelledGuide(GuideUser user) {
+        String deletedUsername = "deleted_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24);
+        userMapper.anonymizeCancelledGuide(user.getId(), deletedUsername);
+    }
+
     private void ensureUniqueGuideIdentity(GuideApplication application, GuideUser existingUser) {
+        if (StringUtils.hasText(application.getIdCardNo())) {
+            String idCardNo = application.getIdCardNo().trim().toUpperCase();
+            // 兼容旧注销数据:释放已注销账号占用的身份证唯一索引。
+            userMapper.clearCancelledIdentityByIdCard(idCardNo);
+            // 兼容旧驳回数据:释放待审核账号占用的身份证唯一索引,避免其阻塞重新注册。
+            userMapper.clearPendingIdentityByIdCard(idCardNo);
+        }
         if (StringUtils.hasText(application.getCertificateNo())
                 && userMapper.selectCount(new LambdaQueryWrapper<GuideUser>()
                 .eq(GuideUser::getCertificateNo, application.getCertificateNo())
+                .ne(GuideUser::getStatus, STATUS_CANCELLED)
                 .ne(existingUser != null && existingUser.getId() != null,
                         GuideUser::getId, existingUser == null ? null : existingUser.getId())) > 0) {
             throw new BusinessException(409, "该导游证号已存在导游账号");
         }
         if (StringUtils.hasText(application.getIdCardNo())
                 && userMapper.selectCount(new LambdaQueryWrapper<GuideUser>()
-                .eq(GuideUser::getIdCardNo, application.getIdCardNo())
+                .eq(GuideUser::getIdCardNo, application.getIdCardNo().trim().toUpperCase())
+                .ne(GuideUser::getStatus, STATUS_CANCELLED)
                 .ne(existingUser != null && existingUser.getId() != null,
                         GuideUser::getId, existingUser == null ? null : existingUser.getId())) > 0) {
             throw new BusinessException(409, "该身份证号已存在导游账号");
         }
     }
 
+    /** 申请被驳回后释放待审核账号占用的身份证/导游证唯一索引,避免其阻塞重新注册。 */
+    public void releaseIdentityForRejectedApplication(String mobile) {
+        if (!StringUtils.hasText(mobile)) return;
+        GuideUser user = findByPhone(mobile);
+        if (user != null && Integer.valueOf(STATUS_PENDING).equals(user.getStatus())) {
+            userMapper.releasePendingIdentity(user.getId());
+        }
+    }
+
     private void copyApplicationProfile(GuideUser user, GuideApplication application) {
         user.setRealName(application.getRealName());
         user.setMobile(application.getMobile());

--
Gitblit v1.7.1