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

import com.alibaba.fastjson.JSONObject;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dao.TeacherLeaveRecordDao;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.CourseSchedule.CourseScheduleType;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.entity.TeacherLeaveRecord;
import com.ym.mec.biz.dal.entity.TeacherLeaveRecord.CoursesScheduleHistory;
import com.ym.mec.biz.dal.enums.AuditStatusEnum;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.dal.enums.SysUserRoleEnum;
import com.ym.mec.biz.event.source.SendSeoMessageSource;
import com.ym.mec.biz.service.CourseScheduleService;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.biz.service.TeacherLeaveRecordService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
import com.ym.mec.util.date.DateUtil;
import com.ym.mec.util.json.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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;

import static com.ym.mec.biz.dal.enums.SysUserRoleEnum.EDUCATION;
import static com.ym.mec.biz.dal.enums.SysUserRoleEnum.ORGAN_MANAGER;

@Service
public class TeacherLeaveRecordServiceImpl extends BaseServiceImpl<Long, TeacherLeaveRecord> implements TeacherLeaveRecordService {

	@Autowired
	private TeacherLeaveRecordDao teacherLeaveRecordDao;
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private CourseScheduleService courseScheduleService;
	@Autowired
	private SysMessageService sysMessageService;
	@Autowired
	private SendSeoMessageSource sendSeoMessageSource;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Long, TeacherLeaveRecord> getDAO() {
		return teacherLeaveRecordDao;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean askForLeave(TeacherLeaveRecord teacherLeaveRecord) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			throw new BizException("获取用户信息失败");
		}
		Date date = new Date();

		teacherLeaveRecord.setUserId(sysUser.getId());
		teacherLeaveRecord.setUpdateTime(date);
		teacherLeaveRecord.setCreateTime(date);
		teacherLeaveRecord.setStatus(AuditStatusEnum.ING);
		teacherLeaveRecord.setDays(DateUtil.hoursBetween(teacherLeaveRecord.getStartTime(), teacherLeaveRecord.getEndTime()) / 8d);

		if (StringUtils.isNotBlank(teacherLeaveRecord.getCoursesScheduleJson())) {
			List<CoursesScheduleHistory> coursesScheduleHistoryList = JsonUtil.parseArray(teacherLeaveRecord.getCoursesScheduleJson(),
					CoursesScheduleHistory.class);

			if (coursesScheduleHistoryList != null && coursesScheduleHistoryList.size() > 0) {

				List<CourseSchedule> list = new ArrayList<CourseSchedule>();

				for (CoursesScheduleHistory his : coursesScheduleHistoryList) {
					if (his.getBefore().getType() != CourseScheduleType.VIP) {
						continue;
					}
					if(!teacherLeaveRecord.getEndTime().before(his.getAfter().getStartClassTime())
							&&!teacherLeaveRecord.getStartTime().after(his.getAfter().getEndClassTime())){
						throw new BizException("课程时间与老师请假时间存在冲突");
					}

					list.add(his.getAfter());
				}

				if(list.size() > 0){
					// checkCourseSchedulePlansExitLeaveRecord(list);
				}
			}
		}
		teacherLeaveRecordDao.insert(teacherLeaveRecord);
		Teacher teacher = teacherDao.get(sysUser.getId());
		Map<String,Object> memo = new HashMap<>(1);
		memo.put("leaveRecordId",teacherLeaveRecord.getId());
		sendSeoMessageSource.sendSeoMessage(teacher.getTeacherOrganId(),new SysUserRoleEnum[]{ORGAN_MANAGER,EDUCATION},
				JSONObject.toJSONString(memo),null,MessageTypeEnum.EDUCATION_PUSH_TEACHER_LEAVE_APPLY, sysUser.getRealName());
		return true;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public HttpResponseResult approve(Long id, Integer userId, AuditStatusEnum status, String remark, Boolean allowZeroSalary) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			throw new BizException("获取用户信息失败");
		}
		TeacherLeaveRecord teacherLeaveRecord = teacherLeaveRecordDao.get(id);
		if (teacherLeaveRecord == null) {
			throw new BizException("请假记录不存在");
		}

		if (teacherLeaveRecord.getStatus() != AuditStatusEnum.ING || status == AuditStatusEnum.ING) {
			throw new BizException("审批状态不正确");
		}

		teacherLeaveRecord.setStatus(status);
		teacherLeaveRecord.setRemark(remark);

		teacherLeaveRecordDao.update(teacherLeaveRecord);

		if (StringUtils.isNotBlank(teacherLeaveRecord.getCoursesScheduleJson())&&status==AuditStatusEnum.PASS) {
			List<CoursesScheduleHistory> coursesScheduleHistoryList = JsonUtil.parseArray(teacherLeaveRecord.getCoursesScheduleJson(),
					CoursesScheduleHistory.class);

			if (coursesScheduleHistoryList != null && coursesScheduleHistoryList.size() > 0) {

				List<CourseSchedule> list = new ArrayList<CourseSchedule>();

				for (CoursesScheduleHistory his : coursesScheduleHistoryList) {
					if (his.getBefore().getType() != CourseScheduleType.VIP) {
						continue;
					}
					list.add(his.getAfter());
				}
				// 课时调整
				if (list.size() > 0) {
					HttpResponseResult httpResponseResult = courseScheduleService.courseAdjust(list, allowZeroSalary);
					if(httpResponseResult.getCode()== HttpStatus.PARTIAL_CONTENT.value()){
						return httpResponseResult;
					}
				}
			}
		}
		//教师请假结果通知
		Map<Integer,String> map = new HashMap<>(1);
		Teacher teacher = teacherDao.get(teacherLeaveRecord.getUserId());
		map.put(teacher.getId(),teacher.getId() + "");
		sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.TEACHER_PUSH_LEAVE_RESULT,
				map,null,0,"7","TEACHER",status.getMsg());
		return BaseController.succeed();
	}

	@Override
	public void checkCourseSchedulePlansExitLeaveRecord(List<CourseSchedule> courseSchedules){
		Date now=new Date();
		List<Integer> teacherIds = courseSchedules.stream().map(CourseSchedule::getActualTeacherId).collect(Collectors.toList());
		List<TeacherLeaveRecord> teacherLeaveRecords = teacherLeaveRecordDao.findByTeacherIds(teacherIds, now);
		Map<Integer, List<TeacherLeaveRecord>> teacherLeaveRecordByTeacher = teacherLeaveRecords.stream().collect(Collectors.groupingBy(TeacherLeaveRecord::getUserId));
		courseSchedules.forEach(courseSchedule -> {
			List<TeacherLeaveRecord> teacherLeaveRecordsWithTeacher = teacherLeaveRecordByTeacher.get(courseSchedule.getActualTeacherId());
			if(CollectionUtils.isEmpty(teacherLeaveRecordsWithTeacher)){
				return;
			}
			teacherLeaveRecordsWithTeacher.forEach(teacherLeaveRecord -> {
				if(!teacherLeaveRecord.getEndTime().before(courseSchedule.getStartClassTime())
					&&!teacherLeaveRecord.getStartTime().after(courseSchedule.getEndClassTime())){
					throw new BizException("课程时间与老师请假时间存在冲突");
				}
			});
		});
	}
}
