package com.yonge.cooleshow.bbs.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yonge.cooleshow.bbs.entity.BbsCollect;
import com.yonge.cooleshow.bbs.vo.BbsCollectVo;
import com.yonge.cooleshow.bbs.dto.search.BbsCollectSearch;
import com.yonge.cooleshow.bbs.dao.BbsCollectDao;
import com.yonge.cooleshow.bbs.service.BbsCollectService;


@Service
public class BbsCollectServiceImpl extends ServiceImpl<BbsCollectDao, BbsCollect> implements BbsCollectService {
    private final static Logger log = LoggerFactory.getLogger(BbsCollectServiceImpl.class);

	@Override
    public BbsCollectVo detail(Long id) {
        return baseMapper.detail(id);
    }
    
     @Override
    public IPage<BbsCollectVo> selectPage(IPage<BbsCollectVo> page, BbsCollectSearch query){
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    /**
     * @Description: 收藏/取消收藏
     * @Author: cy
     * @Date: 2022/6/13
     */
    @Override
    public void collect(Long userId, Long articleId) {
        BbsCollect collect = baseMapper.selectOne(Wrappers.<BbsCollect>lambdaQuery()
                .eq(BbsCollect::getArticleId, articleId)
                .eq(BbsCollect::getCollectUserId, userId));
        if (collect==null){
            BbsCollect bbsCollect = new BbsCollect();
            bbsCollect.setArticleId(articleId);
            bbsCollect.setCollectUserId(userId);
            baseMapper.insert(bbsCollect);
        }else {
            baseMapper.delete(Wrappers.<BbsCollect>lambdaQuery()
                    .eq(BbsCollect::getArticleId, articleId)
                    .eq(BbsCollect::getCollectUserId, userId));
        }
    }
}