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

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectTeacherClass;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectTeacherClassService;
import cn.com.qmth.stmms.biz.report.utils.Module;
import cn.com.qmth.stmms.biz.report.utils.ReportContext;
import cn.com.qmth.stmms.biz.report.utils.unit.BaseCalculatorUnit;
import cn.com.qmth.stmms.biz.report.utils.unit.BaseCounter;
import cn.com.qmth.stmms.biz.utils.SpringContextHolder;

public class SubjectTeacherClassModule extends SubjectModule implements Module {

    public SubjectTeacherClassModule(ReportContext context) {
        super(context);
    }

    public String getKey(ExamStudent student) {
        return student.getSubjectCode() + "\t" + StringUtils.trimToEmpty(student.getTeacher()) + "\t"
                + StringUtils.trimToEmpty(student.getClassName());
    }

    public void save() {
        ReportSubjectTeacherClassService service = SpringContextHolder.getBean(ReportSubjectTeacherClassService.class);
        for (String key : this.units.keySet()) {
            String s[] = key.split("\t");
            if (s.length > 2) {
                String subjectCode = s[0];
                String teacherName = s[1];
                String className = s[2];
                BaseCalculatorUnit unit = units.get(key);
                BaseCounter counter = counters.get(key);
                ReportSubjectTeacherClass teacherClass = new ReportSubjectTeacherClass();
                teacherClass.setExamId(this.context.getExamId());
                teacherClass.setSubjectCode(subjectCode);
                teacherClass.setSubjectName(context.getSubject(subjectCode).getName());
                teacherClass.setTeacherName(teacherName);
                teacherClass.setClassName(className);
                teacherClass.setRealityCount(counter.realityCount);
                teacherClass.setPassCount(unit.passCount);
                teacherClass.setExcellentCount(unit.excellentCount);
                teacherClass.setMaxScore(unit.maxScore);
                teacherClass.setMinScore(unit.minScore);
                teacherClass.setPassRate(unit.passRate);
                teacherClass.setExcellentRate(unit.excellentRate);
                teacherClass.setAvgScore(unit.avgScore);
                service.save(teacherClass);
            }
        }

    }

}
