package com.tanpu.community.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tanpu.community.api.enums.CollectionTypeEnum; import com.tanpu.community.api.enums.DeleteTagEnum; import com.tanpu.community.dao.entity.community.CollectionEntity; import com.tanpu.community.dao.mapper.community.CollectionMapper; 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.Set; import java.util.stream.Collectors; @Service public class CollectionService { @Resource private CollectionMapper collectionMapper; // 若不存在则新增,若存在则修改deleteTag @Transactional public void addIfNotExist(String themeId, String userId, CollectionTypeEnum type) { // 判断记录是否存在,无论是否删除 CollectionEntity queryCollection = getTargetCollection(themeId, userId, type); if (queryCollection != null) { queryCollection.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode()); queryCollection.setCollectionTime(LocalDateTime.now()); collectionMapper.updateById(queryCollection); } else { CollectionEntity entity = CollectionEntity.builder() .collectionType(type.getCode()) .userId(userId) .targetId(themeId) .collectionTime(LocalDateTime.now()) .build(); collectionMapper.insert(entity); } } //根据用户、主题、类型查询未删除对象 public CollectionEntity getNotDeleteTargetCollection(String targetId, String userId, CollectionTypeEnum type) { LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getUserId, userId) .eq(CollectionEntity::getTargetId, targetId) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return collectionMapper.selectOne(queryWrapper); } //根据用户、主题、类型查询记录,包括已删除对象 public CollectionEntity getTargetCollection(String themeId, String userId, CollectionTypeEnum type) { LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getUserId, userId) .eq(CollectionEntity::getTargetId, themeId); return collectionMapper.selectOne(queryWrapper); } // 根据用户id和行为type获取target_id列表 public Set<String> getListByUser(String userId, CollectionTypeEnum type) { return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getUserId,userId) .eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())) .stream().map(CollectionEntity::getTargetId).collect(Collectors.toSet()); } // 统计单个对象(主题、评论)的数量(点赞、收藏) public Integer getCountByTypeAndId(String targetId, CollectionTypeEnum type) { return collectionMapper.selectCount((new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getTargetId, targetId) .eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))); } // 统计多个对象(主题、评论)的数量(点赞、收藏) public Integer getCountByTypeAndIds(List<String> targetIds, CollectionTypeEnum type) { return collectionMapper.selectCount((new LambdaQueryWrapper<CollectionEntity>() .in(CollectionEntity::getTargetId, targetIds) .eq(CollectionEntity::getCollectionType, type.getCode())) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())); } //逻辑删除,修改delete_tag @Transactional public void delete(String themeId, String userId, CollectionTypeEnum type) { CollectionEntity queryCollection = getNotDeleteTargetCollection(themeId, userId, type); if (queryCollection != null) { queryCollection.setDeleteTag(DeleteTagEnum.DELETED.getCode()); queryCollection.setUncollectionTime(LocalDateTime.now()); collectionMapper.updateById(queryCollection); } else { // throw new BizException("Collection对象未找到,themeId:" + themeId + ",userId:" + userId + ",type:" + type.getCode()); return; } } }