Jelajahi Sumber

优化用户搜索接口

fangqing 3 hari lalu
induk
melakukan
c585a2f0a0

+ 23 - 34
ruoyi-generator/src/main/resources/mapper/community/CommunityUserInfoMapper.xml

@@ -20,42 +20,31 @@
                 or b.user_id like concat('%', #{searchValue}, '%')
             </where>
         </if>
+        ORDER BY a.user_id ASC
         LIMIT #{offset}, #{limit}
     </select>
     <select id="selectUsersByAccountOrName" resultType="com.ruoyi.generator.vo.CommunityUserInfoVo">
-        select * from (
-        select
-        b.id as id,
-        a.user_id as userId,
-        a.nick_name as nickName,
-        a.avatar as avatar,
-        (select count(1) as fansCount from community_user_like c where c.like_user_id = a.user_id) as fansCount,
-        (select count(1) as articleCount from community_article d where d.user_id = a.user_id) as articleCount
-        from sys_user a
-        left join community_user_info b on a.user_id = b.user_id
-        left join community_user_privacy c on a.user_id  = c.user_id
-        <if test="searchValue != null and searchValue != ''">
-            <where>
-                IFNULL(c.is_accounts,0)= 0 and a.user_name like concat('%', #{searchValue}, '%')
-            </where>
-        </if>
-        union all
-        select
-        b.id as id,
-        a.user_id as userId,
-        a.nick_name as nickName,
-        a.avatar as avatar,
-        (select count(1) as fansCount from community_user_like c where c.like_user_id = a.user_id) as fansCount,
-        (select count(1) as articleCount from community_article d where d.user_id = a.user_id) as articleCount
-        from sys_user a
-        left join community_user_info b on a.user_id = b.user_id
-        left join community_user_privacy c on a.user_id  = c.user_id
-        <if test="searchValue != null and searchValue != ''">
-            <where>
-                IFNULL(c.is_names,0)  = 0 and  a.nick_name like concat('%', #{searchValue}, '%')
-            </where>
-        </if>
-        )  as a
+        SELECT
+        b.id AS id,
+        a.user_id AS userId,
+        a.nick_name AS nickName,
+        a.avatar AS avatar,
+        COUNT(DISTINCT CASE WHEN like_user_id IS NOT NULL THEN like_user_id END) AS fansCount,
+        COUNT(DISTINCT d.user_id) AS articleCount
+        FROM sys_user a
+        LEFT JOIN community_user_info b ON a.user_id = b.user_id
+        LEFT JOIN community_user_privacy c ON a.user_id = c.user_id
+        LEFT JOIN community_user_like c2 ON c2.like_user_id = a.user_id
+        LEFT JOIN community_article d ON d.user_id = a.user_id
+        <where>
+            <if test="searchValue != null and searchValue != ''">
+                (IFNULL(c.is_accounts,0) = 0 AND a.user_name LIKE CONCAT('%', #{searchValue}, '%'))
+                OR
+                (IFNULL(c.is_names, 0) = 0 AND a.nick_name LIKE CONCAT('%', #{searchValue}, '%'))
+            </if>
+        </where>
+        GROUP BY b.id, a.user_id, a.nick_name, a.avatar
+        ORDER BY a.user_id ASC
         LIMIT #{offset}, #{limit}
     </select>
     <select id="selectBlockUserBySearchValue" resultType="com.ruoyi.generator.vo.CommunityUserInfoVo">
@@ -80,7 +69,7 @@
             OR b.user_id LIKE CONCAT('%', #{searchValue}, '%')
             )
         </if>
-        ORDER BY a.user_id  <!-- You can choose the proper field for ordering -->
+        ORDER BY a.user_id ASC
         LIMIT #{offset}, #{limit}
     </select>
 </mapper>