package cn.com.qmth.stmms.biz.report.utils.unit;

public class BaseCalculatorUnit {

    public int count;

    public int zeroCount;

    public int fullCount;

    public double fullScore;

    public Double passScore;

    public int passCount;
    
    public double passRate;

    public Double excellentScore;

    public int excellentCount;
    
    public double excellentRate;

    public double aggScore;

    public double sumScore;

    public double sumScore2;

    public double sumTotalScore;

    public double sumTotalScore2;

    public double maxScore;

    public double minScore;

    public double avgScore;

    public double stdev;

    public double tempSum;

    public double difficulty;

    public double coefficient;

    public double discrimination;
    
    public double fullScoreRate;
    
    public BaseCalculatorUnit() {
        minScore = Double.MAX_VALUE;
    }

    public void process(Double score, Double fullScore, Double totalScore) {
        if (score == null || fullScore == null || totalScore == null) {
            return;
        }
        if (this.fullScore == 0) {
            this.fullScore = fullScore;
        }
        count++;
        if (score == 0) {
            zeroCount++;
        } else if (score.equals(fullScore)) {
            fullCount++;
        }
        if (passScore != null && score >= passScore) {
            passCount++;
        }
        if (excellentScore != null && score >= excellentScore) {
            excellentCount++;
        }
        sumScore += score;
        sumScore2 += (score * score);
        aggScore += (score * totalScore);
        sumTotalScore += totalScore;
        sumTotalScore2 += (totalScore * totalScore);
        maxScore = Math.max(maxScore, score);
        minScore = Math.min(minScore, score);
        avgScore = sumScore / count;
        // 递归法计算标准差
        tempSum = tempSum + 1.0 * (count - 1) / count * (score - avgScore) * (score - avgScore);
        stdev = count==1?0:Math.sqrt(tempSum / (count - 1));
        // 难度
        difficulty = fullScore > 0 ? avgScore / fullScore : 0;
        // 差异系数
        coefficient = avgScore > 0 ? stdev * 100 / avgScore : 0;
        // 区分度
        discrimination = (aggScore - sumScore * sumTotalScore / count) / Math.sqrt(
                (sumScore2 - sumScore * sumScore / count) * (sumTotalScore2 - sumTotalScore * sumTotalScore / count));
        // 及格率
        passRate = passCount * 1.0 / count;
        // 优秀率
        excellentRate = excellentCount * 1.0 / count;
        // 满分率
        fullScoreRate = fullCount * 1.0 / count;
    }

    @Override
    public String toString() {
        return "count=" + count + ",zeroCount=" + zeroCount + ",fullCount=" + fullCount + ",fullScore=" + fullScore
                + ",maxScore=" + maxScore + ",minScore=" + minScore + ",avgScore=" + avgScore + ",stdev=" + stdev
                + ",difficulty=" + difficulty + ",coefficient=" + coefficient + ",discrimination=" + discrimination;
    }
}
