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

22 23
import java.util.List;

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
    @Autowired
    private CsdnCollectService csdnCollectService;
    @Autowired
35
    private CsdnArticleInfoService csdnArticleInfoService;
36 37

    @Override
38 39 40 41 42 43 44 45 46 47
    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() {
48 49
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
50 51 52 53 54 55 56 57 58
        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) {
59 60 61 62 63 64 65 66
                this.resetCsdnUserInfo(csdnUserInfo);
            }
        }
    }

    @Override
    public void resetCsdnUserInfo(CsdnUserInfo csdnUserInfo) {
        final Integer commentStatus = csdnUserInfo.getCommentStatus();
67
        final String userName = csdnUserInfo.getUserName();
68
        final String articleType = csdnUserInfo.getArticleType();
69
        final List<BusinessInfoResponse.ArticleData.Article> articles = csdnArticleInfoService.getArticles(userName);
70 71 72
        if (CollectionUtil.isNotEmpty(articles)) {
            final BusinessInfoResponse.ArticleData.Article article = articles.get(0);
            final String type = article.getType();
73 74
            if (StringUtils.equals(type, CsdnConstant.ARTICLE_TYPE)) {
                final String articleId = article.getArticleId().toString();
75 76 77 78 79 80 81
                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);
82
                }
83 84 85 86 87 88
            } 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);
89 90 91
            }
        }
    }
92

93 94 95 96 97 98 99 100 101 102 103
    @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)) {
104 105 106 107 108 109 110
                        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);
111
                        }
112
                        addInfo.setNickName(csdnUserInfo.getNickName());
113 114 115
                    }
                }
            } else {
116 117 118 119 120 121
                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);
122
                }
123
                addInfo.setNickName(csdnUserInfo.getNickName());
124 125 126 127
            }
        }
    }

128 129
    @Override
    public void resetUserDayStatus() {
130 131 132
        final List<CsdnUserInfo> allUser = this.getAllUser();
        if (CollectionUtil.isNotEmpty(allUser)) {
            for (CsdnUserInfo csdnUserInfo : allUser) {
133
                final String userName = csdnUserInfo.getUserName();
134
                final List<BusinessInfoResponse.ArticleData.Article> articles = csdnArticleInfoService.getArticles(userName);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                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);
                    }
                }
            }
        }
    }
158
}