CollectionService.java 4.87 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.common.exception.BizException;
刘基明's avatar
刘基明 committed
5 6
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
7 8 9
import com.tanpu.community.dao.entity.community.CollectionEntity;
import com.tanpu.community.dao.mapper.community.CollectionMapper;
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
10
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
11 12

import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
13
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
14
import java.util.List;
刘基明's avatar
刘基明 committed
15 16
import java.util.Set;
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 addIfNotExist(String themeId, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
27 28
        // 判断记录是否存在,无论是否删除
        CollectionEntity queryCollection = getTargetCollection(themeId, userId, type);
刘基明's avatar
刘基明 committed
29
        if (queryCollection != null) {
刘基明's avatar
刘基明 committed
30 31
            queryCollection.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
            queryCollection.setCollectionTime(LocalDateTime.now());
刘基明's avatar
刘基明 committed
32 33 34 35
            collectionMapper.updateById(queryCollection);
        } else {
            CollectionEntity entity = CollectionEntity.builder()
                    .collectionType(type.getCode())
刘基明's avatar
刘基明 committed
36
                    .userId(userId)
刘基明's avatar
刘基明 committed
37
                    .targetId(themeId)
刘基明's avatar
刘基明 committed
38
                    .collectionTime(LocalDateTime.now())
刘基明's avatar
刘基明 committed
39 40 41 42
                    .build();

            collectionMapper.insert(entity);
        }
刘基明's avatar
刘基明 committed
43 44
    }

刘基明's avatar
刘基明 committed
45

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

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

刘基明's avatar
刘基明 committed
65 66
    // 根据用户id和行为type获取target_id列表
    public Set<String> getListByUser(String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
67
        return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
68
                .eq(CollectionEntity::getUserId,userId)
刘基明's avatar
刘基明 committed
69 70 71
                .eq(CollectionEntity::getCollectionType, type.getCode())
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
                .stream().map(CollectionEntity::getTargetId).collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
72 73
    }

刘基明's avatar
刘基明 committed
74

刘基明's avatar
刘基明 committed
75
    // 统计单个对象(主题、评论)的数量(点赞、收藏)
刘基明's avatar
刘基明 committed
76 77 78 79 80
    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
81 82
    }

刘基明's avatar
刘基明 committed
83 84
    // 统计多个对象(主题、评论)的数量(点赞、收藏)
    public Integer getCountByTypeAndIds(List<String> targetIds, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
85
        return collectionMapper.selectCount((new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
86
                .in(CollectionEntity::getTargetId, targetIds)
刘基明's avatar
刘基明 committed
87 88
                .eq(CollectionEntity::getCollectionType, type.getCode()))
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
89
    }
刘基明's avatar
刘基明 committed
90
    //逻辑删除,修改delete_tag
刘基明's avatar
刘基明 committed
91
    @Transactional
刘基明's avatar
刘基明 committed
92
    public void delete(String themeId, String userId, CollectionTypeEnum type) {
刘基明's avatar
刘基明 committed
93
        CollectionEntity queryCollection = getNotDeleteTargetCollection(themeId, userId, type);
刘基明's avatar
刘基明 committed
94 95 96 97
        if (queryCollection != null) {
            queryCollection.setDeleteTag(DeleteTagEnum.DELETED.getCode());
            queryCollection.setUncollectionTime(LocalDateTime.now());
            collectionMapper.updateById(queryCollection);
刘基明's avatar
刘基明 committed
98 99
        } else {
            throw new BizException("Collection对象未找到,themeId:" + themeId + ",userId:" + userId + ",type:" + type.getCode());
刘基明's avatar
刘基明 committed
100
        }
刘基明's avatar
刘基明 committed
101
    }
刘基明's avatar
刘基明 committed
102
}