package com.qmth.teachcloud.report.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.report.business.bean.result.SurveyTeacherViewResult;
import com.qmth.teachcloud.report.business.bean.result.TeacherClassRankResult;
import com.qmth.teachcloud.report.business.bean.result.TeacherMyClassRankResult;
import com.qmth.teachcloud.report.business.enums.OrderEnum;
import com.qmth.teachcloud.report.business.service.TAExamCourseCollegeTeacherService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 参考课程学院下授课教师维度分析表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2021-06-01
 */
@Api(tags = "教研分析教师模板Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.reportSchool}/teacher")
public class TAExamCourseCollegeTeacherController {

    @Autowired
    private TAExamCourseCollegeTeacherService taExamCourseCollegeTeacherService;

    @ApiOperation(value = "教师各课堂成绩排名")
    @RequestMapping(value = "/list_class_rank", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "教师各课堂成绩排名", response = TeacherClassRankResult.class)})
    public Result listClassRank(
            @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
            @ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
            @ApiParam(value = "课程代码", required = false) @RequestParam(required = false) String courseCode,
            @ApiParam(value = "考查学院ID", required = false) @RequestParam(required = false) Long inspectCollegeId,
            @ApiParam(value = "分页页码", required = true) @RequestParam Integer pageNumber,
            @ApiParam(value = "分页数量", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "排序列", required = false) @RequestParam(required = false) String column,
            @ApiParam(value = "排序规则", required = false) @RequestParam(required = false) OrderEnum order) {
        return ResultUtil.ok(taExamCourseCollegeTeacherService.listClassRank(SystemConstant.convertIdToLong(semesterId), examId, courseCode, inspectCollegeId, pageNumber, pageSize, column, order));
    }

    @ApiOperation(value = "我的课堂考成绩排名")
    @RequestMapping(value = "/list_myclass_rank", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "我的课堂考成绩排名", response = TeacherMyClassRankResult.class)})
    public Result listMyClassRank(
            @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
            @ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
            @ApiParam(value = "课程代码", required = false) @RequestParam(required = false) String courseCode,
            @ApiParam(value = "考查学院ID", required = false) @RequestParam(required = false) Long inspectCollegeId,
            @ApiParam(value = "分页页码", required = true) @RequestParam Integer pageNumber,
            @ApiParam(value = "分页数量", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "排序列", required = false) @RequestParam(required = false) String column,
            @ApiParam(value = "排序规则", required = false) @RequestParam(required = false) OrderEnum order) {
        return ResultUtil.ok(taExamCourseCollegeTeacherService.listMyClassRank(SystemConstant.convertIdToLong(semesterId), examId, courseCode, inspectCollegeId, pageNumber, pageSize, column, order));
    }

    @ApiOperation(value = "我的课堂考试分析-图表接口")
    @RequestMapping(value = "/view", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试科目信息", response = SurveyTeacherViewResult.class)})
    public Result teacherView(@ApiParam(value = "考试id", required = true) @RequestParam String examId,
                              @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
                              @ApiParam(value = "学校id", required = false) @RequestParam(required = false) String schoolId,
                              @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
                              @ApiParam(value = "考查学院id", required = true) @RequestParam String inspectCollegeId) {
        return ResultUtil.ok(taExamCourseCollegeTeacherService.teacherView(Objects.isNull(schoolId) ? SystemConstant.convertIdToLong(String.valueOf(ServletUtil.getRequestHeaderSchoolId())) : SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(semesterId), SystemConstant.convertIdToLong(examId), courseCode, Objects.nonNull(inspectCollegeId) ? SystemConstant.convertIdToLong(inspectCollegeId) : null));
    }

    @ApiOperation(value = "我的课堂考试分析-试题难度得分情况分析接口")
    @RequestMapping(value = "/question_view", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试科目信息", response = SurveyTeacherViewResult.class)})
    public Result teacherQuestionView(@ApiParam(value = "考试id", required = true) @RequestParam String examId,
                                      @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
                                      @ApiParam(value = "学校id", required = false) @RequestParam(required = false) String schoolId,
                                      @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
                                      @ApiParam(value = "学院id", required = false) @RequestParam(required = false) String collegeId) {
        // 道理上讲必须传学院id 但如果老师想看自己的整体情况，不传学院id得到的就是整体情况
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        if (Objects.isNull(sysUser)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return ResultUtil.ok(taExamCourseCollegeTeacherService.teacherQuestionView(Objects.isNull(schoolId) ? SystemConstant.convertIdToLong(String.valueOf(ServletUtil.getRequestHeaderSchoolId())) : SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(semesterId), SystemConstant.convertIdToLong(examId), courseCode, SystemConstant.convertIdToLong(collegeId), sysUser.getId()));
    }

    @ApiOperation(value = "我的课堂考试分析-各难度水平题目上的作答分析接口")
    @RequestMapping(value = "/question_answer_view", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试科目信息", response = SurveyTeacherViewResult.class)})
    public Result teacherQuestionAnswerView(@ApiParam(value = "考试id", required = true) @RequestParam String examId,
                                            @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
                                            @ApiParam(value = "学校id", required = false) @RequestParam(required = false) String schoolId,
                                            @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
                                            @ApiParam(value = "学院id", required = false) @RequestParam(required = false) String collegeId) {
        // 道理上讲必须传学院id 但如果老师想看自己的整体情况，不传学院id得到的就是整体情况
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        if (Objects.isNull(sysUser)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return ResultUtil.ok(taExamCourseCollegeTeacherService.teacherQuestionAnswerView(Objects.isNull(schoolId) ? SystemConstant.convertIdToLong(String.valueOf(ServletUtil.getRequestHeaderSchoolId())) : SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(semesterId), SystemConstant.convertIdToLong(examId), courseCode, SystemConstant.convertIdToLong(collegeId), sysUser.getId()));
    }
}
