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

import com.cxy.classroll.common.execption.CxyException;
import com.cxy.classroll.common.result.Result;
import com.cxy.classroll.common.result.ResultCodeEnum;
import com.cxy.classroll.model.entity.Lesson;
import com.cxy.classroll.teacher.client.TeacherFeignClient;
import com.cxy.classroll.teacher.service.TeacherWebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TeacherWebServiceImpl implements TeacherWebService {

    @Autowired
    TeacherFeignClient teacherFeignClient;

    @Override
    public List<Lesson> getTeacherLessonsList(Long id) {
        Result<List<Lesson>> result = teacherFeignClient.getTeacherLessonList(id);
        Integer code = result.getCode();
        if (code != 200) {
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        List<Lesson> data = result.getData();
        if (data == null) {
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return data;
    }

    @Override
    public Boolean lessonStart(Long lesson_id) {
        Result<Boolean> result = teacherFeignClient.classStart(lesson_id);
        if (result.getCode()!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        Boolean data = result.getData();
        if (data == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return data;
    }

    @Override
    public Boolean lessonEnd(Long lesson_id) {
        Result<Boolean> result = teacherFeignClient.classEnd(lesson_id);
        if (result.getCode()!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        Boolean data = result.getData();
        if (data == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return data;
    }
}
