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

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.TeacherAttendanceDto;
import com.ym.mec.biz.dal.dto.TeacherPersonalAttendanceDto;
import com.ym.mec.biz.dal.dto.TeacherSignOutDto;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.CourseStatusEnum;
import com.ym.mec.biz.dal.enums.ParamEnum;
import com.ym.mec.biz.dal.enums.SignStatusEnum;
import com.ym.mec.biz.dal.page.TeacherAttendanceQueryInfo;
import com.ym.mec.biz.service.CourseHomeworkService;
import com.ym.mec.biz.service.TeacherAttendanceService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TeacherAttendanceServiceImpl extends BaseServiceImpl<Long, TeacherAttendance>  implements TeacherAttendanceService {
	
	@Autowired
	private TeacherAttendanceDao teacherAttendanceDao;
	@Autowired
	private CourseScheduleDao courseScheduleDao;
	@Autowired
	private CourseHomeworkService courseHomeworkService;
	@Autowired
	private StudentAttendanceDao studentAttendanceDao;
	@Autowired
	private StudentCourseHomeworkDao studentCourseHomeworkDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private ClassGroupDao classGroupDao;

	@Override
	public BaseDAO<Long, TeacherAttendance> getDAO() {
		return teacherAttendanceDao;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addTeacherAttendanceRecord(TeacherSignOutDto teacherSignOutDto) {
		SysUser user = sysUserFeignService.queryUserInfo();
		TeacherAttendance teacherAttendance=teacherSignOutDto.getTeacherAttendanceInfo();
		teacherAttendance.setTeacherId(Long.valueOf(user.getId()));
		CourseSchedule courseSchedule=new CourseSchedule();
		TeacherAttendanceDto currentCourseDetail = courseScheduleDao.getCurrentCourseDetail(teacherAttendance.getCourseScheduleId());
		courseSchedule.setId(currentCourseDetail.getCourseScheduleId());
		Date date = new Date();
		if(teacherAttendance.getStatus()!= SignStatusEnum.YES_QUIT){
			Long timeGap=(date.getTime()-currentCourseDetail.getStartClassTime().getTime())/1000/60;
			if(timeGap<= ParamEnum.TEACHER_ATTENDANCE_TIME_GAP.getCode()){
				teacherAttendance.setStatus(SignStatusEnum.SIGN);
			}else{
				teacherAttendance.setStatus(SignStatusEnum.EXCEPTION);
			}
			courseSchedule.setStatus(CourseStatusEnum.UNDERWAY);
		}else{
			courseSchedule.setStatus(CourseStatusEnum.OVER);
		}
		courseScheduleDao.update(courseSchedule);
		teacherAttendance.setMusicGroupId(currentCourseDetail.getMusicGroupId());
		teacherAttendance.setClassGroupId(currentCourseDetail.getClassId());
		teacherAttendance.setSignInTime(date);
		teacherAttendance.setCreateTime(date);
		teacherAttendanceDao.insert(teacherAttendance);
		if(teacherAttendance.getStatus()==SignStatusEnum.YES_QUIT){
			//更新课次
			ClassGroup classGroup=new ClassGroup();
			classGroup.setId(currentCourseDetail.getClassId().intValue());
			classGroup.setCurrentClassTimes(teacherAttendanceDao.countClassTime(currentCourseDetail.getClassId()));
			classGroupDao.update(classGroup);
			//新增课堂作业
			CourseHomework courseHomework=teacherSignOutDto.getCourseHomeworkInfo();
			courseHomework.setCourseScheduleId(teacherAttendance.getCourseScheduleId());
			courseHomework.setMusicGroupId(currentCourseDetail.getMusicGroupId().intValue());
			courseHomework.setClassGroupId(currentCourseDetail.getClassId().intValue());
			courseHomework.setExpectNum(studentAttendanceDao.countNormalAttendanceStudentNums(teacherAttendance.getCourseScheduleId()));
			courseHomeworkService.insert(courseHomework);
			List<StudentCourseHomework> studentCourseHomeworks = studentCourseHomeworkDao.constructInitialStudentHomeworkRecords(teacherAttendance.getCourseScheduleId(),
					courseHomework.getId());
			studentCourseHomeworkDao.batchInsertStudentCourseHomeworkRecord(studentCourseHomeworks);
		}
	}

	@Override
	public PageInfo getTeacherPersonalAttendances(TeacherAttendanceQueryInfo queryInfo) {
		PageInfo<TeacherPersonalAttendanceDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List<TeacherPersonalAttendanceDto> dataList = null;
		int count = teacherAttendanceDao.getTeacherPersonalAttendancesCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherAttendanceDao.getTeacherPersonalAttendances(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}


}