ソースを参照

个人中心收到多少评论 + 评论占比

fangqing 5 ヶ月 前
コミット
c1122d1ff0

+ 18 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/community/CommunityArticleCommentMapper.java

@@ -2,9 +2,12 @@ package com.ruoyi.generator.mapper.community;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.ruoyi.generator.domain.Community.CommunityArticleComment;
+import io.swagger.models.auth.In;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 @Mapper
 public interface CommunityArticleCommentMapper extends BaseMapper<CommunityArticleComment> {
 
@@ -14,4 +17,19 @@ public interface CommunityArticleCommentMapper extends BaseMapper<CommunityArtic
      * @return 总数
      */
     Integer queryCommentCount(@Param("articleId") Long articleId);
+
+    /**
+     * 获取用户具体评论文章的数量
+     * @param articleIds
+     * @return
+     */
+    Integer queryCommentReplyCount (@Param("articleIds")  List<Long> articleIds);
+
+    /**
+     * 获取数据
+     * @param commentSum
+     * @return
+     */
+    Integer queryCommentReplyCountPerson (@Param("commentSum")  int commentSum);
+
 }

+ 97 - 40
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityAccompanyImpl.java

@@ -2,10 +2,10 @@ package com.ruoyi.generator.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.ruoyi.common.core.domain.entity.SysUser;
-import com.ruoyi.generator.domain.Community.CommunityAccompany;
-import com.ruoyi.generator.domain.Community.CommunityArticle;
-import com.ruoyi.generator.domain.Community.CommunityUserLike;
+import com.ruoyi.generator.domain.Community.*;
+import com.ruoyi.generator.mapper.community.CommunityArticleCommentMapper;
 import com.ruoyi.generator.mapper.community.CommunityArticleMapper;
+import com.ruoyi.generator.mapper.community.CommunityCommentReplyMapper;
 import com.ruoyi.generator.mapper.community.CommunityUserLikeMapper;
 import com.ruoyi.generator.vo.CommunityUserLikeVo;
 import com.ruoyi.system.mapper.SysUserMapper;
@@ -20,6 +20,7 @@ import java.time.temporal.Temporal;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author fangqing
@@ -38,6 +39,12 @@ public class CommunityAccompanyImpl implements ICommunityAccompanyService {
     @Autowired
     private CommunityUserLikeMapper communityUserLikeMapper;
 
+    @Autowired
+    private CommunityArticleCommentMapper communityArticleCommentMapper;
+
+    @Autowired
+    private CommunityCommentReplyMapper communityCommentReplyMapper;
+
     @Override
     public CommunityAccompany selectCommunityAccompanyById(Long userId) {
         DecimalFormat df = new DecimalFormat("#.00");
@@ -70,36 +77,49 @@ public class CommunityAccompanyImpl implements ICommunityAccompanyService {
             //设置用户创作数
             communityAccompany.setCreationCount((long) communityArticles.size());
 
-            //查询当前用户下文章的数量
-            List<CommunityArticle> communityArticlesTotal = communityArticleMapper.selectList(new QueryWrapper<CommunityArticle>()
+            //计算小于自己粉丝的有多少人
+            List<CommunityArticle> communityArticlesLess = new ArrayList<>(communityArticleMapper.selectList(new QueryWrapper<CommunityArticle>()
+                    .select("user_id", "COUNT(1) as count")
+                    .and((wrapper) -> {
+                        wrapper.ne("is_delete", true).or().isNull("is_delete");
+                    })
+                    .groupBy("user_id")
+                    .having("COUNT(*) <= {0}", (long) communityArticles.size())
+                    .orderByDesc("count"))) ;
+
+            //计算所有文章的数量
+            List<CommunityArticle> communityArticlesTotal = new ArrayList<>(communityArticleMapper.selectList(new QueryWrapper<CommunityArticle>()
+                    .select("user_id", "COUNT(1) as count")
                     .and((wrapper) -> {
                         wrapper.ne("is_delete", true).or().isNull("is_delete");
-                    }));
-            //获取文章总数
-            long articles = communityArticlesTotal.size();
-            //计算百分比
-            //计算创作数的百分比 防止总数取0
-            if (articles != 0) {
-                Double CreationCountPercent = Double.valueOf(df.format((double) communityArticles.size() / articles * 100));
-                communityAccompany.setCreationCountPercent(CreationCountPercent);
-            }
-
-            //获取用户阅览量
+                    })
+                    .groupBy("user_id")
+                    .orderByDesc("count")));
+
+            //计算创作数的百分比 计算百分比
+            Double CreationCountPercent = Double.valueOf(df.format((double) communityArticlesLess.size() / communityArticlesTotal.size() * 100));
+            communityAccompany.setCreationCountPercent(CreationCountPercent);
+
+
+            //计算用户阅览量
             long read = communityArticles.stream()
                     .mapToLong(article -> article.getPageViews()).sum();
+            communityAccompany.setRead(read);
 
-            if (read != 0  ){
-                communityAccompany.setRead(read);
-            }
 
-            //阅览量百分比
-            long readSUm = communityArticlesTotal.stream()
-                    .mapToLong(article -> article.getPageViews()).sum();
+            //计算阅览量百分比
+            List<CommunityArticle> communityArticlesReadSumLess = new ArrayList<>(communityArticleMapper.selectList(new QueryWrapper<CommunityArticle>()
+                    .select("user_id","sum(page_views) page_views","COUNT(1) as count")
+                    .and((wrapper) -> {
+                        wrapper.ne("is_delete", true).or().isNull("is_delete");
+                    })
+                    .groupBy("user_id")
+                    .having("page_views <= {0}", read )
+                    .orderByDesc("count"))) ;
+
+            Double ReadPercent = Double.valueOf(df.format((double)  communityArticlesReadSumLess.size() / communityArticlesTotal.size() * 100));
+            communityAccompany.setReadPercent(ReadPercent);
 
-            if (readSUm != 0){
-                Double ReadPercent = Double.valueOf(df.format((double)  read / readSUm * 100));
-                communityAccompany.setReadPercent(ReadPercent);
-            }
         }
 
 
@@ -107,23 +127,60 @@ public class CommunityAccompanyImpl implements ICommunityAccompanyService {
         Long fans = communityUserLikeMapper.selectCount(new QueryWrapper<CommunityUserLike>()
                 .eq("like_user_id", userId));
 
-        if (fans != 0 ){
-            communityAccompany.setFan(fans);
-            //粉丝数占比
-            List<CommunityUserLike> userLikeFan = new ArrayList<>(communityUserLikeMapper.selectList(new QueryWrapper<CommunityUserLike>()
-                    .select("like_user_id", "COUNT(1) as count")
-                    .groupBy("like_user_id")
-                    .having("COUNT(*) <= {0}", fans)
-                    .orderByDesc("count"))) ;
 
-            //计算总人数
-            List<CommunityUserLike> userLikeFans = new ArrayList<>(communityUserLikeMapper.selectList(new QueryWrapper<CommunityUserLike>()
-                    .select("like_user_id", "COUNT(1) as count")
-                    .groupBy("like_user_id")));
-            Double  ReadPercent = Double.valueOf(df.format((double) userLikeFan.size() / userLikeFans.size() * 100));
-            communityAccompany.setFanPercent(ReadPercent);
+        communityAccompany.setFan(fans);
+        //计算小于自己粉丝的有多少人
+        List<CommunityUserLike> userLikeFan = new ArrayList<>(communityUserLikeMapper.selectList(new QueryWrapper<CommunityUserLike>()
+                .select("like_user_id", "COUNT(1) as count")
+                .groupBy("like_user_id")
+                .having("COUNT(*) <= {0}", fans)
+                .orderByDesc("count"))) ;
+
+        //计算总人数
+        List<CommunityUserLike> userLikeFans = new ArrayList<>(communityUserLikeMapper.selectList(new QueryWrapper<CommunityUserLike>()
+                .select("like_user_id", "COUNT(1) as count")
+                .groupBy("like_user_id")));
+        //粉丝数占比
+        Double  ReadPercent = Double.valueOf(df.format((double) userLikeFan.size() / userLikeFans.size() * 100));
+        communityAccompany.setFanPercent(ReadPercent);
+
+
+        //查询评论数
+        /*//一级评论
+        Long comment = communityArticleCommentMapper.selectCount(new QueryWrapper<CommunityArticleComment>()
+                .eq("user_id", userId)
+                .and((wrapper) -> {
+                    wrapper.ne("is_delete", true).or().isNull("is_delete");
+                })
+        );
+        //二级评论
+        Long commentReply = communityCommentReplyMapper.selectCount(new QueryWrapper<CommunityCommentReply>()
+                .eq("user_id", userId)
+                .and((wrapper) -> {
+                    wrapper.ne("is_delete", true).or().isNull("is_delete");
+                })
+        );*/
+        //查询哪几篇文章是我发布的
+        List<Long> articleIds = communityArticles.stream().map(item -> item.getId()).collect(Collectors.toList());
+        System.out.println(articleIds);
+        Integer commentSum = 0;
+        //查询评论数 别人评论我的数量 首先自己得有文章才能有评论
+        if (articleIds.size() > 0 ) {
+            commentSum = communityArticleCommentMapper.queryCommentReplyCount(articleIds);
+        }
+        communityAccompany.setComment(Long.valueOf(commentSum));
+
+        //评论数占比
+        if (commentSum > 0 ){
+            Integer commentless = communityArticleCommentMapper.queryCommentReplyCountPerson(commentSum);
+            Integer commentCount = communityArticleCommentMapper.queryCommentReplyCountPerson(0);
+            Double  commentPercent = Double.valueOf(df.format((double) commentless / commentCount * 100));
+            communityAccompany.setCommentPercent(commentPercent);
         }
 
+
+
+
         return communityAccompany;
     }
 

+ 72 - 1
ruoyi-generator/src/main/resources/mapper/community/CommunityArticleCommentMapper.xml

@@ -14,4 +14,75 @@
               from community_article_comment c
               where c.article_id = #{articleId}) as d
     </select>
-</mapper>
+
+    <select id="queryCommentReplyCount" resultType="java.lang.Integer">
+        select  d.commentCount + d.replyCount from  (
+                                                        select
+                                                            count(1) as   commentCount,
+                                                            (select
+                                                                 count(*)  count
+                                                        from
+                                                            community_article_comment   a
+                                                            left join community_comment_reply b on a.id = b.comment_id
+    <where>
+          <if test=" articleIds != null and articleIds.size > 0">
+            AND article_id in
+            <foreach collection="articleIds" item="articleId" index="index" open="(" close=")" separator=",">
+                #{articleId}
+            </foreach>
+          </if>
+          and b.id is not null
+          and (a.is_delete != 1 or a.is_delete is null)
+          and (b.is_delete != 1 or b.is_delete is null)
+    </where>
+            ) as replyCount
+            from community_article_comment c
+    <where>
+        <if test="articleIds  != null and articleIds.size > 0">
+            AND c.article_id in
+            <foreach collection="articleIds" item="articleId" index="index" open="(" close=")" separator=",">
+                #{articleId}
+            </foreach>
+        </if>
+        and (c.is_delete != 1 or c.is_delete is null)
+    </where>
+        )  as d
+    </select>
+    <select id="queryCommentReplyCountPerson" resultType="java.lang.Integer">
+        select count(1)  count  from (
+            select
+            f.user_id,sum(g.commentSum)  sum
+            from (
+            select
+            a.user_id,b.id   as  article_id
+            from sys_user a
+            left join community_article b on a.user_id = b. user_id
+            where  (a.status != 1 or a.status is null) and  (b.is_delete != 1 or b.is_delete is null)
+            and b.id is not null ) f
+            left join (select
+            d.article_id, d.commentCount + COALESCE(e.replyCount, 0) as commentSum
+            from (
+            select
+            c.article_id,
+            count(1) as   commentCount
+            from community_article_comment c
+            where (c.is_delete != 1 or c.is_delete is null)
+            group by c.article_id ) d
+            left join (select
+            a.article_id,count(1) replyCount
+            from
+            community_article_comment   a
+            left join community_comment_reply b on a.id = b.comment_id
+            where b.id is not null
+            and (a.is_delete != 1 or a.is_delete is null)
+            and (b.is_delete != 1 or b.is_delete is null)
+            group by a.article_id) e
+            on d.article_id = e.article_id ) g
+            on f.article_id = g.article_id
+            group by  f.user_id
+                <if test="commentSum  != null and commentSum > 0">
+                HAVING sum &lt;= #{commentSum}
+                </if>
+            ) h
+    </select>
+</mapper>