package com.tanpu.community.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tanpu.biz.common.enums.community.CollectionTypeEnum; import com.tanpu.community.api.enums.DeleteTagEnum; import com.tanpu.community.dao.entity.community.CollectionEntity; import com.tanpu.community.dao.entity.community.TimesCountEntity; import com.tanpu.community.dao.mapper.community.CollectionMapper; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @Service public class CollectionService { @Resource private CollectionMapper collectionMapper; // 若不存在则新增,若存在则修改deleteTag @Transactional public boolean saveOrUpdate(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); CollectionEntity queryCollection = collectionMapper.selectOne(queryWrapper); if (queryCollection != null) { queryCollection.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode()); queryCollection.setCollectionTime(LocalDateTime.now()); collectionMapper.updateById(queryCollection); return false; } else { CollectionEntity entity = CollectionEntity.builder() .collectionType(type.getCode()) .userId(userId) .targetId(targetId) .collectionTime(LocalDateTime.now()) .build(); collectionMapper.insert(entity); return true; } } //根据用户、主题、类型查询未删除对象 public CollectionEntity queryCollection(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 queryIncludeDelete(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); return collectionMapper.selectOne(queryWrapper); } //根据用户、主题、类型查询未删除对象 public Set<String> getTargets(List<String> targetIds, String userId, CollectionTypeEnum type) { if (CollectionUtils.isEmpty(targetIds)) { return new HashSet<>(); } LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getUserId, userId) .in(CollectionEntity::getTargetId, targetIds) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return collectionMapper.selectList(queryWrapper).stream().map(CollectionEntity::getTargetId) .collect(Collectors.toSet()); } // 根据用户id和行为type获取target_id列表 public Set<String> getSetByUser(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()); } // 根据用户id和行为type获取target_id列表 public List<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()) .orderByDesc(CollectionEntity::getCollectionTime)) .stream().map(CollectionEntity::getTargetId).collect(Collectors.toList()); } // 根据用户id和行为type获取target_id列表 public List<String> getListByUser(String userId, CollectionTypeEnum type, String lastId, Integer pageSize) { LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getUserId, userId) .eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()) .orderByDesc(CollectionEntity::getCollectionTime); if (StringUtils.isNotEmpty(lastId)) { CollectionEntity target = queryIncludeDelete(lastId, userId, type); if (target == null) return Collections.emptyList(); queryWrapper.lt(CollectionEntity::getCollectionTime, target.getCollectionTime()); } if (pageSize != null) { queryWrapper.last("limit " + pageSize); } return collectionMapper.selectList(queryWrapper) .stream().map(CollectionEntity::getTargetId).collect(Collectors.toList()); } // 统计单个对象(主题、评论)的数量(点赞、收藏) 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 Map<String, Integer> getCountMapByType(List<String> targetIds, CollectionTypeEnum type) { if (CollectionUtils.isEmpty(targetIds)) { return new HashMap<>(); } LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()) .in(CollectionEntity::getTargetId, targetIds).groupBy(CollectionEntity::getTargetId); return collectionMapper.selectCountByTargetIds(queryWrapper).stream() .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes)); } // 统计多个对象(主题、评论)的数量(点赞、收藏) 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 = queryCollection(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; } } public List<CollectionEntity> queryALlLikeTheme() { return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>() .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE_THEME.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()) .orderByAsc(CollectionEntity::getCreateTime)); } }