package cn.com.qmth.stmms.biz.exam.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.biz.exam.dao.MarkGroupDao;
import cn.com.qmth.stmms.biz.exam.model.MarkGroup;
import cn.com.qmth.stmms.biz.exam.model.MarkGroupPK;
import cn.com.qmth.stmms.biz.exam.service.ExamQuestionService;
import cn.com.qmth.stmms.biz.exam.service.MarkGroupService;
import cn.com.qmth.stmms.biz.mark.model.PictureConfigItem;

@Service("markGroupService")
public class MarkGroupServiceImpl extends BaseQueryService<MarkGroup> implements MarkGroupService {

    @Autowired
    private MarkGroupDao groupDao;

    @Autowired
    private ExamQuestionService questionService;

    @Transactional
    @Override
    public MarkGroup save(MarkGroup group) {
        return groupDao.save(group);
    }

    @Transactional
    @Override
    public void resetBuildTime(int examId, String subjectCode) {
        groupDao.updateBuildTime(examId, subjectCode, null);
    }

    @Transactional
    @Override
    public void updatePicList(int examId, String subjectCode, int number, List<PictureConfigItem> configList) {
        groupDao.updatePicList(examId, subjectCode, number,
                configList != null && configList.size() > 0
                        ? StringUtils.join(configList, PictureConfigItem.DB_ITEM_JOINER)
                        : "");
    }

    @Transactional
    @Override
    public void updateTitle(int examId, String subjectCode, int number, String title) {
        groupDao.updateTitle(examId, subjectCode, number, title);
        questionService.updateMainTitle(examId, subjectCode, false, number, title);
    }

    @Transactional
    @Override
    public void updateBuildTime(int examId, String subjectCode, int number, Date time) {
        groupDao.updateBuildTime(examId, subjectCode, number, time);
    }

    @Override
    public MarkGroup findOne(int examId, String subjectCode, int number) {
        MarkGroupPK pk = new MarkGroupPK();
        pk.setExamId(examId);
        pk.setSubjectCode(subjectCode);
        pk.setNumber(number);
        return groupDao.findOne(pk);
    }

    @Override
    public long countByExamAndSubject(int examId, String subjectCode) {
        return groupDao.countByExamIdAndSubjectCode(examId, subjectCode);
    }

    @Override
    public List<MarkGroup> findByExam(int examId) {
        return groupDao.findByExamId(examId);
    }

    @Override
    public List<MarkGroup> findByExamAndSubject(int examId, String code) {
        return groupDao.findByExamIdAndSubjectCode(examId, code);
    }

    @Override
    public List<MarkGroup> findByExamAndSubjectWithDouble(int examId, String code) {
        return groupDao.findByExamIdAndSubjectCodeWithDouble(examId, code);
    }

    @Override
    public long countByExam(int examId) {
        return groupDao.countByExamId(examId);
    }

    @Override
    public long sumLibraryCount(Integer examId) {
        Integer count = groupDao.sumLibraryCount(examId);
        return count != null ? count : 0;
    }

    @Override
    public long sumMarkedCount(Integer examId) {
        Integer count = groupDao.sumMarkedCount(examId);
        return count != null ? count : 0;
    }

    @Override
    public void updateDoubleRate(int examId, String subjectCode, Integer number, Double doubleRate) {
        MarkGroup group = findOne(examId, subjectCode, number);
        group.setDoubleRate(doubleRate);
        save(group);
    }

    @Override
    public void updateArbitrateThreshold(int examId, String subjectCode, Integer number, Double arbitrateThreshold) {
        MarkGroup group = findOne(examId, subjectCode, number);
        group.setArbitrateThreshold(arbitrateThreshold);
        save(group);

    }

}
