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

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.StudentAttendance;
import com.ym.mec.biz.dal.enums.StudentAttendanceStatusEnum;
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.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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

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

	@Override
	public void addStudentAttendances(List<StudentAttendance> studentAttendances) {
		studentAttendances.forEach(studentAttendance -> {
			if (studentAttendance.getStatus() != StudentAttendanceStatusEnum.DROP_OUT) {
				studentAttendanceDao.insert(studentAttendanceDao.getStudentAttendanceInfo(studentAttendance));
			}
		});
	}

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

		PageInfo 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;

			}
		});

		return result;
	}

	@Override
	public PageInfo 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;
	}
}