CollectionService.java 4.43 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
import com.tanpu.community.dao.entity.community.CollectionEntity;
import com.tanpu.community.dao.mapper.community.CollectionMapper;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
9 10 11 12 13 14 15 16 17 18
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

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

刘基明's avatar
刘基明 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    // 若不存在则新增,若存在则修改deleteTag
    public void addIfNotExist(String themeId, String userId, CollectionTypeEnum type) {
        // 判断是否存在
        CollectionEntity queryCollection = getTargetCollection(themeId, userId, type);
        if (queryCollection != null) {
            Integer oldDeleteTag = queryCollection.getDeleteTag();
            queryCollection.setDeleteTag(ConvertUtil.deleteTagShift(oldDeleteTag));
            collectionMapper.updateById(queryCollection);
        } else {
            CollectionEntity entity = CollectionEntity.builder()
                    .collectionType(type.getCode())
                    .authorId(userId)
                    .targetId(themeId)
                    .createBy(userId)
                    .build();

            collectionMapper.insert(entity);
        }
刘基明's avatar
刘基明 committed
37 38
    }

刘基明's avatar
刘基明 committed
39 40 41 42

    //根据用户、主题、类型查询
    public CollectionEntity getTargetCollection(String themeId, String userId, CollectionTypeEnum type) {
        return collectionMapper.selectOne(new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
43
                .eq(CollectionEntity::getCollectionType, type.getCode())
刘基明's avatar
刘基明 committed
44 45 46 47 48 49
                .eq(CollectionEntity::getAuthorId, userId)
                .eq(CollectionEntity::getTargetId, themeId));
    }

    // 根据用户id获取点赞列表
    public List<CollectionEntity> getLikeListByUser(String userId) {
刘基明's avatar
刘基明 committed
50
        return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
51 52
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
53 54
    }

刘基明's avatar
刘基明 committed
55 56 57

    // 根据用户id获取收藏列表
    public List<CollectionEntity> getBookListByUser(String userId) {
刘基明's avatar
刘基明 committed
58
        return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
刘基明's avatar
刘基明 committed
59 60
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
61 62
    }

刘基明's avatar
刘基明 committed
63 64
    // 统计单个主题的点赞量
    public Long getLikeAmountByThemeId(String themeId) {
刘基明's avatar
刘基明 committed
65 66
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getTargetId, themeId)
刘基明's avatar
刘基明 committed
67 68
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())))
刘基明's avatar
刘基明 committed
69 70 71
                .size();
    }

刘基明's avatar
刘基明 committed
72 73
    // 统计多个主题的点赞量
    public Long getLikeAmountByThemeIds(List<String> themeIds) {
刘基明's avatar
刘基明 committed
74 75
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .in(CollectionEntity::getTargetId, themeIds)
刘基明's avatar
刘基明 committed
76 77
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode()))
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
刘基明's avatar
刘基明 committed
78 79 80
                .size();
    }

刘基明's avatar
刘基明 committed
81 82
    //统计单个主题的收藏量
    public Long getBookAmountByThemeId(String themeId) {
刘基明's avatar
刘基明 committed
83 84
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getTargetId, themeId)
刘基明's avatar
刘基明 committed
85 86
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode()))
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
刘基明's avatar
刘基明 committed
87 88 89
                .size();
    }

刘基明's avatar
刘基明 committed
90 91
    //统多个主题的收藏量
    public Long getBookAmountByThemeIds(List<String> themeIds) {
刘基明's avatar
刘基明 committed
92 93
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .in(CollectionEntity::getTargetId, themeIds)
刘基明's avatar
刘基明 committed
94 95
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode()))
                .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
刘基明's avatar
刘基明 committed
96
                .size();
刘基明's avatar
刘基明 committed
97 98 99
    }

}