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.ExamQuestionService;
import cn.com.qmth.stmms.biz.exam.service.query.ExamQuestionSearchQuery;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectClassGroup;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectClassGroupService;
import cn.com.qmth.stmms.biz.report.utils.Module;
import cn.com.qmth.stmms.biz.report.utils.QuestionGroup;
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.utils.SpringContextHolder;

public class SubjectClassGroupModule extends SubjectGroupModule implements Module {

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

    public String getKey(ExamStudent student, QuestionGroup group) {
        return student.getSubjectCode() + "\t" + student.getClassName() + "\t" + group.isObjective() + "\t"
                + StringUtils.trimToEmpty(group.getPaperType()) + "\t" + +group.getMainNumber();
    }

    public void save() {
        ReportSubjectClassGroupService service = SpringContextHolder.getBean(ReportSubjectClassGroupService.class);
        ExamQuestionService questionService = SpringContextHolder.getBean(ExamQuestionService.class);
        for (String key : this.calculators.keySet()) {
            String s[] = key.split("\t");
            if (s.length > 4) {
                String subjectCode = s[0];
                String className = s[1];
                Boolean objective = Boolean.parseBoolean(s[2]);
                String paperType = s[3];
                Integer mainNumber = Integer.parseInt(s[4]);
                BaseCalculatorUnit unit = calculators.get(key);
                ReportSubjectClassGroup r = new ReportSubjectClassGroup();
                r.setExamId(this.context.getExamId());
                r.setSubjectCode(subjectCode);
                r.setClassName(className);
                r.setSubjectName(context.getSubject(subjectCode).getName());
                String groupKey = subjectCode + "\t" + objective + "\t" + paperType + "\t" + mainNumber;
                r.setGroupName(context.getGroup(groupKey).getMainTitle());
                r.setGroupNumber(mainNumber);
                r.setTotalScore(unit.fullScore);
                r.setObjective(objective);
                r.setDiscrimination(unit.discrimination);
                r.setDifficulty(unit.difficulty);
                r.setCoefficient(unit.coefficient);

                paperType = StringUtils.trimToNull(paperType);
                ExamQuestionSearchQuery query = new ExamQuestionSearchQuery();
                query.setExamId(context.getExamId());
                query.setSubjectCode(subjectCode);
                query.setObjective(objective);
                query.setTotalScoreGt(0d);
                query.setPaperType(paperType);
                query.setMainNumber(mainNumber);
                int questionCount = (int) questionService.countByQuery(query);
                r.setQuestionCount(questionCount);
                r.setPaperType(paperType);
                service.save(r);
            }
        }
    }

}
