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.biz.exam.model.MarkGroup;
import cn.com.qmth.stmms.common.annotation.ExcelField;
import cn.com.qmth.stmms.common.enums.ScorePolicy;

public class SubjectiveQuestionDTO implements QuestionDTO {

    private static final String NULL_PAPER_TYPE_PLACEHOLDER = "#";

    @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 String title;

    @ExcelField(title = "题目昵称", align = 2, sort = 40)
    private String name;

    @ExcelField(title = "大题号(只能用小写数字)*", align = 2, sort = 50)
    private Integer mainNumber;

    @ExcelField(title = "小题号(只能用小写数字)*", align = 2, sort = 60)
    private String subNumber;

    @ExcelField(title = "小题满分*", align = 2, sort = 70)
    private Double totalScore;

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

    @ExcelField(title = "评卷分组(只能用小写数字)*", align = 2, sort = 90)
    private Integer groupNumber;

    @ExcelField(title = "图片序号(用英文逗号分割)", align = 2, sort = 100)
    private String picList;

    @ExcelField(title = "双评比例(0~1)", align = 2, sort = 110)
    private Double doubleRate;

    @ExcelField(title = "仲裁阀值", align = 2, sort = 120)
    private Double arbitrateThreshold;

    @ExcelField(title = "合分策略(1-平均，2-最高，3-最低)", align = 2, sort = 130)
    private Integer scorePolicy;

    @ExcelField(title = "评卷模式(common-普通，track-轨迹)", align = 2, sort = 140)
    private String markMode;

    @ExcelField(title = "试评数量(0-跳过试评)", align = 2, sort = 150)
    private Integer trialCount;

    @ExcelField(title = "选做题数量", align = 2, sort = 160)
    private Integer selectiveCount;

    public SubjectiveQuestionDTO() {

    }

    public SubjectiveQuestionDTO(ExamQuestion question, ExamSubject subject, MarkGroup group) {
        setSubjectCode(question.getSubjectCode());
        setSubjectName(subject != null ? subject.getName() : "");
        setMainNumber(question.getMainNumber());
        setTitle(question.getMainTitle());
        setName(question.getName());
        setSubNumber(question.getSubNumber());
        setGroupNumber(question.getGroupNumber());
        setTotalScore(question.getTotalScore());
        setIntervalScore(question.getIntervalScore());
        setPicList(group != null ? group.getPicList() : "");
        setDoubleRate(group != null && group.getDoubleRate() != null ? group.getDoubleRate() : 0d);
        setArbitrateThreshold(group != null && group.getArbitrateThreshold() != null ? group.getArbitrateThreshold()
                : 0d);
        setScorePolicy(group != null && group.getScorePolicy() != null ? group.getScorePolicy().getValue()
                : ScorePolicy.AVG.getValue());
        setMarkMode(group != null && group.getMarkMode() != null ? group.getMarkMode().toString() : "");
        setTrialCount(subject != null && subject.getTrialCount() != null ? subject.getTrialCount() : 0);
    }

    public ExamQuestion transform() {
        ExamQuestion question = new ExamQuestion();
        question.setSubjectCode(StringUtils.trimToNull(subjectCode));
        question.setPaperType(NULL_PAPER_TYPE_PLACEHOLDER);
        question.setMainTitle(StringUtils.trimToNull(title));
        question.setName(StringUtils.trimToNull(name));
        question.setMainNumber(mainNumber);
        question.setSubNumber(subNumber);
        question.setGroupNumber(groupNumber);
        question.setObjective(false);
        question.setTotalScore(totalScore);
        question.setIntervalScore(intervalScore != null && intervalScore >= 0.01 ? intervalScore : 1d);
        question.setPicList(picList);
        question.setDoubleRate(doubleRate);
        question.setArbitrateThreshold(arbitrateThreshold);
        question.setScorePolicy(scorePolicy);
        question.setMarkMode(markMode);
        question.setTrialCount(trialCount);
        return question;
    }

    /**
     * 判断是否总分含小数点
     * 
     * @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 String getSubNumber() {
        return subNumber;
    }

    public void setSubNumber(String 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;
    }

    public String getPicList() {
        return picList;
    }

    public void setPicList(String picList) {
        this.picList = picList;
    }

    public Double getDoubleRate() {
        return doubleRate;
    }

    public void setDoubleRate(Double doubleRate) {
        this.doubleRate = doubleRate;
    }

    public Double getArbitrateThreshold() {
        return arbitrateThreshold;
    }

    public void setArbitrateThreshold(Double arbitrateThreshold) {
        this.arbitrateThreshold = arbitrateThreshold;
    }

    public Integer getScorePolicy() {
        return scorePolicy;
    }

    public void setScorePolicy(Integer scorePolicy) {
        this.scorePolicy = scorePolicy;
    }

    public String getMarkMode() {
        return markMode;
    }

    public void setMarkMode(String markMode) {
        this.markMode = markMode;
    }

    public Integer getTrialCount() {
        return trialCount;
    }

    public void setTrialCount(Integer trialCount) {
        this.trialCount = trialCount;
    }

    public Integer getGroupNumber() {
        return groupNumber;
    }

    public void setGroupNumber(Integer groupNumber) {
        this.groupNumber = groupNumber;
    }

    public Integer getSelectiveCount() {
        return selectiveCount;
    }

    public void setSelectiveCount(Integer selectiveCount) {
        this.selectiveCount = selectiveCount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
