教练分页查询接口 - API 文档
<p>@GetMapping("/queryCoachByPageByParam")
public Result<Object> queryCoachByPageByParam(QueryRequest queryRequest, Admin admin,
String text, String drivingProjectType,
String workStatus, HttpServletRequest request) {</p>
<pre><code>//权限验证
String token = (String) request.getAttribute("claims_admin");
if (token == null || "".equals(token)) {
throw new RuntimeException("权限不足!");
}
Map<String, Object> map = new HashMap<>();
//查出当前管理员
Admin admin1 = adminService.queryOneAdminByID(admin.getAdminId());
//1.当前管理员为总管理
if (admin1.getTotalAdminId() != null && admin1.getTotalAdminId() == 0) {
//找到当前总管理名下所有管理员
List<Admin> admins = adminService.queryAdminsByTotalAdminId(admin.getAdminId());
//(1)通过姓名和电话查询-总管理
if (!StringUtils.isEmpty(text)) {
List<CoachDto> coachDtoListByText = new ArrayList<>();
List<Coach> coachList = new ArrayList<>();
for (Admin admin2 : admins) {
coachList.addAll(adminService.queryAllCoachByAdminId(admin2.getAdminId()));
}
for (Coach coach : coachList) {
if (coach.getRealName() != null) {
if (coach.getRealName().contains(text) || coach.getCoachPhone().contains(text)) {
coachDtoListByText.add(createCoachDto(coach));
}
}
}
return getPageResult(coachDtoListByText, queryRequest, map);
}
//(2)查全部教练-总管理
List<CoachDto> coachDtoListByAll = new ArrayList<>();
List<Coach> coaches = new ArrayList<>();
for (Admin admin2 : admins) {
coaches.addAll(adminService.queryAllCoachByAdminId(admin2.getAdminId()));
}
for (Coach coach : coaches) {
coachDtoListByAll.add(createCoachDto(coach));
}
return getPageResult(coachDtoListByAll, queryRequest, map);
}
//2.管理员不是总管理
//查询当前管理员所有教练
List<Coach> coaches = adminService.queryAllCoachByAdminId(admin.getAdminId());
//(1)通过姓名和电话查询-普通管理员
if (!StringUtils.isEmpty(text)) {
List<CoachDto> coachDtoList = new ArrayList<>();
for (Coach coach : coaches) {
if (coach.getRealName() != null) {
if (coach.getRealName().contains(text) || coach.getCoachPhone().contains(text)) {
coachDtoList.add(createCoachDto(coach));
}
}
}
return getPageResult(coachDtoList, queryRequest, map);
}
//(2)查询所有教练-普通管理员
List<CoachDto> coachDtoList = new ArrayList<>();
for (Coach coach : coaches) {
coachDtoList.add(createCoachDto(coach));
}
return getPageResult(coachDtoList, queryRequest, map);
</code></pre>
<p>}</p>
<p>private CoachDto createCoachDto(Coach coach) {
CoachDto coachDto = new CoachDto();
BeanUtils.copyProperties(coach, coachDto);
// 查询当前教练课程
List<CourseDto> courseDtoList = courseInfoService.queryCourseByCoachId(coach.getCoachId(), RcqtUtils.getDay());
boolean isWorking = courseDtoList != null && courseDtoList.stream()
.anyMatch(c -> {
LocalTime currentTime = LocalTime.now();
return currentTime.isAfter(c.getStartReservationTime().toLocalTime()) && currentTime.isBefore(c.getEndReservationTime().toLocalTime());
});
coachDto.setWorkStatus(isWorking);
return coachDto;
}</p>
<p>private Result<Object> getPageResult(List<CoachDto> coachDtoList, QueryRequest queryRequest, Map<String, Object> map) {
//按照姓名与工作状态排序
Comparator<CoachDto> comparator = Comparator.comparing(CoachDto::getWorkStatus, Comparator.reverseOrder())
.thenComparing((c1, c2) -> Collator.getInstance(Locale.CHINESE).compare(c1.getRealName(), c2.getRealName()));
coachDtoList.sort(comparator);</p>
<pre><code>//进行分页
List<CoachDto> coachDtoListByPage = coachDtoList.stream()
.skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
.limit(queryRequest.getPageSize())
.collect(Collectors.toList());
map.put("coaches", coachDtoListByPage);
map.put("total", coachDtoList.size());
return new Result<>(ResultCode.SUCCESS, map);
</code></pre>
<p>}</p>
原文地址: https://www.cveoy.top/t/topic/qhJ5 著作权归作者所有。请勿转载和采集!