<p>@GetMapping(&quot;/queryCourseByCoachIDByDate&quot;)
public Result<Object> queryCourseByCoachIDByDate(Coach coach, String date, HttpServletRequest request) throws ParseException {</p>
<pre><code>// 权限验证
String token = (String) request.getAttribute(&quot;claims_coach&quot;);
if (token == null || &quot;&quot;.equals(token)) {
    throw new RuntimeException(&quot;权限不足!&quot;);
}

if (coach.getCoachId() == null) {
    log.info(&quot;coachId为null&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}

// 转换前端传递日期格式
Date convertedDate = RcqtUtils.getconvertedDate(date);

Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
// 查询日期对应的数据
List&lt;UpdateTime&gt; updateTimeList = updateTimeService.queryUpdateTimeByDate(coach.getCoachId(), convertedDate);

List&lt;UpdateTimeDto&gt; updateTimeDtoList = new ArrayList&lt;&gt;();

if (CollectionUtils.isEmpty(updateTimeList)) {
    log.info(&quot;查询当前日期数据为空!&quot;);
    // 查询教练自己设置的默认时间段
    List&lt;DefaultTime&gt; defaultTimes = defaultTimeService.queryDefaultTimeByCoachID(coach.getCoachId());

    if (!defaultTimes.isEmpty()) {
        List&lt;UpdateTime&gt; updateTimes = defaultTimes.stream()
                .filter(defaultTime -&gt; defaultTime.getStartReservationTime() != null &amp;&amp; defaultTime.getEndReservationTime() != null)
                .map(defaultTime -&gt; {
                    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(&quot;yyyy-MM-dd&quot;);
                    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(&quot;新增失败&quot;);
            return new Result&lt;&gt;(ResultCode.FAIL);
        }

        // Get all the update time IDs
        List&lt;Long&gt; updateTimeIds = updateTimes.stream()
                .map(UpdateTime::getUpdateTimeId)
                .collect(Collectors.toList());

        // Get all the course information
        List&lt;CourseInfo&gt; courseInfos = courseInfoService.queryCourseByUpdateTimeIds(updateTimeIds);

        if (!courseInfos.isEmpty()) {
            // Get all the user IDs
            List&lt;Long&gt; userIds = courseInfos.stream()
                    .map(CourseInfo::getUserId)
                    .collect(Collectors.toList());

            // Query all the user information at once
            Map&lt;Long, CarUser&gt; carUserMap = carUserService.queryCarUserListByUserIds(userIds)
                    .stream()
                    .collect(Collectors.toMap(CarUser::getUserId, Function.identity()));

            // Group the course information by update time ID using parallel stream
            Map&lt;Long, List&lt;CarUser&gt;&gt; courseInfoMap = courseInfos.parallelStream()
                    .collect(Collectors.groupingByConcurrent(CourseInfo::getUpdateTimeId,
                            Collectors.mapping(courseInfo -&gt; carUserMap.get(courseInfo.getUserId()), Collectors.toList())));

            updateTimeList = updateTimes.stream()
                    .map(updateTime -&gt; {
                        UpdateTimeDto updateTimeDto = new UpdateTimeDto();
                        BeanUtils.copyProperties(updateTime, updateTimeDto);
                        List&lt;CarUser&gt; 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()) &amp;&amp; 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 -&gt; {
                        UpdateTimeDto updateTimeDto = new UpdateTimeDto();
                        BeanUtils.copyProperties(updateTime, updateTimeDto);
                        updateTimeDto.setCurrentReservation(0);// Current reservation count
                        if (updateTimeDto.getCourseDate().equals(RcqtUtils.getDay()) &amp;&amp; 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&lt;Long&gt; updateTimeIds = updateTimeList.stream()
            .map(UpdateTime::getUpdateTimeId)
            .collect(Collectors.toList());

    // Get all the course information
    List&lt;CourseInfo&gt; courseInfos = courseInfoService.queryCourseByUpdateTimeIds(updateTimeIds);

    if (!courseInfos.isEmpty()) {
        // Get all the user IDs
        List&lt;Long&gt; userIds = courseInfos.stream()
                .map(CourseInfo::getUserId)
                .collect(Collectors.toList());

        // Query all the user information at once
        Map&lt;Long, CarUser&gt; carUserMap = carUserService.queryCarUserListByUserIds(userIds)
                .stream()
                .collect(Collectors.toMap(CarUser::getUserId, Function.identity()));

        // Group the course information by update time ID using parallel stream
        Map&lt;Long, List&lt;CarUser&gt;&gt; courseInfoMap = courseInfos.parallelStream()
                .collect(Collectors.groupingByConcurrent(CourseInfo::getUpdateTimeId,
                        Collectors.mapping(courseInfo -&gt; carUserMap.get(courseInfo.getUserId()), Collectors.toList())));

        updateTimeList = updateTimeList.stream()
                .map(updateTime -&gt; {
                    UpdateTimeDto updateTimeDto = new UpdateTimeDto();
                    BeanUtils.copyProperties(updateTime, updateTimeDto);
                    List&lt;CarUser&gt; 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()) &amp;&amp; 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 -&gt; {
                    UpdateTimeDto updateTimeDto = new UpdateTimeDto();
                    BeanUtils.copyProperties(updateTime, updateTimeDto);
                    updateTimeDto.setCurrentReservation(0);// Current reservation count
                    if (updateTimeDto.getCourseDate().equals(RcqtUtils.getDay()) &amp;&amp; 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 &amp;&amp; updateTimeDto.getIsTimeOut()) {
        map.put(&quot;isTimeOutAll&quot;, true);
        break;
    }
}

map.put(&quot;isBreakDate&quot;, updateTimeList.stream().anyMatch(UpdateTime::getIsBreakDate));
map.put(&quot;updateTimeList&quot;, updateTimeDtoList);
log.info(&quot;通过日期查询约课安排成功!&quot;);
return new Result&lt;&gt;(ResultCode.SUCCESS, map);
</code></pre>
<p>}</p>
查询教练日期约课安排接口

原文地址: https://www.cveoy.top/t/topic/qeq3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录