package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.CourseGroupCreateDto;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.dal.page.GroupCourseScheduleQueryInfo;
import com.ym.mec.biz.service.CoursesGroupService;
import com.ym.mec.biz.service.GroupClassService;
import com.ym.mec.biz.service.GroupCourseScheduleService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2020/3/8
 */
@Api(tags = "对外课程组服务")
@RequestMapping("courseGroup")
@RestController
public class CourseGroupController extends BaseController {

    @Autowired
    private CoursesGroupService coursesGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private GroupClassService groupClassService;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private GroupCourseScheduleService groupCourseScheduleService;

    @ApiOperation(value = "创建课程组")
    @PostMapping("/createCourseGroup")
    public HttpResponseResult createCourseGroup(@RequestBody CourseGroupCreateDto courseGroupCreateInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        Teacher teacher = teacherDao.get(sysUser.getId());
        if(Objects.isNull(teacher)){
            return failed("教师不存在");
        }
        courseGroupCreateInfo.getCoursesGroup().setOrganId(teacher.getOrganId());
        courseGroupCreateInfo.getCoursesGroup().setTeacherId(sysUser.getId());
        return coursesGroupService.createCourseGroup(courseGroupCreateInfo);
    }

    @ApiOperation(value = "获取课程组列表")
    @GetMapping("/findTeacherCourseGroups")
    public HttpResponseResult findTeacherCourseGroups(GroupCourseScheduleQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        return succeed(coursesGroupService.findTeacherCourseGroups(sysUser.getId(),queryInfo));
    }

    @ApiOperation(value = "获取课程对应班级头部信息")
    @GetMapping("/getGroupHeadInfo")
    public HttpResponseResult getGroupHeadInfo(Long courseScheduleId){
        return succeed(courseScheduleId);
    }

    @ApiOperation(value = "获取课程组下指定教师的课表")
    @GetMapping("/findGroupCourses")
    public HttpResponseResult findGroupCourses(GroupCourseScheduleQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(groupCourseScheduleService.findGroupCourses(queryInfo));
    }

    @ApiOperation(value = "关闭课程组")
    @PostMapping("/cancelCourseGroup")
    public HttpResponseResult cancelCourseGroup(Long groupId){
        groupClassService.cancelGroup(groupId, GroupType.COMM, null);
        return succeed();
    }

}
