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

22
import java.util.Date;
23 24
import java.util.List;

25 26

@Slf4j
27 28 29
@Service
public class CsdnServiceImpl implements CsdnService {

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

45
    @Override
46 47
    public void singleArticle(CsdnUserInfo csdnUserInfo) {
        final String username = csdnUserInfo.getUserName();
48
        List<BusinessInfoResponse.ArticleData.Article> list = csdnArticleService.getArticles(username);
49 50 51 52 53 54 55 56 57 58 59
        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;
60
                }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                //获取每日三连总信息
                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);
89 90 91 92 93 94 95
            }
        }
    }


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