package com.cxy.classroll.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxy.classroll.model.entity.Lesson;
import com.cxy.classroll.teacher.mapper.TeacherLessonMapper;
import com.cxy.classroll.teacher.service.TeacherLessonService;
import com.cxy.classroll.teacher.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.dgc.Lease;
import java.util.List;

@Service
@Slf4j
public class TeacherLessonServiceImpl extends ServiceImpl<TeacherLessonMapper, Lesson> implements TeacherLessonService {

    @Autowired
    TeacherLessonMapper teacherLessonMapper;

    @Autowired
    UserService userService;

    @Override
    public List<Lesson> getTeacherLessons(Long teacher_id) {
        LambdaQueryWrapper<Lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lesson::getTeacher_id, teacher_id);
        return teacherLessonMapper.selectList(wrapper);
    }

    @Override
    public Boolean lessonStart(Long lesson_id) {
        LambdaQueryWrapper<Lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lesson::getId,lesson_id);
        Lesson lesson = teacherLessonMapper.selectOne(wrapper);
        Integer status = lesson.getStatus();
        if (status == 1){
            return false;
        }
        lesson.setStatus(1);
        int i = teacherLessonMapper.updateById(lesson);
        if (i==0){
            return false;
        }
        Long teacherId = lesson.getTeacher_id();
        Boolean res = userService.updateUserScore(teacherId, 1);
        if (!res){
            return false;
        }
        return true;
    }

    @Override
    public Boolean lessonEnd(Long lesson_id) {
        LambdaQueryWrapper<Lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lesson::getId,lesson_id);
        Lesson lesson = teacherLessonMapper.selectOne(wrapper);
        Integer status = lesson.getStatus();
        if (status == 0){
            return false;
        }
        lesson.setStatus(0);
        int i = teacherLessonMapper.updateById(lesson);
        if (i==0){
            return false;
        }
        return true;
    }
}
