CsdnServiceImpl.java 4.0 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 21
import java.util.List;

22 23

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

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

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

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


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