GetMappingqueryCoachesByUserID public ResultObject queryCoachesByUserIDCarUser carUser HttpServletRequest request 权限验证 String token = String requestgetAttributeclaims_user; if
<p>优化代码如下:</p>
<p>@GetMapping("/queryCoachesByUserID")
public Result<Object> queryCoachesByUserID(CarUser carUser, HttpServletRequest request) {</p>
<pre><code>//权限验证
String token = (String) request.getAttribute("claims_user");
if (StringUtils.isEmpty(token)) {
throw new RuntimeException("权限不足!");
}
//调用方法查询出学员
CarUser carUser0 = carUserService.queryOneCarUserById(carUser.getUserId());
if (carUser0 == null) {
log.info("查不到此学员!");
return new Result<>(ResultCode.FAIL);
}
JSONArray jsonArray = JSONArray.parseArray(carUser0.getCoachId());//拿出数据库字符串转为json数组
List<Coach> coaches = new ArrayList<>();
for (Object o : jsonArray) {
//从json数组中拿出的教练id
long coachId = ((Integer) o).longValue();
Coach coach = coachService.queryOneCoachByID(coachId);
coaches.add(coach);
}
List<CoachDto> coachDtoList = new ArrayList<>();
if (!CollectionUtils.isEmpty(coaches)) {
for (Coach coach : coaches) {
//判断教练是否在工作中
boolean isWorking = isCoachWorking(coach);
coach.setWorkStatus(isWorking);
}
boolean b = coachService.updateBatchById(coaches);
if (!b) {
log.info("修改失败!");
return new Result<>(ResultCode.FAIL);
}
for (Coach coach : coaches) {
CoachDto coachDto = convertToCoachDto(coach);
coachDtoList.add(coachDto);
}
//排序(按照工作状态和姓名排序)
Comparator<CoachDto> comparator = Comparator.comparing(CoachDto::getWorkStatus, Comparator.reverseOrder())
.thenComparing((c1, c2) -> Collator.getInstance(Locale.CHINESE).compare(c1.getRealName(), c2.getRealName()));
coachDtoList.sort(comparator);
}
//把查询到的的对象放入map集合
Map<String, Object> map = new HashMap<>();
map.put("coaches", coachDtoList);
//查询到学员传给前端
log.info("查询学员绑定的教练成功!");
return new Result<>(ResultCode.SUCCESS, map);
</code></pre>
<p>}</p>
<p>private boolean isCoachWorking(Coach coach) {
//获取当前查询时间
Date date = RcqtUtils.getDay();</p>
<pre><code>//查询当前教练课程
List<CourseDto> courseDtoList = courseInfoService.queryCourseByCoachId(coach.getCoachId(), date);
return courseDtoList.stream()
.anyMatch(c -> LocalTime.now().isAfter(c.getStartReservationTime().toLocalTime()) && 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<String> 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 + "年");
}
return coachDto;
</code></pre>
原文地址: https://www.cveoy.top/t/topic/iqa3 著作权归作者所有。请勿转载和采集!