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

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.ym.mec.biz.dal.dao.ClassGroupStudentMapperDao;
import com.ym.mec.biz.dal.dao.CourseScheduleDao;
import com.ym.mec.biz.dal.dao.MusicGroupStudentFeeDao;
import com.ym.mec.biz.dal.dao.StudentAttendanceDao;
import com.ym.mec.biz.dal.dto.StudentPersonalAttendanceDto;
import com.ym.mec.biz.dal.dto.StudentStatusCountUtilEntity;
import com.ym.mec.biz.dal.entity.ClassGroupStudentMapper;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.StudentAttendance;
import com.ym.mec.biz.dal.enums.ClassGroupStudentStatusEnum;
import com.ym.mec.biz.dal.enums.CourseStatusEnum;
import com.ym.mec.biz.dal.enums.StudentAttendanceStatusEnum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.StudentAttendanceQueryInfo;
import com.ym.mec.biz.service.StudentAttendanceService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import com.ym.mec.util.date.DateUtil;

@Service
public class StudentAttendanceServiceImpl extends BaseServiceImpl<Long, StudentAttendance>  implements StudentAttendanceService {
	
	@Autowired
	private StudentAttendanceDao studentAttendanceDao;
	@Autowired
	private MusicGroupStudentFeeDao studentFeeDao;
	@Autowired
	private CourseScheduleDao courseScheduleDao;
	
	@Autowired
	private ClassGroupStudentMapperDao classGroupStudentMapperDao;

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

	@Override
	public void addStudentAttendances(List<StudentAttendance> studentAttendances) {
		int classTimes=courseScheduleDao.countClassTimes(studentAttendances.get(0).getClassGroupId().longValue());
		studentAttendances.forEach(studentAttendance -> {
			studentAttendance.setCurrentClassTimes(classTimes);
			StudentAttendance studentAttendanceInfo = studentAttendanceDao.getStudentAttendanceInfo(studentAttendance);
			if (studentAttendance.getStatus() != StudentAttendanceStatusEnum.DROP_OUT) {
				//判断是否为连续旷课
				if(StudentAttendanceStatusEnum.TRUANT.equals(studentAttendance.getStatus())
						||StudentAttendanceStatusEnum.LEAVE.equals(studentAttendance.getStatus())){
					StudentAttendance studentLatestAttendanceInfo = studentAttendanceDao.getStudentLatestAttendanceInfo(studentAttendanceInfo);
					if(StudentAttendanceStatusEnum.TRUANT.equals(studentLatestAttendanceInfo.getStatus())
							||StudentAttendanceStatusEnum.LEAVE.equals(studentLatestAttendanceInfo.getStatus())){
						studentFeeDao.updateStudentAbsenteeismTimes(studentAttendanceInfo.getUserId(),
								studentAttendanceInfo.getMusicGroupId(),
								YesOrNoEnum.YES.getCode());
					}
				}else{
					studentFeeDao.updateStudentAbsenteeismTimes(studentAttendanceInfo.getUserId(),
							studentAttendanceInfo.getMusicGroupId(),
							YesOrNoEnum.NO.getCode());
				}
				studentAttendanceDao.insert(studentAttendanceInfo);
			}else{
				studentFeeDao.updateStudentAbsenteeismTimes(studentAttendanceInfo.getUserId(),
						studentAttendanceInfo.getMusicGroupId(),
						YesOrNoEnum.NO.getCode());
			}
		});
	}

	@Override
	public Map<String, Object> getCurrentCourseStudents(QueryInfo queryInfo) {
		Map<String,Object> result=new HashMap<>();

		PageInfo<StudentAttendance> pageInfo = super.queryPage(queryInfo);

		result.put("pageInfo",pageInfo);

		List<StudentStatusCountUtilEntity> stringIntegerMap = studentAttendanceDao.countStudentStatus(((StudentAttendanceQueryInfo) queryInfo).getClassGroupId());

		stringIntegerMap.forEach(studentStatusCount->{
			switch (studentStatusCount.getStudentStatus()){
				case LEAVE:
					result.put("numberOfLeavePeoples",studentStatusCount.getNumberOfStudent());
					break;
			default:
				break;

			}
		});

		return result;
	}

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

		List<StudentPersonalAttendanceDto> dataList = null;
		int count = studentAttendanceDao.queryStudentPersonalAttendancesCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentAttendanceDao.queryStudentPersonalAttendances(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	@Transactional
	public boolean leave(Integer userId, Long courseScheduleId, String remark) {
		
		CourseSchedule courseSchedule = courseScheduleDao.get(courseScheduleId);
		if(courseSchedule == null){
			throw new BizException("课程编号异常");
		}
		
		if(courseSchedule.getStatus() == CourseStatusEnum.OVER){
			throw new BizException("课程已结束");
		}
		
		Date date = new Date();
		
		if(DateUtil.addHours(date, 2).after(courseSchedule.getStartClassTime())){
			throw new BizException("开课2个小时之前才可以请假");
		}
		
		StudentAttendance studentAttendance  = new StudentAttendance();
		studentAttendance.setClassGroupId(courseSchedule.getClassGroupId());
		studentAttendance.setCourseScheduleId(courseScheduleId);
		studentAttendance.setCreateTime(date);
		studentAttendance.setCurrentClassTimes(0);
		studentAttendance.setRemark(remark);
		studentAttendance.setStatus(StudentAttendanceStatusEnum.LEAVE);
		studentAttendance.setUserId(userId);
		
		studentAttendanceDao.insert(studentAttendance);
		
		ClassGroupStudentMapper classGroupStudentMapper = classGroupStudentMapperDao.query(courseSchedule.getClassGroupId(), userId);
		classGroupStudentMapper.setStatus(ClassGroupStudentStatusEnum.LEAVE);
		classGroupStudentMapperDao.update(classGroupStudentMapper);
		
		//发送消息至老师
		
		return true;
	}
}