FollowRelService.java 3.17 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 24 25
import java.util.List;
import java.util.stream.Collectors;

@EnableCaching
@Service
public class FollowRelService {

    @Resource
    private FollowRelMapper followRelMapper;

    public List<String> queryFansByFollowerId(String followerId) {
        return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
刘基明's avatar
刘基明 committed
26 27 28 29
                .eq(FollowRelEntity::getFollowerId, followerId)
                .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
                .stream().map(FollowRelEntity::getFollowUserId)
                .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 36
                .eq(FollowRelEntity::getFollowUserId, idolId)
                .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
37
        return followRelMapper.selectList(queryWrapper)
刘基明's avatar
刘基明 committed
38 39 40
                .stream().map(FollowRelEntity::getFollowerId).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
41
    @Transactional
刘基明's avatar
刘基明 committed
42 43 44 45 46 47 48 49
    public void addFollowRel(String idolId, String followerId) {
        FollowRelEntity searchResult = queryRecord(idolId, followerId);
        if (searchResult==null){
            FollowRelEntity entity = FollowRelEntity.builder()
                    .followUserId(idolId)
                    .followerId(followerId)
                    .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 78 79 80 81 82

    public FollowRelEntity queryRecord(String idolId, String followerId){
        return followRelMapper.selectOne(new LambdaQueryWrapper<FollowRelEntity>()
                .eq(FollowRelEntity::getFollowUserId,idolId)
                .eq(FollowRelEntity::getFollowerId,followerId));
    }


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