From 9437600612eb0243a3371ff1e4fa3689cce8c83a Mon Sep 17 00:00:00 2001
From: dd <gitluke@outlook.com>
Date: Sat, 29 Nov 2025 22:40:41 +0800
Subject: [PATCH] 1

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/user/UserController.java |  190 ++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 170 insertions(+), 20 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/user/UserController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/user/UserController.java
index 165e02c..739d358 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/user/UserController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/user/UserController.java
@@ -13,16 +13,21 @@
 import com.ruoyi.im.dto.RegisterDto;
 import com.ruoyi.im.out.UserAccountOut;
 import com.ruoyi.im.service.ImApiServcie;
+import com.ruoyi.im.service.UserKycService;
+import com.ruoyi.im.service.UserPolicyService;
 import com.ruoyi.im.util.ConverterUtil;
 import com.ruoyi.im.util.PhoneNumberValidatorUtil;
 import com.ruoyi.im.util.SymmetricCryptoUtil;
 import com.ruoyi.system.domain.UserAccount;
+import com.ruoyi.system.domain.UserKyc;
+import com.ruoyi.system.domain.UserPolicy;
 import com.ruoyi.system.domain.vo.UserAccountUpdateVo;
 import com.ruoyi.system.domain.vo.UserAccountVo;
 import com.ruoyi.system.service.UserAccountService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.util.CollectionUtils;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
@@ -40,6 +45,12 @@
     @Autowired
     private ImApiServcie imApiServcie;
 
+    @Autowired
+    private UserKycService userKycService;
+
+    @Autowired
+    UserPolicyService userPolicyService;
+
     /**
      * 获取会员列表
      */
@@ -49,17 +60,36 @@
         // 创建查询条件包装器
         LambdaQueryWrapper<UserAccount> queryWrapper = new LambdaQueryWrapper<>();
 
-        // 只有当 keyword 不为空时才添加 OR 条件
+
+        LambdaQueryWrapper<UserPolicy> userPolicyWrapper = new LambdaQueryWrapper<>();
+        userPolicyWrapper
+                .eq(UserPolicy::getApprovalStatus, 1)
+                .eq(UserPolicy::getPayStatus,2)
+                .select(UserPolicy::getUserId);
+        List<UserPolicy> userPolicies = userPolicyService.list(userPolicyWrapper);
+        List<Integer> userIds = userPolicies.stream()
+                .map(UserPolicy::getUserId)
+                .collect(Collectors.toList());
+        if(null != vo.getActivate() && vo.getActivate()){
+            queryWrapper.in(UserAccount::getId,userIds);
+        }else if(null != vo.getActivate() && !vo.getActivate()){
+            queryWrapper.notIn(UserAccount::getId,userIds);
+        }
+
         if (ObjectUtil.isNotEmpty(vo.getKeywords())) {
-            queryWrapper.and(wrapper -> wrapper
-                    .eq(UserAccount::getId, vo.getKeywords())
-                    .or()
-                    .like(UserAccount::getPhoneNumber, vo.getKeywords())
-                    .or()
-                    .like(UserAccount::getAccount, vo.getKeywords())
-                    .or()
-                    .like(UserAccount::getNickname, vo.getKeywords())
-            );
+            String keywords = vo.getKeywords().trim();
+            if (StringUtils.isNotEmpty(keywords)) {
+                queryWrapper.and(wrapper -> wrapper
+                        .eq(UserAccount::getInvitationCode, keywords)
+                        .or()
+                        // 使用右模糊匹配,可以利用索引
+                        .likeRight(UserAccount::getPhoneNumber, keywords)
+                        .or()
+                        .likeRight(UserAccount::getAccount, keywords)
+                        .or()
+                        .likeRight(UserAccount::getNickname, keywords)
+                );
+            }
         }
 
         // 添加其他条件
@@ -74,6 +104,25 @@
         queryWrapper.orderByDesc(UserAccount::getCreateTime);
         startPage();
         List<UserAccount> list = userAccountService.list(queryWrapper);
+
+        List<Integer> idList = list.stream()
+                .map(UserAccount::getId)
+                .collect(Collectors.toList());
+        if(!CollectionUtils.isEmpty(idList)){
+            List<UserKyc> kycList = userKycService.list(new LambdaQueryWrapper<UserKyc>()
+                    .in(UserKyc::getUserId, idList)
+            );
+
+            Map<Integer, UserKyc> kycMap = kycList.stream()
+                    .collect(Collectors.toMap(UserKyc::getUserId, kyc -> kyc));
+
+            list.forEach(f->{
+                if(ObjectUtil.isNotEmpty(kycMap.get(f.getId()))){
+                    f.setName(kycMap.get(f.getId()).getName());
+                    f.setIdCard(kycMap.get(f.getId()).getIdCard());
+                }
+            });
+        }
 
         PageInfo<UserAccount> pageInfo = new PageInfo<>(list);
 
@@ -98,9 +147,34 @@
         if (ObjectUtil.isEmpty(userAccounts) || maxLevel <= 0) {
             return new ArrayList<>();
         }
+        // 收集用户ID用于查询激活状态
+        List<Integer> idList = userAccounts.stream()
+                .map(UserAccount::getId)
+                .filter(ObjectUtil::isNotEmpty)
+                .collect(Collectors.toList());
 
-        // 先转换基础信息
-        List<UserAccountOut> result = ConverterUtil.convertToList(userAccounts, UserAccountOut.class);
+        // 批量查询激活状态
+        Set<Integer> activatedUserIds = Collections.emptySet();
+        if (!idList.isEmpty()) {
+            LambdaQueryWrapper<UserPolicy> userPolicyWrapper = new LambdaQueryWrapper<>();
+            userPolicyWrapper.in(UserPolicy::getUserId, idList)
+                    .eq(UserPolicy::getApprovalStatus, 1)
+                    .select(UserPolicy::getUserId);
+
+            List<UserPolicy> policyList = userPolicyService.list(userPolicyWrapper);
+            activatedUserIds = policyList.stream()
+                    .map(UserPolicy::getUserId)
+                    .collect(Collectors.toSet());
+        }
+
+        // 转换基础信息并设置激活状态
+        List<UserAccountOut> result = new ArrayList<>();
+        for (UserAccount user : userAccounts) {
+            UserAccountOut out = ConverterUtil.convert(user, UserAccountOut.class);
+            // 设置激活状态
+            out.setActivate(activatedUserIds.contains(user.getId()));
+            result.add(out);
+        }
 
         // 递归加载下级用户
         loadSubordinateUsersRecursive(result, maxLevel);
@@ -129,10 +203,6 @@
         }
     }
 
-    /**
-     * 加载当前层级的直接下级用户
-     * @param userAccountOuts 当前层级的用户列表
-     */
     private void loadCurrentLevelSubordinates(List<UserAccountOut> userAccountOuts) {
         if (ObjectUtil.isEmpty(userAccountOuts)) {
             return;
@@ -146,7 +216,6 @@
                 .collect(Collectors.toList());
 
         if (accounts.isEmpty()) {
-            // 如果没有账号,为所有用户设置空列表
             userAccountOuts.forEach(user -> user.setSubordinateList(new ArrayList<>()));
             return;
         }
@@ -158,6 +227,18 @@
 
         // 转换为输出对象
         List<UserAccountOut> allSubordinateOuts = ConverterUtil.convertToList(allSubordinates, UserAccountOut.class);
+
+        // === 新增:为下级用户设置激活状态 ===
+        setActivateStatusForUsers(allSubordinateOuts);
+        // =================================
+
+        // 收集所有需要设置 KYC 的用户(当前层级 + 下级)
+        List<UserAccountOut> allUsers = new ArrayList<>();
+        allUsers.addAll(userAccountOuts);
+        allUsers.addAll(allSubordinateOuts);
+
+        // 一次性设置所有用户的 KYC 信息
+        setKycInfoForUsers(allUsers);
 
         // 按邀请人账号分组
         Map<String, List<UserAccountOut>> subordinateMap = allSubordinateOuts.stream()
@@ -175,7 +256,77 @@
         }
     }
 
+    /**
+     * 为用户列表设置激活状态
+     */
+    private void setActivateStatusForUsers(List<UserAccountOut> users) {
+        if (ObjectUtil.isEmpty(users)) {
+            return;
+        }
 
+        // 收集用户ID
+        List<Integer> idList = users.stream()
+                .map(UserAccountOut::getId)
+                .filter(ObjectUtil::isNotEmpty)
+                .collect(Collectors.toList());
+
+        if (idList.isEmpty()) {
+            return;
+        }
+
+        // 批量查询激活状态
+        LambdaQueryWrapper<UserPolicy> userPolicyWrapper = new LambdaQueryWrapper<>();
+        userPolicyWrapper.in(UserPolicy::getUserId, idList)
+                .eq(UserPolicy::getApprovalStatus, 1)
+                .select(UserPolicy::getUserId);
+
+        List<UserPolicy> policyList = userPolicyService.list(userPolicyWrapper);
+        Set<Integer> activatedUserIds = policyList.stream()
+                .map(UserPolicy::getUserId)
+                .collect(Collectors.toSet());
+
+        // 设置激活状态
+        users.forEach(user -> {
+            user.setActivate(activatedUserIds.contains(user.getId()));
+        });
+    }
+
+    /**
+     * 为用户列表设置 KYC 信息
+     */
+    private void setKycInfoForUsers(List<UserAccountOut> users) {
+        if (ObjectUtil.isEmpty(users)) {
+            return;
+        }
+
+        // 收集用户ID
+        List<Integer> idList = users.stream()
+                .map(UserAccountOut::getId)
+                .filter(ObjectUtil::isNotEmpty)
+                .collect(Collectors.toList());
+
+        if (idList.isEmpty()) {
+            return;
+        }
+
+        // 批量查询 KYC 信息
+        List<UserKyc> kycList = userKycService.list(new LambdaQueryWrapper<UserKyc>()
+                .in(UserKyc::getUserId, idList)
+        );
+
+        // 转换为 Map
+        Map<Integer, UserKyc> kycMap = kycList.stream()
+                .collect(Collectors.toMap(UserKyc::getUserId, kyc -> kyc, (existing, replacement) -> existing));
+
+        // 设置 KYC 信息
+        users.forEach(user -> {
+            if (ObjectUtil.isNotEmpty(user.getId()) && ObjectUtil.isNotEmpty(kycMap.get(user.getId()))) {
+                UserKyc kyc = kycMap.get(user.getId());
+                user.setName(kyc.getName());
+                user.setIdCard(kyc.getIdCard());
+            }
+        });
+    }
 
     /**
      * 获取用户的下级树形结构
@@ -224,7 +375,6 @@
             List<UserAccount> subordinates = userAccountService.list(queryWrapper);
 
             List<UserAccountOut> result = ConverterUtil.convertToList(subordinates, UserAccountOut.class);
-
             return AjaxResult.success(result);
         } catch (Exception e) {
             log.error("获取直接下级列表失败", e);
@@ -256,10 +406,10 @@
                     return AjaxResult.error("手机号格式不正确!");
                 }
             }
-            if(StringUtils.isNotEmpty(vo.getPassword()) && StringUtils.isEmpty(vo.getOldPassword())){
+            if(null == vo.getStatus() && StringUtils.isNotEmpty(vo.getPassword()) && StringUtils.isEmpty(vo.getOldPassword())){
                 return AjaxResult.error("旧密码不能为空!");
             }
-            if(StringUtils.isNotEmpty(vo.getPassword())){
+            if(null == vo.getStatus() && StringUtils.isNotEmpty(vo.getPassword())){
                 String s = SymmetricCryptoUtil.decryptPassword(userAccount.getPassword());
                 if(!vo.getOldPassword().equals(s)){
                     return AjaxResult.error("旧密码不正确!");

--
Gitblit v1.9.3