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

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.MarkGroupService;
import cn.com.qmth.stmms.biz.mark.model.MarkConfigItem;
import cn.com.qmth.stmms.common.enums.ExamStatus;
import cn.com.qmth.stmms.common.enums.MarkMode;
import cn.com.qmth.stmms.common.enums.MarkStatus;
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 java.util.Date;
import java.util.List;

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

    @Autowired
    private MarkGroupDao groupDao;

    @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<MarkConfigItem> configList) {
        groupDao.updatePicList(examId, subjectCode, number, configList != null && configList.size() > 0 ?
                StringUtils.join(configList, MarkConfigItem.DB_ITEM_JOINER) :
                "");
    }

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

    @Transactional
    @Override
    public void updateDoubleRate(int examId, String subjectCode, Integer number, Double doubleRate) {
        groupDao.updateDoubleRate(examId, subjectCode, number, doubleRate);
    }

    @Transactional
    @Override
    public void updateMarkMode(int examId, String subjectCode, Integer number, MarkMode markMode) {
        groupDao.updateMarkMode(examId, subjectCode, number, markMode);
    }

    @Transactional
    @Override
    public void updateArbitrateThreshold(int examId, String subjectCode, Integer number, Double arbitrateThreshold) {
        groupDao.updateArbitrateThreshold(examId, subjectCode, number, arbitrateThreshold);
    }

    @Transactional
    @Override
    public void updateSheetView(int examId, String subjectCode, Integer number, boolean sheetView) {
        groupDao.updateSheetView(examId, subjectCode, number, sheetView);
    }

    @Transactional
    @Override
    public void updateEnableAllZero(int examId, String subjectCode, Integer number, boolean enableAllZero) {
        groupDao.updateEnableAllZero(examId, subjectCode, number, enableAllZero);
    }

    @Transactional
    @Override
    public boolean updateStatus(int examId, String subjectCode, Integer number, MarkStatus newStatus,
            MarkStatus... currentStatus) {
        return groupDao.updateStatus(examId, subjectCode, number, newStatus, currentStatus) > 0;
    }

    @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 countByExamAndSubjectAndStatus(Integer examId, String subjectCode, MarkStatus... status) {
        return groupDao.countByExamIdAndSubjectCodeAndStatus(examId, subjectCode, status);
    }

    @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> findByExamAndSubjectAndStatus(Integer examId, String subjectCode, MarkStatus... status) {
        return groupDao.findByExamIdAndSubjectCodeAndStatus(examId, subjectCode, status);
    }

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

    @Override
    public List<Integer> findExamIdByStatus(ExamStatus examStatus, MarkStatus... markStatus) {
        return groupDao.findExamIdByStatus(examStatus, markStatus);
    }

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

    @Override
    public boolean validateStatus(Integer examId, String subjectCode, Integer number, MarkStatus... status) {
        return groupDao.countByExamIdAndSubjectCodeAndNumberAndStatus(examId, subjectCode, number, status) > 0;
    }

    @Override
    public int findMaxNumberByExamIdAndSubjectCode(int examId, String subjectCode) {
        List<MarkGroup> groups = groupDao.findByExamIdAndSubjectCode(examId, subjectCode);
        int number = 0;
        for (MarkGroup group : groups) {
            if (number < group.getNumber()) {
                number = group.getNumber();
            }
        }
        return number;
    }

    @Override
    public List<MarkGroup> findByExamAndSubjectAndSelective(Integer examId, String subjectCode, boolean selective) {
        return groupDao.findByExamAndSubjectAndSelective(examId, subjectCode, selective);
    }

}
