<p>@GetMapping(&quot;/queryCoachesByUserID&quot;)
public Result<Object> queryCoachesByUserID(CarUser carUser, HttpServletRequest request) {</p>
<pre><code>//权限验证
String token = (String) request.getAttribute(\&quot;claims_user\&quot;);
if (StringUtils.isEmpty(token)) {
    throw new RuntimeException(\&quot;权限不足!\&quot;);
}

//调用方法查询出学员
CarUser carUser0 = carUserService.queryOneCarUserById(carUser.getUserId());

if (carUser0 != null) {

    JSONArray jsonArray = JSONArray.parseArray(carUser0.getCoachId()); //拿出数据库字符串转为json数组

    List&lt;Coach&gt; coaches = new ArrayList&lt;&gt;();

    for (Object o : jsonArray) {
        //从json数组中拿出的教练id
        long coachId = ((Integer) o).longValue();

        Coach coach = coachService.queryOneCoachByID(coachId);

        coaches.add(coach);
    }

    List&lt;CoachDto&gt; coachDtoList = new ArrayList&lt;&gt;();

    if (!CollectionUtils.isEmpty(coaches)){
        LocalDate currentDate = RcqtUtils.getDay().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        for (Coach coach : coaches){
            //判断教练是否在工作中
            //1.获取当前查询时间
            LocalTime currentTime = LocalTime.now();

            //2.查询当前教练课程
            //判断教练的工作状态(true工作中/false休息中)
            List&lt;CourseDto&gt; courseDtoList = courseInfoService.queryCourseByCoachId(coach.getCoachId(), currentDate);
            boolean isWorking = courseDtoList.stream()
                    .anyMatch(c -&gt; currentTime.isAfter(c.getStartReservationTime().toLocalTime()) &amp;&amp; currentTime.isBefore(c.getEndReservationTime().toLocalTime()));

            coach.setWorkStatus(isWorking);
        }

        boolean isUpdateSuccessful = coachService.updateBatchById(coaches);
        if (!isUpdateSuccessful){
            log.info(\&quot;修改失败!\&quot;);
            return new Result&lt;&gt;(ResultCode.FAIL);
        }

        for (Coach coach : coaches){
            CoachDto coachDto = new CoachDto();
            BeanUtils.copyProperties(coach,coachDto);
            DrivingSchool drivingSchool = drivingSchoolService.getById(coach.getDrivingSchoolId());

            if (drivingSchool != null){
                coachDto.setDrivingSchoolName(drivingSchool.getDrivingSchoolName());
                coachDto.setAddress1(drivingSchool.getAddress1());
                coachDto.setAddress2(drivingSchool.getAddress2());
                coachDto.setAddress3(drivingSchool.getAddress3());

                //获得此车辆的地址经纬度
                String address3 = drivingSchool.getAddress3();
                if (!StringUtils.isEmpty(address3)){
                    List&lt;String&gt; addressList =tengXunMapService.getLongitudeAndLatitudeByAddress(address3);
                    if (!CollectionUtils.isEmpty(addressList)){
                        coachDto.setLongitude(addressList.get(0));
                        coachDto.setLatitude(addressList.get(1));
                    }
                }
            }

            if (coach.getServeDate() != null){
                LocalDate serveDate = coach.getServeDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                //计算驾龄
                int years = RcqtUtils.calculateYears(serveDate, currentDate);
                coachDto.setDrivingYear(years + \&quot;年\&quot;);
            }

            coachDtoList.add(coachDto);
        }

        //排序(按照工作状态和姓名排序)
        Comparator&lt;CoachDto&gt; comparator = Comparator.comparing(CoachDto::getWorkStatus, Comparator.reverseOrder())
                .thenComparing((c1, c2) -&gt; Collator.getInstance(Locale.CHINESE).compare(c1.getRealName(), c2.getRealName()));

        coachDtoList.sort(comparator);
    }

    //把查询到的的对象放入map集合
    Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
    map.put(\&quot;coaches\&quot;, coachDtoList);

    //查询到学员传给前端
    log.info(\&quot;查询学员绑定的教练成功!\&quot;);
    return new Result&lt;&gt;(ResultCode.SUCCESS,map);
} else {
    log.info(\&quot;查不到此学员!\&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}
</code></pre>
<p>}</p>
查询学员绑定的教练接口 - /queryCoachesByUserID

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

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