FollowRelService.java 5.35 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
刘基明's avatar
刘基明 committed
4
import com.tanpu.common.constant.BizStatus;
刘基明's avatar
刘基明 committed
5
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
6
import com.tanpu.community.api.beans.req.page.Page;
刘基明's avatar
刘基明 committed
7
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
8 9 10 11
import com.tanpu.community.dao.entity.community.FollowRelEntity;
import com.tanpu.community.dao.mapper.community.FollowRelMapper;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
12
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
13 14

import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
15
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
16 17 18 19 20 21 22 23 24 25
import java.util.List;
import java.util.stream.Collectors;

@EnableCaching
@Service
public class FollowRelService {

    @Resource
    private FollowRelMapper followRelMapper;

刘基明's avatar
刘基明 committed
26
    public List<String> queryIdolsByFansId(String followerId) {
刘基明's avatar
刘基明 committed
27
        return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
28
                .eq(FollowRelEntity::getFansId, followerId)
刘基明's avatar
刘基明 committed
29
                .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
刘基明's avatar
刘基明 committed
30
                .stream().map(FollowRelEntity::getIdolId)
刘基明's avatar
刘基明 committed
31
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
32 33
    }

刘基明's avatar
刘基明 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    public Page<String> queryIdolsByFansId(String fansId, Integer pageNum, Integer pageSize) {
        Integer pageStart = (pageNum - 1) * pageSize;

        List<FollowRelEntity> idols = followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
                .eq(FollowRelEntity::getFansId, fansId)
                .last("limit " + pageStart + ", " + pageSize)
                .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));

        List<String> list = idols.stream().map(FollowRelEntity::getIdolId)
                .collect(Collectors.toList());

        Integer totalSize = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getFansId,fansId)
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));

        Page<String> tPage = new Page<>(pageNum, pageSize, (long) totalSize, 0, list);
        int totalPage = list.size() / pageSize;
        if (list.size() % pageSize == 0) {
            tPage.setTotalPages(totalPage);
        } else {
            tPage.setTotalPages(totalPage + 1);
        }
        return tPage;
    }

    public Page<String> queryFansByIdolId(String idolId, Integer pageNum, Integer pageSize) {
        Integer pageStart = (pageNum - 1) * pageSize;
刘基明's avatar
刘基明 committed
60
        LambdaQueryWrapper<FollowRelEntity> queryWrapper = new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
61
                .eq(FollowRelEntity::getIdolId, idolId)
刘基明's avatar
刘基明 committed
62
                .last("limit " + pageStart + ", " + pageSize)
刘基明's avatar
刘基明 committed
63
                .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
64
        List<String> list = followRelMapper.selectList(queryWrapper)
刘基明's avatar
刘基明 committed
65
                .stream().map(FollowRelEntity::getFansId).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
66 67 68 69 70 71 72 73 74 75 76
        // 分页
        Integer totalSize = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getIdolId, idolId)
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
        Page<String> tPage = new Page<>(pageNum, pageSize, (long) totalSize, 0, list);
        int totalPage = list.size() / pageSize;
        if (list.size() % pageSize == 0) {
            tPage.setTotalPages(totalPage);
        } else {
            tPage.setTotalPages(totalPage + 1);
        }
        return tPage;
刘基明's avatar
刘基明 committed
77 78
    }

刘基明's avatar
刘基明 committed
79
    @Transactional
刘基明's avatar
刘基明 committed
80 81
    public void addFollowRel(String idolId, String followerId) {
        FollowRelEntity searchResult = queryRecord(idolId, followerId);
刘基明's avatar
刘基明 committed
82
        if (searchResult == null) {
刘基明's avatar
刘基明 committed
83
            FollowRelEntity entity = FollowRelEntity.builder()
刘基明's avatar
刘基明 committed
84 85
                    .idolId(idolId)
                    .fansId(followerId)
刘基明's avatar
刘基明 committed
86 87
                    .followTime(LocalDateTime.now())
                    .build();
刘基明's avatar
刘基明 committed
88

刘基明's avatar
刘基明 committed
89
            followRelMapper.insert(entity);
刘基明's avatar
刘基明 committed
90
        } else {
刘基明's avatar
刘基明 committed
91 92 93 94 95 96 97 98 99
            searchResult.setFollowTime(LocalDateTime.now());
            searchResult.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
            followRelMapper.updateById(searchResult);
        }

    }

    /**
     * 逻辑删除关注关系
刘基明's avatar
刘基明 committed
100
     *
刘基明's avatar
刘基明 committed
101 102 103 104 105 106
     * @param idolId
     * @param followerId
     */
    @Transactional
    public void deleteFollowRel(String idolId, String followerId) {
        FollowRelEntity searchResult = queryRecord(idolId, followerId);
刘基明's avatar
刘基明 committed
107
        if (searchResult == null) {
刘基明's avatar
刘基明 committed
108 109 110 111 112
            throw new BizException("未找到关注关系");
        }
        searchResult.setUnfollowTime(LocalDateTime.now());
        searchResult.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        followRelMapper.updateById(searchResult);
刘基明's avatar
刘基明 committed
113
    }
刘基明's avatar
刘基明 committed
114

刘基明's avatar
刘基明 committed
115
    public FollowRelEntity queryRecord(String idolId, String followerId) {
刘基明's avatar
刘基明 committed
116
        return followRelMapper.selectOne(new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
117 118
                .eq(FollowRelEntity::getIdolId, idolId)
                .eq(FollowRelEntity::getFansId, followerId));
刘基明's avatar
刘基明 committed
119 120
    }

刘基明's avatar
刘基明 committed
121
    public boolean checkFollow(String idolId, String followerId) {
122
        return followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
123 124 125 126
                .eq(FollowRelEntity::getIdolId, idolId)
                .eq(FollowRelEntity::getFansId, followerId)
                .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
                > 0;
127 128
    }

刘基明's avatar
刘基明 committed
129

刘基明's avatar
刘基明 committed
130
}