package com.qmth.distributed.print.api;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.annotation.BOOL;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.open.GradeOpenPageParams;
import com.qmth.distributed.print.business.bean.params.open.GradeOpenParams;
import com.qmth.distributed.print.business.service.OpenApiService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicSchool;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.AuthThirdUtil;
import com.qmth.teachcloud.common.util.JacksonUtil;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Optional;

/**
 * <p>
 * 知学知考开放接口前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2022-04-26
 */
@Api(tags = "知学知考开放接口Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.open}")
@Validated
public class OpenApiController {
    private static final Logger log = LoggerFactory.getLogger(OpenApiController.class);

    @Resource
    private OpenApiService openApiService;

    @ApiOperation(value = "试卷基础配置查询")
    @ApiResponses({@ApiResponse(code = 200, message = "试卷基础配置查询", response = Object.class)})
    @PostMapping("/paper_config")
    @Aac(auth = BOOL.FALSE)
    public Result paperConfig(@ApiParam(value = "接收试卷配置数据信息", required = true) @RequestBody String result) throws UnsupportedEncodingException {
        Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
        String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
        log.info("paperConfigSelect进来了,result:{}", decodeJson);
        GradeOpenParams gradeOpenParams = JacksonUtil.readJson(decodeJson, GradeOpenParams.class);
        Optional.ofNullable(gradeOpenParams).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("转换后的数据为空"));
        gradeOpenParams.validParams();
        BasicSchool basicSchool = AuthThirdUtil.hasPermission();
        return ResultUtil.ok(openApiService.getPaperConfig(basicSchool, gradeOpenParams.getThirdExamId(), gradeOpenParams.getGradeCourseCode()));
    }

    @ApiOperation(value = "试卷考察点查询")
    @ApiResponses({@ApiResponse(code = 200, message = "试卷考察点查询", response = Object.class)})
    @PostMapping("/paper_dimension")
    @Aac(auth = BOOL.FALSE)
    public Result paperDimension(@ApiParam(value = "试卷考察点查询", required = true) @RequestBody String result) throws UnsupportedEncodingException {

        Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
        String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
        log.info("paperDimensionSelect进来了,result:{}", decodeJson);
        GradeOpenParams gradeOpenParams = JacksonUtil.readJson(decodeJson, GradeOpenParams.class);
        Optional.ofNullable(gradeOpenParams).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("转换后的数据为空"));
        gradeOpenParams.validParams();
        BasicSchool basicSchool = AuthThirdUtil.hasPermission();
        return ResultUtil.ok(openApiService.getPaperDimension(basicSchool, gradeOpenParams.getThirdExamId(), gradeOpenParams.getGradeCourseCode()));
    }

    @ApiOperation(value = "试卷蓝图查询")
    @ApiResponses({@ApiResponse(code = 200, message = "试卷蓝图查询", response = Object.class)})
    @PostMapping("/paper_structure")
    @Aac(auth = BOOL.FALSE)
    public Result paperStructure(@ApiParam(value = "试卷考察点查询", required = true) @RequestBody String result) throws UnsupportedEncodingException {
        Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
        String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
        log.info("paperStructureSelect进来了,result:{}", decodeJson);
        GradeOpenParams gradeOpenParams = JacksonUtil.readJson(decodeJson, GradeOpenParams.class);
        Optional.ofNullable(gradeOpenParams).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("转换后的数据为空"));
        gradeOpenParams.validParams();
        BasicSchool basicSchool = AuthThirdUtil.hasPermission();
        return ResultUtil.ok(openApiService.getPaperStructure(basicSchool, gradeOpenParams.getThirdExamId(), gradeOpenParams.getGradeCourseCode()));
    }

    @ApiOperation(value = "试卷评价模型查询")
    @ApiResponses({@ApiResponse(code = 200, message = "试卷评价模型查询", response = Object.class)})
    @PostMapping("/paper_evaluation")
    @Aac(auth = BOOL.FALSE)
    public Result paperEvaluation(@ApiParam(value = "试卷评价模型查询", required = true) @RequestBody String result) throws UnsupportedEncodingException {
        Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
        String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
        log.info("paperEvaluation进来了,result:{}", decodeJson);
        GradeOpenParams gradeOpenParams = JacksonUtil.readJson(decodeJson, GradeOpenParams.class);
        Optional.ofNullable(gradeOpenParams).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("转换后的数据为空"));
        gradeOpenParams.validParams();
        BasicSchool basicSchool = AuthThirdUtil.hasPermission();
        return ResultUtil.ok(openApiService.getPaperEvaluation(basicSchool, gradeOpenParams.getThirdExamId(), gradeOpenParams.getGradeCourseCode()));
    }

    @ApiOperation(value = "考生成绩查询")
    @ApiResponses({@ApiResponse(code = 200, message = "考生成绩查询", response = Object.class)})
    @PostMapping("/exam_student_score")
    @Aac(auth = BOOL.FALSE)
    public Result examStudentScore(@ApiParam(value = "考生成绩查询", required = true) @RequestBody String result) throws UnsupportedEncodingException {
        Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
        String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
        log.info("examStudentScore进来了,result:{}", decodeJson);
        GradeOpenPageParams gradeOpenPageParams = JacksonUtil.readJson(decodeJson, GradeOpenPageParams.class);
        Optional.ofNullable(gradeOpenPageParams).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("转换后的数据为空"));
        gradeOpenPageParams.validParams();
        BasicSchool basicSchool = AuthThirdUtil.hasPermission();
        return ResultUtil.ok(openApiService.listExamStudentScore(basicSchool, gradeOpenPageParams.getThirdExamId(), gradeOpenPageParams.getGradeCourseCode(), gradeOpenPageParams.getPageNumber(), gradeOpenPageParams.getPageSize()));
    }
}
