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

3
import cn.hutool.core.collection.CollectionUtil;
4
import com.kwan.springbootkwan.entity.CsdnArticleInfo;
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.CsdnArticleInfoService;
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
import java.util.List;
24
import java.util.Objects;
25

26 27

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

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

46
    @Override
47 48
    public void singleArticle(CsdnUserInfo csdnUserInfo) {
        final String username = csdnUserInfo.getUserName();
49
        List<BusinessInfoResponse.ArticleData.Article> list = csdnArticleInfoService.getArticles(username);
50 51 52 53 54 55 56 57 58 59 60
        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;
61
                }
62 63 64 65 66 67
                //先去查询文章,没有查到的话就插入文章
                final String articleUrl = article.getUrl();
                String articleIdFormUrl = articleUrl.substring(articleUrl.lastIndexOf("/") + 1);
                final Object articleId = article.getArticleId();
                if (Objects.isNull(articleId)) {
                    article.setArticleId(articleIdFormUrl);
68
                }
69 70
                CsdnArticleInfo csdnArticleInfo = this.csdnArticleInfoService.getArticleByArticleId(article.getArticleId().toString());
                if (Objects.isNull(csdnArticleInfo)) {
71 72 73 74 75 76 77
                    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());
78
                    this.csdnArticleInfoService.saveArticle(csdnArticleInfo);
79
                }
80
                this.tripletByArticle(csdnUserInfo, article, csdnArticleInfo);
81 82 83 84 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
    /**
     * 根据文章三连
     *
     * @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);
    }

132
    @Override
133
    public void allTriplet() {
134 135 136
        final List<CsdnUserInfo> allUser = csdnUserInfoService.getAllUser();
        if (CollectionUtil.isNotEmpty(allUser)) {
            for (CsdnUserInfo csdnUserInfo : allUser) {
137
                singleArticle(csdnUserInfo);
138 139 140 141
            }
        }
        log.info("全部三连完成");
    }
142
}