CsdnUserInfoServiceImpl.java 8.6 KB
Newer Older
1 2
package com.kwan.springbootkwan.service.impl;

3 4
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
6
import com.kwan.springbootkwan.constant.CsdnConstant;
7
import com.kwan.springbootkwan.entity.CsdnArticleInfo;
8
import com.kwan.springbootkwan.entity.CsdnUserInfo;
9
import com.kwan.springbootkwan.entity.query.CsdnUserInfoQuery;
10 11 12 13
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
import com.kwan.springbootkwan.enums.CollectStatus;
import com.kwan.springbootkwan.enums.CommentStatus;
import com.kwan.springbootkwan.enums.LikeStatus;
14
import com.kwan.springbootkwan.mapper.CsdnUserInfoMapper;
15
import com.kwan.springbootkwan.service.CsdnArticleInfoService;
16
import com.kwan.springbootkwan.service.CsdnCollectService;
17
import com.kwan.springbootkwan.service.CsdnUserInfoService;
18
import org.apache.commons.lang3.StringUtils;
19
import org.springframework.beans.BeanUtils;
20
import org.springframework.beans.factory.annotation.Autowired;
21 22
import org.springframework.stereotype.Service;

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

26 27
@Service("csdnUserInfoService")
public class CsdnUserInfoServiceImpl extends ServiceImpl<CsdnUserInfoMapper, CsdnUserInfo> implements CsdnUserInfoService {
28

29 30 31
    @Autowired
    private CsdnCollectService csdnCollectService;
    @Autowired
32
    private CsdnArticleInfoService csdnArticleInfoService;
33 34

    @Override
35 36 37 38 39 40 41 42 43 44
    public CsdnUserInfo getUserByUserName(String username) {
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        wrapper.eq("user_name", username)
                .last("limit 1");
        return this.getOne(wrapper);
    }

    @Override
    public List<CsdnUserInfo> getAllUser() {
45 46
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
47 48 49 50 51 52 53 54 55
        wrapper.orderByAsc("rand()");
        return this.list(wrapper);
    }

    @Override
    public void resetAllCurrentStatus() {
        final List<CsdnUserInfo> allUser = this.getAllUser();
        if (CollectionUtil.isNotEmpty(allUser)) {
            for (CsdnUserInfo csdnUserInfo : allUser) {
56 57 58 59 60 61 62 63
                this.resetCsdnUserInfo(csdnUserInfo);
            }
        }
    }

    @Override
    public void resetCsdnUserInfo(CsdnUserInfo csdnUserInfo) {
        final Integer commentStatus = csdnUserInfo.getCommentStatus();
64
        final String userName = csdnUserInfo.getUserName();
65
        final String articleType = csdnUserInfo.getArticleType();
66
        final List<BusinessInfoResponse.ArticleData.Article> articles = csdnArticleInfoService.getArticles(userName);
67 68 69
        if (CollectionUtil.isNotEmpty(articles)) {
            final BusinessInfoResponse.ArticleData.Article article = articles.get(0);
            final String type = article.getType();
70
            if (StringUtils.equals(type, CsdnConstant.ARTICLE_TYPE)) {
71 72
                final String articleUrl = article.getUrl();
                String articleId = articleUrl.substring(articleUrl.lastIndexOf("/") + 1);
73 74 75 76 77 78 79
                final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo);
                if (!collect || CommentStatus.COMMENT_TOO_FAST.getCode().equals(commentStatus) || !StringUtils.equals(articleType, type)) {
                    csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode());
                    csdnUserInfo.setCollectStatus(CollectStatus.UN_PROCESSED.getCode());
                    csdnUserInfo.setCommentStatus(CommentStatus.UN_PROCESSED.getCode());
                    csdnUserInfo.setArticleType(type);
                    this.updateById(csdnUserInfo);
80
                }
81 82 83 84 85 86
            } else {
                csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode());
                csdnUserInfo.setCollectStatus(CollectStatus.UN_PROCESSED.getCode());
                csdnUserInfo.setCommentStatus(CommentStatus.UN_PROCESSED.getCode());
                csdnUserInfo.setArticleType(type);
                this.updateById(csdnUserInfo);
87 88 89
            }
        }
    }
90

91 92 93 94 95 96 97 98 99 100 101
    @Override
    public void add(CsdnUserInfoQuery addInfo) {
        final String userName = addInfo.getUserName();
        final Integer addType = addInfo.getAddType();
        if (StringUtils.isNotEmpty(userName)) {
            //批量添加
            if (addType == 1) {
                final String[] split = userName.split("\n");
                for (String str : split) {
                    str = str.trim();
                    if (StringUtils.isNotEmpty(str)) {
102 103 104 105 106 107 108
                        CsdnUserInfo csdnUserInfo = this.getUserByUserName(str);
                        if (csdnUserInfo == null) {
                            csdnUserInfo = new CsdnUserInfo();
                            BeanUtils.copyProperties(addInfo, csdnUserInfo);
                            csdnUserInfo.setUserName(str);
                            csdnUserInfo.setUserHomeUrl("https://blog.csdn.net/" + str);
                            this.save(csdnUserInfo);
109
                        }
110
                        addInfo.setNickName(csdnUserInfo.getNickName());
111 112 113
                    }
                }
            } else {
114 115 116 117 118 119
                CsdnUserInfo csdnUserInfo = this.getUserByUserName(userName);
                if (csdnUserInfo == null) {
                    csdnUserInfo = new CsdnUserInfo();
                    BeanUtils.copyProperties(addInfo, csdnUserInfo);
                    csdnUserInfo.setUserHomeUrl("https://blog.csdn.net/" + userName);
                    this.save(csdnUserInfo);
120
                }
121
                addInfo.setNickName(csdnUserInfo.getNickName());
122 123 124 125
            }
        }
    }

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    @Override
    public void add10Blog(String username) {
        final CsdnUserInfo csdnUserInfo = this.getUserByUserName(username);
        final List<BusinessInfoResponse.ArticleData.Article> blogs = csdnArticleInfoService.getBlogs(username);
        for (BusinessInfoResponse.ArticleData.Article article : blogs) {
            CsdnArticleInfo csdnArticleInfo = this.csdnArticleInfoService.getArticleByArticleId(article.getArticleId().toString());
            if (Objects.isNull(csdnArticleInfo)) {
                csdnArticleInfo = new CsdnArticleInfo();
                csdnArticleInfo.setArticleId(article.getArticleId().toString());
                csdnArticleInfo.setArticleUrl(article.getUrl());
                csdnArticleInfo.setArticleTitle(article.getTitle());
                csdnArticleInfo.setArticleDescription(article.getDescription());
                csdnArticleInfo.setUserName(username);
                csdnArticleInfo.setNickName(csdnUserInfo.getNickName());
                this.csdnArticleInfoService.saveArticle(csdnArticleInfo);
            }
        }
    }

145 146
    @Override
    public void resetUserDayStatus() {
147 148 149
        final List<CsdnUserInfo> allUser = this.getAllUser();
        if (CollectionUtil.isNotEmpty(allUser)) {
            for (CsdnUserInfo csdnUserInfo : allUser) {
150
                final String userName = csdnUserInfo.getUserName();
151
                final List<BusinessInfoResponse.ArticleData.Article> articles = csdnArticleInfoService.getArticles(userName);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                if (CollectionUtil.isNotEmpty(articles)) {
                    final BusinessInfoResponse.ArticleData.Article article = articles.get(0);
                    final String type = article.getType();
                    if (StringUtils.equals(type, "blog")) {
                        //看看之前的状态
                        final Integer likeStatus = csdnUserInfo.getLikeStatus();
                        if (!LikeStatus.HAVE_ALREADY_LIKED.getCode().equals(likeStatus) && !LikeStatus.LIKE_SUCCESSFUL.getCode().equals(likeStatus)) {
                            csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode());
                        }
                        final Integer collectStatus = csdnUserInfo.getCollectStatus();
                        if (!CollectStatus.COLLECT_SUCCESSFUL.getCode().equals(collectStatus) && !CollectStatus.HAVE_ALREADY_COLLECT.getCode().equals(collectStatus)) {
                            csdnUserInfo.setCollectStatus(CollectStatus.UN_PROCESSED.getCode());
                        }
                        final Integer commentStatus = csdnUserInfo.getCommentStatus();
                        if (!CommentStatus.COMMENT_SUCCESSFUL.getCode().equals(commentStatus) && !CommentStatus.HAVE_ALREADY_COMMENT.getCode().equals(commentStatus)) {
                            csdnUserInfo.setCommentStatus(CommentStatus.UN_PROCESSED.getCode());
                        }
                        this.updateById(csdnUserInfo);
                    }
                }
            }
        }
    }
175
}