CsdnServiceImpl.java 6.4 KB
Newer Older
1 2
package com.kwan.springbootkwan.service.impl;

3
import cn.hutool.core.collection.CollectionUtil;
4
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5
import com.kwan.springbootkwan.entity.CsdnArticleInfo;
6
import com.kwan.springbootkwan.entity.CsdnTripletDayInfo;
7
import com.kwan.springbootkwan.entity.CsdnUserInfo;
8
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
9
import com.kwan.springbootkwan.enums.CommentStatus;
10
import com.kwan.springbootkwan.service.CsdnArticleInfoService;
11 12
import com.kwan.springbootkwan.service.CsdnCollectService;
import com.kwan.springbootkwan.service.CsdnCommentService;
13
import com.kwan.springbootkwan.service.CsdnLikeService;
14
import com.kwan.springbootkwan.service.CsdnService;
15
import com.kwan.springbootkwan.service.CsdnTripletDayInfoService;
16 17
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
18
import org.apache.commons.lang3.StringUtils;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Value;
21 22
import org.springframework.stereotype.Service;

23
import java.util.Date;
24
import java.util.List;
25
import java.util.Objects;
26

27 28

@Slf4j
29 30 31
@Service
public class CsdnServiceImpl implements CsdnService {

32 33
    @Value("${csdn.num_of_articles_per_person}")
    private Integer numOfArticlesPerPerson;
34 35
    @Autowired
    private CsdnLikeService csdnLikeService;
36 37
    @Autowired
    private CsdnUserInfoService csdnUserInfoService;
38
    @Autowired
39
    private CsdnArticleInfoService csdnArticleInfoService;
40
    @Autowired
41 42 43
    private CsdnCollectService csdnCollectService;
    @Autowired
    private CsdnCommentService csdnCommentService;
44
    @Autowired
45
    private CsdnTripletDayInfoService csdnTripletDayInfoService;
46

47
    @Override
48 49
    public void singleArticle(CsdnUserInfo csdnUserInfo) {
        final String username = csdnUserInfo.getUserName();
50
        List<BusinessInfoResponse.ArticleData.Article> list = csdnArticleInfoService.getArticles(username);
51 52 53 54 55 56 57 58 59 60 61
        if (CollectionUtil.isNotEmpty(list)) {
            final int size = list.size();
            numOfArticlesPerPerson = size < numOfArticlesPerPerson ? size : numOfArticlesPerPerson;
            for (int i = 0; i < numOfArticlesPerPerson; i++) {
                final BusinessInfoResponse.ArticleData.Article article = list.get(i);
                final String type = article.getType();
                if (!StringUtils.equals("blog", type)) {
                    csdnUserInfo.setArticleType(type);
                    csdnUserInfo.setUpdateTime(new Date());
                    csdnUserInfoService.updateById(csdnUserInfo);
                    continue;
62
                }
63 64 65 66 67 68 69
                //先去查询文章,没有查到的话就插入文章
                QueryWrapper<CsdnArticleInfo> wrapper = new QueryWrapper<>();
                final String articleUrl = article.getUrl();
                String articleIdFormUrl = articleUrl.substring(articleUrl.lastIndexOf("/") + 1);
                final Object articleId = article.getArticleId();
                if (Objects.isNull(articleId)) {
                    article.setArticleId(articleIdFormUrl);
70
                }
71 72 73 74 75 76 77 78 79 80 81 82
                wrapper.eq("article_id", article.getArticleId().toString());
                wrapper.eq("is_delete", 0);
                CsdnArticleInfo csdnArticleInfo = this.csdnArticleInfoService.getOne(wrapper);
                if (csdnArticleInfo == null) {
                    csdnArticleInfo = new CsdnArticleInfo();
                    csdnArticleInfo.setArticleId(article.getArticleId().toString());
                    csdnArticleInfo.setArticleUrl(articleUrl);
                    csdnArticleInfo.setArticleTitle(article.getTitle());
                    csdnArticleInfo.setArticleDescription(article.getDescription());
                    csdnArticleInfo.setUserName(username);
                    csdnArticleInfo.setNickName(csdnUserInfo.getNickName());
                    this.csdnArticleInfoService.save(csdnArticleInfo);
83
                }
84
                this.tripletByArticle(csdnUserInfo, article, csdnArticleInfo);
85 86 87 88 89
            }
        }
    }


90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    /**
     * 根据文章三连
     *
     * @param csdnUserInfo
     * @param article
     */
    @Override
    public void tripletByArticle(CsdnUserInfo csdnUserInfo, BusinessInfoResponse.ArticleData.Article article, CsdnArticleInfo csdnArticleInfo) {
        //获取每日三连总信息
        final CsdnTripletDayInfo csdnTripletDayInfo = csdnTripletDayInfoService.todayInfo();
        final String urlInfo = article.getUrl();
        String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
        //点赞
        final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo);
        if (!isLike) {
            csdnLikeService.like(articleId, csdnUserInfo, csdnTripletDayInfo);
        }
        final Integer commentNum = csdnTripletDayInfo.getCommentNum();
        if (commentNum < 49) {
            //评论
            final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo);
            if (!comment) {
                csdnCommentService.comment(articleId, csdnUserInfo, csdnTripletDayInfo);
            }
        } else {
            csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_NUM_49.getCode());
        }
        //收藏
        final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo);
        if (!collect) {
            csdnCollectService.collect(article, csdnUserInfo, csdnTripletDayInfo);
        }

        csdnTripletDayInfo.setUpdateTime(new Date());
        csdnTripletDayInfoService.updateById(csdnTripletDayInfo);

        csdnUserInfo.setUpdateTime(new Date());
        csdnUserInfoService.updateById(csdnUserInfo);

        csdnArticleInfo.setUpdateTime(new Date());
        csdnArticleInfo.setLikeStatus(csdnUserInfo.getLikeStatus());
        csdnArticleInfo.setCollectStatus(csdnUserInfo.getCollectStatus());
        csdnArticleInfo.setCommentStatus(csdnUserInfo.getCommentStatus());
        csdnArticleInfoService.updateById(csdnArticleInfo);
    }

136 137
    @Override
    public void multiTriplet() {
138 139
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
140
        wrapper.orderByDesc("user_weight");
141
        final List<CsdnUserInfo> list = csdnUserInfoService.list(wrapper);
142 143
        if (CollectionUtil.isNotEmpty(list)) {
            for (CsdnUserInfo csdnUserInfo : list) {
144
                singleArticle(csdnUserInfo);
145 146 147 148
            }
        }
        log.info("全部三连完成");
    }
149
}