FollowRelService.java 5.76 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
    public boolean addFollowRel(String idolId, String followerId) {
刘基明's avatar
刘基明 committed
81
        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
            return true;
刘基明's avatar
刘基明 committed
91
        } else {
刘基明's avatar
刘基明 committed
92 93 94
            searchResult.setFollowTime(LocalDateTime.now());
            searchResult.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
            followRelMapper.updateById(searchResult);
刘基明's avatar
刘基明 committed
95
            return false;
刘基明's avatar
刘基明 committed
96 97 98 99 100 101
        }

    }

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

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

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

刘基明's avatar
刘基明 committed
131

刘基明's avatar
刘基明 committed
132 133 134 135 136 137
    public List<FollowRelEntity> queryAll() {
        // todo 会丢失关注后 取消关注的情况,怎么选择?
        return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
                .eq(FollowRelEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())
                .orderByAsc(FollowRelEntity::getUpdateTime));
    }
刘基明's avatar
刘基明 committed
138
}