package com.tanpu.community.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tanpu.common.constant.BizStatus; import com.tanpu.common.exception.BizException; import com.tanpu.community.api.beans.req.page.Page; 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> queryIdolsByFansId(String followerId) { return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>() .eq(FollowRelEntity::getFansId, followerId) .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())) .stream().map(FollowRelEntity::getIdolId) .collect(Collectors.toList()); } 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; LambdaQueryWrapper<FollowRelEntity> queryWrapper = new LambdaQueryWrapper<FollowRelEntity>() .eq(FollowRelEntity::getIdolId, idolId) .last("limit " + pageStart + ", " + pageSize) .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); List<String> list = followRelMapper.selectList(queryWrapper) .stream().map(FollowRelEntity::getFansId).collect(Collectors.toList()); // 分页 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; } @Transactional public boolean addFollowRel(String idolId, String followerId) { FollowRelEntity searchResult = queryRecord(idolId, followerId); if (searchResult == null) { FollowRelEntity entity = FollowRelEntity.builder() .idolId(idolId) .fansId(followerId) .followTime(LocalDateTime.now()) .build(); followRelMapper.insert(entity); return true; } else { searchResult.setFollowTime(LocalDateTime.now()); searchResult.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode()); followRelMapper.updateById(searchResult); return false; } } /** * 逻辑删除关注关系 * * @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::getIdolId, idolId) .eq(FollowRelEntity::getFansId, followerId)); } public boolean checkFollow(String idolId, String followerId) { return followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>() .eq(FollowRelEntity::getIdolId, idolId) .eq(FollowRelEntity::getFansId, followerId) .eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())) > 0; } public List<FollowRelEntity> queryAll() { // todo 会丢失关注后 取消关注的情况,怎么选择? return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>() .eq(FollowRelEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode()) .orderByAsc(FollowRelEntity::getUpdateTime)); } }