package com.qmth.teachcloud.mark.entity;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.github.jeffreyning.mybatisplus.anno.MppMultiId;
import com.qmth.teachcloud.common.entity.MarkQuestion;
import com.qmth.teachcloud.mark.bean.mark.MarkConfigItem;
import com.qmth.teachcloud.mark.dto.mark.ScoreItem;
import com.qmth.teachcloud.mark.enums.ScorePolicy;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 评卷分组表
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@TableName("mark_group")
@ApiModel(value = "MarkGroup对象", description = "评卷分组表")
public class MarkGroup implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "考试ID")
    @MppMultiId(value = "exam_id")
    private Long examId;

    @ApiModelProperty(value = "课程代码")
    private String courseCode;

    @ApiModelProperty(value = "课程名称")
    private String courseName;

    @ApiModelProperty(value = "试卷编号")
    @MppMultiId(value = "paper_number")
    private String paperNumber;

    @ApiModelProperty(value = "序号")
    @MppMultiId(value = "number")
    private Integer number;

    @ApiModelProperty(value = "小图配置")
    private String picList;

    @ApiModelProperty(value = "满分")
    private Double totalScore;


    @ApiModelProperty(value = "是否开启双评")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Boolean doubleEnable;

    @ApiModelProperty(value = "双评比例")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Double doubleRate;

    @ApiModelProperty(value = "仲裁阈值")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Double arbitrateThreshold;

    @ApiModelProperty(value = "合分策略")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private ScorePolicy scorePolicy;

    @ApiModelProperty(value = "任务总量")
    private Integer taskCount;

    @ApiModelProperty(value = "已评数量")
    private Integer markedCount;

    @ApiModelProperty(value = "剩余数量")
    private Integer leftCount;

    /**
     * 大题号集合（逗号分隔）
     */
    @TableField(exist = false)
    private String mainNumber;
    /**
     * 大题名称集合（逗号分隔）
     */
    @TableField(exist = false)
    private String title;
    /**
     * 大题名称+小题号集合（逗号分隔）
     */
    @TableField(exist = false)
    private String questionTitle;
    /**
     * 大题号+小题号集合（逗号分隔）
     */
    @TableField(exist = false)
    private String groupQuestions;
    /**
     * 主观题小题满分集合（逗号分隔）
     */
    @TableField(exist = false)
    private String scoreList;

    @TableField(exist = false)
    private double markScore;

    @TableField(exist = false)
    private List<ScoreItem> markScoreDetail;

    public Long getExamId() {
        return examId;
    }

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

    public String getCourseCode() {
        return courseCode;
    }

    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getPaperNumber() {
        return paperNumber;
    }

    public void setPaperNumber(String paperNumber) {
        this.paperNumber = paperNumber;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getPicList() {
        return picList;
    }

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

    public Double getTotalScore() {
        return totalScore;
    }

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

    public Boolean getDoubleEnable() {
        return doubleEnable;
    }

    public void setDoubleEnable(Boolean doubleEnable) {
        this.doubleEnable = doubleEnable;
    }

    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 ScorePolicy getScorePolicy() {
        return scorePolicy;
    }

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

    public Integer getTaskCount() {
        return taskCount;
    }

    public void setTaskCount(Integer taskCount) {
        this.taskCount = taskCount;
    }

    public Integer getMarkedCount() {
        return markedCount;
    }

    public void setMarkedCount(Integer markedCount) {
        this.markedCount = markedCount;
    }

    public Integer getLeftCount() {
        return leftCount;
    }

    public void setLeftCount(Integer leftCount) {
        this.leftCount = leftCount;
    }

    public String getMainNumber() {
        return mainNumber;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getQuestionTitle() {
        return questionTitle;
    }

    public void setQuestionTitle(String questionTitle) {
        this.questionTitle = questionTitle;
    }

    public String getGroupQuestions() {
        return groupQuestions;
    }

    public void setGroupQuestions(String groupQuestions) {
        this.groupQuestions = groupQuestions;
    }

    public String getScoreList() {
        return scoreList;
    }

    public void setScoreList(String scoreList) {
        this.scoreList = scoreList;
    }

    public double getMarkScore() {
        return markScore;
    }

    public void setMarkScore(double markScore) {
        this.markScore = markScore;
    }

    public List<ScoreItem> getMarkScoreDetail() {
        return markScoreDetail;
    }

    public void setMarkScoreDetail(List<ScoreItem> markScoreDetail) {
        this.markScoreDetail = markScoreDetail;
    }

    public void setQuestionList(List<MarkQuestion> questionList) {
        DecimalFormat format = new DecimalFormat("####.##");
        Set<Integer> mainNumbers = new LinkedHashSet<>();
        Set<String> mainTitles = new LinkedHashSet<>();
        Set<String> questionTitles = new LinkedHashSet<>();
        Set<String> groupQuestions = new LinkedHashSet<>();
        StringBuilder score = new StringBuilder();
        for (MarkQuestion question : questionList) {
            if (score.length() > 0) {
                score.append(",");
            }
            score.append(format.format(question.getTotalScore()));
            mainNumbers.add(question.getMainNumber());
            mainTitles.add(question.getMainTitle());
            questionTitles.add(question.getMainTitle() + '-' + question.getSubNumber());
            groupQuestions.add(String.valueOf(question.getMainNumber()) + '-' + question.getSubNumber());
        }
        this.mainNumber = StringUtils.join(mainNumbers, ",");
        this.title = StringUtils.join(mainTitles, ",");
        this.questionTitle = StringUtils.join(questionTitles, ",");
        this.groupQuestions = StringUtils.join(groupQuestions, ",");
        this.scoreList = score.toString();
    }

    public List<MarkConfigItem> getPictureConfigList() {
        return MarkConfigItem.parse(this.picList);
    }

    @Override
    public String toString() {
        return "MarkGroup{" +
                "examId=" + examId +
                ", courseCode=" + courseCode +
                ", paperNumber=" + paperNumber +
                ", number=" + number +
                ", picList=" + picList +
                ", totalScore=" + totalScore +
                ", doubleRate=" + doubleRate +
                ", arbitrateThreshold=" + arbitrateThreshold +
                ", scorePolicy=" + scorePolicy +
                ", taskCount=" + taskCount +
                ", markedCount=" + markedCount +
                ", leftCount=" + leftCount +
                "}";
    }
}
