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

import java.util.HashSet;
import java.util.List;
import java.util.Set;

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.MarkerClassDao;
import cn.com.qmth.stmms.biz.exam.model.MarkerClass;
import cn.com.qmth.stmms.biz.exam.service.MarkerClassService;
import cn.com.qmth.stmms.biz.exam.service.MarkerService;

@Service("markerClassService")
public class MarkerClassServiceImpl extends BaseQueryService<MarkerClass> implements MarkerClassService {

    @Autowired
    private MarkerClassDao markerClassDao;

    @Autowired
    private MarkerService markerService;

    @Transactional
    @Override
    public void save(int userId, int examId, String[] classes) {
        this.deleteByUserIdAndExamId(userId, examId);
        if (classes != null) {
            for (String className : classes) {
                MarkerClass m = new MarkerClass(userId, examId, className);
                markerClassDao.save(m);
            }
        }
    }

    @Override
    public long countByUserIdAndExamId(int userId, int examId) {
        return markerClassDao.countByUserIdAndExamId(userId, examId);
    }

    @Transactional
    @Override
    public void deleteByUserIdAndExamId(int userId, int examId) {
        markerClassDao.deleteByUserIdAndExamId(userId, examId);
    }

    @Override
    public Set<String> findClassNameByUserIdAndExamId(int userId, int examId) {
        Set<String> set = new HashSet<String>();
        List<MarkerClass> classes = this.findByUserIdAndExamId(userId, examId);
        for (MarkerClass markerClass : classes) {
            set.add(markerClass.getClassName());
        }
        return set;
    }

    @Override
    public List<MarkerClass> findByUserIdAndExamId(int userId, int examId) {
        return markerClassDao.findByUserIdAndExamId(userId, examId);
    }

    @Override
    public MarkerClass findByUserIdAndExamIdAndClassName(Integer userId, Integer examId, String className) {
        return markerClassDao.findByUserIdAndExamIdAndClassName(userId, examId, className);
    }

    @Transactional
    @Override
    public int batchSave(List<MarkerClass> list) {
        markerClassDao.save(list);
        return list.size();
    }
}
