CollectionService.java 8.61 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;
10
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
11
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
12
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
13 14

import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
15
import java.time.LocalDateTime;
16 17 18 19 20 21
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
刘基明's avatar
刘基明 committed
22
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
23 24 25 26 27 28

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

刘基明's avatar
刘基明 committed
29

刘基明's avatar
刘基明 committed
30
    // 若不存在则新增,若存在则修改deleteTag
刘基明's avatar
刘基明 committed
31
    @Transactional
刘基明's avatar
刘基明 committed
32
    public void saveOrUpdate(String themeId, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
33
        // 判断记录是否存在,无论是否删除
刘基明's avatar
刘基明 committed
34 35 36 37 38
        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
39
        if (queryCollection != null) {
刘基明's avatar
刘基明 committed
40 41
            queryCollection.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
            queryCollection.setCollectionTime(LocalDateTime.now());
刘基明's avatar
刘基明 committed
42 43 44 45
            collectionMapper.updateById(queryCollection);
        } else {
            CollectionEntity entity = CollectionEntity.builder()
                    .collectionType(type.getCode())
刘基明's avatar
刘基明 committed
46
                    .userId(userId)
刘基明's avatar
刘基明 committed
47
                    .targetId(themeId)
刘基明's avatar
刘基明 committed
48
                    .collectionTime(LocalDateTime.now())
刘基明's avatar
刘基明 committed
49 50 51 52
                    .build();

            collectionMapper.insert(entity);
        }
刘基明's avatar
刘基明 committed
53 54
    }

刘基明's avatar
刘基明 committed
55

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

66 67 68 69 70 71 72 73 74
    //根据用户、主题、类型查询未删除对象
    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);
    }

刘基明's avatar
刘基明 committed
75 76
    //根据用户、主题、类型查询未删除对象
    public Set<String> getTargets(List<String> targetIds, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
77 78 79
        if (CollectionUtils.isEmpty(targetIds)){
            return new HashSet<>();
        }
刘基明's avatar
刘基明 committed
80
        LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
81 82
                .eq(CollectionEntity::getCollectionType, type.getCode())
                .eq(CollectionEntity::getUserId, userId)
刘基明's avatar
刘基明 committed
83 84 85 86
                .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
87 88
    }

刘基明's avatar
刘基明 committed
89

刘基明's avatar
刘基明 committed
90
    // 根据用户id和行为type获取target_id列表
91
    public Set<String> getSetByUser(String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
92
        return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
93
                .eq(CollectionEntity::getUserId, userId)
刘基明's avatar
刘基明 committed
94 95 96
                .eq(CollectionEntity::getCollectionType, type.getCode())
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
                .stream().map(CollectionEntity::getTargetId).collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
97 98
    }

99 100 101 102 103
    // 根据用户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
104 105
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(CollectionEntity::getCollectionTime))
106 107 108
                .stream().map(CollectionEntity::getTargetId).collect(Collectors.toList());
    }

109 110 111 112 113 114 115 116 117 118
    // 根据用户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();
刘基明's avatar
刘基明 committed
119
            queryWrapper.lt(CollectionEntity::getCollectionTime, target.getCollectionTime());
120 121 122 123 124 125 126 127 128 129
        }
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
        }
        return collectionMapper.selectList(queryWrapper)
                .stream().map(CollectionEntity::getTargetId).collect(Collectors.toList());


    }

刘基明's avatar
刘基明 committed
130

刘基明's avatar
刘基明 committed
131
    // 统计单个对象(主题、评论)的数量(点赞、收藏)
刘基明's avatar
刘基明 committed
132 133 134 135 136
    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
137 138
    }

刘基明's avatar
刘基明 committed
139 140
    // 统计多个对象(主题、评论)的数量(点赞、收藏)
    public Map<String, Integer> getCountMapByType(List<String> targetIds, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
141
        if (CollectionUtils.isEmpty(targetIds)) {
刘基明's avatar
刘基明 committed
142 143
            return new HashMap<>();
        }
刘基明's avatar
刘基明 committed
144 145 146
        LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getCollectionType, type.getCode())
                .eq(CollectionEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())
刘基明's avatar
刘基明 committed
147 148 149 150 151 152
                .in(CollectionEntity::getTargetId, targetIds).groupBy(CollectionEntity::getTargetId);
        return collectionMapper.selectCountByTargetIds(queryWrapper).stream()
                .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
    }


刘基明's avatar
刘基明 committed
153 154
    // 统计多个对象(主题、评论)的数量(点赞、收藏)
    public Integer getCountByTypeAndIds(List<String> targetIds, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
155
        return collectionMapper.selectCount((new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
156
                .in(CollectionEntity::getTargetId, targetIds)
刘基明's avatar
刘基明 committed
157 158
                .eq(CollectionEntity::getCollectionType, type.getCode()))
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
159
    }
刘基明's avatar
刘基明 committed
160

刘基明's avatar
刘基明 committed
161
    //逻辑删除,修改delete_tag
刘基明's avatar
刘基明 committed
162
    @Transactional
刘基明's avatar
刘基明 committed
163
    public void delete(String themeId, String userId, CollectionTypeEnum type) {
164
        CollectionEntity queryCollection = queryCollection(themeId, userId, type);
刘基明's avatar
刘基明 committed
165 166 167 168
        if (queryCollection != null) {
            queryCollection.setDeleteTag(DeleteTagEnum.DELETED.getCode());
            queryCollection.setUncollectionTime(LocalDateTime.now());
            collectionMapper.updateById(queryCollection);
刘基明's avatar
刘基明 committed
169
        } else {
刘基明's avatar
刘基明 committed
170 171
//            throw new BizException("Collection对象未找到,themeId:" + themeId + ",userId:" + userId + ",type:" + type.getCode());
            return;
刘基明's avatar
刘基明 committed
172
        }
刘基明's avatar
刘基明 committed
173
    }
刘基明's avatar
刘基明 committed
174
}