CollectionService.java 6.72 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
张辰's avatar
张辰 committed
4
import com.tanpu.biz.common.enums.community.CollectionTypeEnum;
刘基明's avatar
刘基明 committed
5
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
6
import com.tanpu.community.dao.entity.community.CollectionEntity;
刘基明's avatar
刘基明 committed
7
import com.tanpu.community.dao.entity.community.TimesCountEntity;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.dao.mapper.community.CollectionMapper;
刘基明's avatar
刘基明 committed
9
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
10
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
11
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
12 13

import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
14
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
15
import java.util.*;
刘基明's avatar
刘基明 committed
16
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
17 18 19 20 21 22

@Service
public class CollectionService {
    @Resource
    private CollectionMapper collectionMapper;

刘基明's avatar
刘基明 committed
23

刘基明's avatar
刘基明 committed
24
    // 若不存在则新增,若存在则修改deleteTag
刘基明's avatar
刘基明 committed
25
    @Transactional
刘基明's avatar
刘基明 committed
26
    public void saveOrUpdate(String themeId, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
27
        // 判断记录是否存在,无论是否删除
刘基明's avatar
刘基明 committed
28 29 30 31 32
        LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getCollectionType, type.getCode())
                .eq(CollectionEntity::getUserId, userId)
                .eq(CollectionEntity::getTargetId, themeId);
        CollectionEntity queryCollection = collectionMapper.selectOne(queryWrapper);
刘基明's avatar
刘基明 committed
33
        if (queryCollection != null) {
刘基明's avatar
刘基明 committed
34 35
            queryCollection.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
            queryCollection.setCollectionTime(LocalDateTime.now());
刘基明's avatar
刘基明 committed
36 37 38 39
            collectionMapper.updateById(queryCollection);
        } else {
            CollectionEntity entity = CollectionEntity.builder()
                    .collectionType(type.getCode())
刘基明's avatar
刘基明 committed
40
                    .userId(userId)
刘基明's avatar
刘基明 committed
41
                    .targetId(themeId)
刘基明's avatar
刘基明 committed
42
                    .collectionTime(LocalDateTime.now())
刘基明's avatar
刘基明 committed
43 44 45 46
                    .build();

            collectionMapper.insert(entity);
        }
刘基明's avatar
刘基明 committed
47 48
    }

刘基明's avatar
刘基明 committed
49

刘基明's avatar
刘基明 committed
50
    //根据用户、主题、类型查询未删除对象
刘基明's avatar
刘基明 committed
51
    public CollectionEntity getTarget(String targetId, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
52
        LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
53
                .eq(CollectionEntity::getCollectionType, type.getCode())
刘基明's avatar
刘基明 committed
54
                .eq(CollectionEntity::getUserId, userId)
刘基明's avatar
刘基明 committed
55
                .eq(CollectionEntity::getTargetId, targetId)
刘基明's avatar
刘基明 committed
56 57
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return collectionMapper.selectOne(queryWrapper);
刘基明's avatar
刘基明 committed
58 59
    }

刘基明's avatar
刘基明 committed
60 61
    //根据用户、主题、类型查询未删除对象
    public Set<String> getTargets(List<String> targetIds, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
62 63 64
        if (CollectionUtils.isEmpty(targetIds)){
            return new HashSet<>();
        }
刘基明's avatar
刘基明 committed
65
        LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
66 67
                .eq(CollectionEntity::getCollectionType, type.getCode())
                .eq(CollectionEntity::getUserId, userId)
刘基明's avatar
刘基明 committed
68 69 70 71
                .in(CollectionEntity::getTargetId, targetIds)
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return collectionMapper.selectList(queryWrapper).stream().map(CollectionEntity::getTargetId)
                .collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
72 73
    }

刘基明's avatar
刘基明 committed
74

刘基明's avatar
刘基明 committed
75
    // 根据用户id和行为type获取target_id列表
76
    public Set<String> getSetByUser(String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
77
        return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
78
                .eq(CollectionEntity::getUserId, userId)
刘基明's avatar
刘基明 committed
79 80 81
                .eq(CollectionEntity::getCollectionType, type.getCode())
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
                .stream().map(CollectionEntity::getTargetId).collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
82 83
    }

84 85 86 87 88
    // 根据用户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())
刘基明's avatar
刘基明 committed
89 90
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(CollectionEntity::getCollectionTime))
91 92 93
                .stream().map(CollectionEntity::getTargetId).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
94

刘基明's avatar
刘基明 committed
95
    // 统计单个对象(主题、评论)的数量(点赞、收藏)
刘基明's avatar
刘基明 committed
96 97 98 99 100
    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())));
刘基明's avatar
刘基明 committed
101 102
    }

刘基明's avatar
刘基明 committed
103 104
    // 统计多个对象(主题、评论)的数量(点赞、收藏)
    public Map<String, Integer> getCountMapByType(List<String> targetIds, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
105
        if (CollectionUtils.isEmpty(targetIds)) {
刘基明's avatar
刘基明 committed
106 107
            return new HashMap<>();
        }
刘基明's avatar
刘基明 committed
108 109 110 111 112 113 114
        LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>().eq(CollectionEntity::getCollectionType, 1)
                .in(CollectionEntity::getTargetId, targetIds).groupBy(CollectionEntity::getTargetId);
        return collectionMapper.selectCountByTargetIds(queryWrapper).stream()
                .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
    }


刘基明's avatar
刘基明 committed
115 116
    // 统计多个对象(主题、评论)的数量(点赞、收藏)
    public Integer getCountByTypeAndIds(List<String> targetIds, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
117
        return collectionMapper.selectCount((new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
118
                .in(CollectionEntity::getTargetId, targetIds)
刘基明's avatar
刘基明 committed
119 120
                .eq(CollectionEntity::getCollectionType, type.getCode()))
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
121
    }
刘基明's avatar
刘基明 committed
122

刘基明's avatar
刘基明 committed
123
    //逻辑删除,修改delete_tag
刘基明's avatar
刘基明 committed
124
    @Transactional
刘基明's avatar
刘基明 committed
125
    public void delete(String themeId, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
126
        CollectionEntity queryCollection = getTarget(themeId, userId, type);
刘基明's avatar
刘基明 committed
127 128 129 130
        if (queryCollection != null) {
            queryCollection.setDeleteTag(DeleteTagEnum.DELETED.getCode());
            queryCollection.setUncollectionTime(LocalDateTime.now());
            collectionMapper.updateById(queryCollection);
刘基明's avatar
刘基明 committed
131
        } else {
刘基明's avatar
刘基明 committed
132 133
//            throw new BizException("Collection对象未找到,themeId:" + themeId + ",userId:" + userId + ",type:" + type.getCode());
            return;
刘基明's avatar
刘基明 committed
134
        }
刘基明's avatar
刘基明 committed
135
    }
刘基明's avatar
刘基明 committed
136
}