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 com.yonge.cooleshow.bbs.dao.BbsArticleDao;
import com.yonge.cooleshow.bbs.dao.BbsAuthRecordDao;
import com.yonge.cooleshow.bbs.dto.search.BbsAuthRecordSearch;
import com.yonge.cooleshow.bbs.entity.BbsArticle;
import com.yonge.cooleshow.bbs.entity.BbsAuthRecord;
import com.yonge.cooleshow.bbs.enums.ArticleEnum;
import com.yonge.cooleshow.bbs.service.BbsAuthRecordService;
import com.yonge.cooleshow.bbs.vo.BbsAuthRecordVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class BbsAuthRecordServiceImpl extends ServiceImpl<BbsAuthRecordDao, BbsAuthRecord> implements BbsAuthRecordService {
    private final static Logger log = LoggerFactory.getLogger(BbsAuthRecordServiceImpl.class);

    @Autowired
    private BbsArticleDao bbsArticleDao;

    /**
     * @Description: 后台管理-审核-查询文章审核列表
     * @Author: cy
     * @Date: 2022/6/11
     */
    @Override
    public IPage<BbsAuthRecordVo> selectPage(IPage<BbsAuthRecordVo> page, BbsAuthRecordSearch query) {
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    /**
     * @Description: 后台管理-审核-修改文章审核状态
     * @Author: cy
     * @Date: 2022/6/11
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAuditState(BbsAuthRecord record) {
        ArticleEnum auditState = record.getAuditState();
        if (auditState.equals(ArticleEnum.PASS)) {
            bbsArticleDao.update(null, Wrappers.<BbsArticle>lambdaUpdate()
                    .eq(BbsArticle::getId, record.getArticleId())
                    .set(BbsArticle::getStatus, record.getAuditState())
                    .set(BbsArticle::getExamineTime, new Date()));
        } else {
            bbsArticleDao.update(null, Wrappers.<BbsArticle>lambdaUpdate()
                    .eq(BbsArticle::getId, record.getArticleId())
                    .set(BbsArticle::getStatus, record.getAuditState()));
        }
        baseMapper.insert(record);
    }
}