CollectionService.java 9.05 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 boolean 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
            collectionMapper.updateById(queryCollection);
刘基明's avatar
刘基明 committed
43
            return false;
刘基明's avatar
刘基明 committed
44 45 46
        } else {
            CollectionEntity entity = CollectionEntity.builder()
                    .collectionType(type.getCode())
刘基明's avatar
刘基明 committed
47
                    .userId(userId)
刘基明's avatar
刘基明 committed
48
                    .targetId(themeId)
刘基明's avatar
刘基明 committed
49
                    .collectionTime(LocalDateTime.now())
刘基明's avatar
刘基明 committed
50 51 52
                    .build();

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

刘基明's avatar
刘基明 committed
57

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

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

刘基明's avatar
刘基明 committed
91

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

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

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


    }

刘基明's avatar
刘基明 committed
132

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

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


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

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

    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
184
}