|
@@ -0,0 +1,128 @@
|
|
|
+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.mapper.community.CommunityArticleMapper;
|
|
|
+import com.ruoyi.generator.mapper.community.CommunityUserLikeMapper;
|
|
|
+import com.ruoyi.generator.vo.CommunityUserLikeVo;
|
|
|
+import com.ruoyi.system.mapper.SysUserMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.time.temporal.Temporal;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author fangqing
|
|
|
+ * @date 2024/12/30 15:26
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class CommunityAccompanyImpl implements ICommunityAccompanyService {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysUserMapper userMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CommunityArticleMapper communityArticleMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CommunityUserLikeMapper communityUserLikeMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommunityAccompany selectCommunityAccompanyById(Long userId) {
|
|
|
+
|
|
|
+ CommunityAccompany communityAccompany = new CommunityAccompany();
|
|
|
+
|
|
|
+ //获取当前用户信息 天数
|
|
|
+ SysUser sysUsers = userMapper.selectOne( new QueryWrapper<SysUser>().select("create_time").eq("user_id", userId).and((wrapper) -> {wrapper.ne("status", true).or().isNull("status");}));
|
|
|
+
|
|
|
+ if (sysUsers == null){
|
|
|
+ return communityAccompany;
|
|
|
+ }
|
|
|
+ Date createTime = sysUsers.getCreateTime();
|
|
|
+ // 将 Date 对象转换为 LocalDate 对象
|
|
|
+ LocalDate localCreateTime = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
+ // 获取当前日期
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ // 计算两个日期之间的天数差
|
|
|
+ long daysDifference = ChronoUnit.DAYS.between(localCreateTime, today);
|
|
|
+ communityAccompany.setAccompanyDays(daysDifference);
|
|
|
+
|
|
|
+ //查询当前用户下文章的数量
|
|
|
+ List<CommunityArticle> communityArticles = communityArticleMapper.selectList(new QueryWrapper<CommunityArticle>()
|
|
|
+ .eq("create_by", userId)
|
|
|
+ .and((wrapper) -> {
|
|
|
+ wrapper.ne("is_delete", true).or().isNull("is_delete");
|
|
|
+ }));
|
|
|
+ //如果用户文章数量不为空则执行以下的操作
|
|
|
+ if (communityArticles != null){
|
|
|
+ DecimalFormat df = new DecimalFormat("#.00");
|
|
|
+ //设置用户创作数
|
|
|
+ communityAccompany.setCreationCount((long) communityArticles.size());
|
|
|
+
|
|
|
+ //查询当前用户下文章的数量
|
|
|
+ List<CommunityArticle> communityArticlesTotal = communityArticleMapper.selectList(new QueryWrapper<CommunityArticle>()
|
|
|
+ .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);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取用户阅览量
|
|
|
+ long read = communityArticles.stream()
|
|
|
+ .mapToLong(article -> article.getPageViews()).sum();
|
|
|
+
|
|
|
+ if (read != 0 ){
|
|
|
+ communityAccompany.setRead(read);
|
|
|
+ }
|
|
|
+
|
|
|
+ //阅览量百分比
|
|
|
+ long readSUm = communityArticlesTotal.stream()
|
|
|
+ .mapToLong(article -> article.getPageViews()).sum();
|
|
|
+
|
|
|
+ if (readSUm != 0){
|
|
|
+ Double ReadPercent = Double.valueOf(df.format((double) read / readSUm * 100));
|
|
|
+ communityAccompany.setReadPercent(ReadPercent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //粉丝数
|
|
|
+ Long fans = communityUserLikeMapper.selectCount(new QueryWrapper<CommunityUserLike>()
|
|
|
+ .eq("like_user_id", userId));
|
|
|
+
|
|
|
+ if (fans != 0 ){
|
|
|
+ communityAccompany.setFan(fans);
|
|
|
+ }
|
|
|
+
|
|
|
+ //粉丝数占比
|
|
|
+ CommunityUserLike communityUserLike = communityUserLikeMapper.selectOne(new QueryWrapper<CommunityUserLike>()
|
|
|
+ .select("like_user_id", "COUNT(1) as count")
|
|
|
+ .groupBy("like_user_id")
|
|
|
+ .orderByDesc("count")
|
|
|
+ .last("LIMIT 1"));
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println(communityUserLike);
|
|
|
+
|
|
|
+
|
|
|
+ return communityAccompany;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|