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

import java.util.ArrayList;
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.AnswerCardDao;
import cn.com.qmth.stmms.biz.exam.dao.AnswerCardSubjectDao;
import cn.com.qmth.stmms.biz.exam.model.AnswerCard;
import cn.com.qmth.stmms.biz.exam.model.AnswerCardSubject;
import cn.com.qmth.stmms.biz.exam.service.AnswerCardService;
import cn.com.qmth.stmms.common.enums.CardSource;

@Service
public class AnswerCardServiceImpl extends BaseQueryService<AnswerCard> implements AnswerCardService {

    @Autowired
    private AnswerCardDao cardDao;

    @Autowired
    private AnswerCardSubjectDao cardSubjectDao;

    @Override
    public AnswerCard findByExamIdAndNumber(Integer examId, Integer number) {
        return cardDao.findByExamIdAndNumber(examId, number);
    }

    @Override
    public int findMaxNumberByExamId(Integer examId) {
        List<AnswerCard> cards = this.findByExamId(examId);
        int number = 0;
        for (AnswerCard card : cards) {
            if (number < card.getNumber()) {
                number = card.getNumber();
            }
        }
        return number;
    }

    @Override
    public List<AnswerCard> findByExamId(Integer examId) {
        return cardDao.findByExamId(examId);
    }

    @Transactional
    @Override
    public void save(AnswerCard card, String subjectCode) {
        if (StringUtils.isNotBlank(subjectCode)) {
            AnswerCardSubject answerCardSubject = cardSubjectDao
                    .findByExamIdAndCardNumberAndSubjectCode(card.getExamId(), card.getNumber(), subjectCode);
            if (answerCardSubject == null) {
                answerCardSubject = new AnswerCardSubject(card.getExamId(), card.getNumber(), subjectCode);
                cardSubjectDao.save(answerCardSubject);
            }
        }
        this.cardDao.save(card);
    }

    @Transactional
    @Override
    public void delete(AnswerCard card) {
        cardSubjectDao.deleteByExamIdAndCardNumber(card.getExamId(), card.getNumber());
        this.cardDao.delete(card);
    }

    @Override
    public AnswerCard findByExamIdAndSubjectCode(Integer examId, String subjectCode, CardSource source) {
        AnswerCard card = cardDao.findByExamIdAndSubjectCodeAndCodeIsNull(examId, subjectCode, source);
        return card;
    }

    @Override
    public List<AnswerCardSubject> getSubjectByExamIdAndNumber(Integer examId, Integer number) {
        return cardSubjectDao.findByExamIdAndCardNumber(examId, number);
    }

    @Override
    public AnswerCard findByExamIdAndCode(Integer examId, String code) {
        return cardDao.findByExamIdAndCode(examId, code);
    }

    @Transactional
    @Override
    public void save(AnswerCard card, List<String> subjectCodeList) {
        if (!subjectCodeList.isEmpty()) {
            cardSubjectDao.deleteByExamIdAndCardNumber(card.getExamId(), card.getNumber());
            List<AnswerCardSubject> list = new ArrayList<AnswerCardSubject>();
            for (String subjectCode : subjectCodeList) {
                if (StringUtils.isNotBlank(subjectCode)) {
                    AnswerCardSubject answerCardSubject = new AnswerCardSubject(card.getExamId(), card.getNumber(),
                            subjectCode);
                    list.add(answerCardSubject);
                }
            }
            cardSubjectDao.save(list);
        }
        this.cardDao.save(card);
    }

    @Override
    public boolean hasSubject(int examId, String code) {
        return cardSubjectDao.findByExamIdAndSubjectCode(examId, code).size() > 0;
    }
}
