package com.qmth.teachcloud.eds.vo;

import com.baomidou.mybatisplus.annotation.TableField;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 云阅卷考生对象
 */
public class ExamStudent implements Serializable {

    protected static final long serialVersionUID = 2944870647483950851L;

    public static final String ANSWER_SPLIT = ",";

    public static final String SPLIT = ";";

    /**
     * 考试ID
     */
    @TableField(value = "exam_id")
    private String examId;

    /**
     * 科目代码
     */
    @TableField(value = "subject_code")
    private String subjectCode;

    /**
     * 科目名称
     */
    @TableField(value = "subject_name")
    private String subjectName;

    /**
     * 学号
     */
    @TableField(value = "student_code")
    private String studentCode;


    /**
     * 姓名
     */
    @TableField(value = "name")
    private String name;

    /**
     * 客观总分
     */
    @TableField(value = "objective_score")
    private String objectiveScore;

    /**
     * 客观题得分明细
     */
    @TableField(value = "objective_score_list")
    private String objectiveScoreList;

    /**
     * 主观总分
     */
    @TableField(value = "subjective_score")
    private String subjectiveScore;

    /**
     * 主观题得分明细
     */
    @TableField(value = "subjective_score_list")
    private String subjectiveScoreList;

    public String getExamId() {
        return examId;
    }

    public void setExamId(String examId) {
        this.examId = examId;
    }

    public String getSubjectCode() {
        return subjectCode;
    }

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

    public String getName() {
        return name;
    }

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

    public String getSubjectName() {
        return subjectName;
    }

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

    public String getStudentCode() {
        return studentCode;
    }

    public void setStudentCode(String studentCode) {
        this.studentCode = studentCode;
    }

    public String getObjectiveScore() {
        return objectiveScore;
    }

    public void setObjectiveScore(String objectiveScore) {
        this.objectiveScore = objectiveScore;
    }

    public String getSubjectiveScore() {
        return subjectiveScore;
    }

    public void setSubjectiveScore(String subjectiveScore) {
        this.subjectiveScore = subjectiveScore;
    }

    public String getObjectiveScoreList() {
        return objectiveScoreList;
    }

    public void setObjectiveScoreList(String objectiveScoreList) {
        this.objectiveScoreList = objectiveScoreList;
    }

    public String getSubjectiveScoreList() {
        return subjectiveScoreList;
    }

    public void setSubjectiveScoreList(String subjectiveScoreList) {
        this.subjectiveScoreList = subjectiveScoreList;
    }

    public static String buildScoreList(List<ScoreItem> scoreList) {
        if (scoreList != null) {
            return StringUtils.join(scoreList, SPLIT);
        } else {
            return null;
        }
    }

    public void setScoreList(List<ScoreItem> scoreList, boolean objective) {
        if (scoreList != null) {
            if (objective) {
                setObjectiveScoreList(StringUtils.join(scoreList, SPLIT));
            } else {
                setSubjectiveScoreList(StringUtils.join(scoreList, SPLIT));
            }
        }
    }

    public List<ScoreItem> getScoreList(boolean objective) {
        List<ScoreItem> scoreList = new LinkedList<ScoreItem>();
        try {
            String[] values = StringUtils.split(objective ? objectiveScoreList : subjectiveScoreList, SPLIT);
            for (String value : values) {
                ScoreItem item = ScoreItem.parse(value, objective);
                item.setObjective(objective);
                if (item != null) {
                    scoreList.add(item);
                }
            }
        } catch (Exception e) {
        }
        return scoreList;
    }

    public double getTotalScore() {
        double score = 0;
        if (objectiveScore != null) {
            score = add(score, Double.parseDouble(objectiveScore));
        }
        if (subjectiveScore != null) {
            score = add(score, Double.parseDouble(subjectiveScore));
        }
        return score;
    }

    public String getObjectiveScoreString() {
        try {
            return ScoreItem.NUMBER_FORMAT.format(objectiveScore);
        } catch (Exception e) {
            return "";
        }
    }

    public String getSubjectiveScoreString() {
        try {
            return ScoreItem.NUMBER_FORMAT.format(subjectiveScore);
        } catch (Exception e) {
            return "";
        }
    }

    public String getTotalScoreString() {
        try {
            return ScoreItem.NUMBER_FORMAT.format(getTotalScore());
        } catch (Exception e) {
            return "";
        }
    }

    public static double add(double v1, double v2) {

        BigDecimal b1 = new BigDecimal(Double.toString(v1));

        BigDecimal b2 = new BigDecimal(Double.toString(v2));

        return b1.add(b2).doubleValue();

    }

}
