CollectionService.java 9.06 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;

29

刘基明's avatar
刘基明 committed
30

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

            collectionMapper.insert(entity);
54

刘基明's avatar
刘基明 committed
55
            return true;
刘基明's avatar
刘基明 committed
56
        }
刘基明's avatar
刘基明 committed
57 58
    }

刘基明's avatar
刘基明 committed
59

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

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

刘基明's avatar
刘基明 committed
93

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

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

113
    // 根据用户id和行为type获取target_id列表
刘基明's avatar
刘基明 committed
114
    public List<String> getListByUser(String userId, CollectionTypeEnum type, String lastId, Integer pageSize) {
115 116 117 118 119 120 121 122
        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
123
            queryWrapper.lt(CollectionEntity::getCollectionTime, target.getCollectionTime());
124 125 126 127 128 129 130 131 132 133
        }
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
        }
        return collectionMapper.selectList(queryWrapper)
                .stream().map(CollectionEntity::getTargetId).collect(Collectors.toList());


    }

刘基明's avatar
刘基明 committed
134

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

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


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

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

    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));

    }
刘基明's avatar
刘基明 committed
186
}