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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.CourseScheduleDao;
import com.yonge.cooleshow.biz.dal.dao.CourseScheduleRepliedDao;
import com.yonge.cooleshow.biz.dal.dao.CourseScheduleStudentPaymentDao;
import com.yonge.cooleshow.biz.dal.dto.search.MyCourseSearch;
import com.yonge.cooleshow.biz.dal.entity.CourseSchedule;
import com.yonge.cooleshow.biz.dal.entity.CourseScheduleReplied;
import com.yonge.cooleshow.biz.dal.entity.CourseScheduleStudentPayment;
import com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum;
import com.yonge.cooleshow.biz.dal.service.CourseRepliedService;
import com.yonge.cooleshow.biz.dal.vo.CourseRepliedVo;
import com.yonge.cooleshow.biz.dal.vo.MyRepliedVo;
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;

/**
 * @Author: cy
 * @Date: 2022/4/12
 */
@Service
public class CourseRepliedServiceImpl extends ServiceImpl<CourseScheduleRepliedDao, CourseScheduleReplied> implements CourseRepliedService {
    private final static Logger log = LoggerFactory.getLogger(CourseRepliedServiceImpl.class);

    @Autowired
    private CourseScheduleRepliedDao repliedDao;
    @Autowired
    private CourseScheduleDao scheduleDao;
    @Autowired
    private CourseScheduleStudentPaymentDao paymentDao;

    public CourseScheduleRepliedDao getDao() {
        return repliedDao;
    }

    /**
     * @Description: 首页-我的课程-课程详情-评价陪练课
     * @Author: cy
     * @Date: 2022/4/12
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void replied(CourseScheduleReplied replied, SysUser sysUser) {
        Long userId = sysUser.getId();
        Long scheduleId = replied.getCourseScheduleId();
        Long studentId = replied.getStudentId();

        CourseSchedule courseSchedule = scheduleDao.selectById(scheduleId);
        if (courseSchedule == null) {
            throw new RuntimeException("课程不存在");
        }
        if (courseSchedule.getStatus().equals(CourseScheduleEnum.NOT_START.getCode())) {
            throw new RuntimeException("课程未开始无法评论");
        }

        //当前用户为老师
        if (userId == courseSchedule.getTeacherId()) {
            if (studentId == null) {
                throw new RuntimeException("评价的学生id不能为空");
            }
            isPayment(studentId, scheduleId);
            replied.setScore(null);
            replied.setStudentReplied(null);
            if (!repliedIsNull(replied)) {
                repliedDao.updateReplied(replied);
                return;
            }
            repliedDao.insert(replied);
            return;
        }

        //当前用户为学员
        isPayment(userId, scheduleId);
        replied.setStudentId(userId);
        replied.setTeacherReplied(null);
        if (!repliedIsNull(replied)) {
            repliedDao.updateReplied(replied);
            return;
        }
        repliedDao.insert(replied);
    }

    /**
     * 判断评论是否存在
     * @param replied
     * @return
     */
    public boolean repliedIsNull(CourseScheduleReplied replied) {
        QueryWrapper<CourseScheduleReplied> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id_", replied.getStudentId());
        queryWrapper.eq("course_schedule_id_", replied.getCourseScheduleId());
        queryWrapper.eq("course_group_id_", replied.getCourseGroupId());
        queryWrapper.eq("course_group_type_", CourseScheduleEnum.PRACTICE.getCode());
        CourseScheduleReplied scheduleReplied = repliedDao.selectOne(queryWrapper);
        return scheduleReplied == null ? true : false;
    }

    /**
     * 校验当前用户是否购买课程
     * @param userId     学员id
     * @param scheduleId 课程id
     */
    public void isPayment(Long userId, Long scheduleId) {
        QueryWrapper<CourseScheduleStudentPayment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id_", userId);
        wrapper.eq("course_id_", scheduleId);
        wrapper.eq("course_type_", CourseScheduleEnum.PRACTICE.getCode());
        CourseScheduleStudentPayment payment = paymentDao.selectOne(wrapper);
        if (payment == null) {
            throw new RuntimeException("学生未购买该课无法评论");
        }
    }

    /**
     * @Description: 首页-我的课程-课程详情-查询评价
     * @Author: cy
     * @Date: 2022/4/12
     */
    @Override
    public CourseRepliedVo selectReplied(CourseScheduleReplied replied) {
//        QueryWrapper<CourseScheduleReplied> wrapper = new QueryWrapper<>();
//        wrapper.eq("course_group_type_", CourseScheduleEnum.PRACTICE);
//        wrapper.eq("student_id_", replied.getStudentId());
//        wrapper.eq("course_schedule_id_", replied.getCourseScheduleId());
//        wrapper.eq("course_group_id_", replied.getCourseGroupId());
        CourseRepliedVo repliedVo=repliedDao.selectReplied(replied);
        return repliedVo;
    }

    /**
     * @Description: 首页-我收到的评价
     * @Author: cy
     * @Date: 2022/4/13
     */
    @Override
    public IPage<MyRepliedVo> myReplied(IPage<MyRepliedVo> page, MyCourseSearch search) {
        return page.setRecords(repliedDao.myReplied(page, search));
    }

}