package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.community.api.constants.CollectionTypeEnum;
import com.tanpu.community.dao.entity.community.CollectionEntity;
import com.tanpu.community.dao.mapper.community.CollectionMapper;
import org.springframework.stereotype.Service;

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

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

    public void addCollection(String themeId, String userId, CollectionTypeEnum type){
        // todo 判断是否存在
        
        CollectionEntity collectionEntity = new CollectionEntity();
        collectionEntity.setCollectionType(type.getCode());
        collectionEntity.setAuthorId(userId);
        collectionEntity.setTargetId(themeId);
        collectionEntity.setCreateBy(userId);
        collectionMapper.insert(collectionEntity);
    }

    //根据用户id获取点赞列表
    public List<CollectionEntity> getLikeListByUser(String userId){
        return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode()));
    }

    //根据用户id获取收藏列表
    public List<CollectionEntity> getBookListByUser(String userId){
        return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getCollectionType,CollectionTypeEnum.BOOK.getCode()));
    }

    //统计主题的点赞量
    public Long getLikeAmountByThemeId(String themeId){
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getTargetId, themeId)
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())))
                .size();
    }

    //统计主题集合的点赞量
    public Long getLikeAmountByThemeIds(List<String> themeIds){
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .in(CollectionEntity::getTargetId, themeIds)
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())))
                .size();
    }

    //统计主题的收藏量
    public Long getBookAmountByThemeId(String themeId){
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .eq(CollectionEntity::getTargetId, themeId)
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())))
                .size();
    }

    //统计主题集合的收藏量
    public Long getBookAmountByThemeIds(List<String> themeIds){
        return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
                .in(CollectionEntity::getTargetId, themeIds)
                .eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())))
                .size();
    }

}