package cn.com.qmth.examcloud.core.questions.api.controller;


import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.api.request.GetExtractConfigReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetPaperReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetQuestionListReq;
import cn.com.qmth.examcloud.core.questions.service.PaperProviderService;
import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;

/**
 * @author weiwenhai
 * @date 2018.10.8
 * @company qmth
 * @description controller
 * @code 016
 */
@RestController
@RequestMapping("${api_cqb}/default_paper")
public class DefaultPaperController extends ControllerSupport {

    @Autowired
    PaperProviderService paperProviderService;

    @ApiOperation(value = "外部接口组卷", notes = "外部接口组卷")
    @PostMapping("/genPaper")
    public ResponseEntity<Object> genPaper(@RequestBody GetQuestionListReq req) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException("Q-016038", "用户不存在！");
        }
        //组卷
        String paperId = paperProviderService.genPaper(req.getQuestionIds(), req.getMap(), req.getName(), user.getRootOrgId().toString(), user.getDisplayName());
        return new ResponseEntity<>(paperId, HttpStatus.OK);
    }

    @ApiOperation(value = "外部接口查询试卷", notes = "外部接口查询试卷")
    @PostMapping("/findPaper")
    public ResponseEntity<Object> findPaper(@RequestBody GetPaperReq req) {
        String paperId = req.getPaperId();
        if (StringUtils.isBlank(paperId)) {
            throw new StatusException("Q-016057", "paperId is null");
        }
        DefaultPaper defaultPaper = paperProviderService.findPaper(paperId);
        return new ResponseEntity<>(defaultPaper, HttpStatus.OK);
    }

    @ApiOperation(value = "外部接口查询试卷", notes = "外部接口查询试卷")
    @PostMapping("/findExamPaper")
    public ResponseEntity<Object> findExamPaper(@RequestBody GetExtractConfigReq req) {
        String rootOrgId = req.getRootOrgId();
        if (StringUtils.isBlank(rootOrgId)) {
            throw new StatusException("Q-016068", "rootOrgId is null");
        }
        Long examId = req.getExamId();
        if (null == examId) {
            throw new StatusException("Q-016073", "examId is null");
        }
        DefaultPaper defaultPaper = paperProviderService.findExamPaper(rootOrgId, examId);
        return new ResponseEntity<>(defaultPaper, HttpStatus.OK);
    }

    @ApiOperation(value = "外部接口组卷并绑定考卷", notes = "外部接口组卷并绑定考卷")
    @PostMapping("/genAndExamPaper")
    public ResponseEntity<Object> genPaperAndExamPaper(@RequestBody GetExtractConfigReq req) {
        String rootOrgId = req.getRootOrgId();
        if (StringUtils.isBlank(rootOrgId)) {
            throw new StatusException("Q-016087", "rootOrgId is null");
        }
        Long examId = req.getExamId();
        if (null == examId) {
            throw new StatusException("Q-016091", "examId is null");
        }
        //组卷
        String paperId = paperProviderService.genPaper(req.getQuestionIds(), req.getMap(), req.getPaperName(), req.getRootOrgId(), req.getUserName());
        paperProviderService.examPaper(req.getRootOrgId(), paperId, req.getExamId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
