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.UserExamDao;
import cn.com.qmth.stmms.biz.exam.model.UserExam;
import cn.com.qmth.stmms.biz.exam.service.UserExamService;

@Service("userExamService")
public class UserExamServiceImpl extends BaseQueryService<UserExam> implements UserExamService {

    @Autowired
    private UserExamDao userExamDao;

    @Override
    public Set<Integer> findExamIds(Integer userId) {
        Set<Integer> set = new HashSet<>();
        List<UserExam> list = userExamDao.findByUserId(userId);
        for (UserExam u : list) {
            set.add(u.getExamId());
        }
        return set;
    }

    @Transactional
    @Override
    public void updateByUserId(Integer userId, Set<Integer> examIdSet) {
        userExamDao.deleteByUserId(userId);
        for (Integer examId : examIdSet) {
            UserExam userExam = new UserExam();
            userExam.setExamId(examId);
            userExam.setUserId(userId);
            userExamDao.save(userExam);
        }

    }

}
