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

import com.ym.mec.biz.dal.dao.CourseHomeworkDao;
import com.ym.mec.biz.dal.dao.StudentCourseHomeworkDao;
import com.ym.mec.biz.dal.dao.StudentCourseHomeworkReplyDao;
import com.ym.mec.biz.dal.dto.Mapper;
import com.ym.mec.biz.dal.dto.TeacherHomeworkListDto;
import com.ym.mec.biz.dal.dto.WebCourseHomeworkListDto;
import com.ym.mec.biz.dal.entity.CourseHomework;
import com.ym.mec.biz.dal.entity.StudentCourseHomework;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.CourseHomeWorkTemplateQueryInfo;
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.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext.MessageSender;
import com.ym.mec.util.collection.MapUtil;
import com.ym.mec.util.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

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

	@Autowired
	private CourseHomeworkDao courseHomeworkDao;

	@Autowired
	private StudentCourseHomeworkDao studentCourseHomeworkDao;

	@Autowired
	private StudentCourseHomeworkReplyDao studentCourseHomeworkReplyDao;

	@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<Long> courseIds = dataList.stream().mapToLong(TeacherHomeworkListDto::getCourseScheduleId).boxed().collect(Collectors.toList());
			List<StudentCourseHomework> allStudentCourseHomeworks = studentCourseHomeworkDao.findByCourses(courseIds);
			Map<Long, List<StudentCourseHomework>> homeworkStudentMap = allStudentCourseHomeworks.stream().collect(Collectors.groupingBy(StudentCourseHomework::getCourseScheduleId));
			for (TeacherHomeworkListDto teacherHomeworkListDto : dataList) {
				List<StudentCourseHomework> studentCourseHomeworks = homeworkStudentMap.get(teacherHomeworkListDto.getCourseScheduleId().longValue());
				if(CollectionUtils.isEmpty(studentCourseHomeworks)){
					continue;
				}
				long noRepliedNum=studentCourseHomeworks.stream().filter(e -> YesOrNoEnum.YES.equals(e.getStatus())&&YesOrNoEnum.NO.equals(e.getIsReplied())).count();
				if(noRepliedNum<=0){
					teacherHomeworkListDto.setIsReplied(YesOrNoEnum.YES.getCode());
				}
				long notSubmitNum = studentCourseHomeworks.stream().filter(e -> e.getStatus().equals(YesOrNoEnum.NO)).count();
				if(notSubmitNum<=0){
					teacherHomeworkListDto.setIsSubmit(1);
				}
				if(noRepliedNum>0){
					teacherHomeworkListDto.setStatus(1);
				}else if(noRepliedNum<=0&&notSubmitNum>0){
					teacherHomeworkListDto.setStatus(2);
				}else if(noRepliedNum<=0&&notSubmitNum<=0){
					teacherHomeworkListDto.setStatus(3);
				}
			}
		}
		if (count != 0) {
			Map<String, List<TeacherHomeworkListDto>> collect = dataList.stream().collect(Collectors.groupingBy(TeacherHomeworkListDto::getDay));
			List<Date> dates=new ArrayList<>();
			collect.keySet().forEach(ds-> dates.add(DateUtil.stringToDate(ds,"yyyy-MM-dd")));
			dates.sort(Comparator.comparing(Date::getTime));
			dates.sort(Comparator.reverseOrder());
			for (Date date : dates) {
				String key=DateUtil.dateToString(date, "yyyy-MM-dd");
				Map<String, Object> r = new HashMap<>();
				r.put("day", key);
				collect.get(key).sort(Comparator.comparing(TeacherHomeworkListDto::getStartClassTime).reversed());
				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());
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean delHomwworkByCourseScheduleId(Long courseScheduleId) {
		// 删除作业
		courseHomeworkDao.delByCourseScheduleId(courseScheduleId);
		// 删除学生作业
		studentCourseHomeworkDao.delByCourseScheduleId(courseScheduleId);
		// 删除作业回复
		studentCourseHomeworkReplyDao.delByCourseScheduleId(courseScheduleId);
		return true;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean delHomwworkByCourseScheduleId(List<Long> courseScheduleIdList) {
		// 删除作业
		courseHomeworkDao.batchDelByCourseScheduleId(courseScheduleIdList);
		// 删除学生作业
		studentCourseHomeworkDao.batchDelByCourseScheduleId(courseScheduleIdList);
		// 删除作业回复
		studentCourseHomeworkReplyDao.delByCourseScheduleIds(courseScheduleIdList);
		return true;
	}

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

		List<WebCourseHomeworkListDto> dataList = null;
		int count = courseHomeworkDao.findHomeCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = courseHomeworkDao.queryHomePage(params);
			List<Long> homeWorkIds = dataList.stream().map(e -> e.getId()).collect(Collectors.toList());
			List<Map<Long,Long>> repliedList = studentCourseHomeworkDao.queryReplied(homeWorkIds);
			Map<Long,Long> map = MapUtil.convertIntegerMap(repliedList);
			dataList.forEach(e->{
				Long aLong = map.get(e.getId());
				if(aLong != null){
					e.setRepliedNum(aLong.intValue());
				}
			});
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}
}
