CsdnServiceImpl.java 4.1 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.CsdnUserInfo;
6
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
7
import com.kwan.springbootkwan.enums.CommentStatus;
8
import com.kwan.springbootkwan.service.CsdnArticleService;
9 10
import com.kwan.springbootkwan.service.CsdnCollectService;
import com.kwan.springbootkwan.service.CsdnCommentService;
11
import com.kwan.springbootkwan.service.CsdnLikeService;
12
import com.kwan.springbootkwan.service.CsdnService;
13 14
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
15
import org.apache.commons.lang3.StringUtils;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Value;
18 19
import org.springframework.stereotype.Service;

20
import java.util.Date;
21 22
import java.util.List;

23 24

@Slf4j
25 26 27
@Service
public class CsdnServiceImpl implements CsdnService {

28 29
    @Value("${csdn.num_of_articles_per_person}")
    private Integer numOfArticlesPerPerson;
30

31 32
    @Autowired
    private CsdnUserInfoService csdnUserInfoService;
33
    @Autowired
34 35
    private CsdnArticleService csdnArticleService;
    @Autowired
36 37 38
    private CsdnCollectService csdnCollectService;
    @Autowired
    private CsdnCommentService csdnCommentService;
39 40
    @Autowired
    private CsdnLikeService csdnLikeService;
41

42
    @Override
43 44
    public void singleArticle(CsdnUserInfo csdnUserInfo) {
        final String username = csdnUserInfo.getUserName();
45
        List<BusinessInfoResponse.ArticleData.Article> list = csdnArticleService.getArticles(username);
46 47 48
        if (list == null) {
            return;
        }
49 50
        final int size = list.size();
        numOfArticlesPerPerson = size < numOfArticlesPerPerson ? size : numOfArticlesPerPerson;
51 52 53 54
        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)) {
55
                csdnUserInfo.setArticleType(type);
56 57 58 59 60
                continue;
            }
            final String urlInfo = article.getUrl();
            String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
            //点赞
61
            final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo);
62
            if (!isLike) {
63
                csdnLikeService.like(articleId, csdnUserInfo);
64
            }
65 66 67 68 69
            //查看已经评论总数量
            QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("is_delete", 0);
            wrapper.eq("comment_status", 9);
            final List<CsdnUserInfo> comments = csdnUserInfoService.list(wrapper);
70 71 72 73 74
            if (CollectionUtil.isEmpty(comments) || comments.size() < 49) {
                //评论
                final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo);
                if (!comment) {
                    csdnCommentService.comment(articleId, csdnUserInfo);
75
                }
76
            }
77 78 79
            if (CollectionUtil.isNotEmpty(comments) && comments.size() >= 49) {
                csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_NUM_49.getCode());
            }
80
            //收藏
81
            final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo);
82
            if (!collect) {
83
                csdnCollectService.collect(article, csdnUserInfo);
84 85
            }
        }
86
        csdnUserInfo.setUpdateTime(new Date());
87
        csdnUserInfoService.updateById(csdnUserInfo);
88 89 90 91 92
    }


    @Override
    public void multiTriplet() {
93 94
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
95
        wrapper.orderByDesc("user_weight");
96
        final List<CsdnUserInfo> list = csdnUserInfoService.list(wrapper);
97 98
        if (CollectionUtil.isNotEmpty(list)) {
            for (CsdnUserInfo csdnUserInfo : list) {
99
                singleArticle(csdnUserInfo);
100 101 102 103
            }
        }
        log.info("全部三连完成");
    }
104
}