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.exam.service.ExamStudentService;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectCollege;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectCollegeService;
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 SubjectCollegeModule extends SubjectModule implements Module {

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

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

    public void save() {
        ReportSubjectCollegeService service = SpringContextHolder.getBean(ReportSubjectCollegeService.class);
        ExamStudentService studentService = SpringContextHolder.getBean(ExamStudentService.class);
        for (String key : this.units.keySet()) {
            String s[] = key.split("\t");
            if (s.length > 1) {
                String subjectCode = s[0];
                String collegeName = s[1];
                BaseCalculatorUnit unit = units.get(key);
                BaseCounter counter = counters.get(key);
                ReportSubjectCollege r = new ReportSubjectCollege();
                r.setExamId(this.context.getExamId());
                r.setSubjectCode(subjectCode);
                r.setSubjectName(context.getSubject(subjectCode).getName());
                r.setCollegeName(collegeName);
                r.setAvgScore(unit.avgScore);
                r.setMaxScore(unit.maxScore);
                r.setMinScore(unit.minScore);
                r.setPassCount(unit.passCount);
                r.setPassRate(unit.passRate);
                r.setExcellentCount(unit.excellentCount);
                r.setExcellentRate(unit.excellentRate);
                r.setTotalCount((int) studentService.countByExamIdAndSubjectCodeAndCollege(this.context.getExamId(),
                        subjectCode, collegeName));
                r.setRealityCount(counter.realityCount);
                service.save(r);
            }
        }
    }

}
