package com.keao.edu.user.service.impl;


import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.api.entity.StudentAttendance;
import com.keao.edu.user.dao.StudentAttendanceDao;
import com.keao.edu.user.service.StudentAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class StudentAttendanceServiceImpl extends BaseServiceImpl<Long, StudentAttendance> implements StudentAttendanceService {
	
	@Autowired
	private StudentAttendanceDao studentAttendanceDao;

	@Override
	public BaseDAO<Long, StudentAttendance> getDAO() {
		return studentAttendanceDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class,isolation = Isolation.SERIALIZABLE)
	public void upsetAttendance(Long examRegistrationId, Integer signFlag) {
		StudentAttendance studentAttendance = studentAttendanceDao.findByRegistrationId(examRegistrationId);
		Date date = new Date();
		if(studentAttendance == null){
			studentAttendance = new StudentAttendance();
			studentAttendance.setExamRegistrationId(examRegistrationId);
//			studentAttendance.setSignInTime(date);
			studentAttendanceDao.insert(studentAttendance);
		}
		//0代表签到1签退
		if(signFlag == 1){
			studentAttendance.setSignOutTime(date);
		}else {
			studentAttendance.setSignInTime(date);
		}
		studentAttendanceDao.update(studentAttendance);
	}
}