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

import java.util.List;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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.QuestionCloudService;
import cn.com.qmth.examcloud.core.questions.api.request.GetDefaultQuesionListReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetDefaultQuesionReq;
import cn.com.qmth.examcloud.core.questions.api.request.GetDefaultQuesionsReq;
import cn.com.qmth.examcloud.core.questions.api.response.GetCommonResp;
import cn.com.qmth.examcloud.core.questions.api.response.GetDefaultQuesionIdResp;
import cn.com.qmth.examcloud.core.questions.api.response.GetDefaultQuestionsResp;
import cn.com.qmth.examcloud.core.questions.service.QuestionProviderService;
import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestion;

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

@Transactional
@RestController
@RequestMapping("${$rmp.cloud.questions}" + "default_question")
public class QuestionCloudServiceProvider implements QuestionCloudService {

    private static final long serialVersionUID = 263247747318241591L;

    @Autowired
    QuestionProviderService questionProviderService;

    @ApiOperation(value = "保存试题")
    @PostMapping("save")
    @Override
    public GetDefaultQuesionIdResp saveQuestion(@RequestBody GetDefaultQuesionReq req) {
        DefaultQuestion defaultQuestion = req.getDefaultQuestion();
        if (defaultQuestion.getRootOrgId() == null) {
            throw new StatusException("Q-011028", "rootOrgId is null");
        }
        String id = questionProviderService.save(defaultQuestion);
        GetDefaultQuesionIdResp resp = new GetDefaultQuesionIdResp();
        resp.setId(id);
        return resp;
    }

    @ApiOperation(value = "修改试题")
    @PostMapping("update")
    @Override
    public GetDefaultQuesionIdResp updateQuestion(@RequestBody GetDefaultQuesionReq req) {
        DefaultQuestion defaultQuestion = req.getDefaultQuestion();
        if (defaultQuestion.getRootOrgId() == null) {
            throw new StatusException("Q-011058", "rootOrgId is null");
        }
        String id = questionProviderService.save(defaultQuestion);
        GetDefaultQuesionIdResp resp = new GetDefaultQuesionIdResp();
        resp.setId(id);
        return resp;
    }

    @ApiOperation(value = "查询试题")
    @PostMapping("find")
    @Override
    public GetDefaultQuestionsResp findQuestions(@RequestBody GetDefaultQuesionsReq req) {
        Long rootOrgId = req.getRootOrgId();
        if (rootOrgId == null) {
            throw new StatusException("Q-011058", "rootOrgId is null");
        }
        Page<DefaultQuestion> defaultQuestions = questionProviderService.findQustions(rootOrgId, req.getProperties(), req.getCurPage(), req.getPageSize());
        GetDefaultQuestionsResp resp = new GetDefaultQuestionsResp();
        resp.setDefaultQuestions(defaultQuestions);
        return resp;
    }

    @ApiOperation(value = "保存多个试题")
    @PostMapping("save_list")
    @Override
    public GetCommonResp saveQuestionList(@RequestBody GetDefaultQuesionListReq req) {
        List<DefaultQuestion> defaultQuestions = req.getDefaultQuestions();
        String message = questionProviderService.saveList(defaultQuestions);
        GetCommonResp resp = new GetCommonResp();
        resp.setMessage(message);
        return resp;
    }

}
