package com.tanpu.community.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tanpu.community.api.constants.CollectionTypeEnum; import com.tanpu.community.api.constants.DeleteTagEnum; import com.tanpu.community.dao.entity.community.CollectionEntity; import com.tanpu.community.dao.mapper.community.CollectionMapper; import com.tanpu.community.util.ConvertUtil; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class CollectionService { @Resource private CollectionMapper collectionMapper; // 若不存在则新增,若存在则修改deleteTag public void addIfNotExist(String themeId, String userId, CollectionTypeEnum type) { // 判断是否存在 CollectionEntity queryCollection = getTargetCollection(themeId, userId, type); if (queryCollection != null) { Integer oldDeleteTag = queryCollection.getDeleteTag(); queryCollection.setDeleteTag(ConvertUtil.deleteTagShift(oldDeleteTag)); collectionMapper.updateById(queryCollection); } else { CollectionEntity entity = CollectionEntity.builder() .collectionType(type.getCode()) .authorId(userId) .targetId(themeId) .createBy(userId) .build(); collectionMapper.insert(entity); } } //根据用户、主题、类型查询 public CollectionEntity getTargetCollection(String themeId, String userId, CollectionTypeEnum type) { return collectionMapper.selectOne(new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, type) .eq(CollectionEntity::getAuthorId, userId) .eq(CollectionEntity::getTargetId, themeId)); } // 根据用户id获取点赞列表 public List<CollectionEntity> getLikeListByUser(String userId) { return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())); } // 根据用户id获取收藏列表 public List<CollectionEntity> getBookListByUser(String userId) { return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())); } // 统计单个主题的点赞量 public Long getLikeAmountByThemeId(String themeId) { return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getTargetId, themeId) .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))) .size(); } // 统计多个主题的点赞量 public Long getLikeAmountByThemeIds(List<String> themeIds) { return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>() .in(CollectionEntity::getTargetId, themeIds) .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())) .size(); } //统计单个主题的收藏量 public Long getBookAmountByThemeId(String themeId) { return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getTargetId, themeId) .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())) .size(); } //统多个主题的收藏量 public Long getBookAmountByThemeIds(List<String> themeIds) { return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>() .in(CollectionEntity::getTargetId, themeIds) .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())) .size(); } }