package cn.com.qmth.stmms.admin.dto;

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.ExamQuestion;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.common.annotation.ExcelField;

public class SubjectiveQuestionDTO implements QuestionDTO {

    @ExcelField(title = "科目代码", align = 2, sort = 10)
    private String subjectCode;

    @ExcelField(title = "科目名称", align = 2, sort = 20)
    private String subjectName;

    @ExcelField(title = "大题号", align = 2, sort = 30)
    private Integer mainNumber;

    @ExcelField(title = "名称", align = 2, sort = 40)
    private String title;

    @ExcelField(title = "小题号", align = 2, sort = 50)
    private Integer subNumber;

    @ExcelField(title = "满分", align = 2, sort = 60)
    private Double totalScore;

    @ExcelField(title = "间隔分", align = 2, sort = 70)
    private Double intervalScore;

    public SubjectiveQuestionDTO() {

    }

    public SubjectiveQuestionDTO(ExamQuestion question, ExamSubject subject) {
        setSubjectCode(question.getSubjectCode());
        setSubjectName(subject != null ? subject.getName() : "");
        setMainNumber(question.getMainNumber());
        setTitle(question.getMainTitle());
        setSubNumber(question.getSubNumber());
        setTotalScore(question.getTotalScore());
        setIntervalScore(question.getIntervalScore());
    }

    public ExamQuestion transform() {
        ExamQuestion question = new ExamQuestion();
        question.setSubjectCode(StringUtils.trimToNull(subjectCode));
        question.setMainNumber(mainNumber);
        question.setSubNumber(subNumber);
        question.setObjective(false);
        question.setMainTitle(StringUtils.trimToNull(title));
        question.setTotalScore(totalScore);
        question.setIntervalScore((intervalScore != null  && intervalScore > 0d) ? intervalScore : 1d);
        question.setTotalCount(0);
        question.setZeroCount(0);
        question.setFullCount(0);
        return question;
    }

    public boolean validate() {
        // 只校验科目代码，其他留待后续校验
        if (StringUtils.isBlank(subjectCode)) {
            return false;
        }
        // if (intervalScore == null) {
        // return false;
        // }
        // if (StringUtils.isBlank(title)) {
        // return false;
        // }
        // if (mainNumber == null || subNumber == null || totalScore == null) {
        // return false;
        // }
        return true;
    }

    /**
     * 判断是否总分含小数点
     * 
     * @return
     */
    public boolean hasPointScore() {
        if (totalScore != null) {
            return totalScore.doubleValue() > (double) (totalScore.intValue());
        } else {
            return false;
        }
    }

    public String getSubjectCode() {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {
        this.subjectCode = subjectCode;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public Integer getMainNumber() {
        return mainNumber;
    }

    public void setMainNumber(Integer mainNumber) {
        this.mainNumber = mainNumber;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getSubNumber() {
        return subNumber;
    }

    public void setSubNumber(Integer subNumber) {
        this.subNumber = subNumber;
    }

    public Double getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(Double totalScore) {
        this.totalScore = totalScore;
    }

    public Double getIntervalScore() {
        return intervalScore;
    }

    public void setIntervalScore(Double intervalScore) {
        this.intervalScore = intervalScore;
    }

}
