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.auth.api.entity.SysUserRole;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.CourseScheduleEndDto;
import com.ym.mec.biz.dal.dto.IndexErrorDataExportDto;
import com.ym.mec.biz.dal.dto.SimpleUserDto;
import com.ym.mec.biz.dal.dto.TeacherVisitDto;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.IndexErrorType;
import com.ym.mec.biz.dal.enums.InspectionItemEnum;
import com.ym.mec.biz.dal.enums.SysUserRoleEnum;
import com.ym.mec.biz.dal.page.EndCourseScheduleQueryInfo;
import com.ym.mec.biz.dal.page.IndexErrRecordQueryInfo;
import com.ym.mec.biz.dal.page.InspectionItemPlanQueryInfo;
import com.ym.mec.biz.dal.page.InspectionQueryInfo;
import com.ym.mec.biz.service.SysEmployeePositionService;
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.util.collection.MapUtil;
import com.ym.mec.util.date.DateUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;

import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.common.tenant.TenantContextHolder;
import com.ym.mec.biz.service.IndexErrDataRecordService;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

import static com.ym.mec.biz.dal.enums.GroupType.*;

@Service
public class IndexErrDataRecordServiceImpl extends BaseServiceImpl<Long, IndexErrDataRecord>  implements IndexErrDataRecordService {
	
	@Autowired
	private IndexErrDataRecordDao indexErrDataRecordDao;
	@Autowired
	private StudentVisitDao studentVisitDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private ClassGroupDao classGroupDao;
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private CourseScheduleDao courseScheduleDao;
	@Autowired
	private CourseScheduleStudentPaymentDao courseScheduleStudentPaymentDao;
	@Autowired
	private MusicGroupDao musicGroupDao;
	@Autowired
	private VipGroupDao vipGroupDao;
	@Autowired
	private PracticeGroupDao practiceGroupDao;
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private SysEmployeePositionService employeePositionService;

	@Override
	public BaseDAO<Long, IndexErrDataRecord> getDAO() {
		return indexErrDataRecordDao;
	}

	@Override
	public void indexErrDataRecordTask(String date) {
		if(StringUtils.isBlank(date)){
			date = LocalDate.now().plusDays(-1).toString();
		}
		Date dt = DateUtil.stringToDate(date, "yyyy-MM-dd");
		
		int errDateNum = indexErrDataRecordDao.countWithGenerateTime(date);

		if(errDateNum<=0){
			//乐团巡查事项异常
			List<IndexErrDataRecord> errInspections = indexErrDataRecordDao.queryErrInspection(date, null);
			if(!CollectionUtils.isEmpty(errInspections)){
				errInspections.forEach(e->{
					e.setDataType(IndexErrorType.MUSIC_PATROL_ITEM);
					e.setGenerateTime(dt);
				});
				indexErrDataRecordDao.batchInsert(errInspections);
			}

			//乐团巡查任务未提交
			List<IndexErrDataRecord> errInspectionItemPlans = indexErrDataRecordDao.queryInspectionItemPlan(date, null);
			if(!CollectionUtils.isEmpty(errInspectionItemPlans)) {
				errInspectionItemPlans.forEach(e -> {
					e.setDataType(IndexErrorType.INSPECTION_ITEM_PLAN);
					e.setGenerateTime(dt);
				});
				indexErrDataRecordDao.batchInsert(errInspectionItemPlans);
			}

			//课程考勤异常
			List<IndexErrDataRecord> errStudentAttendances = indexErrDataRecordDao.getAttendanceError(date, null);
			if(!CollectionUtils.isEmpty(errStudentAttendances)) {
				errStudentAttendances.forEach(e -> {
					e.setDataType(IndexErrorType.TEACHER_EXCEPTION_ATTENDANCE);
					e.setGenerateTime(dt);
				});
				indexErrDataRecordDao.batchInsert(errStudentAttendances);
			}

			//课程异常
			List<IndexErrDataRecord> errNoAttendances = indexErrDataRecordDao.getNoAttendance(date, null);
			if(!CollectionUtils.isEmpty(errNoAttendances)) {
				errNoAttendances.forEach(e -> {
					e.setDataType(IndexErrorType.TEACHER_NOT_A_CLASS);
					e.setGenerateTime(dt);
				});
				indexErrDataRecordDao.batchInsert(errNoAttendances);
			}
		}

		//处理历史异常数据
		checkUntreatedRecord();
	}

	private void checkUntreatedRecord(){
		List<IndexErrDataRecord> untreatedRecord = indexErrDataRecordDao.findUntreatedRecord();
		if(CollectionUtils.isEmpty(untreatedRecord)){
			return;
		}
		Map<IndexErrorType, List<IndexErrDataRecord>> typeDataMap = untreatedRecord.stream().collect(Collectors.groupingBy(IndexErrDataRecord::getDataType));

		List<IndexErrDataRecord> needUpdateRecord = new ArrayList<>();
		Date now = new Date();

		for (Map.Entry<IndexErrorType, List<IndexErrDataRecord>> typeDataMapEntry : typeDataMap.entrySet()) {
			Set<String> dates = typeDataMapEntry.getValue().stream().map(i -> DateUtil.dateToString(i.getGenerateTime(), "yyyy-MM-dd")).collect(Collectors.toSet());
			List<IndexErrDataRecord> newRecord = new ArrayList<>();
			switch (typeDataMapEntry.getKey()){
				case MUSIC_PATROL_ITEM:
					newRecord = indexErrDataRecordDao.queryErrInspection(null, dates);
					break;
				case INSPECTION_ITEM_PLAN:
					newRecord = indexErrDataRecordDao.queryInspectionItemPlan(null, dates);
					break;
				case STUDENT_VISIT:
					newRecord = indexErrDataRecordDao.queryStudentVisit(null, dates);
					break;
				case TEACHER_EXCEPTION_ATTENDANCE:
					newRecord = indexErrDataRecordDao.getAttendanceError(null, dates);
					break;
				case TEACHER_NOT_A_CLASS:
					newRecord = indexErrDataRecordDao.getNoAttendance(null, dates);
					break;
			}
			Set<String> dataIds = newRecord.stream().map(IndexErrDataRecord::getDataId).collect(Collectors.toSet());
			for (IndexErrDataRecord record : typeDataMapEntry.getValue()) {
				if(dataIds.contains(record.getDataId())){
					continue;
				}
				record.setDealTime(now);
				needUpdateRecord.add(record);
			}
		}
		if(!CollectionUtils.isEmpty(needUpdateRecord)){
			indexErrDataRecordDao.batchUpdate(needUpdateRecord);
		}
	}

	@Override
	public List<SimpleUserDto> getAllEducationUsers(String organIds) {
		Set<Integer> queryOrganIds = null;
		if(StringUtils.isNotBlank(organIds)){
			queryOrganIds = Arrays.stream(organIds.split(",")).map(Integer::new).collect(Collectors.toSet());
		}
		List<Integer> allDealUserIds = indexErrDataRecordDao.getAllDealUserIds(queryOrganIds, TenantContextHolder.getTenantId());
		List<SimpleUserDto> users = new ArrayList<>();
		if(!CollectionUtils.isEmpty(allDealUserIds)){
			users = teacherDao.getUsersSimpleInfo(allDealUserIds);
		}
		return users;
	}

	@Override
	public PageInfo<IndexErrorDataExportDto> queryRecord(IndexErrRecordQueryInfo queryInfo) {
		PageInfo<IndexErrorDataExportDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		Set<Integer> queryOrganIds = null;
		if(StringUtils.isNotBlank(queryInfo.getOrganIds())){
			queryOrganIds = Arrays.stream(queryInfo.getOrganIds().split(",")).map(Integer::new).collect(Collectors.toSet());
		}
		params.put("queryOrganIds", queryOrganIds);

		Integer tenantId = TenantContextHolder.getTenantId();
		int count = indexErrDataRecordDao.queryErrStringsCount(params);
		pageInfo.setTotal(count);
		params.put("offset", pageInfo.getOffset());
		List<IndexErrorDataExportDto> dataList = new ArrayList<>();
		List<String> strings = indexErrDataRecordDao.queryErrStrings(params);
		if (!CollectionUtils.isEmpty(strings)) {
			List<IndexErrDataRecord> errRecords = indexErrDataRecordDao.getWithOrganMonthUserStr(strings,tenantId);
			List<Integer> organIds = errRecords.stream().map(IndexErrDataRecord::getOrganId).collect(Collectors.toList());
			List<Organization> organs = organizationDao.getOrgans(organIds,queryInfo.getTenantId());
			Map<Integer, String> idOrganNameMap = organs.stream().collect(Collectors.toMap(Organization::getId, o -> o.getName()));

			List<Integer> userIds = errRecords.stream().map(IndexErrDataRecord::getDealUserId).collect(Collectors.toList());
			Map<Integer, SimpleUserDto> idUserMap = new HashMap<>();
			if(!CollectionUtils.isEmpty(userIds)){
				List<SimpleUserDto> users = teacherDao.getUsersSimpleInfo(userIds);
				idUserMap = users.stream().collect(Collectors.toMap(SimpleUserDto::getUserId, u->u, (u1, u2)->u1));
			}
			Map<Integer, List<IndexErrDataRecord>> organDataMap = errRecords.stream().collect(Collectors.groupingBy(IndexErrDataRecord::getOrganId));
			for (Map.Entry<Integer, List<IndexErrDataRecord>> organDataMapEntry : organDataMap.entrySet()) {
				Map<Date, Map<Integer, List<IndexErrDataRecord>>> dateUserRecordMap = organDataMapEntry.getValue().stream().collect(Collectors.groupingBy(IndexErrDataRecord::getGenerateTime, Collectors.groupingBy(IndexErrDataRecord::getDealUserId)));
				for (Map.Entry<Date, Map<Integer, List<IndexErrDataRecord>>> dateUserRecordMapEntry : dateUserRecordMap.entrySet()) {
					String generateTimeStr = DateUtil.dateToString(dateUserRecordMapEntry.getKey(), DateUtil.DATE_FORMAT_MIN);
					for (Map.Entry<Integer, List<IndexErrDataRecord>> userRecordMapEntry : dateUserRecordMapEntry.getValue().entrySet()) {
						IndexErrorDataExportDto record = new IndexErrorDataExportDto();
						record.setGenerateTime(generateTimeStr);
						record.setOrganName(idOrganNameMap.get(organDataMapEntry.getKey()));
						if(idUserMap.containsKey(userRecordMapEntry.getKey())){
							record.setDealUserName(idUserMap.get(userRecordMapEntry.getKey()).getUserName());
						}
						Map<IndexErrorType, Long> typeDataNumMap = userRecordMapEntry.getValue().stream().collect(Collectors.groupingBy(IndexErrDataRecord::getDataType, Collectors.counting()));
						Map<IndexErrorType, Long> typeSurplusDataNumMap = userRecordMapEntry.getValue().stream().filter(e->Objects.isNull(e.getDealTime())).collect(Collectors.groupingBy(IndexErrDataRecord::getDataType, Collectors.counting()));
						for (Map.Entry<IndexErrorType, Long> typeDataNumMapEntry : typeDataNumMap.entrySet()) {
							switch (typeDataNumMapEntry.getKey()){
								case MUSIC_PATROL_ITEM:
									record.setMusicPatrolItem(typeDataNumMapEntry.getValue().intValue());
									if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
										record.setMusicPatrolItemSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
									}
									break;
								case INSPECTION_ITEM_PLAN:
									record.setInspectionItemPlan(typeDataNumMapEntry.getValue().intValue());
									if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
										record.setInspectionItemPlanSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
									}
									break;
								case STUDENT_VISIT:
									record.setStudentVisit(typeDataNumMapEntry.getValue().intValue());
									if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
										record.setStudentVisitSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
									}
									break;
								case TEACHER_EXCEPTION_ATTENDANCE:
									record.setTeacherExceptionAttendance(typeDataNumMapEntry.getValue().intValue());
									if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
										record.setTeacherExceptionAttendanceSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
									}
									break;
								case TEACHER_NOT_A_CLASS:
									record.setTeacherNotAClass(typeDataNumMapEntry.getValue().intValue());
									if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
										record.setTeacherNotAClassSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
									}
									break;
							}
						}
						dataList.add(record);
					}
				}
			}
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public IndexErrorDataExportDto errDataSummarizing(IndexErrRecordQueryInfo queryInfo) {
		PageInfo<IndexErrorDataExportDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		Set<Integer> queryOrganIds = null;
		if(StringUtils.isNotBlank(queryInfo.getOrganIds())){
			queryOrganIds = Arrays.stream(queryInfo.getOrganIds().split(",")).map(Integer::new).collect(Collectors.toSet());
		}
		params.put("queryOrganIds", queryOrganIds);

		List<IndexErrDataRecord> allErrDatas = indexErrDataRecordDao.getAllErrDatas(params);
		IndexErrorDataExportDto record = new IndexErrorDataExportDto();

		Map<IndexErrorType, Long> typeDataNumMap = allErrDatas.stream().collect(Collectors.groupingBy(IndexErrDataRecord::getDataType, Collectors.counting()));
		Map<IndexErrorType, Long> typeSurplusDataNumMap = allErrDatas.stream().filter(e->Objects.isNull(e.getDealTime())).collect(Collectors.groupingBy(IndexErrDataRecord::getDataType, Collectors.counting()));
		for (Map.Entry<IndexErrorType, Long> typeDataNumMapEntry : typeDataNumMap.entrySet()) {
			switch (typeDataNumMapEntry.getKey()){
				case MUSIC_PATROL_ITEM:
					record.setMusicPatrolItem(typeDataNumMapEntry.getValue().intValue());
					if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
						record.setMusicPatrolItemSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
					}
					break;
				case INSPECTION_ITEM_PLAN:
					record.setInspectionItemPlan(typeDataNumMapEntry.getValue().intValue());
					if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
						record.setInspectionItemPlanSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
					}
					break;
				case STUDENT_VISIT:
					record.setStudentVisit(typeDataNumMapEntry.getValue().intValue());
					if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
						record.setStudentVisitSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
					}
					break;
				case TEACHER_EXCEPTION_ATTENDANCE:
					record.setTeacherExceptionAttendance(typeDataNumMapEntry.getValue().intValue());
					if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
						record.setTeacherExceptionAttendanceSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
					}
					break;
				case TEACHER_NOT_A_CLASS:
					record.setTeacherNotAClass(typeDataNumMapEntry.getValue().intValue());
					if(typeSurplusDataNumMap.containsKey(typeDataNumMapEntry.getKey())) {
						record.setTeacherNotAClassSurplus(typeSurplusDataNumMap.get(typeDataNumMapEntry.getKey()).intValue());
					}
					break;
			}
		}

		return record;
	}

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

		int count = indexErrDataRecordDao.errInspectionDataCount(params);
		pageInfo.setTotal(count);
		params.put("offset", pageInfo.getOffset());
		List<InspectionItemPlan> dataList = indexErrDataRecordDao.queryErrInspectionData(params);

		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public PageInfo<InspectionItem> queryErrStudentVisit(InspectionQueryInfo queryInfo) {
		if (queryInfo.getMonth() != null) {
			queryInfo.setStartTime(DateUtil.getFirstDayOfMonth(queryInfo.getMonth()));
			queryInfo.setEndTime(DateUtil.getLastTimeWithDay(DateUtil.getLastDayOfMonth(queryInfo.getMonth())));
		}
		if (queryInfo.getStartTime() != null) {
			queryInfo.setStartTime(DateUtil.getFirstDayOfMonth(queryInfo.getStartTime()));
		}
		if (queryInfo.getEndTime() != null) {
			queryInfo.setEndTime(DateUtil.getLastTimeWithDay(DateUtil.getLastDayOfMonth(queryInfo.getEndTime())));
		}
		PageInfo<InspectionItem> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		int count = indexErrDataRecordDao.studentVisitCount(params);
		pageInfo.setTotal(count);
		params.put("offset", pageInfo.getOffset());
		List<InspectionItem> dataList = indexErrDataRecordDao.queryErrStudentVisit(params);
		pageInfo.setRows(dataList);
		if (pageInfo.getRows().size() <= 0) {
			return pageInfo;
		}
		List<InspectionItem> rows = pageInfo.getRows();
		List<Integer> teacherIds = rows.stream().filter(e -> e.getItem().equals(InspectionItemEnum.VISIT)).map(InspectionItem::getUserId).collect(Collectors.toList());
		if (teacherIds.size() > 0) {
			Date startTime = rows.stream().map(InspectionItem::getMonth).min(Date::compareTo).get();
			Date EndTime = rows.stream().map(InspectionItem::getMonth).max(Date::compareTo).get();
			startTime = DateUtil.getFirstDayOfMonth(startTime);
			EndTime = DateUtil.getLastTimeWithDay(DateUtil.getLastDayOfMonth(EndTime));
			List<TeacherVisitDto> teacherVisitCounts = studentVisitDao.getTeacherVisitCount(teacherIds, startTime, EndTime);
			for (InspectionItem row : rows) {
				if (row.getItem().equals(InspectionItemEnum.INSPECT)) continue;
				for (TeacherVisitDto teacherVisitCount : teacherVisitCounts) {
					if (teacherVisitCount.getTeacherId().equals(row.getUserId()) &&
							DateUtil.dateToString(row.getMonth(), "yyyy-MM").equals(teacherVisitCount.getMonth())
					) {
						row.setSubmittedTimes(teacherVisitCount.getNum());
					}
				}
			}
		}
		return pageInfo;
	}

	@Override
	public PageInfo<CourseScheduleEndDto> queryHistoryErrCourseData(EndCourseScheduleQueryInfo queryInfo) {
		PageInfo<CourseScheduleEndDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());

		Map<String, Object> params = new HashMap<>(16);
		MapUtil.populateMap(params, queryInfo);

		List<CourseScheduleEndDto> results;
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		//如果当前用户只有教务老师角色，那么只能看到他所管理的课程组的信息
		Boolean onlyForRole = employeePositionService.onlyForRole(SysUserRoleEnum.EDUCATION, sysUser.getId(), sysUser.getIsSuperAdmin());
		if (onlyForRole) {
			List<Long> classGroupIds = classGroupDao.queryGroupClassGroupIds(sysUser.getId());
			if(classGroupIds.size() == 0){
				return pageInfo;
			}
			params.put("classGroupIds",classGroupIds);
		}
		int count = indexErrDataRecordDao.historyErrCourseDataCount(params);
		if(queryInfo.getIsExport() && count > 50000){
			throw new BizException("数据集太大，不能导出.最大数据集不能超过50000");
		}
		pageInfo.setTotal(count);
		params.put("offset", pageInfo.getOffset());
		results = indexErrDataRecordDao.queryHistoryErrCourseData(params);
		if (!CollectionUtils.isEmpty(results)) {
			List<Long> courseScheduleIds = results.stream().map(CourseScheduleEndDto::getId).collect(Collectors.toList());
			List<Integer> teacherIds=results.stream().filter(t->Objects.nonNull(t.getActualTeacherId())).map(CourseSchedule::getActualTeacherId).collect(Collectors.toList());

			List<Map<Integer, String>> nameIdMaps = teacherDao.queryNameByIdList(teacherIds);
			Map<Integer, String> nameIdMap=MapUtil.convertIntegerMap(nameIdMaps);

			List<Map<Long, Long>> courseMergeCourseNum = courseScheduleDao.countCourseMergeCourseNum(courseScheduleIds);
			Map<Long, Long> courseMergeCourseNumMap = new HashMap<>();
			if(!CollectionUtils.isEmpty(courseMergeCourseNum)){
				courseMergeCourseNumMap = MapUtil.convertIntegerMap(courseMergeCourseNum);
			}
			//获取课程的预计人数
			List<CourseSchedule> courseScheduleStudentNum = courseScheduleStudentPaymentDao.getCourseScheduleStudentNum(courseScheduleIds);
			Map<Long, Integer> courseScheduleStudentNumMap = courseScheduleStudentNum.stream().collect(Collectors.toMap(CourseSchedule::getId, CourseSchedule::getStudentNum));
			//获取班级声部名称
			List<Map<Integer, String>> classGroupSubjectNameMaps = classGroupDao.findClassGroupSubjectNameMaps(courseScheduleIds);
			Map<Integer, String> classGroupSubjectNameMap = MapUtil.convertIntegerMap(classGroupSubjectNameMaps);
			Map<Long, String> eduName = null;
			if(queryInfo.getIsExport()){
				List<Long> musicCourseIds = results.stream().filter(e -> e.getGroupType() == MUSIC).map(CourseScheduleEndDto::getId).collect(Collectors.toList());
				eduName = new HashMap<>(courseScheduleIds.size());
				if(musicCourseIds != null && musicCourseIds.size() > 0){
					eduName.putAll(MapUtil.convertMybatisMap(musicGroupDao.queryEduNameMapByCourseId(musicCourseIds)));
				}
				List<Long> vipCourseIds = results.stream().filter(e -> e.getGroupType() == VIP).map(CourseScheduleEndDto::getId).collect(Collectors.toList());
				if(vipCourseIds != null && vipCourseIds.size() > 0){
					eduName.putAll(MapUtil.convertMybatisMap(vipGroupDao.queryEduNameMapByCourseId(vipCourseIds)));
				}
				List<Long> practiceCourseIds = results.stream().filter(e -> e.getGroupType() == PRACTICE).map(CourseScheduleEndDto::getId).collect(Collectors.toList());
				if(practiceCourseIds != null && practiceCourseIds.size() > 0){
					eduName.putAll(MapUtil.convertMybatisMap(practiceGroupDao.queryEduNameMapByCourseId(practiceCourseIds)));
				}
			}
			for (CourseScheduleEndDto result : results) {
				if(eduName != null){
					result.setEducationTeacherName(eduName.get(result.getId()));
				}
				result.setTeacher(null);
				result.setTeacherName(nameIdMap.get(result.getActualTeacherId()));
				result.setSubjectName(classGroupSubjectNameMap.get(result.getId()));
				result.setIsSettlement(Objects.isNull(result.getSettlementTime())?0:1);
				if(courseMergeCourseNumMap.containsKey(result.getId())){
					result.setBeMerged(courseMergeCourseNumMap.get(result.getId())>0?true:false);
				}
				result.setStudentNum(courseScheduleStudentNumMap.getOrDefault(result.getId(), 0));
			}
		}
		pageInfo.setRows(results);
		return pageInfo;
	}
}
