查询学员绑定的教练接口 - /queryCoachesByUserID
<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) {
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)){
LocalDate currentDate = RcqtUtils.getDay().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
for (Coach coach : coaches){
//判断教练是否在工作中
//1.获取当前查询时间
LocalTime currentTime = LocalTime.now();
//2.查询当前教练课程
//判断教练的工作状态(true工作中/false休息中)
List<CourseDto> courseDtoList = courseInfoService.queryCourseByCoachId(coach.getCoachId(), currentDate);
boolean isWorking = courseDtoList.stream()
.anyMatch(c -> currentTime.isAfter(c.getStartReservationTime().toLocalTime()) && currentTime.isBefore(c.getEndReservationTime().toLocalTime()));
coach.setWorkStatus(isWorking);
}
boolean isUpdateSuccessful = coachService.updateBatchById(coaches);
if (!isUpdateSuccessful){
log.info(\"修改失败!\");
return new Result<>(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<String> 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 + \"年\");
}
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);
} else {
log.info(\"查不到此学员!\");
return new Result<>(ResultCode.FAIL);
}
</code></pre>
<p>}</p>
原文地址: https://www.cveoy.top/t/topic/p7Sn 著作权归作者所有。请勿转载和采集!