package com.qmth.paper.library.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.business.bean.result.EditResult;
import com.qmth.paper.library.business.service.PaperScanTaskService;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
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 javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * 试卷扫描任务管理
 */

@Api(tags = "扫描任务管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_PAPER + "/scan_task")
@Validated
public class PaperScanTaskController {
    @Resource
    private PaperScanTaskService paperScanTaskService;

    @ApiOperation(value = "查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findPaperScanTaskPage(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                                        @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                                        @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                        @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(paperScanTaskService.paperScanTaskPage(examId, courseCode, pageNumber, pageSize));
    }

    @ApiOperation(value = "任务详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findPaperScanTaskDetailPage(@ApiParam(value = "扫描任务ID", required = true) @RequestParam Long paperScanTaskId,
                                              @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                              @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(paperScanTaskService.findPaperScanTaskDetailPageByScanTaskId(paperScanTaskId, pageNumber, pageSize));
    }

    @ApiOperation(value = "清除数据")
    @RequestMapping(value = "/clear_data", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result clearScanData(@ApiParam(value = "扫描任务ID", required = true) @RequestParam Long paperScanTaskId) {
        paperScanTaskService.clearScanData(paperScanTaskId);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "单个考生清除数据")
    @RequestMapping(value = "/clear_single_data", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "删除成功", response = EditResult.class) })
    public Result clearScanSingleData(@ApiParam(value = "扫描任务ID", required = true) @RequestParam Long paperScanTaskId,
                                      @ApiParam(value = "考生ID", required = true) @RequestParam Long studentId) {
        paperScanTaskService.clearSingleStudentData(paperScanTaskId, studentId);
        return ResultUtil.ok();
    }
}
