package com.qmth.themis.admin.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qmth.themis.business.bean.admin.OpenExamBean;
import com.qmth.themis.business.bean.admin.OpenExamCourseBean;
import com.qmth.themis.business.bean.mobile.MobileAuthorizationBean;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.service.TEOpenService;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 java.io.IOException;

/**
 * 开放接口
 *
 * @Description:
 * @Author: xiatian
 * @Date: 2020-10-19
 */
@Api(tags = "开放接口")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_OPEN)
public class TEOpenController {
    private final static Logger log = LoggerFactory.getLogger(TEOpenController.class);

    @Resource
    private TEOpenService openService;

    @ApiOperation(value = "获取考试详情")
    @RequestMapping(value = "/exam/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试详情信息", response = OpenExamBean.class)})
    public Object examQueryPage(@ApiParam(value = "考试id", required = false) @RequestParam(required = false) Long id,
                                @ApiParam(value = "考试code", required = false) @RequestParam(required = false) String code,
                                @ApiParam(value = "分页页码", required = false) @RequestParam(required = false) Integer pageNumber,
                                @ApiParam(value = "分页数", required = false) @RequestParam(required = false) Integer pageSize) {
        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize < 1 || pageSize > 20) {
            pageSize = 20;
        }
        return openService.examQueryPage(id, StringUtils.trimToNull(code), pageNumber, pageSize);
    }

    @ApiOperation(value = "获取考试课程详情")
    @RequestMapping(value = "/exam/course/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试课程详情信息", response = OpenExamCourseBean.class)})
    public Object examCourseQueryPage(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
                                      @ApiParam(value = "课程code", required = false) @RequestParam(required = false) String courseCode,
                                      @ApiParam(value = "是否有试卷", required = false) @RequestParam(required = false) Boolean hasPaper,
                                      @ApiParam(value = "分页页码", required = false) @RequestParam(required = false) Integer pageNumber,
                                      @ApiParam(value = "分页数", required = false) @RequestParam(required = false) Integer pageSize) {
        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 20;
        }
        if (pageSize > 100) {
            pageSize = 100;
        }
        return openService.examCourseQueryPage(examId, StringUtils.trimToNull(courseCode), hasPaper, pageNumber, pageSize);
    }

    @ApiOperation(value = "获取考试试卷详情")
    @RequestMapping(value = "/exam/paper/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息", response = Result.class)})
    public JSONObject examPaperDetail(@ApiParam(value = "试卷id", required = true) @RequestParam Long id,
                                      @ApiParam(value = "内容过滤", required = false) @RequestParam(required = false) String filter)
            throws IOException {
        filter = StringUtils.trimToNull(filter);
        if (filter != null && !"objective".equals(filter) && !"subjective".equals(filter)) {
            throw new BusinessException("filter参数无效");
        }
        return openService.examPaperDetail(id, filter);
    }

    @ApiOperation(value = "待评卷考试记录查询")
    @RequestMapping(value = "/exam/record/need_mark", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息", response = Result.class)})
    public JSONArray examRecordNeedMark(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
                                        @ApiParam(value = "课程代码", required = false) @RequestParam(required = false) String courseCode,
                                        @ApiParam(value = "考生ID大于此参数", required = false) @RequestParam(required = false) Long examStudentIdGt,
                                        @ApiParam(value = "数量（最大20）", required = false) @RequestParam(required = false) Integer count) {
        if (examStudentIdGt == null || examStudentIdGt < 0) {
            examStudentIdGt = 0L;
        }
        if (count == null || count < 0 || count > 20) {
            count = 20;
        }
        return openService.examRecordNeedMark(examId, StringUtils.trimToNull(courseCode), examStudentIdGt, count);
    }
}