package cn.com.qmth.stmms.ms.marking.api;

import cn.com.qmth.stmms.ms.commons.config.ImageConfig;
import cn.com.qmth.stmms.ms.core.domain.ExamQuestion;
import cn.com.qmth.stmms.ms.core.domain.enums.TrialEnum;
import cn.com.qmth.stmms.ms.core.repository.ExamQuestionRepo;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
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.util.List;

/**
 * 试题api
 * Created by zhengmin on 2016/11/8.
 */
@RestController
@RequestMapping("api/questions")
public class ExamQuestionApi {

    @Autowired
    private ExamQuestionRepo examQuestionRepo;

    @Resource
    ImageConfig imageConfig;

    /**
     * 试题列表
     *
     * @param workId  评卷工作id
     * @param subject 科目
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<ExamQuestion> list(@RequestParam Long workId, @RequestParam Subject subject) {
        if (!imageConfig.isCustomSubject()) {
            List<ExamQuestion> questions = examQuestionRepo.findByWorkIdAndSubjectAndTestNot(workId, subject, TrialEnum.DEFAULT.getId());
            if(questions == null || questions.size() == 0){
                questions = examQuestionRepo.findByWorkIdAndSubjectAndTest(workId, subject, TrialEnum.DEFAULT.getId());
            }
            return questions;
        } else {
            return examQuestionRepo.findByWorkIdAndSubjectAndTest(workId, Subject.CUSTOM, TrialEnum.DEFAULT.getId());
        }
    }
}
