package com.qmth.teachcloud.report.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.service.BasicSemesterService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.report.business.bean.result.TBExamResult;
import com.qmth.teachcloud.report.business.bean.result.TBExamStudentResult;
import com.qmth.teachcloud.report.business.entity.TBExam;
import com.qmth.teachcloud.report.business.entity.TBExamCourse;
import com.qmth.teachcloud.report.business.enums.PublishStatusEnum;
import com.qmth.teachcloud.report.business.service.TBExamCourseService;
import com.qmth.teachcloud.report.business.service.TBExamService;
import com.qmth.teachcloud.report.business.service.TBExamStudentService;
import io.swagger.annotations.*;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api(tags = "教研分析学校公用Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.reportSchool}")
public class SysReportController {

    @Resource
    TBExamService tbExamService;

    @Resource
    TBExamCourseService tbExamCourseService;

    @Resource
    TBExamStudentService tbExamStudentService;

    @Resource
    BasicSemesterService basicSemesterService;

    @ApiOperation(value = "学期列表")
    @RequestMapping(value = "/common/list_semester", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试信息", response = TBExamResult.class)})
    public Result listSemester(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId) {
        QueryWrapper<TBExam> tbExamQueryWrapper = new QueryWrapper<>();
        tbExamQueryWrapper.select(" DISTINCT id,semester_id,exam_time ").eq("school_id", SystemConstant.convertIdToLong(schoolId)).orderByDesc("exam_time");
        List<TBExam> tbExamList = tbExamService.list(tbExamQueryWrapper);
        List<TBExamResult> tbExamResultList = null;
        if (Objects.nonNull(tbExamList) && tbExamList.size() > 0) {
            tbExamResultList = new ArrayList<>();
            for (TBExam tbExam : tbExamList) {
                tbExamResultList.add(new TBExamResult(tbExam.getSemesterId(), basicSemesterService.getById(tbExam.getSemesterId()).getName()));
            }
        }
        return ResultUtil.ok(tbExamResultList);
    }

    @ApiOperation(value = "考试列表")
    @RequestMapping(value = "/common/list_exam", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试信息", response = TBExamResult.class)})
    public Result listExam(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                           @ApiParam(value = "学期id", required = true) @RequestParam String semesterId) {
        QueryWrapper<TBExam> tbExamQueryWrapper = new QueryWrapper<>();
        tbExamQueryWrapper.select(" DISTINCT id,exam_name as examName,exam_code as examCode ")
                .eq("school_id", SystemConstant.convertIdToLong(schoolId))
                .eq("semester_id", SystemConstant.convertIdToLong(semesterId));
        List<TBExam> tbExamList = tbExamService.list(tbExamQueryWrapper);
        List<TBExamResult> tbExamResultList = null;
        if (Objects.nonNull(tbExamList) && tbExamList.size() > 0) {
            tbExamResultList = new ArrayList<>();
            for (TBExam tbExam : tbExamList) {
                tbExamResultList.add(new TBExamResult(tbExam.getId(), tbExam.getExamName(), tbExam.getExamCode()));
            }
        }
        return ResultUtil.ok(tbExamResultList);
    }

    @ApiOperation(value = "课程列表")
    @RequestMapping(value = "/common/list_course", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "科目信息", response = TBExamCourse.class)})
    public Result listCourse(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                             @ApiParam(value = "考试id", required = true) @RequestParam String examId,
                             @ApiParam(value = "是否考查学院", required = true) @RequestParam boolean inspect,
                             @ApiParam(value = "是否缺考，false：没缺考，true：缺考", required = false) @RequestParam(required = false) Boolean absent,
                             @ApiParam(value = "是否本届，false：非本届，true：本届", required = false) @RequestParam(required = false) Boolean current,
                             @ApiParam(value = "发布状态（UN_COMPUTE：未计算、COMPUTING：计算中、 UN_PUBLISH：未发布、PUBLISH：已发布）", required = false) @RequestParam(required = false) PublishStatusEnum status) {
        return ResultUtil.ok(tbExamCourseService.findCourseList(SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(examId), inspect, absent, current, status));
    }

    @ApiOperation(value = "任课老师-课程列表")
    @RequestMapping(value = "/common/list_course_teach", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "科目信息", response = TBExamCourse.class)})
    public Result listCourseTeach(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                                  @ApiParam(value = "考试id", required = true) @RequestParam String examId,
                                  @ApiParam(value = "是否缺考，false：没缺考，true：缺考", required = false) @RequestParam(required = false) Boolean absent,
                                  @ApiParam(value = "是否本届，false：非本届，true：本届", required = false) @RequestParam(required = false) Boolean current,
                                  @ApiParam(value = "发布状态（UN_COMPUTE：未计算、COMPUTING：计算中、 UN_PUBLISH：未发布、PUBLISH：已发布）", required = false) @RequestParam(required = false) PublishStatusEnum status) {
        if (Objects.isNull(status)){
            status = PublishStatusEnum.PUBLISH;
        }
        return ResultUtil.ok(tbExamCourseService.findCourseList(SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(examId), absent, current, status));
    }

    @ApiOperation(value = "开课学院列表")
    @RequestMapping(value = "/common/list_teach_college", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考生信息", response = TBExamStudentResult.class)})
    public Result listTeachCollege(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                                   @ApiParam(value = "考试id", required = true) @RequestParam String examId,
                                   @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                   @ApiParam(value = "是否缺考，false：没缺考，true：缺考", required = false) @RequestParam(required = false) Boolean absent,
                                   @ApiParam(value = "是否本届，false：非本届，true：本届", required = false) @RequestParam(required = false) Boolean current,
                                   @ApiParam(value = "发布状态（UN_COMPUTE：未计算、COMPUTING：计算中、 UN_PUBLISH：未发布、PUBLISH：已发布）", required = false) @RequestParam(required = false) PublishStatusEnum status) {
        return ResultUtil.ok(tbExamStudentService.selectTeachCollegeList(SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(examId), courseCode, absent, current, status));
    }

    @ApiOperation(value = "考查学院列表")
    @RequestMapping(value = "/common/list_inspect_college", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考生信息", response = TBExamStudentResult.class)})
    public Result listInspectCollege(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                                     @ApiParam(value = "考试id", required = true) @RequestParam String examId,
                                     @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                     @ApiParam(value = "是否缺考，false：没缺考，true：缺考", required = false) @RequestParam(required = false) Boolean absent,
                                     @ApiParam(value = "是否本届，false：非本届，true：本届", required = false) @RequestParam(required = false) Boolean current,
                                     @ApiParam(value = "发布状态（UN_COMPUTE：未计算、COMPUTING：计算中、 UN_PUBLISH：未发布、PUBLISH：已发布）", required = false) @RequestParam(required = false) PublishStatusEnum status) {
        return ResultUtil.ok(tbExamStudentService.selectInspectCollegeList(SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(examId), courseCode, absent, current, status));
    }

    @ApiOperation(value = "教师列表")
    @RequestMapping(value = "/common/list_teacher", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考生信息", response = TBExamStudentResult.class)})
    public Result listTeacher(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                              @ApiParam(value = "考试id", required = true) @RequestParam String examId,
                              @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
                              @ApiParam(value = "考查学院id", required = false) @RequestParam(required = false) String inspectCollegeId,
                              @ApiParam(value = "开课学院id", required = false) @RequestParam(required = false) String teachCollegeId,
                              @ApiParam(value = "是否缺考，false：没缺考，true：缺考", required = false) @RequestParam(required = false) Boolean absent,
                              @ApiParam(value = "是否本届，false：非本届，true：本届", required = false) @RequestParam(required = false) Boolean current,
                              @ApiParam(value = "发布状态（UN_COMPUTE：未计算、COMPUTING：计算中、 UN_PUBLISH：未发布、PUBLISH：已发布）", required = false) @RequestParam(required = false) PublishStatusEnum status) {
        return ResultUtil.ok(tbExamStudentService.selectTeachList(SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(examId), courseCode, Objects.nonNull(inspectCollegeId) ? SystemConstant.convertIdToLong(inspectCollegeId) : null, Objects.nonNull(teachCollegeId) ? SystemConstant.convertIdToLong(teachCollegeId) : null, absent, current, status));
    }

    @ApiOperation(value = "班级列表")
    @RequestMapping(value = "/common/list_class", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考生信息", response = TBExamStudentResult.class)})
    public Result listClass(@ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                            @ApiParam(value = "考试id", required = true) @RequestParam String examId,
                            @ApiParam(value = "考试id", required = false) @RequestParam(required = false) String teacherId,
                            @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode,
                            @ApiParam(value = "考察学院", required = false) @RequestParam(required = false) String inspectCollegeId,
                            @ApiParam(value = "是否缺考，false：没缺考，true：缺考", required = false) @RequestParam(required = false) Boolean absent,
                            @ApiParam(value = "是否本届，false：非本届，true：本届", required = false) @RequestParam(required = false) Boolean current,
                            @ApiParam(value = "发布状态（UN_COMPUTE：未计算、COMPUTING：计算中、 UN_PUBLISH：未发布、PUBLISH：已发布）", required = false) @RequestParam(required = false) PublishStatusEnum status) {
        return ResultUtil.ok(tbExamStudentService.selectClassList(SystemConstant.convertIdToLong(schoolId), SystemConstant.convertIdToLong(examId), SystemConstant.convertIdToLong(teacherId), courseCode, SystemConstant.convertIdToLong(inspectCollegeId), absent, current, status));
    }
}
