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

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

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

刘基明's avatar
刘基明 committed
22

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

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

刘基明's avatar
刘基明 committed
44

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

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

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

刘基明's avatar
刘基明 committed
73

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

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