package com.qmth.teachcloud.report.business.bean.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModelProperty;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 考生作答
 * @Author: CaoZixuan
 * @Date: 2022-06-15
 */
public class Answer {
    @ApiModelProperty(value = "题号类型（客观题、主观题）")
    @TableField(value = "number_type")
    private String numberType;

    @ApiModelProperty(value = "大题号")
    @TableField(value = "main_number")
    private String mainNumber;

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

    public String getNumberType() {
        return numberType;
    }

    public void setNumberType(String numberType) {
        this.numberType = numberType;
    }

    public String getMainNumber() {
        return mainNumber;
    }

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

    public String getSubNumber() {
        return subNumber;
    }

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

    /**
     * 判断两个题目是否完全相同
     * <p>
     * 1.题目类型（主观题，客观题）
     * 2.大题号
     * 3.小题号
     * </p>
     *
     * @param a 题目a
     * @param b 题目b
     * @return true:相同 false:不相同
     */
    public static boolean matchTwoAnswer(Answer a, Answer b) {
        if (!a.getNumberType().equals(b.getNumberType())) {
            return false;
        }
        if (!a.getMainNumber().equals(b.getMainNumber())) {
            return false;
        }
        return a.getSubNumber().equals(b.getSubNumber());
    }

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

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

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