查询教练日期约课安排接口
<p>@GetMapping("/queryCourseByCoachIDByDate")
public Result<Object> queryCourseByCoachIDByDate(Coach coach, String date, HttpServletRequest request) throws ParseException {</p>
<pre><code>// 权限验证
String token = (String) request.getAttribute("claims_coach");
if (token == null || "".equals(token)) {
throw new RuntimeException("权限不足!");
}
if (coach.getCoachId() == null) {
log.info("coachId为null");
return new Result<>(ResultCode.FAIL);
}
// 转换前端传递日期格式
Date convertedDate = RcqtUtils.getconvertedDate(date);
Map<String, Object> map = new HashMap<>();
// 查询日期对应的数据
List<UpdateTime> updateTimeList = updateTimeService.queryUpdateTimeByDate(coach.getCoachId(), convertedDate);
List<UpdateTimeDto> updateTimeDtoList = new ArrayList<>();
if (CollectionUtils.isEmpty(updateTimeList)) {
log.info("查询当前日期数据为空!");
// 查询教练自己设置的默认时间段
List<DefaultTime> defaultTimes = defaultTimeService.queryDefaultTimeByCoachID(coach.getCoachId());
if (!defaultTimes.isEmpty()) {
List<UpdateTime> updateTimes = defaultTimes.stream()
.filter(defaultTime -> defaultTime.getStartReservationTime() != null && defaultTime.getEndReservationTime() != null)
.map(defaultTime -> {
UpdateTime updateTime = new UpdateTime();
updateTime.setCoachId(coach.getCoachId());
updateTime.setProjectType(defaultTime.getProjectType());
updateTime.setCourseDate(convertedDate);
updateTime.setStartReservationTime(defaultTime.getStartReservationTime());
updateTime.setEndReservationTime(defaultTime.getEndReservationTime());
updateTime.setMaxReservation(defaultTime.getMaxReservation());
updateTime.setCourseStatus(false);// 人数未满
updateTime.setCurrentReservation(0);// 当前预约人数0
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = dateFormat.format(convertedDate);
String week = RcqtUtils.getDayOfWeek(currentDate);// 得到当前日期对应周几
if (!StringUtils.isEmpty(week)) {
updateTime.setIsBreakDate(defaultTime.getRestDay().contains(week));
}
return updateTime;
})
.collect(Collectors.toList());
// Batch save the updateTimes
boolean b = updateTimeService.saveBatch(updateTimes);
if (!b) {
log.info("新增失败");
return new Result<>(ResultCode.FAIL);
}
// Get all the update time IDs
List<Long> updateTimeIds = updateTimes.stream()
.map(UpdateTime::getUpdateTimeId)
.collect(Collectors.toList());
// Get all the course information
List<CourseInfo> courseInfos = courseInfoService.queryCourseByUpdateTimeIds(updateTimeIds);
if (!courseInfos.isEmpty()) {
// Get all the user IDs
List<Long> userIds = courseInfos.stream()
.map(CourseInfo::getUserId)
.collect(Collectors.toList());
// Query all the user information at once
Map<Long, CarUser> carUserMap = carUserService.queryCarUserListByUserIds(userIds)
.stream()
.collect(Collectors.toMap(CarUser::getUserId, Function.identity()));
// Group the course information by update time ID using parallel stream
Map<Long, List<CarUser>> courseInfoMap = courseInfos.parallelStream()
.collect(Collectors.groupingByConcurrent(CourseInfo::getUpdateTimeId,
Collectors.mapping(courseInfo -> carUserMap.get(courseInfo.getUserId()), Collectors.toList())));
updateTimeList = updateTimes.stream()
.map(updateTime -> {
UpdateTimeDto updateTimeDto = new UpdateTimeDto();
BeanUtils.copyProperties(updateTime, updateTimeDto);
List<CarUser> carUserList = courseInfoMap.get(updateTime.getUpdateTimeId());
if (carUserList != null) {
updateTimeDto.setCurrentReservation(carUserList.size());// Current reservation count
updateTimeDto.setCarUserList(carUserList);
if (carUserList.size() == updateTime.getMaxReservation()) {
updateTimeDto.setCourseStatus(true);// Set course status to full
}
} else {
updateTimeDto.setCurrentReservation(0);// Current reservation count
}
if (updateTimeDto.getCourseDate().equals(RcqtUtils.getDay()) && updateTimeDto.getStartReservationTime() != null) {
updateTimeDto.setIsTimeOut(LocalTime.now().isAfter(updateTimeDto.getStartReservationTime().toLocalTime()));
}
updateTimeDtoList.add(updateTimeDto);
return updateTimeDto;
})
.collect(Collectors.toList());
} else {
updateTimeList = updateTimes.stream()
.map(updateTime -> {
UpdateTimeDto updateTimeDto = new UpdateTimeDto();
BeanUtils.copyProperties(updateTime, updateTimeDto);
updateTimeDto.setCurrentReservation(0);// Current reservation count
if (updateTimeDto.getCourseDate().equals(RcqtUtils.getDay()) && updateTimeDto.getStartReservationTime() != null) {
updateTimeDto.setIsTimeOut(LocalTime.now().isAfter(updateTimeDto.getStartReservationTime().toLocalTime()));
}
updateTimeDtoList.add(updateTimeDto);
return updateTimeDto;
})
.collect(Collectors.toList());
}
}
} else {
// Get all the update time IDs
List<Long> updateTimeIds = updateTimeList.stream()
.map(UpdateTime::getUpdateTimeId)
.collect(Collectors.toList());
// Get all the course information
List<CourseInfo> courseInfos = courseInfoService.queryCourseByUpdateTimeIds(updateTimeIds);
if (!courseInfos.isEmpty()) {
// Get all the user IDs
List<Long> userIds = courseInfos.stream()
.map(CourseInfo::getUserId)
.collect(Collectors.toList());
// Query all the user information at once
Map<Long, CarUser> carUserMap = carUserService.queryCarUserListByUserIds(userIds)
.stream()
.collect(Collectors.toMap(CarUser::getUserId, Function.identity()));
// Group the course information by update time ID using parallel stream
Map<Long, List<CarUser>> courseInfoMap = courseInfos.parallelStream()
.collect(Collectors.groupingByConcurrent(CourseInfo::getUpdateTimeId,
Collectors.mapping(courseInfo -> carUserMap.get(courseInfo.getUserId()), Collectors.toList())));
updateTimeList = updateTimeList.stream()
.map(updateTime -> {
UpdateTimeDto updateTimeDto = new UpdateTimeDto();
BeanUtils.copyProperties(updateTime, updateTimeDto);
List<CarUser> carUserList = courseInfoMap.get(updateTime.getUpdateTimeId());
if (carUserList != null) {
updateTimeDto.setCurrentReservation(carUserList.size());// Current reservation count
updateTimeDto.setCarUserList(carUserList);
if (carUserList.size() == updateTime.getMaxReservation()) {
updateTimeDto.setCourseStatus(true);// Set course status to full
}
} else {
updateTimeDto.setCurrentReservation(0);// Current reservation count
}
if (updateTimeDto.getCourseDate().equals(RcqtUtils.getDay()) && updateTimeDto.getStartReservationTime() != null) {
updateTimeDto.setIsTimeOut(LocalTime.now().isAfter(updateTimeDto.getStartReservationTime().toLocalTime()));
}
updateTimeDtoList.add(updateTimeDto);
return updateTimeDto;
})
.collect(Collectors.toList());
} else {
updateTimeList = updateTimeList.stream()
.map(updateTime -> {
UpdateTimeDto updateTimeDto = new UpdateTimeDto();
BeanUtils.copyProperties(updateTime, updateTimeDto);
updateTimeDto.setCurrentReservation(0);// Current reservation count
if (updateTimeDto.getCourseDate().equals(RcqtUtils.getDay()) && updateTimeDto.getStartReservationTime() != null) {
updateTimeDto.setIsTimeOut(LocalTime.now().isAfter(updateTimeDto.getStartReservationTime().toLocalTime()));
}
updateTimeDtoList.add(updateTimeDto);
return updateTimeDto;
})
.collect(Collectors.toList());
}
}
for (UpdateTimeDto updateTimeDto : updateTimeDtoList) {
if (updateTimeDto.getIsTimeOut() != null && updateTimeDto.getIsTimeOut()) {
map.put("isTimeOutAll", true);
break;
}
}
map.put("isBreakDate", updateTimeList.stream().anyMatch(UpdateTime::getIsBreakDate));
map.put("updateTimeList", updateTimeDtoList);
log.info("通过日期查询约课安排成功!");
return new Result<>(ResultCode.SUCCESS, map);
</code></pre>
<p>}</p>
原文地址: https://www.cveoy.top/t/topic/qeq3 著作权归作者所有。请勿转载和采集!