package com.ssj.service.kmt.exercisesys.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.service.kmt.constant.KnowledgeVersion;
import com.ssj.service.kmt.exercisesys.enums.ExerciseBookHalfBookEnum;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Optional;

/**
 * @description
 * @author yrf
 * @time 2020/3/23 17:36
 */
public class ExerciseTeacherDetailDTO implements Serializable {

    private static final long serialVersionUID = 8352235140300284629L;


    /**
     * bookId :
     * bookName :
     * grade :
     * semester : 1
     * courseName : 语文
     * versionName : 人教版
     * bookStatus : 1
     */

    private String bookId;
    private String exerciseBookId;
    private String bookName;
    private String grade;
    private Integer semester;
    private String courseName;

    @JSONField(serialize = false)
    private Integer version;

    private String versionName;

    @JSONField(serialize = false)
    private Integer bookType;

    private String halfBook;

    /**
     * 练习册状态，1未加工，2打回，3加工中，4已加工
     */
    private Integer bookStatus;

    /**
     *
     */
    private Integer unfinishedUnitCount;
    private Integer finishedUnitCount;
    private Integer totalUnitCount;

    /**
     * 正确率
     */
    private String accuracy;
    @JSONField(serialize = false)
    private Integer accuracyInt;

    public String getBookId() {
        return bookId;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }

    public String getExerciseBookId() {
        return exerciseBookId;
    }

    public void setExerciseBookId(String exerciseBookId) {
        this.exerciseBookId = exerciseBookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public Integer getSemester() {
        return semester;
    }

    public void setSemester(Integer semester) {
        this.semester = semester;
    }

    public String getCourseName() {
        return courseName;
    }

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

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getVersionName() {
        if (StringUtils.isNotBlank(versionName)) {
            return versionName;
        }

        KnowledgeVersion knowledgeVersion = SystemResourceLocator.getBean(KnowledgeVersion.class);
        versionName = knowledgeVersion.getVersionNameBySubjectAndVersionNum(courseName, version);

        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public Integer getBookType() {
        return bookType;
    }

    public void setBookType(Integer bookType) {
        this.bookType = bookType;
    }

    public String getHalfBook() {
        if (StringUtils.isNotBlank(halfBook)) {
            return halfBook;
        }

        halfBook = ExerciseBookHalfBookEnum.getDescriptionByCode(bookType);

        return halfBook;
    }

    public void setHalfBook(String halfBook) {
        this.halfBook = halfBook;
    }

    public Integer getBookStatus() {
        return bookStatus;
    }

    public void setBookStatus(Integer bookStatus) {
        this.bookStatus = bookStatus;
    }

    public Integer getUnfinishedUnitCount() {
        return unfinishedUnitCount;
    }

    public void setUnfinishedUnitCount(Integer unfinishedUnitCount) {
        this.unfinishedUnitCount = unfinishedUnitCount;
    }

    public Integer getFinishedUnitCount() {
        if (finishedUnitCount == null) {
            finishedUnitCount = Optional.ofNullable(totalUnitCount).orElse(0) -
                    Optional.ofNullable(unfinishedUnitCount).orElse(0);
        }
        return finishedUnitCount;
    }

    public void setFinishedUnitCount(Integer finishedUnitCount) {
        this.finishedUnitCount = finishedUnitCount;
    }

    public Integer getTotalUnitCount() {
        return totalUnitCount;
    }

    public void setTotalUnitCount(Integer totalUnitCount) {
        this.totalUnitCount = totalUnitCount;
    }

    public String getAccuracy() {
        if (StringUtils.isBlank(accuracy) && accuracyInt != null) {
            accuracy = BigDecimal.valueOf(accuracyInt * 0.01).setScale(2, BigDecimal.ROUND_DOWN).toPlainString();
        }
        return accuracy;
    }

    public void setAccuracy(String accuracy) {
        this.accuracy = accuracy;
    }

    public Integer getAccuracyInt() {
        return accuracyInt;
    }

    public void setAccuracyInt(Integer accuracyInt) {
        this.accuracyInt = accuracyInt;
    }
}
