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

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.ScanStatus;
import com.qmth.teachcloud.common.enums.mark.MarkPaperStatus;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.mark.bean.UpdateTimeVo;
import com.qmth.teachcloud.mark.bean.student.AbsentManualUpdateVo;
import com.qmth.teachcloud.mark.bean.student.StudentQuery;
import com.qmth.teachcloud.mark.entity.MarkPaper;
import com.qmth.teachcloud.mark.entity.MarkStudent;
import com.qmth.teachcloud.mark.service.MarkStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 考生 前端控制器
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Api(tags = "扫描-考生")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SCAN + "/student")
public class ScanStudentController extends BaseController {
    @Autowired
    private MarkStudentService markStudentService;

    @ApiOperation(value = "考生查询")
    @RequestMapping(value = "find", method = RequestMethod.POST)
    public Result student(@Validated StudentQuery query) {
        return ResultUtil.ok(markStudentService.findOne(query));
    }

    @ApiOperation(value = "更新缺考状态")
    @PostMapping("absent/update")
    public AbsentManualUpdateVo absentManualUpdate(@ApiParam(name = "考试ID", required = true) @RequestParam Long examId,
                                                   @ApiParam(name = "课程试卷号", required = true) @RequestParam String coursePaperId,
                                                   @ApiParam(name = "学号", required = true) @RequestParam String studentCode,
                                                   @ApiParam(name = "状态", required = true) @RequestParam ScanStatus status) {
        validMarkPaperForScan(examId, coursePaperId);
        return markStudentService.absentManualUpdate(examId, coursePaperId, studentCode, status);
    }

    @ApiOperation(value = "人工绑定-任务确认")
    @PostMapping("/assign/confirm")
    public Result confirm(@RequestParam Long studentId) {
        MarkStudent markStudent = markStudentService.getById(studentId);
        if (markStudent == null) {
            throw ExceptionResultEnum.ERROR.exception("考生不存在");
        }
        validMarkPaperForScan(markStudent.getExamId(), markStudent.getCoursePaperId());
        return ResultUtil.ok(markStudentService.updateAssignConfirm(studentId, true));
    }

    @ApiOperation(value = "识别缺考-任务确认")
    @PostMapping("confirm")
    public UpdateTimeVo confirm(@RequestParam Long examId, @RequestParam String coursePaperId,
                                @RequestParam String studentCode,
                                @RequestParam(required = false) Boolean omrAbsent) {
        validMarkPaperForScan(examId, coursePaperId);
        return markStudentService.confirm(examId, coursePaperId, studentCode, omrAbsent);
    }

    /**
     * 导出评卷员工作量
     */
    @ApiOperation(value = "未扫描-导出")
    @RequestMapping(value = "/unexist/export", method = RequestMethod.POST)
    public void exportUnexist(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                              @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                              @ApiParam(value = "试卷编号") @RequestParam(required = false) String coursePaperId,
                              HttpServletResponse response) {
        markStudentService.exportUnexist(examId, courseCode, coursePaperId, response);
    }
}
