package com.qmth.distributed.print.api.mark;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.service.BasicExamService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicExam;
import com.qmth.teachcloud.common.entity.BasicSemester;
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.mark.bean.archivescore.ArchiveScoreQuery;
import com.qmth.teachcloud.mark.bean.archivescore.ArchiveStudentQuery;
import com.qmth.teachcloud.mark.bean.archivescore.ScoreReportVo;
import com.qmth.teachcloud.mark.service.MarkPaperService;
import com.qmth.teachcloud.mark.service.MarkStudentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 成绩管理 前端控制器
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Api(tags = "成绩管理")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_MARK + "/archive")
public class MarkArchiveController {

    @Autowired
    private MarkPaperService markPaperService;
    @Autowired
    private MarkStudentService markStudentService;
    @Resource
    private BasicExamService basicExamService;
    @Resource
    private BasicSemesterService basicSemesterService;

    @ApiOperation(value = "成绩管理列表")
    @RequestMapping(value = "score/list", method = RequestMethod.POST)
    public Result scoreList(@Validated ArchiveScoreQuery query) {
        return ResultUtil.ok(markPaperService.scoreList(query));
    }

    @ApiOperation(value = "班级详情列表")
    @RequestMapping(value = "student/list", method = RequestMethod.POST)
    public Result studentList(@Validated ArchiveStudentQuery query) {
        return ResultUtil.ok(markStudentService.studentList(query));
    }

    @ApiOperation(value = "成绩导出")
    @RequestMapping(value = "score/export", method = RequestMethod.POST)
    public void scoreExport(@Validated ArchiveStudentQuery query, HttpServletResponse response) {
        markStudentService.scoreExport(query, response);
    }

    @ApiOperation(value = "成绩下载")
    @RequestMapping(value = "score/download", method = RequestMethod.POST)
    public void scoreDownload(@Validated ArchiveStudentQuery query, HttpServletResponse response) {
        markStudentService.scoreDownload(query, response);
    }

    @ApiOperation(value = "成绩导出")
    @RequestMapping(value = "/score/report/download", method = RequestMethod.POST)
    public void scoreReportDownload(@RequestBody JSONObject jsonObject, HttpServletResponse response) {
        markStudentService.scoreReportDownload(jsonObject, response);
    }

    @ApiOperation(value = "成绩报告")
    @RequestMapping(value = "score/report", method = RequestMethod.POST)
    public Result scoreReport(@Validated ArchiveStudentQuery query) {
        ScoreReportVo scoreReportVo = markStudentService.scoreReport(query);
        BasicExam basicExam = basicExamService.getById(query.getExamId());
        BasicSemester basicSemester = basicSemesterService.getById(basicExam.getSemesterId());
        scoreReportVo.setExamName(basicExam.getName());
        scoreReportVo.setSemesterName(basicSemester.getName());
        return ResultUtil.ok(scoreReportVo);
    }
}
