package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.VideoLessonPurchaseRecordDao;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonEvaluateSearch;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonEvaluate;
import com.yonge.cooleshow.biz.dal.dao.VideoLessonEvaluateDao;
import com.yonge.cooleshow.biz.dal.service.VideoLessonEvaluateService;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonEvaluateVo;
import com.yonge.cooleshow.biz.dal.wordfilter.WordFilter;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * VideoLessonEvaluateService服务实现类
 *
 * @author yzp
 * @version v1.0
 * @date 2022-03-26 00:01:38
 **/
@Service
public class VideoLessonEvaluateServiceImpl extends ServiceImpl<VideoLessonEvaluateDao, VideoLessonEvaluate> implements VideoLessonEvaluateService {

    private final static Logger log = LoggerFactory.getLogger(VideoLessonEvaluateServiceImpl.class);

    @Autowired
    private VideoLessonEvaluateDao videoLessonEvaluateDao;
    @Autowired
    private VideoLessonPurchaseRecordDao videoLessonPurchaseRecordDao;
    @Autowired
    private WordFilter wordFilter;

    public VideoLessonEvaluateDao getDao() {
        return videoLessonEvaluateDao;
    }

    /**
     * @Description: 视频课评价
     * @Author: cy
     * @Date: 2022/4/8
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void evaluate(VideoLessonEvaluate evaluate, SysUser sysUser) {
        if (wordFilter.include(evaluate.getContent(), 3)) {
            throw new BizException("文本包含敏感信息");
        }
        Long userId = sysUser.getId();
//        String userType = sysUser.getUserType();
        Long videoId = evaluate.getVideoId();

        //根据课程id查询已购学生id
        List<Long> studentIdList = videoLessonPurchaseRecordDao.selectStudentByDetail(videoId);
        //根据课程id查询老师id
        Long teacherId = videoLessonPurchaseRecordDao.selectTeacherByDetail(videoId);

        //校验用户是否已购该课或未本课老师
        if (!(userId.equals(teacherId)) && !studentIdList.contains(userId)) {
            throw new BizException("未购买该课程无法参与讨论");
        }

//        List<String> typeList = Arrays.asList(userType.split(","));
//        if (typeList.contains("TEACHER")) {
//            evaluate.setIsTeacher(YesOrNoEnum.YES);
//        }
        //当前用户为课程创建者  是:老师  否:学生
        if (teacherId == userId) {
            evaluate.setIsTeacher(YesOrNoEnum.YES);
        }

        evaluate.setStudentId(userId);
        videoLessonEvaluateDao.insert(evaluate);
    }

    /**
     * @Description: 查询视频课评论
     * @Author: cy
     * @Date: 2022/4/11
     */
    public IPage<VideoLessonEvaluateVo> selectEvaluate(IPage<VideoLessonEvaluateVo> page, VideoLessonEvaluateSearch search) {
        return page.setRecords(baseMapper.selectEvaluate(page, search));
    }
}