package com.tanpu.community.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tanpu.common.exception.BizException; import com.tanpu.community.api.enums.DeleteTagEnum; 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; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDateTime; 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>() .eq(FollowRelEntity::getFollowerId, followerId) .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())) .stream().map(FollowRelEntity::getFollowUserId) .collect(Collectors.toList()); } // @Cacheable(value = "tempCache", keyGenerator = "communityKeyGenerator") public List<String> queryFansByIdolId(String idolId) { LambdaQueryWrapper<FollowRelEntity> queryWrapper = new LambdaQueryWrapper<FollowRelEntity>() .eq(FollowRelEntity::getFollowUserId, idolId) .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return followRelMapper.selectList(queryWrapper) .stream().map(FollowRelEntity::getFollowerId).collect(Collectors.toList()); } @Transactional 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(); 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); } public FollowRelEntity queryRecord(String idolId, String followerId){ return followRelMapper.selectOne(new LambdaQueryWrapper<FollowRelEntity>() .eq(FollowRelEntity::getFollowUserId,idolId) .eq(FollowRelEntity::getFollowerId,followerId)); } }