package com.qmth.distributed.print.business.bean.marking;

import io.swagger.annotations.ApiModelProperty;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 题目结构
 * @Author: CaoZixuan
 * @Date: 2022-04-13
 */
public class Question {
    @ApiModelProperty(value = "大题号")
    private Integer mainNumber;

    @ApiModelProperty(value = "大题名称")
    private String mainTitle;

    @ApiModelProperty(value = "小题号")
    private String subNumber;

    @ApiModelProperty(value = "小题总分")
    private BigDecimal totalScore;

    @ApiModelProperty(value = "客观题答案")
    private String answer;

    public Integer getMainNumber() {
        return mainNumber;
    }

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

    public String getMainTitle() {
        return mainTitle;
    }

    public void setMainTitle(String mainTitle) {
        this.mainTitle = mainTitle;
    }

    public String getSubNumber() {
        return subNumber;
    }

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

    public BigDecimal getTotalScore() {
        return totalScore;
    }

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

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    /**
     * 判断两个题目是否完全相同
     * <p>
     *     1.大题号
     *     2.小题号
     *     3.题目分数
     *     4.大题名称
     * </p>
     * @param a 题目a
     * @param b 题目b
     * @return true:相同 false:不相同
     */
    public static boolean matchTwoObjects(Question a, Question b) {
        if (!a.getMainNumber().equals(b.getMainNumber())) {
            return false;
        }
        if (!a.getSubNumber().equals(b.getSubNumber())) {
            return false;
        }
        if (a.getTotalScore().compareTo(b.getTotalScore()) != 0) {
            return false;
        }
        return a.getMainTitle().equals(b.getMainTitle());
    }

    /**
     * 匹配两个题目集合是否在关键字段上相同
     * @param ql1 题目集合1
     * @param ql2 题目集合2
     * @return true:相同 false:不相同
     */
    public static boolean matchTwoQuestionList(List<Question> ql1, List<Question> ql2){
        // 两个结构内元素数量不一致直接返回错误
        if (ql1.size() != ql2.size()) {
            return false;
        }

        // 排序
        List<Question> ql1Sort = ql1.stream().sorted(Comparator.comparing(Question::getMainNumber).thenComparing(Question::getSubNumber)).collect(Collectors.toList());
        List<Question> ql2Sort = ql2.stream().sorted(Comparator.comparing(Question::getMainNumber).thenComparing(Question::getSubNumber)).collect(Collectors.toList());

        for (int i = 0; i < ql1Sort.size(); i++) {
            Question q1 = ql1Sort.get(i);
            Question q2 = ql2Sort.get(i);
            if (!Question.matchTwoObjects(q1, q2)) {
                return false;
            }
        }
        return true;
    }


}
