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.*;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.dal.enums.SignStatusEnum;
import com.ym.mec.biz.dal.enums.TeachModeEnum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.TeacherCloseQueryInfo;
import com.ym.mec.biz.service.*;
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.redis.service.RedisCache;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class TeacherAttendanceServiceImpl extends BaseServiceImpl<Long, TeacherAttendance>  implements TeacherAttendanceService {
	
	@Autowired
	private TeacherAttendanceDao teacherAttendanceDao;
	@Autowired
	private CourseScheduleDao courseScheduleDao;
	@Autowired
	private CourseHomeworkService courseHomeworkService;
	@Autowired
	private StudentAttendanceDao studentAttendanceDao;
	@Autowired
	private StudentCourseHomeworkDao studentCourseHomeworkDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private CourseScheduleService courseScheduleService;
	@Autowired
	private SysConfigDao sysConfigDao;
	@Autowired
	private SysConfigService sysConfigService;
	@Autowired
	private SysMessageService sysMessageService;
	@Autowired
	private ClassGroupDao classGroupDao;
	@Autowired
	private SubjectDao subjectDao;
	@Autowired
	private SchoolDao schoolDao;

	@Autowired
	private RedisCache<String,Object> redisCache;

	@Autowired
	private CourseHomeworkDao courseHomeworkDao;

	@Override
	public BaseDAO<Long, TeacherAttendance> getDAO() {
		return teacherAttendanceDao;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Map<String, Object> addTeacherAttendanceRecord(TeacherSignOutDto teacherSignOutDto) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if(Objects.isNull(user)){
			throw new BizException("请登录");
		}

		if(Objects.isNull(teacherSignOutDto.getTeacherAttendanceInfo().getCourseScheduleId())){
			throw new BizException("请选择课程!");
		}

		Map<String,Object> result=new HashMap<>();

		TeacherAttendance teacherAttendance=teacherAttendanceDao.findByTeacherAttendanceInfo(user.getId().longValue(),teacherSignOutDto.getTeacherAttendanceInfo().getCourseScheduleId());

		Date date = new Date();
		if(Objects.isNull(teacherAttendance)){
			teacherAttendance=teacherSignOutDto.getTeacherAttendanceInfo();
			teacherAttendance.setTeacherId(user.getId());
			teacherAttendance.setCreateTime(date);
		}else if(teacherAttendance.getSignInTime() != null && teacherAttendance.getSignOutTime() != null){
			throw new BizException("当前课程已签到");
		}

		TeacherAttendanceDto currentCourseDetail = courseScheduleDao.getCurrentCourseDetail(teacherSignOutDto.getTeacherAttendanceInfo().getCourseScheduleId(),user.getId());
		if(Objects.isNull(currentCourseDetail)){
			throw new BizException("课程不存在");
		}

		teacherAttendance.setMusicGroupId(currentCourseDetail.getMusicGroupId());
		teacherAttendance.setClassGroupId(currentCourseDetail.getClassId());
		teacherAttendance.setRemark(teacherSignOutDto.getTeacherAttendanceInfo().getRemark());
		int advanceSignMinutes = Integer.parseInt(sysConfigDao.findConfigValue(SysConfigService.ADVANCE_SIGN_IN_MINUTES));

		CourseSchedule courseSchedule = courseScheduleDao.get(teacherAttendance.getCourseScheduleId());
		teacherAttendance.setGroupType(courseSchedule.getGroupType());
		if(courseSchedule == null){
			throw new BizException("课程信息不存在");
		}

		Date tomorrow = DateUtil.addDays1(date, 1);

		if(tomorrow.before(courseSchedule.getStartClassTime())){
			throw new BizException("明天的课程不能进行签到");
		}

		String classDate = DateUtil.format(courseSchedule.getClassDate(), DateUtil.DEFAULT_PATTERN);
		String startClassTime = DateUtil.format(courseSchedule.getStartClassTime(), DateUtil.EXPANDED_TIME_FORMAT);
		String endClassTime = DateUtil.format(courseSchedule.getEndClassTime(), DateUtil.EXPANDED_TIME_FORMAT);
		//上课时间
		Date classStartDateTime = DateUtil.stringToDate(classDate + " " + startClassTime, DateUtil.EXPANDED_DATE_TIME_FORMAT);
		Date classEndDateTime = DateUtil.stringToDate(classDate + " " + endClassTime, DateUtil.EXPANDED_DATE_TIME_FORMAT);
		Date classEndDateAdd60Minutes = DateUtil.addMinutes(classEndDateTime, 60);
		Date add20Minutes = DateUtil.addMinutes(classStartDateTime, advanceSignMinutes * -1);
		if(teacherAttendance.getSignInTime() == null && teacherSignOutDto.getTeacherAttendanceInfo().getStatus().equals(SignStatusEnum.SIGN_IN.getCode())){

			if(StringUtils.isBlank(teacherSignOutDto.getTeacherAttendanceInfo().getSignInLongitudeLatitude())){
				throw new BizException("未获取到您的位置");
			}

			School school = schoolDao.get(courseSchedule.getSchoolId());

			//是否在范围内
			boolean isInScore = true;
			if(StringUtils.isBlank(school.getLongitudeLatitude())){
				if(teacherSignOutDto.getTeacherAttendanceInfo().getUpdate().equals(YesOrNoEnum.YES.getCode())){
					school.setLongitudeLatitude(teacherSignOutDto.getTeacherAttendanceInfo().getSignInLongitudeLatitude());
					schoolDao.update(school);
				}else{
					isInScore = false;
				}
			}else{
				SysConfig sysConfig = sysConfigService.findByParamName(SysConfigService.ATTENDANCE_RANGE);
				double attendanceRange = Double.valueOf(sysConfig.getParanValue());
				double distance = MapUtil.distance(teacherSignOutDto.getTeacherAttendanceInfo().getSignInLongitudeLatitude(),
						school.getLongitudeLatitude());
				if(distance>attendanceRange){
					isInScore=false;
				}
			}

			teacherAttendance.setCurrentClassTimes(courseScheduleDao.countCurrentTimes(teacherAttendance.getClassGroupId(),courseSchedule.getStartClassTime()) + 1);
			teacherAttendance.setSignInLongitudeLatitude(teacherSignOutDto.getTeacherAttendanceInfo().getSignInLongitudeLatitude());
			if (currentCourseDetail.getEndClassTime().before(date)){
				throw new BizException("该课程已结束，不能进行签到!");
			}

			YesOrNoEnum yesOrNoEnum = YesOrNoEnum.NO;
			if(Objects.nonNull(courseSchedule.getSchoolId())){
				yesOrNoEnum = courseScheduleService.enableOnlyNormalAttendance(currentCourseDetail.getStartClassTime(),
						user.getId().longValue(),
						true,
						currentCourseDetail.getSchoolId().intValue());
			}
			teacherAttendance.setSignInTime(date);
			teacherAttendance.setSignInStatus(YesOrNoEnum.YES);
			result.put("signInStatus",0);
			if(yesOrNoEnum == YesOrNoEnum.YES){
				//连堂课
			}else if(DateUtil.minutesBetween(date,add20Minutes) >= 0){
				//正常签到范围(开始前60分钟  ~  开始之前20分钟)
				if(!isInScore){
					teacherAttendance.setSignInStatus(YesOrNoEnum.NO);
				}
			}else if(DateUtil.minutesBetween(add20Minutes,date) > 0){
				if(StringUtils.isEmpty(teacherAttendance.getRemark())&&!teacherSignOutDto.isNotRturnErrorInfo()){
					//当前为异常签到，请填写原因!
					result.put("status",2);
					return result;
				}
				//异常签到范围(开始之后  ~  结束之前)
				teacherAttendance.setSignInStatus(YesOrNoEnum.NO);
				result.put("signInStatus",1);
			}else {
				throw new BizException("签到时间异常");
			}
			if(Objects.nonNull(teacherAttendance.getId())){
				teacherAttendanceDao.update(teacherAttendance);
			}else{
				teacherAttendanceDao.insert(teacherAttendance);
			}
			return result;
		}else if(teacherAttendance.getSignOutTime() == null && teacherSignOutDto.getTeacherAttendanceInfo().getStatus().equals(SignStatusEnum.SIGN_OUT.getCode())){
			//正常签退范围（结束后1小时内）
			teacherAttendance.setSignOutTime(date);
			ClassGroup classGroup = classGroupDao.get(courseSchedule.getClassGroupId());
			teacherAttendance.setCurrentClassTimes(classGroup.getCurrentClassTimes() + 1);
			if(DateUtil.minutesBetween(classEndDateTime,date) >= 0 && DateUtil.minutesBetween(date,classEndDateAdd60Minutes) >= 0){
				teacherAttendance.setSignOutStatus(YesOrNoEnum.YES);
				result.put("signInStatus",0);
			}else if(DateUtil.minutesBetween(classEndDateAdd60Minutes,date) > 0){
				teacherAttendance.setSignOutStatus(YesOrNoEnum.NO);
				result.put("signInStatus",1);
			}else {
				throw new BizException("签退时间异常");
			}
			if(Objects.nonNull(teacherAttendance.getId())){
				teacherAttendanceDao.update(teacherAttendance);
			}else{
				teacherAttendanceDao.insert(teacherAttendance);
			}
			//新增课堂作业
			CourseHomework courseHomework=teacherSignOutDto.getCourseHomeworkInfo();
			courseHomework.setCourseScheduleId(teacherAttendance.getCourseScheduleId());
			courseHomework.setMusicGroupId(currentCourseDetail.getMusicGroupId());
			courseHomework.setGroupType(teacherAttendance.getGroupType());
			courseHomework.setClassGroupId(currentCourseDetail.getClassId().intValue());
			courseHomework.setExpiryDate(DateUtil.addDays(date,7));
			courseHomework.setExpectNum(courseScheduleDao.countCourseStudentNum(teacherAttendance.getCourseScheduleId()));
			courseHomeworkService.insert(courseHomework);
			List<StudentCourseHomework> studentCourseHomeworks = studentCourseHomeworkDao
					.constructInitialStudentHomeworkRecordsWithPayment(teacherAttendance.getCourseScheduleId(),
							courseHomework.getId());
			if(CollectionUtils.isEmpty(studentCourseHomeworks)){
				throw new BizException("此课程没有学生");
			}
			studentCourseHomeworkDao.batchInsertStudentCourseHomeworkRecord(studentCourseHomeworks);
			return result;
		}

		result.put("status",0);
		result.put("currentTime",date);

		return result;
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addTeacherAttendanceRecord(Integer courseScheduleId, Integer userId, SignStatusEnum signStatus, boolean updateCourseScheduleStatus) {
		TeacherAttendance teacherAttendance=teacherAttendanceDao.findByTeacherAttendanceInfo(userId.longValue(),courseScheduleId.longValue());
		ClassGroup classGroup = classGroupDao.findByCourseSchedule(courseScheduleId);
		CourseSchedule courseSchedule = courseScheduleDao.get(courseScheduleId.longValue());

		if(Objects.isNull(teacherAttendance)){
			teacherAttendance=new TeacherAttendance();
			teacherAttendance.setGroupType(courseSchedule.getGroupType());
			teacherAttendance.setCourseScheduleId(courseScheduleId.longValue());
			teacherAttendance.setTeacherId(userId);
			teacherAttendance.setMusicGroupId(classGroup.getMusicGroupId());
			teacherAttendance.setClassGroupId(classGroup.getId());
			teacherAttendance.setClassGroupId(courseScheduleId);
			teacherAttendance.setTeacherId(userId);
			teacherAttendanceDao.insert(teacherAttendance);
		}else {
			teacherAttendance.setGroupType(courseSchedule.getGroupType());
			teacherAttendance.setTeacherId(userId);
			teacherAttendance.setClassGroupId(classGroup.getId());
			teacherAttendance.setMusicGroupId(classGroup.getMusicGroupId());
		}
//		int advanceSignMinutes = Integer.parseInt(sysConfigDao.findConfigValue(SysConfigService.ADVANCE_SIGN_IN_MINUTES));
		Date date = new Date();
		String classDate = DateUtil.format(courseSchedule.getClassDate(), DateUtil.DEFAULT_PATTERN);
		String startClassTime = DateUtil.format(courseSchedule.getStartClassTime(), DateUtil.EXPANDED_TIME_FORMAT);
		String endClassTime = DateUtil.format(courseSchedule.getEndClassTime(), DateUtil.EXPANDED_TIME_FORMAT);
		//上课时间
		Date classStartDateTime = DateUtil.stringToDate(classDate + " " + startClassTime, DateUtil.EXPANDED_DATE_TIME_FORMAT);
		Date classEndDateTime = DateUtil.stringToDate(classDate + " " + endClassTime, DateUtil.EXPANDED_DATE_TIME_FORMAT);
//		Date classEndDateAdd60Minutes = DateUtil.addMinutes(classEndDateTime, 60);
		Date classEndDateCut10Minutes = DateUtil.addMinutes(classEndDateTime, -10);
//		Date add20Minutes = DateUtil.addMinutes(classStartDateTime, advanceSignMinutes * -1);
//		Date add60Minutes = DateUtil.addMinutes(classStartDateTime, -60);
		//签到
		String continueCourseTime = sysConfigDao.findConfigValue(SysConfigService.ONLINE_CONTINUE_COURSE_TIME);
		if(StringUtils.isEmpty(continueCourseTime)){
			continueCourseTime = "10";
		}
		if(teacherAttendance.getSignInTime() == null && SignStatusEnum.SIGN_IN.equals(signStatus)){
			YesOrNoEnum yesOrNoEnum = courseScheduleService.enableOnlyNormalAttendance(courseSchedule.getStartClassTime(),
					userId.longValue(),
					false,
					null);
			teacherAttendance.setSignInTime(date);
			teacherAttendance.setSignInStatus(YesOrNoEnum.YES);
			teacherAttendance.setCurrentClassTimes(classGroup.getCurrentClassTimes() + 1);
			boolean isSign = false;
			if(yesOrNoEnum == YesOrNoEnum.YES){
				isSign = true;
			}else if(DateUtil.minutesBetween(date,classStartDateTime) >= 0){
				//正常签到范围(开始之前)
				isSign = true;
			}else if(DateUtil.minutesBetween(classStartDateTime,date) > 0){
				//异常签到范围(开始之后)
				isSign = true;
				teacherAttendance.setSignInStatus(YesOrNoEnum.NO);
			}
			if(isSign){
				teacherAttendanceDao.update(teacherAttendance);
				if(courseSchedule.getTeachMode() == TeachModeEnum.ONLINE){
					//获取所有当前课程的学生列表
					Map<Integer,String> userMap = JSONObject.parseObject(JSONObject.toJSONString(MapUtil.convertMybatisMap(courseScheduleDao.findStudentMap(courseScheduleId))),HashMap.class);
					if(userMap != null && userMap.size() > 0){
						String name = subjectDao.findNames(classGroup.getSubjectIdList());
						sysMessageService.batchSendMessage(MessageSender.JIGUANG,MessageTypeEnum.STUDENT_PUSH_ONLINE_COURSE_ACTION,userMap,
								null,0,"6?sealClassId=" + courseScheduleId + "&subjectName=" + name ,"STUDENT",courseSchedule.getName());
					}
				}
			}
		}
		//签退
		if(SignStatusEnum.SIGN_OUT.equals(signStatus)){
			if(DateUtil.minutesBetween(classEndDateCut10Minutes,date) >= 0){
				teacherAttendance.setSignOutStatus(YesOrNoEnum.YES);
				List<CourseSchedule> courseSchedules = new ArrayList<>();
				CourseSchedule cs = courseSchedule;
				while (true){
					//获取当前课程的所有连堂课列表
					String courseClassDate = DateUtil.format(cs.getClassDate(), DateUtil.DEFAULT_PATTERN);
					String courseEndDateTime = DateUtil.format(cs.getEndClassTime(), DateUtil.EXPANDED_TIME_FORMAT);
//					Date endDateTime = DateUtil.stringToDate(courseClassDate + " " + courseEndDateTime, DateUtil.EXPANDED_DATE_TIME_FORMAT);
					cs = courseScheduleDao.queryContinueCourse(cs,continueCourseTime,courseClassDate + " " + courseEndDateTime);
					//存在连堂课
					if(cs != null){
						courseSchedules.add(cs);
					}else {
						break;
					}
				}
				if(courseSchedules.size() > 0){
					//获取总上课时长
					int totalMinutes = DateUtil.minutesBetween(classStartDateTime, date);
					//减去第一节课时长
					int firstMinutes = courseScheduleDao.getSingleClassMinutes(courseScheduleId.longValue());
					totalMinutes -= firstMinutes;
					if(totalMinutes > 0){
						String courseClassDate;
						String courseStartDateTime;
						String courseEndDateTime;
						for (CourseSchedule e:courseSchedules) {
							//获取当前课程的单节课时长
							int signClassMinutes = courseScheduleDao.getSingleClassMinutes(e.getId());
							totalMinutes -= signClassMinutes;
							if(totalMinutes >= 0){
								//补充签到签退时间
								courseClassDate = DateUtil.format(e.getClassDate(), DateUtil.DEFAULT_PATTERN);
								courseStartDateTime = DateUtil.format(e.getStartClassTime(), DateUtil.EXPANDED_TIME_FORMAT);
								courseEndDateTime = DateUtil.format(e.getEndClassTime(), DateUtil.EXPANDED_TIME_FORMAT);
								Date startDateTime = DateUtil.stringToDate(courseClassDate + " " + courseStartDateTime, DateUtil.EXPANDED_DATE_TIME_FORMAT);
								Date endDateTime = DateUtil.stringToDate(courseClassDate + " " + courseEndDateTime, DateUtil.EXPANDED_DATE_TIME_FORMAT);
								teacherAttendanceDao.cuntinueCourseSign(e.getId(),startDateTime,endDateTime);
							}else {
								break;
							}
						}
					}
				}
				//异常签退时间段，并且没有签退状态
			}else if(DateUtil.minutesBetween(classStartDateTime,date) > 0 && DateUtil.minutesBetween(date,classEndDateCut10Minutes) > 0){
				teacherAttendance.setSignOutStatus(YesOrNoEnum.NO);
			}else {
				return;
			}
			teacherAttendance.setSignOutTime(date);
			teacherAttendanceDao.update(teacherAttendance);
		}
	}

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

		List<TeacherPersonalAttendanceDto> dataList = null;
		int count = teacherAttendanceDao.getTeacherPersonalAttendancesCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherAttendanceDao.getTeacherPersonalAttendances(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public boolean pushNoSignOutMessage() {
		
		Integer minutes = Integer.parseInt(sysConfigDao.findConfigValue(SysConfigService.SIGN_OUT_MESSAGE_PUSH_MINUTE));;

		List<Mapper> list = teacherAttendanceDao.queryNoSignOutListByOverMinutes(minutes);

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

			if (!redisCache.exists(key)) {
				sysMessageService.batchSendMessage(MessageSender.JIGUANG, MessageTypeEnum.TEACHER_PUSH_SIGN_OUT_MESSAGE, receivers, null, 0,
						"5?courseScheduleId=" + mapper.getCourseScheduleId(), "TEACHER", mapper.getValue());
				redisCache.put(key, 1, (int) DateUtil.getTomorrowZeroSeconds());
			}
		}
		return true;
	}

	@Override
	public List<TeacherAbnormalAttendanceInfoDto> findTeacherAbnormalSignInCourseScheduleRecord(Integer organId, Date date, Integer signInStatus) {
		return teacherAttendanceDao.findTeacherAbnormalSignInCourseScheduleRecord(organId,date,signInStatus);
	}

	@Override
	public List<Date> findTeacherAbnormalSignInDates(Integer organId, Date date) {
		return teacherAttendanceDao.findTeacherAbnormalSignInDates(organId,date);
	}

	@Override
	public Map countTeacherAbnormalSignInClassTimes(Integer organId, Date date) {
		Map<String,Integer> result=new HashMap<>();
		result.put("abnormalSignInNum",teacherAttendanceDao.countTeacherAbnormalSignInClassTimes(organId,date,YesOrNoEnum.NO.getCode()));
		result.put("noSignInNum",teacherAttendanceDao.countTeacherAbnormalSignInClassTimes(organId,date,null));
		return result;
	}

	@Override
	public TeacherSignOutDto getTeacherSignDetail(Integer courseScheduleId, Integer userId) {
		if(Objects.isNull(courseScheduleId)){
			throw new BizException("请指定课程");
		}
		TeacherSignOutDto teacherSignDetail = new TeacherSignOutDto();
		TeacherAttendance teacherAttendanceInfo = teacherAttendanceDao.findByTeacherAttendanceInfo(userId.longValue(), courseScheduleId.longValue());
		teacherSignDetail.setTeacherAttendanceInfo(teacherAttendanceInfo);
		CourseHomework courseHomewok = courseHomeworkDao.findByCourseSchedule(courseScheduleId.longValue());
		teacherSignDetail.setCourseHomeworkInfo(courseHomewok);
		return teacherSignDetail;
	}

	@Override
	public TeacherAttendance getTeacherPersonalAttendance(Integer courseScheduleId) {
		return teacherAttendanceDao.getTeacherPersonalAttendance(courseScheduleId);
	}

	@Override
	public void updateTeacherAttendance(TeacherAttendance teacherAttendanceInfo) {
		if(Objects.isNull(teacherAttendanceInfo.getCourseScheduleId())){
			throw new BizException("请指定课程");
		}
		if(Objects.isNull(teacherAttendanceInfo.getSignInStatus())){
			throw new BizException("请指定签到状态");
		}
		if(Objects.isNull(teacherAttendanceInfo.getSignOutStatus())){
			throw new BizException("请指定签退状态");
		}
		CourseSchedule courseSchedule = courseScheduleDao.get(teacherAttendanceInfo.getCourseScheduleId());
		if(Objects.isNull(courseSchedule)){
			throw new BizException("课程不存在");
		}
		if(Objects.isNull(teacherAttendanceInfo.getTeacherId())||teacherAttendanceInfo.getTeacherId()<=0){
			if(Objects.isNull(courseSchedule.getActualTeacherId())){
				throw new BizException("请指定教师");
			}
			teacherAttendanceInfo.setTeacherId(courseSchedule.getActualTeacherId());
		}
		TeacherAttendance teacherAttendance=teacherAttendanceDao.findByTeacherAttendanceInfo(teacherAttendanceInfo.getTeacherId().longValue(),teacherAttendanceInfo.getCourseScheduleId());

		Date date = new Date();
		if(Objects.isNull(teacherAttendance)){
			teacherAttendance=new TeacherAttendance();
			teacherAttendance.setGroupType(courseSchedule.getGroupType());
			teacherAttendance.setMusicGroupId(courseSchedule.getMusicGroupId());
			teacherAttendance.setClassGroupId(courseSchedule.getClassGroupId());
			teacherAttendance.setCourseScheduleId(courseSchedule.getId());
			teacherAttendance.setTeacherId(teacherAttendanceInfo.getTeacherId());
			teacherAttendance.setCreateTime(date);
		}
		teacherAttendance.setSignInTime(date);
		teacherAttendance.setSignInStatus(teacherAttendanceInfo.getSignInStatus());
		teacherAttendance.setSignOutTime(date);
		teacherAttendance.setSignOutStatus(teacherAttendanceInfo.getSignOutStatus());

		SysUser sysUser = sysUserFeignService.queryUserInfo();
		StringBuilder remark=new StringBuilder("补签到,");
		remark.append(sysUser.getRealName());
		teacherAttendance.setRemark(remark.toString());

		if(Objects.isNull(teacherAttendance.getId())){
			teacherAttendanceDao.insert(teacherAttendance);
		}else{
			teacherAttendanceDao.update(teacherAttendance);
		}
	}
}
