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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.bbs.dao.BbsArticleDao;
import com.yonge.cooleshow.bbs.dao.BbsReplyAuthRecordDao;
import com.yonge.cooleshow.bbs.dto.ReplyAuditDto;
import com.yonge.cooleshow.bbs.entity.BbsArticle;
import com.yonge.cooleshow.bbs.entity.BbsReplyAuthRecord;
import com.yonge.cooleshow.bbs.enums.ArticleEnum;
import com.yonge.cooleshow.bbs.enums.YesOrNoEnum;
import com.yonge.cooleshow.bbs.service.BbsArticleService;
import com.yonge.cooleshow.bbs.service.BbsReplyAuthRecordService;
import com.yonge.cooleshow.bbs.vo.BbsReplyAuditVo;
import com.yonge.toolset.base.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yonge.cooleshow.bbs.entity.BbsReply;
import com.yonge.cooleshow.bbs.vo.BbsReplyVo;
import com.yonge.cooleshow.bbs.dto.search.BbsReplySearch;
import com.yonge.cooleshow.bbs.dao.BbsReplyDao;
import com.yonge.cooleshow.bbs.service.BbsReplyService;


@Service
public class BbsReplyServiceImpl extends ServiceImpl<BbsReplyDao, BbsReply> implements BbsReplyService {
    private final static Logger log = LoggerFactory.getLogger(BbsReplyServiceImpl.class);

    @Autowired
    private BbsReplyAuthRecordService replyAuthRecordService;
    @Autowired
    private BbsReplyAuthRecordDao authRecordDao;
    @Autowired
    private BbsArticleDao articleDao;

    @Override
    public BbsReplyVo detail(Long id) {
        return baseMapper.detail(id);
    }

    @Override
    public IPage<BbsReplyVo> selectPage(IPage<BbsReplyVo> page, BbsReplySearch query) {
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    @Override
    public IPage<BbsReplyVo> selectCommentPage(IPage<BbsReplyVo> page, BbsReplySearch query) {
        return page.setRecords(baseMapper.selectCommentPage(page, query));
    }

    @Override
    public IPage<BbsReplyVo> selectReplyPage(IPage<BbsReplyVo> page, BbsReplySearch query) {
        return page.setRecords(baseMapper.selectReplyPage(page, query));
    }

    @Override
    public IPage<BbsReplyAuditVo> selectAuditListPage(IPage<BbsReplyAuditVo> page, BbsReplySearch query) {
        return page.setRecords(baseMapper.selectAuditListPage(page, query));
    }

    @Override
    public boolean audit(ReplyAuditDto auditDto) {
        BbsReply bbsReply = this.getById(auditDto.getReplyId());
        if (bbsReply == null) {
            throw new BizException("未找到当前评论信息");
        }
        if (!ArticleEnum.DOING.getCode().equals(bbsReply.getStatus().getCode())) {
            throw new BizException("评论已经审核");
        }
        bbsReply.setStatus(auditDto.getStatus());
        this.updateById(bbsReply);
        replyAuthRecordService.audit(auditDto);
        return true;
    }

    /**
     * @Description: 评论文章
     * @Author: cy
     * @Date: 2022/6/14
     */
    @Override
    public void replyArticle(BbsReply reply) {
        BbsArticle article = articleDao.selectById(reply.getArticleId());
        if (article == null) {
            throw new BizException("评论的文章不存在");
        }

        String paramValue = baseMapper.selectConfig("bbs_reply_audit");
        if (paramValue != null && paramValue.equals(YesOrNoEnum.YES.getCode().toString())) {//需审核
            reply.setStatus(ArticleEnum.DOING);
            baseMapper.insert(reply);

            BbsReplyAuthRecord authRecord = new BbsReplyAuthRecord();
            authRecord.setReplyId(reply.getId());
            authRecord.setAuditState(ArticleEnum.DOING);
            authRecordDao.insert(authRecord);
        } else {//不用审核
            reply.setStatus(ArticleEnum.PASS);
            baseMapper.insert(reply);
        }
    }
}