package com.qmth.teachcloud.report.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicAttachment;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.UploadFileEnum;
import com.qmth.teachcloud.common.service.BasicAttachmentService;
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.TBExamStudentResult;
import com.qmth.teachcloud.report.business.enums.OrderEnum;
import com.qmth.teachcloud.report.business.service.TBExamStudentService;
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 org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * <p>
 * 考生表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2021-06-01
 */
@Api(tags = "教研分析学生模块Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.reportSchool}/student")
public class TBExamStudentController {

    @Autowired
    private TBExamStudentService tbExamStudentService;

    @Autowired
    private BasicAttachmentService basicAttachmentService;

    @ApiOperation(value = "开课成绩查询")
    @RequestMapping(value = "/list_teach_college_result", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "开课成绩查询信息", response = TBExamStudentResult.class)})
    public Result listTeachCollegeResult(
            @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
            @ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
            @ApiParam(value = "课程代码", required = true) @RequestParam(required = false) String courseCode,
            @ApiParam(value = "考试类型", required = true) @RequestParam(required = false) Boolean current,
            @ApiParam(value = "考查学院ID", required = true) @RequestParam(required = false) Long inspectCollegeId,
            @ApiParam(value = "教师ID", required = true) @RequestParam(required = false) Long teacherId,
            @ApiParam(value = "模糊查询", required = true) @RequestParam(required = false) String studentParam,
            @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(tbExamStudentService.listTeachCollegeResult(SystemConstant.convertIdToLong(semesterId), examId, courseCode, current, inspectCollegeId, teacherId, studentParam, pageNumber, pageSize, column, order));
    }

    @ApiOperation(value = "开课成绩查询-导入异常考试类型")
    @RequestMapping(value = "/import_abnormal_data", method = RequestMethod.POST)
    public Result importAbnormalData(
            @ApiParam(value = "考试id", required = true) @RequestParam Long examId,
            @ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file) {
        BasicAttachment basicAttachment = null;
        try {
            basicAttachment = basicAttachmentService.saveAttachment(file, ServletUtil.getRequestMd5(), UploadFileEnum.FILE);
            if (Objects.isNull(basicAttachment)) {
                throw ExceptionResultEnum.ATTACHMENT_ERROR.exception();
            } else {
                tbExamStudentService.importFile(examId, file);
                return ResultUtil.ok(true);
            }
        } catch (Exception e) {
            if (Objects.nonNull(basicAttachment)) {
                basicAttachmentService.deleteAttachment(basicAttachment);
            }
            if (e instanceof ApiException) {
                ResultUtil.error((ApiException) e, e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
        }
        return null;
    }

    @ApiOperation(value = "开课成绩查询-导出查询结果")
    @RequestMapping(value = "/export_teach_college_result", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "开课成绩查询信息", response = TBExamStudentResult.class)})
    public void exportTeachCollegeResult(
            @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
            @ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
            @ApiParam(value = "课程代码", required = true) @RequestParam(required = false) String courseCode,
            @ApiParam(value = "考试类型", required = true) @RequestParam(required = false) Boolean current,
            @ApiParam(value = "考查学院ID", required = true) @RequestParam(required = false) Long inspectCollegeId,
            @ApiParam(value = "教师ID", required = true) @RequestParam(required = false) Long teacherId,
            @ApiParam(value = "模糊查询", required = true) @RequestParam(required = false) String studentParam,
            @ApiParam(value = "排序列", required = false) @RequestParam(required = false) String column,
            @ApiParam(value = "排序规则", required = false) @RequestParam(required = false) OrderEnum order) throws Exception {
        tbExamStudentService.exportTeachCollegeResult(SystemConstant.convertIdToLong(semesterId), examId, courseCode, current, inspectCollegeId, teacherId, studentParam, column, order);
    }

    @ApiOperation(value = "考查成绩查询")
    @RequestMapping(value = "/list_inspect_college_result", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "开课成绩查询信息", response = TBExamStudentResult.class)})
    public Result listInspectCollegeResult(
            @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
            @ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
            @ApiParam(value = "课程代码", required = true) @RequestParam(required = false) String courseCode,
            @ApiParam(value = "考试类型", required = true) @RequestParam(required = false) Boolean current,
            @ApiParam(value = "教师ID", required = true) @RequestParam(required = false) Long teacherId,
            @ApiParam(value = "班级ID", required = true) @RequestParam(required = false) Long clazzId,
            @ApiParam(value = "模糊查询", required = true) @RequestParam(required = false) String studentParam,
            @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) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(tbExamStudentService.listInspectCollegeResult(SystemConstant.convertIdToLong(semesterId), examId, courseCode, current, sysUser.getOrgId(), teacherId, clazzId, studentParam, pageNumber, pageSize, column, order));
    }

    @ApiOperation(value = "考查成绩查询-导出查询结果")
    @RequestMapping(value = "/export_inspect_college_result", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "开课成绩查询信息", response = TBExamStudentResult.class)})
    public void exportInspectCollegeResult(
            @ApiParam(value = "学期id", required = true) @RequestParam String semesterId,
            @ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
            @ApiParam(value = "课程代码", required = true) @RequestParam(required = false) String courseCode,
            @ApiParam(value = "考试类型", required = true) @RequestParam(required = false) Boolean current,
            @ApiParam(value = "开课学院ID", required = true) @RequestParam(required = false) Long teachCollegeId,
            @ApiParam(value = "教师ID", required = true) @RequestParam(required = false) Long teacherId,
            @ApiParam(value = "班级ID", required = true) @RequestParam(required = false) Long clazzId,
            @ApiParam(value = "模糊查询", required = true) @RequestParam(required = false) String studentParam,
            @ApiParam(value = "排序列", required = false) @RequestParam(required = false) String column,
            @ApiParam(value = "排序规则", required = false) @RequestParam(required = false) OrderEnum order) throws Exception {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        tbExamStudentService.exportInspectCollegeResult(SystemConstant.convertIdToLong(semesterId), examId, courseCode, current, sysUser.getOrgId(), teacherId, clazzId, studentParam, column, order);
    }
}