<p>优化代码如下:</p>
<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) {
    log.info(&quot;查不到此学员!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}

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)) {
    for (Coach coach : coaches) {
        //判断教练是否在工作中
        boolean isWorking = isCoachWorking(coach);
        coach.setWorkStatus(isWorking);
    }

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

    for (Coach coach : coaches) {
        CoachDto coachDto = convertToCoachDto(coach);
        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);
</code></pre>
<p>}</p>
<p>private boolean isCoachWorking(Coach coach) {
//获取当前查询时间
Date date = RcqtUtils.getDay();</p>
<pre><code>//查询当前教练课程
List&lt;CourseDto&gt; courseDtoList = courseInfoService.queryCourseByCoachId(coach.getCoachId(), date);
return courseDtoList.stream()
        .anyMatch(c -&gt; LocalTime.now().isAfter(c.getStartReservationTime().toLocalTime()) &amp;&amp; LocalTime.now().isBefore(c.getEndReservationTime().toLocalTime()));
</code></pre>
<p>}</p>
<p>private CoachDto convertToCoachDto(Coach coach) {
CoachDto coachDto = new CoachDto();
BeanUtils.copyProperties(coach, coachDto);
DrivingSchool drivingSchool = drivingSchoolService.getById(coach.getDrivingSchoolId());</p>
<pre><code>if (drivingSchool != null) {
    coachDto.setDrivingSchoolName(drivingSchool.getDrivingSchoolName());
    coachDto.setAddress1(drivingSchool.getAddress1());
    coachDto.setAddress2(drivingSchool.getAddress2());
    coachDto.setAddress3(drivingSchool.getAddress3());

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

if (coach.getServeDate() != null) {
    LocalDate localDate1 = coach.getServeDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    LocalDate localDate2 = RcqtUtils.getDay().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    //计算驾龄
    int years = Period.between(localDate1, localDate2).getYears();
    coachDto.setDrivingYear(years + &quot;年&quot;);
}

return coachDto;
</code></pre>
GetMappingqueryCoachesByUserID public ResultObject queryCoachesByUserIDCarUser carUser HttpServletRequest request 权限验证 String token = String requestgetAttributeclaims_user; if

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

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