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

import com.ym.mec.biz.dal.dao.CourseHomeworkDao;
import com.ym.mec.biz.dal.dto.Mapper;
import com.ym.mec.biz.dal.dto.TeacherHomeworkListDto;
import com.ym.mec.biz.dal.entity.CourseHomework;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.dal.page.CourseHomeworkQueryInfo;
import com.ym.mec.biz.service.CourseHomeworkService;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext.MessageSender;
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;
import java.util.stream.Collectors;

@Service
public class CourseHomeworkServiceImpl extends BaseServiceImpl<Long, CourseHomework> implements CourseHomeworkService {

	@Autowired
	private CourseHomeworkDao courseHomeworkDao;
	@Autowired
	private SysMessageService sysMessageService;

	@Override
	public BaseDAO<Long, CourseHomework> getDAO() {
		return courseHomeworkDao;
	}

	@Override
	public List<Map<String, Object>> findTeacherCourseHomeworkByClassGroup(CourseHomeworkQueryInfo queryInfo) {
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);
		List<TeacherHomeworkListDto> dataList = null;
		List<Map<String, Object>> result = new ArrayList<>();
		int count = courseHomeworkDao.countByClassGroupAndTeacher(params);
		if (count > 0) {
			dataList = courseHomeworkDao.findByClassGroupAndTeacher(params);
//			List<Integer> courseScheduleIds=dataList.stream()
//					.map(TeacherHomeworkListDto::getCourseScheduleId)
//					.distinct()
//					.collect(Collectors.toList());
//			List<Map<Integer, Integer>> courseTimeByClassGroup = teacherAttendanceDao.findCourseTimeByCourseSchedules(courseScheduleIds);
//			Map<Integer, Integer> map = MapUtil.convertIntegerMap(courseTimeByClassGroup);
//			dataList.forEach(teacherHomeworkListDto -> {
//				teacherHomeworkListDto.setCurrentClassTimes(map.get(teacherHomeworkListDto.getCourseScheduleId().longValue()));
//			});
//			List<Integer> courseScheduleIds = dataList.stream().map(TeacherHomeworkListDto::getCourseScheduleId).collect(Collectors.toList());
//			if (!CollectionUtils.isEmpty(courseScheduleIds)) {
//				Map<Integer, Integer> courseScheduleCurrentTimes = courseScheduleService.findCourseScheduleCurrentTimes(courseScheduleIds);
//				dataList.forEach(teacherHomeworkListDto -> {
//					teacherHomeworkListDto.setCurrentClassTimes(courseScheduleCurrentTimes.get(teacherHomeworkListDto.getCourseScheduleId()));
//				});
//			}
		}
		if (count != 0) {
			Map<String, List<TeacherHomeworkListDto>> collect = dataList.stream().collect(Collectors.groupingBy(TeacherHomeworkListDto::getDay));
			for (String key : collect.keySet()) {
				Map<String, Object> r = new HashMap<>();
				r.put("day", key);
				r.put("list", collect.get(key));
				result.add(r);
			}
		}
		return result;
	}

	@Override
	public void homeworkNoReplyRemind() {
		List<Mapper> list = courseHomeworkDao.queryTeacerNoReplyNum();

		for (Mapper mapper : list) {
			Map<Integer, String> receivers = new HashMap<>();
			Integer userId = (Integer) mapper.getKey();
			receivers.put(userId, userId + "");

			sysMessageService.batchSendMessage(MessageSender.JIGUANG, MessageTypeEnum.TEACHER_PUSH_JOB_WAIT_REPLY, receivers, null, 0, "6?", "TEACHER", mapper.getValue());
		}
	}
}
