FollowRelService.java 3.5 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 5
import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
6 7 8 9
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
10
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
11 12

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

@EnableCaching
@Service
public class FollowRelService {

    @Resource
    private FollowRelMapper followRelMapper;

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

//    @Cacheable(value = "tempCache", keyGenerator = "communityKeyGenerator")
    public List<String> queryFansByIdolId(String idolId) {
刘基明's avatar
刘基明 committed
34
        LambdaQueryWrapper<FollowRelEntity> queryWrapper = new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
35
                .eq(FollowRelEntity::getIdolId, idolId)
刘基明's avatar
刘基明 committed
36
                .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
37
        return followRelMapper.selectList(queryWrapper)
刘基明's avatar
刘基明 committed
38
                .stream().map(FollowRelEntity::getFansId).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
39 40
    }

刘基明's avatar
刘基明 committed
41
    @Transactional
刘基明's avatar
刘基明 committed
42 43 44 45
    public void addFollowRel(String idolId, String followerId) {
        FollowRelEntity searchResult = queryRecord(idolId, followerId);
        if (searchResult==null){
            FollowRelEntity entity = FollowRelEntity.builder()
刘基明's avatar
刘基明 committed
46 47
                    .idolId(idolId)
                    .fansId(followerId)
刘基明's avatar
刘基明 committed
48 49
                    .followTime(LocalDateTime.now())
                    .build();
刘基明's avatar
刘基明 committed
50

刘基明's avatar
刘基明 committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            followRelMapper.insert(entity);
        }else {
            searchResult.setFollowTime(LocalDateTime.now());
            searchResult.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
            followRelMapper.updateById(searchResult);
        }

    }

    /**
     * 逻辑删除关注关系
     * @param idolId
     * @param followerId
     */
    @Transactional
    public void deleteFollowRel(String idolId, String followerId) {
        FollowRelEntity searchResult = queryRecord(idolId, followerId);
        if (searchResult==null){
            throw new BizException("未找到关注关系");
        }
        searchResult.setUnfollowTime(LocalDateTime.now());
        searchResult.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        followRelMapper.updateById(searchResult);
刘基明's avatar
刘基明 committed
74
    }
刘基明's avatar
刘基明 committed
75 76 77

    public FollowRelEntity queryRecord(String idolId, String followerId){
        return followRelMapper.selectOne(new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
78 79
                .eq(FollowRelEntity::getIdolId,idolId)
                .eq(FollowRelEntity::getFansId,followerId));
刘基明's avatar
刘基明 committed
80 81
    }

82 83
    public boolean checkFollow(String idolId,String followerId){
        return followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
84 85
                .eq(FollowRelEntity::getIdolId,idolId)
                .eq(FollowRelEntity::getFansId,followerId)
刘基明's avatar
刘基明 committed
86
                .eq(FollowRelEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode()))
87 88 89
                >0;
    }

刘基明's avatar
刘基明 committed
90

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