CsdnUserInfoServiceImpl.java 7.3 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 6
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
7
import com.kwan.springbootkwan.entity.query.CsdnUserInfoQuery;
8 9 10 11
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;
12
import com.kwan.springbootkwan.mapper.CsdnUserInfoMapper;
13
import com.kwan.springbootkwan.service.CsdnArticleInfoService;
14
import com.kwan.springbootkwan.service.CsdnCollectService;
15
import com.kwan.springbootkwan.service.CsdnUserInfoService;
16
import org.apache.commons.lang3.StringUtils;
17
import org.springframework.beans.BeanUtils;
18
import org.springframework.beans.factory.annotation.Autowired;
19 20
import org.springframework.stereotype.Service;

21 22
import java.util.List;

23 24 25 26 27 28 29 30 31
/**
 * csdn用户信息(CsdnUserInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-10-23 16:03:14
 */
@Service("csdnUserInfoService")
public class CsdnUserInfoServiceImpl extends ServiceImpl<CsdnUserInfoMapper, CsdnUserInfo> implements CsdnUserInfoService {

32 33 34 35

    @Autowired
    private CsdnCollectService csdnCollectService;
    @Autowired
36
    private CsdnArticleInfoService csdnArticleInfoService;
37 38

    @Override
39
    public void resetAllCurrentStatus() {
40 41 42 43 44
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        final List<CsdnUserInfo> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            for (CsdnUserInfo csdnUserInfo : list) {
45 46 47 48 49 50 51 52 53 54
                this.resetCsdnUserInfo(csdnUserInfo);
            }
        }
    }

    @Override
    public void resetCsdnUserInfo(CsdnUserInfo csdnUserInfo) {
        final String userName = csdnUserInfo.getUserName();
        final Integer commentStatus = csdnUserInfo.getCommentStatus();
        final String articleType = csdnUserInfo.getArticleType();
55
        final List<BusinessInfoResponse.ArticleData.Article> articles = csdnArticleInfoService.getArticles(userName);
56 57 58 59 60 61 62 63 64 65 66 67 68
        if (CollectionUtil.isNotEmpty(articles)) {
            final BusinessInfoResponse.ArticleData.Article article = articles.get(0);
            final String type = article.getType();
            if (StringUtils.equals(type, "blog")) {
                final String urlInfo = article.getUrl();
                String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
                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);
69
                }
70 71 72 73 74 75
            } 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);
76 77 78
            }
        }
    }
79

80 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
    @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)) {

                        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
                        wrapper.eq("user_name", str);
                        wrapper.eq("is_delete", 0);
                        CsdnUserInfo one = this.getOne(wrapper);
                        if (one == null) {
                            one = new CsdnUserInfo();
                            BeanUtils.copyProperties(addInfo, one);
                            one.setUserName(str);
                            one.setUserHomeUrl("https://blog.csdn.net/" + str);
                            this.save(one);
                        }
                        addInfo.setNickName(one.getNickName());
                    }
                }
            } else {
                QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
                wrapper.eq("user_name", userName);
                wrapper.eq("is_delete", 0);
                CsdnUserInfo one = this.getOne(wrapper);
                if (one == null) {
                    one = new CsdnUserInfo();
                    BeanUtils.copyProperties(addInfo, one);
                    one.setUserHomeUrl("https://blog.csdn.net/" + userName);
                    this.save(one);
                }
                addInfo.setNickName(one.getNickName());
            }
        }
    }

122 123 124 125 126 127 128 129
    @Override
    public void resetUserDayStatus() {
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        final List<CsdnUserInfo> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            for (CsdnUserInfo csdnUserInfo : list) {
                final String userName = csdnUserInfo.getUserName();
130
                final List<BusinessInfoResponse.ArticleData.Article> articles = csdnArticleInfoService.getArticles(userName);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                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);
                    }
                }
            }
        }
    }
154 155
}

156