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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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 cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.api.PaperCloudService;
import cn.com.qmth.examcloud.core.questions.api.request.GetExtractConfigReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetQuestionListReq;
import cn.com.qmth.examcloud.core.questions.api.response.GetCommonResp;
import cn.com.qmth.examcloud.core.questions.api.response.GetPaperResp;
import cn.com.qmth.examcloud.core.questions.service.PaperProviderService;
import io.swagger.annotations.ApiOperation;

/**
 * @author weiwenhai
 * @date 20180.10.10
 * @company qmth
 * @description 试卷对象外部接口增删改查请求接口实现类
 * @code 017
 */

@Transactional
@RestController
@RequestMapping("${$rmp.cloud.questions}" + "default_paper")
public class PaperCloudServiceProvider implements PaperCloudService {

    private static final long serialVersionUID = -7354673848303599874L;

    @Autowired
    private PaperProviderService paperProviderService;

    @ApiOperation(value = "外部接口组卷", notes = "外部接口组卷")
    @PostMapping("/genPaper")
    @Override
    public GetPaperResp genPaper(@RequestBody GetQuestionListReq req) {
        if (StringUtils.isBlank(req.getOrgId())) {
            throw new StatusException("Q-017043", "orgId is null");
        }
        if (StringUtils.isBlank(req.getUserName())) {
            throw new StatusException("Q-017046", "userName is null");
        }
        //组卷
        String paperId = paperProviderService.genPaper(req.getQuestionIds(), req.getMap(), req.getName(), req.getOrgId(), req.getUserName());
        GetPaperResp resp = new GetPaperResp();
        resp.setPaperId(paperId);
        return resp;
    }

    @ApiOperation(value = "外部接口组卷", notes = "外部接口组卷")
    @PostMapping("/examPaper")
    @Override
    public GetCommonResp examPaper(@RequestBody GetExtractConfigReq req) {
        if (StringUtils.isBlank(req.getRootOrgId())) {
            throw new StatusException("Q-017063", "rootOrgId is null");
        }
        if (StringUtils.isBlank(req.getPaperId())) {
            throw new StatusException("Q-017066", "paperId is null");
        }
        if (StringUtils.isBlank(req.getExamId() + "")) {
            throw new StatusException("Q-017069", "examId is null");
        }
        String extractConfigId = paperProviderService.examPaper(req.getRootOrgId(), req.getPaperId(), req.getExamId());
        GetCommonResp resp = new GetCommonResp();
        resp.setMessage(extractConfigId);
        return resp;
    }


}
