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.CourseHomeworkDao;
import com.ym.mec.biz.dal.dao.CourseScheduleDao;
import com.ym.mec.biz.dal.dao.StudentCourseHomeworkDao;
import com.ym.mec.biz.dal.dto.CourseHomeworkStudentDetailDto;
import com.ym.mec.biz.dal.entity.CourseHomework;
import com.ym.mec.biz.dal.entity.CourseSchedule;
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.CourseHomeworkQueryInfo;
import com.ym.mec.biz.service.CourseHomeworkService;
import com.ym.mec.biz.service.StudentCourseHomeworkService;
import com.ym.mec.biz.service.SysMessageService;
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.thirdparty.message.MessageSenderPluginContext.MessageSender;

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 StudentCourseHomeworkServiceImpl extends BaseServiceImpl<Long, StudentCourseHomework> implements StudentCourseHomeworkService {

	@Autowired
	private StudentCourseHomeworkDao studentCourseHomeworkDao;
	@Autowired
	private CourseHomeworkService courseHomeworkService;
	@Autowired
	private CourseHomeworkDao courseHomeworkDao;
	@Autowired
	private CourseScheduleDao courseScheduleDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Autowired
	private SysMessageService sysMessageService;

	@Override
	public BaseDAO<Long, StudentCourseHomework> getDAO() {
		return studentCourseHomeworkDao;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public long insert(StudentCourseHomework bean) {
		SysUser user = sysUserFeignService.queryUserInfo();
		bean.setUserId(user.getId());
		bean.setStatus(YesOrNoEnum.YES);
		long insert = super.insert(bean);

		CourseSchedule courseSchedule = courseScheduleDao.get(bean.getCourseScheduleId());

		CourseHomework courseHomework = new CourseHomework();
		courseHomework.setId(bean.getCourseHomeworkId());
		courseHomework.setGroupType(courseSchedule.getGroupType());
		courseHomework.setCompletedNum(studentCourseHomeworkDao.countCompletedStudentNum(bean.getCourseHomeworkId()));

		courseHomeworkService.update(courseHomework);
		return insert;
	}

	@Override
	public int update(StudentCourseHomework bean) {
		super.update(bean);
		bean=studentCourseHomeworkDao.get(bean.getId());
		CourseSchedule courseSchedule = courseScheduleDao.get(bean.getCourseScheduleId());

		CourseHomework courseHomework = new CourseHomework();
		courseHomework.setId(bean.getCourseHomeworkId());
		courseHomework.setGroupType(courseSchedule.getGroupType());
		courseHomework.setCompletedNum(studentCourseHomeworkDao.countCompletedStudentNum(bean.getCourseHomeworkId()));

		return courseHomeworkService.update(courseHomework);
	}

	@Override
	public CourseHomeworkStudentDetailDto findCourseHomeworkStudentDetail(Long courseScheduleID, Long userId) {
		return studentCourseHomeworkDao.findCourseHomeworkStudentDetail(courseScheduleID, userId);
	}

	@Override
	public boolean updateReplyStatus(Integer teacherId, Long courseHomeworkId, Integer studentId) {
		StudentCourseHomework byStudentAndCourseHomewok = studentCourseHomeworkDao.findByStudentAndCourseHomewok(courseHomeworkId, studentId);
		if (Objects.isNull(byStudentAndCourseHomewok)) {
			throw new BizException("作业记录不存在");
		}
		if (byStudentAndCourseHomewok.getIsReplied() == YesOrNoEnum.YES) {
			return true;
		}
		CourseHomework byTeacherAndCourseHomewok = courseHomeworkDao.findByTeacherAndCourseHomewok(courseHomeworkId, teacherId);
		if (Objects.nonNull(byTeacherAndCourseHomewok)) {
			byStudentAndCourseHomewok.setIsReplied(YesOrNoEnum.YES);
			studentCourseHomeworkDao.update(byStudentAndCourseHomewok);
		}
		return true;
	}

	@Override
	public PageInfo<StudentCourseHomework> queryPage(QueryInfo queryInfo) {
		SysUser user = sysUserFeignService.queryUserInfo();
		CourseHomeworkQueryInfo courseHomeworkQueryInfo = (CourseHomeworkQueryInfo) queryInfo;
		courseHomeworkQueryInfo.setUserId(Long.valueOf(user.getId()));
		return super.queryPage(queryInfo);
	}

	@Override
	public List<StudentCourseHomework> findStudentCourseHomeworkByCourse(Long courseSchedule, String userName) {
		if (Objects.isNull(courseSchedule)) {
			throw new BizException("请指定课程");
		}
		return studentCourseHomeworkDao.findByCourseSchedule(courseSchedule, userName);
	}

	@Override
	public void homeworkRemind() {
		List<StudentCourseHomework> list = studentCourseHomeworkDao.queryNotDoneHomeworkList();
		StringBuffer sb;
		for (StudentCourseHomework sch : list) {
			sb = new StringBuffer("courseScheduleID=").append(sch.getCourseScheduleId()).append("&studentCourseHomeworkId=").append(sch.getId());
			Map<Integer, String> receivers = new HashMap<Integer, String>(1);
			receivers.put(sch.getUserId(), sch.getUserId() + "");
			sysMessageService.batchSendMessage(MessageSender.JIGUANG, MessageTypeEnum.STUDENT_PUSH_JOB_MESSAGE, receivers, null, 0, "3?" + sb,"STUDENT", sch.getCourseHomework()
					.getMusicGroup().getName(), sch.getCourseHomework().getClassGroup().getName());
		}
	}

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

		List<StudentCourseHomework> dataList = null;
		int count = studentCourseHomeworkDao.countStudentCourseHomeworks(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentCourseHomeworkDao.findStudentCourseHomeworks(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}
}
