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.auth.api.enums.YesOrNoEnum;
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.GroupType;
import com.ym.mec.biz.dal.page.*;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.TeacherService;
import com.ym.mec.common.constant.CommonConstants;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.entity.ImGroupModel;
import com.ym.mec.common.entity.ImResult;
import com.ym.mec.common.entity.ImUserModel;
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.im.ImFeignService;
import com.ym.mec.util.collection.MapUtil;
import com.ym.mec.util.date.DateUtil;
import com.ym.mec.util.idcard.IdcardValidator;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class TeacherServiceImpl extends BaseServiceImpl<Integer, Teacher>  implements TeacherService {

	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private SubjectDao subjectDao;
	@Autowired
	private ClassGroupDao classGroupDao;
	@Autowired
	private ClassGroupService classGroupService;
	@Autowired
	private MusicGroupDao musicGroupDao;
	@Autowired
	private StudentRegistrationDao studentRegistrationDao;
	@Autowired
	private DemoGroupDao demoGroupDao;
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private SchoolDao schoolDao;
	@Autowired
	private SysUserCashAccountDao sysUserCashAccountDao;
	@Autowired
	private StudentPaymentOrderDao studentPaymentOrderDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private ImFeignService imFeignService;
	@Autowired
	private StudentExtracurricularExercisesSituationDao studentExtracurricularExercisesSituationDao;
	@Autowired
	private StudentDao studentDao;
	@Autowired
	private ImUserFriendDao imUserFriendDao;
	@Autowired
	private RedisTemplate<String,String> redisTemplate;


	@Override
	public BaseDAO<Integer, Teacher> getDAO() {
		return teacherDao;
	}

	@Override
	public Teacher findByCertificateNum(String certificateNum){
		return teacherDao.findByCertificateNum(certificateNum);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int add(Teacher teacher) throws Exception {
		SysUser user = sysUserFeignService.queryUserByMobile(teacher.getPhone());
		if(user != null && user.getId() != null){
			if(user.getUserType().contains("TEACHER")){
				throw new BizException("此手机号已被占用");
			}
			Integer id = user.getId();
			Teacher teacher1 = teacherDao.get(id);
			if(teacher1 == null || teacher1.getId() == null){
				teacher.setId(id);
				teacher.setDelFlag(YesOrNoEnum.NO);
				teacherDao.insert(teacher);
				teacher.setOrganId(null);
				teacher.setUserType(user.getUserType() + ",TEACHER");
				teacherDao.updateUser(teacher);
			}else {
				throw new BizException("用户已存在");
			}
			return id;
		}
		teacher.setUserType("TEACHER");
		teacher.setUsername(teacher.getRealName());
		teacherDao.addSysUser(teacher);
		//添加用户现金账户
		sysUserCashAccountDao.insert(new SysUserCashAccount(teacher.getId(),"CNY"));
		//添加用户电子签章账户
//		contractService.register(teacher.getId(), user.getRealName(), user.getIdCardNo(), user.getPhone());
		teacherDao.insert(teacher);
		ImResult imResult = imFeignService.register(new ImUserModel(teacher.getId().toString(), teacher.getUsername(), null));
		if(imResult != null){
			teacher.setImToken(imResult.getToken());
			teacherDao.updateUser(teacher);
		}
		return teacher.getId();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateTea(Teacher teacher) {
		Teacher teacher1 = teacherDao.get(teacher.getId());
		if(null==teacher1){
			throw new BizException("教师信息不存在");
		}
		if(StringUtils.isNotEmpty(teacher.getPhone())){
			SysUser sysUser = sysUserFeignService.queryUserByMobile(teacher.getPhone());
			if(sysUser != null && !sysUser.getId().equals(teacher.getId())){
				throw new BizException("手机号已存在");
			}
		}
		teacher.setUpdateTime(new Date());
		teacher.setTeacherOrganId(teacher.getOrganId());
		teacherDao.update(teacher);
		teacher.setOrganId(null);
		teacherDao.updateUser(teacher);
		imFeignService.update(new ImUserModel(teacher.getId().toString(),teacher.getRealName(),teacher.getAvatar()));
	}

	@Override
	public void deleteTeacher(Long userId) {
		SysUser sysUser=new SysUser();
		sysUser.setId(userId.intValue());
		sysUser.setDelFlag(YesOrNoEnum.YES);
		teacherDao.updateUser(sysUser);
	}

	@Override
	public List<ImGroupModel> queryTeacherGroups(Integer userId,String search) {
		//获取教师所在班级列表
		List<ImGroupModel> imGroupModels =  classGroupDao.queryTeacherGroups(userId,search);
		if(imGroupModels != null && imGroupModels.size() > 0){
			imGroupModels.forEach(e -> {
				e.setCount(classGroupService.findGroupUsers(Integer.parseInt(e.getId()),null).size());
			});
		}
		return imGroupModels;
	}

	@Override
	public List<BasicUserDto> queryGroupStudentList(Integer teacherId, String search) {
		//获取关联的学员列表
		Set<Integer> studentIds = teacherDao.queryCourseClassStudentIds(teacherId);
		List<BasicUserDto> teachers = new ArrayList<>();
		if(studentIds.size() > 0){
			teachers.addAll(teacherDao.queryTeacherBaseInfo(studentIds,search));
		}
		return teachers.stream().distinct().collect(Collectors.toList());
	}

	@Override
	public List<BasicUserDto> queryGroupStudents(Integer teacherId, String search) {
		//获取关联的学员列表
		Set<Integer> studentIds = teacherDao.queryCourseClassStudentIds(teacherId);
		//获取关联的老师列表
		studentIds.addAll(teacherDao.queryCourseClassTeacherIds(teacherId));
		//获取关联的乐团的管理老师
		List<String> musicLeaderIds = teacherDao.queryMusicLeaderIds(teacherId);
		musicLeaderIds.removeAll(Collections.singleton(null));
		if(musicLeaderIds.size() > 0){
			Set<String> leaderIds = new HashSet<>();
			musicLeaderIds.forEach(e->{
				leaderIds.addAll(Arrays.asList(e.split(",")));
			});
			leaderIds.forEach(e->{
				studentIds.add(Integer.valueOf(e));
			});
		}
		studentIds.removeAll(Collections.singleton(null));
		List<ImGroupModel> musicGroups =  musicGroupDao.queryTeacherGroups(teacherId,search);
		List<BasicUserDto> teachers = new ArrayList<>();
		if(musicGroups != null && musicGroups.size() > 0){
			Set<String> musicGroupId = musicGroups.stream().map(ImGroupModel::getId).collect(Collectors.toSet());
			List<MusicGroup> musicGroupList = musicGroupDao.queryListByIds(StringUtils.join(musicGroupId, ","));
			Set<Integer> teacherIdSet = new HashSet<>();
			if(musicGroupList != null && musicGroupList.size() > 0){
				musicGroupList.forEach(e->{
					teacherIdSet.add(e.getTeamTeacherId());
					teacherIdSet.add(e.getEducationalTeacherId());
					teacherIdSet.add(e.getDirectorUserId());
				});
			}
			teacherIdSet.remove(teacherId);
			teacherIdSet.removeAll(Collections.singleton(null));
			if(teacherIdSet.size() > 0){
				teachers = teacherDao.queryMusicGroupStudents(StringUtils.join(teacherIdSet,","), search);
			}
		}
		List<BasicUserDto> userDtos = teacherDao.queryGroupStudents(teacherId, search);
		if(userDtos != null && userDtos.size() > 0){
			teachers.addAll(userDtos);
			Set<String> musicGroupIds = userDtos.stream().map(e -> e.getMusicGroupId()).collect(Collectors.toSet());
			if(musicGroupIds != null && musicGroupIds.size() > 0){
				Map<String, String> musicGroupNameMap = MapUtil.convertIntegerMap(musicGroupDao.queryMusicGroupNameMap(musicGroupIds));
				userDtos.forEach(e->{
					e.setMusicGroupName(musicGroupNameMap.get(e.getMusicGroupId()));
				});
			}
		}
		studentIds.removeAll(userDtos.stream().map(e->e.getUserId()).collect(Collectors.toSet()));
		studentIds.remove(teacherId);
		//获取课程关联的学生列表
//		teachers.addAll(teacherDao.findCloseCourseStudentIds(teacherId, search));
		//获取相关vip陪练教务的学员编号
//		teachers.addAll(teacherDao.findVipEducationalStudentId(teacherId,search));
//		teachers.addAll(teacherDao.findCommEducationalStudentId(teacherId,search));
//		teachers.addAll(teacherDao.findPracticeEducationalStudentId(teacherId,search));
		if(studentIds.size() > 0){
			teachers.addAll(teacherDao.queryTeacherBaseInfo(studentIds,search));
		}
		return teachers.stream().distinct().collect(Collectors.toList());
	}

	@Transactional(rollbackFor = Exception.class)
	public List<ImUserFriendDto> queryGroupStudents1(Integer teacherId, String search) {
		Set<BasicUserDto> basicUserDtos = new HashSet<>();
		//学员关联的通讯录
		//获取相关课程，班级老师列表
		Set<Integer> teacherIds = teacherDao.findMusicTeacherIds(teacherId);
		//获取学员所在乐团列表
		List<String> musicGroupIds = studentRegistrationDao.queryStudentMusicGroup(teacherId);
		if (musicGroupIds != null && musicGroupIds.size() > 0) {
			List<MusicGroup> musicGroups = musicGroupDao.queryListByIds(StringUtils.join(musicGroupIds, ","));
			musicGroups.forEach(e -> {
				teacherIds.add(e.getDirectorUserId());
				teacherIds.add(e.getEducationalTeacherId());
				teacherIds.add(e.getTeamTeacherId());
			});
		}
		//获取相关vip课老师列表
		teacherIds.addAll(teacherDao.findVipTeacherIds(teacherId));
		//获取陪练课老师列表
		teacherIds.addAll(teacherDao.findPracticeTeacherIds(teacherId));
		//获取对外课程老师列表
		teacherIds.addAll(teacherDao.findCloseCourseTeacherIds(teacherId));
		//获取陪练课课程教务老师
		teacherIds.addAll(teacherDao.findPracticeEducationalTeacherId(teacherId));
		//获取VIP课程教务老师
		teacherIds.addAll(teacherDao.findVipEducationalTeacherId(teacherId));
		//获取对外课程务老师
		teacherIds.addAll(teacherDao.findCommEducationalTeacherId(teacherId));
		//获取关联的指导老师
		Student student = studentDao.get(teacherId);
		if (student != null && student.getTeacherId() != null) {
			teacherIds.add(student.getTeacherId());
		}
		if (teacherIds.size() > 0) {
			basicUserDtos.addAll(teacherDao.queryTeacherBaseInfo(teacherIds, search));
		}

		//获取关联的学员列表
		Set<Integer> studentIds = teacherDao.queryCourseClassStudentIds(teacherId);
		//获取关联的老师列表
		studentIds.addAll(teacherDao.queryCourseClassTeacherIds(teacherId));
		//获取关联的乐团的管理老师
		List<String> musicLeaderIds = teacherDao.queryMusicLeaderIds(teacherId);
		musicLeaderIds.removeAll(Collections.singleton(null));
		if(musicLeaderIds.size() > 0){
			Set<String> leaderIds = new HashSet<>();
			musicLeaderIds.forEach(e->{
				leaderIds.addAll(Arrays.asList(e.split(",")));
			});
			leaderIds.forEach(e->{
				studentIds.add(Integer.valueOf(e));
			});
		}
		studentIds.removeAll(Collections.singleton(null));
		List<ImGroupModel> musicGroups =  musicGroupDao.queryTeacherGroups(teacherId,search);

		if(musicGroups != null && musicGroups.size() > 0){
			Set<String> musicGroupId = musicGroups.stream().map(ImGroupModel::getId).collect(Collectors.toSet());
			List<MusicGroup> musicGroupList = musicGroupDao.queryListByIds(StringUtils.join(musicGroupId, ","));
			Set<Integer> teacherIdSet = new HashSet<>();
			if(musicGroupList != null && musicGroupList.size() > 0){
				musicGroupList.forEach(e->{
					teacherIdSet.add(e.getTeamTeacherId());
					teacherIdSet.add(e.getEducationalTeacherId());
					teacherIdSet.add(e.getDirectorUserId());
				});
			}
			teacherIdSet.remove(teacherId);
			teacherIdSet.removeAll(Collections.singleton(null));
			if(teacherIdSet.size() > 0){
				basicUserDtos.addAll(teacherDao.queryMusicGroupStudents(StringUtils.join(teacherIdSet,","), search));
			}
		}

		List<BasicUserDto> userDtos = teacherDao.queryGroupStudents(teacherId, search);
		if(userDtos != null && userDtos.size() > 0){
			basicUserDtos.addAll(userDtos);
			Set<String> musicGroupIds1 = userDtos.stream().map(e -> e.getMusicGroupId()).collect(Collectors.toSet());
			if(musicGroupIds1 != null && musicGroupIds1.size() > 0){
				Map<String, String> musicGroupNameMap = MapUtil.convertIntegerMap(musicGroupDao.queryMusicGroupNameMap(musicGroupIds1));
				userDtos.forEach(e->{
					e.setMusicGroupName(musicGroupNameMap.get(e.getMusicGroupId()));
				});
			}
		}
		studentIds.removeAll(userDtos.stream().map(e->e.getUserId()).collect(Collectors.toSet()));
		studentIds.remove(teacherId);
		if(studentIds.size() > 0){
			basicUserDtos.addAll(teacherDao.queryTeacherBaseInfo(studentIds,search));
		}

		String syncImUserFriendKey = "syncImUserFriend:" + teacherId;
		if(basicUserDtos.size() > 0){
			for (BasicUserDto basicUserDto : basicUserDtos) {
				if(basicUserDto.getUserId().equals(teacherId)){
					continue;
				}
				// 检查是否已经是好友
				ImUserFriend userFriend = imUserFriendDao.query(teacherId, basicUserDto.getUserId());
				if (userFriend != null) {
					continue;
				}
				imUserFriendDao.insertByBasicUser(basicUserDto,teacherId);
			}
			redisTemplate.opsForValue().setIfAbsent(syncImUserFriendKey,teacherId.toString());
			return imUserFriendDao.queryFriendListByUserId(teacherId, search);
		}
		redisTemplate.opsForValue().setIfAbsent(syncImUserFriendKey,teacherId.toString());
		return new ArrayList<>();
	}

    @Override
    public List<TeacherBasicDto> findTeachers(String organId) {
		return teacherDao.findByFlowOrganRangeTeachers(organId);
    }

	@Override
	public List<Teacher> findOrganTeacherBySubject(Integer subjectId) {
		return teacherDao.findOrganTeacherBySubject(subjectId);
	}

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

		List<TeacherCloseDto> dataList = null;
		int count = teacherDao.countCloses(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherDao.queryCloses(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public BigDecimal findTeacherNoPay(Integer teacherId) {
		return teacherDao.findTeacherNoPay(teacherId);
	}

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

		List<MusicGroupTeachersDto> dataList = null;
		int count = teacherDao.countMusicGroupTeachers(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherDao.queryMusicGroupTeachers(params);
			//获取合奏班id列表
//			Set<Integer> classGroupIds = dataList.stream().map(e -> e.getClassGroupId()).collect(Collectors.toSet());
//			String classGroupIdStr = StringUtils.join(classGroupIds,",");
			//获取教师id列表
			Set<Integer> teacherIds = dataList.stream().map(e -> e.getTeacherId()).collect(Collectors.toSet());
//			String teacherIdStr = StringUtils.join(teacherIds,",");

			//根据教师编号和班级编号，获取老师签到次数
			Map<Integer,Integer> signNumMap =  JSONObject.parseObject(JSONObject.toJSONString(MapUtil.convertIntegerMap(teacherDao.findSignNum(StringUtils.join(teacherIds,","),queryInfo.getMusicGroupId()))),HashMap.class);
//			Map<Integer,Integer> signNumMap = JSONObject.parseObject(JSONObject.toJSONString(MapUtil.convertMybatisMap(signNum)),HashMap.class);

			//根据合奏班编号，获取子班级名称列表
//			List<Map<Integer,String>> classNames =  classGroupDao.queryNamesByClassId(classGroupIdStr);
//			Map<Integer,String> classNameMap = MapUtil.convertMybatisMap(classNames);

			dataList.forEach(e->{
//				e.setCourseScheduleName(classNameMap.get(e.getClassGroupId()));
				e.setNum(signNumMap.get(e.getTeacherId()));
			});
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

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

		List<MusicGroupTeacherAttendanceDto> dataList = null;
		int count = teacherDao.countTeacherAttendance(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherDao.queryTeacherAttendance(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

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

		List<MusicGroupStudentApplyDto> dataList = null;
		int count = teacherDao.countMusicGroupStudentApply(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherDao.queryMusicGroupStudentApply(params);
			String paymentStatus = queryInfo.getPaymentStatus();
			if(StringUtils.isNotEmpty(paymentStatus) && paymentStatus.equals("2")){
				List<StudentPaymentOrder> studentPaymentOrders = studentPaymentOrderDao.findStudentPaymentOrder("MUSIC",queryInfo.getMusicGroupId());
				Map<Integer, List<StudentPaymentOrder>> collect = studentPaymentOrders.stream().collect(Collectors.groupingBy(StudentPaymentOrder::getUserId));
				dataList.forEach(e->{
					List<StudentPaymentOrder> paymentOrders = collect.get(e.getUserId());
					if(paymentOrders != null && paymentOrders.size() > 0){
						e.setPaymentAmount(paymentOrders.get(0).getActualAmount());
						e.setPayTime(paymentOrders.get(0).getPayTime());
					}
				});
			}
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public List<Teacher> findTeacherByOrganId(String organId,String subjectIds,String jobNature) {
		return teacherDao.findTeacherByOrganId(organId,subjectIds,jobNature);
	}

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

		List<Teacher> dataList = null;
		int count = teacherDao.countTeacherName(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherDao.queryPageName(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public CourseHomework getCourseScheduleHomework(Integer courseScheduleId) {
		return teacherDao.getCourseScheduleHomework(courseScheduleId);
	}

	@Override
	public List<BasicUserDto> queryEducationIpa(Integer userId, String search) {
//		List<BasicUserDto> basicUserDtos = queryGroupStudents(userId, search);
		//获取当前管理人员所在分部，所有老师，以及没有乐团的学生
//		List<BasicUserDto> userDtos = teacherDao.queryEducationIpa(userId, search);
//		userDtos.addAll(basicUserDtos);
//		return userDtos.stream().distinct().collect(Collectors.toList());
		return queryGroupStudents(userId, search);
	}

	@Override
	public List<BasicUserDto> queryMusicGroupTeacher(String musicGroupId) {
		if(Objects.isNull(musicGroupId)){
			throw new BizException("请指定乐团");
		}
		return teacherDao.findTeacherByMusicGroup(musicGroupId);
	}

	@Override
	public PageInfo<Teacher> queryPageDetail(TeacherQueryInfo queryInfo) {
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);
		PageInfo<Teacher> pageInfo = queryPage(queryInfo);
		List<Teacher> rows = pageInfo.getRows();
		if(rows != null && rows.size() > 0){
			Set<Integer> teachers = rows.stream().map(e -> e.getId()).collect(Collectors.toSet());
			String teacherIds = StringUtils.join(teachers, ",");
			//获取小课数量
			Map<Integer,Integer> vipNumMap = JSONObject.parseObject(JSONObject.toJSONString(MapUtil.convertIntegerMap(classGroupDao.countTeacherVipNum(teacherIds))),HashMap.class);
			//获取试听课数量
			Map<Integer,Integer> demoNumMap = JSONObject.parseObject(JSONObject.toJSONString(MapUtil.convertIntegerMap(demoGroupDao.countTeacherDemoGroupNum(teacherIds))),HashMap.class);

			Set<Integer> organIds = rows.stream().map(e -> e.getTeacherOrganId()).collect(Collectors.toSet());
			Map<Integer,String> organNames = MapUtil.convertMybatisMap(organizationDao.findOrganNameMapList(organIds));

			LocalDate nowDate = LocalDateTime.now(DateUtil.zoneId).toLocalDate();
			LocalDate monDayDate = nowDate.with(DateUtil.weekFields.dayOfWeek(), DayOfWeek.MONDAY.getValue());
			List<TeacherExercisesServiceDto> teacherExercisesServiceSituations = studentExtracurricularExercisesSituationDao.findTeacherExercisesServiceSituations(monDayDate.toString(), new ArrayList<>(teachers));
			Map<Integer, TeacherExercisesServiceDto> teacherServiceMap = new HashMap<>();
			if(!CollectionUtils.isEmpty(teacherExercisesServiceSituations)){
				teacherServiceMap = teacherExercisesServiceSituations.stream().collect(Collectors.toMap(TeacherExercisesServiceDto::getTeacherId, t -> t));
			}

			Map<Integer,String> operatingStudentsNum = MapUtil.convertMybatisMap(studentDao.getTeacherOperatingStudentsNum(teacherIds));
			Map<Integer,String> practiceStudentsNum = MapUtil.convertMybatisMap(studentDao.getBuyNums(teacherIds, GroupType.PRACTICE));
			Map<Integer,String> vipStudentsNum = MapUtil.convertMybatisMap(studentDao.getBuyNums(teacherIds, GroupType.VIP));
			Map<Integer,String> practiceAndVipStudentsNum = MapUtil.convertMybatisMap(studentDao.getPracticeAndVipNums(teacherIds));

			for (Teacher e : rows) {
				e.setSubjectName(subjectDao.findBySubIds(e.getSubjectId()));
				e.setOrganName(organNames.get(e.getTeacherOrganId()));
				if(StringUtils.isNotEmpty(e.getSplitSubjectName())){
					e.setSubjectName(Arrays.asList(e.getSplitSubjectName().split(",")));
				}
				Integer num = vipNumMap.get(e.getId());
				e.setVipNum(num == null?0:num);
				Integer demoNum = demoNumMap.get(e.getId());
				e.setDemoNum(demoNum == null?0:demoNum);

				TeacherExercisesServiceDto teacherService = teacherServiceMap.get(e.getId());
				if(Objects.nonNull(teacherService)){
					BigDecimal serviceIndex = new BigDecimal(teacherService.getActualExercisesNum()).multiply(new BigDecimal(100)).divide(new BigDecimal(teacherService.getExpectExercisesNum()), CommonConstants.DECIMAL_FINAL_PLACE, BigDecimal.ROUND_HALF_UP);
					e.setServiceIndex(serviceIndex);
				}else{
					e.setServiceIndex(BigDecimal.ZERO);
				}
				//运营指标
				String studentNum = operatingStudentsNum.get(e.getId());
				if(studentNum != null){
					String practiceNum = practiceStudentsNum.get(e.getId()) ==null ? "0" : practiceStudentsNum.get(e.getId());
					String vipNum = vipStudentsNum.get(e.getId()) ==null ? "0" : vipStudentsNum.get(e.getId());
					String practiceAndVipNum = practiceAndVipStudentsNum.get(e.getId()) ==null ? "0" : practiceAndVipStudentsNum.get(e.getId());
					int allNum = Integer.parseInt(practiceNum) + Integer.parseInt(vipNum) - Integer.parseInt(practiceAndVipNum);
					BigDecimal operatingIndex = new BigDecimal(allNum).multiply(new BigDecimal(100)).divide(new BigDecimal(studentNum), CommonConstants.DECIMAL_FINAL_PLACE, BigDecimal.ROUND_HALF_UP);
					e.setOperatingIndex(operatingIndex);
				}else {
					e.setOperatingIndex(BigDecimal.ZERO);
				}
			}
		}
		return pageInfo;
	}

	@Override
	public Teacher getDetail(Integer id) {
		Teacher teacher = teacherDao.get(id);
		if(teacher != null){
			//获取老师教学点列表
			List<School> teacherSchools = schoolDao.findByUserId(id);
			teacher.setTeacherSchools(teacherSchools);
			//获取流动范围
			if(StringUtils.isNotEmpty(teacher.getFlowOrganRange())){
				teacher.setFlowOrganRangeId(teacher.getFlowOrganRange());
				List<String> organNames = organizationDao.findByOrganIds(teacher.getFlowOrganRange());
				teacher.setFlowOrganRange(StringUtils.join(organNames,","));
			}
			if(teacher.getTeacherOrganId() != null){
				teacher.setOrganName(organizationDao.get(teacher.getTeacherOrganId()).getName());
			}
			//专业技能
			if(StringUtils.isNotEmpty(teacher.getSubjectId())){
				teacher.setSubjectName(subjectDao.findBySubIds(teacher.getSubjectId()));
			}
		}
		return teacher;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean realNameAuthentication(Integer userId, String realName, String idcardNo, String idcardFrontImg, String idcardBackImg, String idcardHandImg) {

		SysUser user = sysUserFeignService.queryUserById(userId);
		if (user == null) {
			throw new BizException("用户不存在");
		}

		Teacher teacher = teacherDao.get(userId);
		if (teacher == null) {
			throw new BizException("用户信息不存在");
		}
		// 查询是否已实名认证
		if (StringUtils.isNotBlank(teacher.getIdcardBackImg()) && StringUtils.isNotBlank(teacher.getIdcardFrontImg())
				&& StringUtils.isNotBlank(teacher.getIdcardHandImg())) {
			throw new BizException("已实名认证，不能重复操作");
		}

		IdcardValidator validator = new IdcardValidator();
		if (validator.isValidatedAllIdcard(idcardNo) == false) {
			throw new BizException("身份证校验失败");
		}

		Date date = new Date();

		user.setRealName(realName);
		user.setIdCardNo(idcardNo);
		user.setUpdateTime(date);
		sysUserFeignService.updateSysUser(user);

		teacher.setIdcardBackImg(idcardBackImg);
		teacher.setIdcardFrontImg(idcardFrontImg);
		teacher.setIdcardHandImg(idcardHandImg);
		teacher.setUpdateTime(date);
		teacherDao.update(teacher);

		return true;
	}

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

		List<BasicUserDto> dataList = null;
		int count = teacherDao.countStudent(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = teacherDao.queryStudent(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public List<TeacherDefaultSalaryDto> queryTeacherDefaultSalary(String organIdList) {
		return teacherDao.queryTeacherDefaultSalary(organIdList);
	}

	@Override
	public List<Integer> queryAllIds() {
		return teacherDao.queryAllIds();
	}

}
