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

import com.qmth.boot.core.exception.ParameterException;
import com.qmth.teachcloud.common.entity.BasicClazz;
import com.qmth.teachcloud.mark.entity.MarkStudent;
import com.qmth.teachcloud.mark.entity.ScanBatch;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.mark.bean.UpdateTimeVo;
import com.qmth.teachcloud.mark.bean.omredit.OmrEditDomain;
import com.qmth.teachcloud.mark.bean.papermigrate.PaperMigrateDomain;
import com.qmth.teachcloud.mark.bean.papermigrate.PaperMigrateVo;
import com.qmth.teachcloud.mark.bean.scananswer.AnswerQueryDomain;
import com.qmth.teachcloud.mark.bean.scananswer.SheetUploadVo;
import com.qmth.teachcloud.mark.dto.refix.AnswerRefixDomain;
import com.qmth.teachcloud.mark.service.MarkStudentService;
import com.qmth.teachcloud.mark.service.ScanBatchService;
import com.qmth.teachcloud.mark.service.ScanPaperService;
import com.qmth.teachcloud.mark.service.ScanRefixBatchService;

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_SCAN + "/answer")
public class ScanAnswerController extends BaseController {

    @Autowired
    private ScanRefixBatchService refixBatchService;

    @Autowired
    private ScanBatchService scanBatchService;
    @Autowired
    private ScanPaperService scanPaperService;
    @Autowired
    private MarkStudentService markStudentService;

    @ApiOperation(value = "答题卡二次识别")
    @RequestMapping(value = "refix", method = RequestMethod.POST)
    public Result refix(@Validated @RequestBody AnswerRefixDomain query) {
        MarkStudent student = markStudentService.getById(query.getStudentId());
        if (student == null) {
            throw new ParameterException("考生信息未找到");
        }
        validMarkPaperForScan(student.getExamId(), student.getCoursePaperId());
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(refixBatchService.answerRefix(user.getId(), query));
    }

    @ApiOperation(value = "答题卡扫描原图上传")
    @RequestMapping(value = "sheet/upload", method = RequestMethod.POST)
    public Result sheetUpload(@RequestParam Long batchId, @RequestParam String coursePaperId,
                              @RequestParam String studentCode, @RequestParam Integer paperNumber, @RequestParam Integer pageIndex,
                              @RequestParam MultipartFile file, @RequestParam String md5) {
        ScanBatch batch = scanBatchService.getById(batchId);
        checkBatchStatus(batch);
        validMarkPaperForScan(batch.getExamId(), batch.getCoursePaperId());
        SheetUploadVo sheetUploadVo = scanBatchService.sheetUpload(batch, coursePaperId, studentCode, paperNumber, pageIndex, file, md5);
        return ResultUtil.ok(sheetUploadVo);
    }

    @ApiOperation(value = "查询答题卡扫描详情")
    @RequestMapping(value = "query", method = RequestMethod.POST)
    public Result query(@Validated AnswerQueryDomain query) {
        return ResultUtil.ok(markStudentService.query(query));
    }

    @ApiOperation(value = "查询答题卡扫描概要")
    @RequestMapping(value = "summary", method = RequestMethod.POST)
    public Result summary(@Validated AnswerQueryDomain query) {
        return ResultUtil.ok(markStudentService.summary(query));
    }

    @ApiOperation(value = "修改答题卡识别结果")
    @RequestMapping(value = "omr/edit", method = RequestMethod.POST)
    public UpdateTimeVo omrEdit(@Validated @RequestBody OmrEditDomain domain) {
        validMarkPaperForScan(domain.getExamId(), domain.getCoursePaperId());
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return markStudentService.omrEdit(user.getId(), domain);
    }

    @ApiOperation(value = "修改答题卡扫描图片绑定考生")
    @RequestMapping(value = "paper/migrate", method = RequestMethod.POST)
    public PaperMigrateVo paperMigrate(@Validated @RequestBody PaperMigrateDomain domain) {
        validMarkPaperForScan(domain.getExamId(), domain.getCoursePaperId());
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return scanPaperService.paperMigrate(user, domain);
    }
}
