package com.ym.mec.biz.service.impl;

import com.ym.mec.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.TeacherFreeTimeDao;
import com.ym.mec.biz.dal.entity.TeacherFreeTime;
import com.ym.mec.biz.service.TeacherFreeTimeService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TeacherFreeTimeServiceImpl extends BaseServiceImpl<Integer, TeacherFreeTime> implements TeacherFreeTimeService {

	@Autowired
	private TeacherFreeTimeDao teacherFreeTimeDao;

	@Override
	public BaseDAO<Integer, TeacherFreeTime> getDAO() {
		return teacherFreeTimeDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateSet(TeacherFreeTime teacherFreeTime) {
		if(teacherFreeTime == null || teacherFreeTime.getUserId() == null){
			throw new BizException("参数校验失败");
		}
		TeacherFreeTime oldTeacherFreeTime = teacherFreeTimeDao.findTeacherFreeTime(teacherFreeTime.getUserId());
		if(oldTeacherFreeTime == null){
			teacherFreeTimeDao.insert(teacherFreeTime);
		}else {
			teacherFreeTime.setUpdateTime(new Date());
			teacherFreeTimeDao.update(teacherFreeTime);
		}
	}

	@Override
	public TeacherFreeTime findTeacherFreeTime(Integer id) {
		return teacherFreeTimeDao.findTeacherFreeTime(id);
	}
}