分页查询教练信息并优化查询速度
public\u0020Map\u003CString,Object\u003E\u0020queryCoachByPageByParam(QueryRequest\u0020queryRequest,\u0020Admin\u0020admin)\u0020{\n\u0020\u0020Map\u003CString,Object\u003E\u0020map\u0020=\u0020new\u0020HashMap\u003C\u003E();\n\u0020\u0020Admin\u0020admin1\u0020=\u0020adminMapper.selectById(admin.getAdminId());\n\n\u0020\u0020//总管理\n\u0020\u0020if\u0020(admin1.getTotalAdminId()\u0020!=\u0020null\u0020&&\u0020admin1.getTotalAdminId()\u0020==\u00200)\u0020{\n\u0020\u0020\u0020\u0020QueryWrapper\u003CAdmin\u003E\u0020queryWrapper\u0020=\u0020new\u0020QueryWrapper\u003C\u003E();\n\u0020\u0020\u0020\u0020queryWrapper.eq("total_admin_id",\u0020admin.getAdminId());\n\u0020\u0020\u0020\u0020List\u003CAdmin\u003E\u0020admins\u0020=\u0020adminMapper.selectList(queryWrapper);\n\u0020\u0020\u0020\u0020List\u003CCoachDto\u003E\u0020coachList\u0020=\u0020new\u0020ArrayList\u003C\u003E();\n\n\u0020\u0020\u0020\u0020for\u0020(Admin\u0020admin2\u0020:\u0020admins)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020List\u003CCoach\u003E\u0020coaches\u0020=\u0020queryAllCoachByAdminId(admin2.getAdminId());\n\n\u0020\u0020\u0020\u0020\u0020\u0020if\u0020(!CollectionUtils.isEmpty(coaches))\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020Date\u0020date\u0020=\u0020RcqtUtils.getDay();\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020List\u003CCompletableFuture\u003CList\u003CCourseDto\u003E\u003E\u003E\u0020courseDtoFutures\u0020=\u0020new\u0020ArrayList\u003C\u003E();\n\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020for\u0020(Coach\u0020coach\u0020:\u0020coaches)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020CompletableFuture\u003CList\u003CCourseDto\u003E\u003E\u0020courseDtoFuture\u0020=\u0020CompletableFuture.supplyAsync(()\u0020-\u003E\u0020courseInfoService.queryCourseByCoachId(coach.getCoachId(),\u0020date));\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020courseDtoFutures.add(courseDtoFuture);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020}\n\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020CompletableFuture\u003CVoid\u003E\u0020allCourseDtoFutures\u0020=\u0020CompletableFuture.allOf(courseDtoFutures.toArray(new\u0020CompletableFuture[0]));\n\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020try\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020allCourseDtoFutures.get();\n\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020for\u0020(int\u0020i\u0020=\u00200;\u0020i\u0020\u003C\u0020coaches.size();\u0020i++)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020Coach\u0020coach\u0020=\u0020coaches.get(i);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020List\u003CCourseDto\u003E\u0020courseDtoList\u0020=\u0020courseDtoFutures.get(i).join();\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020boolean\u0020isWorking\u0020=\u0020courseDtoList.stream()\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020.anyMatch(c\u0020-\u003E\u0020LocalTime.now().isAfter(c.getStartReservationTime().toLocalTime())\u0020&&\u0020LocalTime.now().isBefore(c.getEndReservationTime().toLocalTime()));\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020CoachDto\u0020coachDto\u0020=\u0020new\u0020CoachDto();\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020BeanUtils.copyProperties(coach,\u0020coachDto);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachDto.setWorkStatus(isWorking);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachDto.setAdminRealName(admin2.getRealName());\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachDto.setAdminPhone(admin2.getAdminPhone());\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachDto.setCarCount(queryAllDeviceEntitiesByCoachIdByCount(coach.getCoachId()));\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachList.add(coachDto);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020} catch (InterruptedException | ExecutionException e) {\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020// Handle exception\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020}\n\n\u0020\u0020\u0020\u0020Comparator\u003CCoachDto\u003E\u0020comparator\u0020=\u0020Comparator.comparing(CoachDto::getWorkStatus,\u0020Comparator.reverseOrder())\n\u0020\u0020\u0020\u0020\u0020\u0020.thenComparing((c1,\u0020c2)\u0020-\u003E\u0020Collator.getInstance(Locale.CHINESE).compare(c1.getRealName(),\u0020c2.getRealName()));\n\n\u0020\u0020\u0020\u0020coachList.sort(comparator);\n\n\u0020\u0020\u0020\u0020int\u0020total\u0020=\u0020coachList.size();\n\u0020\u0020\u0020\u0020int\u0020startIndex\u0020=\u0020(queryRequest.getPageNum()\u0020-\u00201)\u0020*\u0020queryRequest.getPageSize();\n\u0020\u0020\u0020\u0020int\u0020endIndex\u0020=\u0020Math.min(startIndex\u0020+\u0020queryRequest.getPageSize(),\u0020total);\n\u0020\u0020\u0020\u0020List\u003CCoachDto\u003E\u0020coaches\u0020=\u0020coachList.subList(startIndex,\u0020endIndex);\n\n\u0020\u0020\u0020\u0020map.put("coaches",\u0020coaches);\n\u0020\u0020\u0020\u0020map.put("total",\u0020total);\n\n\u0020\u0020\u0020\u0020return\u0020map;\n\u0020\u0020}\n\n\u0020\u0020//1.查询条件\n\u0020\u0020QueryWrapper\u003CCoach\u003E\u0020queryWrapper\u0020=\u0020new\u0020QueryWrapper\u003C\u003E();\n\u0020\u0020queryWrapper.eq("admin_id",\u0020admin.getAdminId());\n\n\u0020\u0020List\u003CCoach\u003E\u0020coaches\u0020=\u0020coachMapper.selectList(queryWrapper);\n\u0020\u0020List\u003CCoachDto\u003E\u0020coachList\u0020=\u0020new\u0020ArrayList\u003C\u003E();\n\n\u0020\u0020if\u0020(!CollectionUtils.isEmpty(coaches))\u0020{\n\u0020\u0020\u0020\u0020Date\u0020date\u0020=\u0020RcqtUtils.getDay();\n\u0020\u0020\u0020\u0020List\u003CCompletableFuture\u003CList\u003CCourseDto\u003E\u003E\u003E\u0020courseDtoFutures\u0020=\u0020new\u0020ArrayList\u003C\u003E();\n\n\u0020\u0020\u0020\u0020for\u0020(Coach\u0020coach\u0020:\u0020coaches)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020CompletableFuture\u003CList\u003CCourseDto\u003E\u003E\u0020courseDtoFuture\u0020=\u0020CompletableFuture.supplyAsync(()\u0020-\u003E\u0020courseInfoService.queryCourseByCoachId(coach.getCoachId(),\u0020date));\n\u0020\u0020\u0020\u0020\u0020\u0020courseDtoFutures.add(courseDtoFuture);\n\u0020\u0020\u0020\u0020}\n\n\u0020\u0020\u0020\u0020CompletableFuture\u003CVoid\u003E\u0020allCourseDtoFutures\u0020=\u0020CompletableFuture.allOf(courseDtoFutures.toArray(new\u0020CompletableFuture[0]));\n\n\u0020\u0020\u0020\u0020try\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020allCourseDtoFutures.get();\n\n\u0020\u0020\u0020\u0020\u0020\u0020for\u0020(int\u0020i\u0020=\u00200;\u0020i\u0020\u003C\u0020coaches.size();\u0020i++)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020Coach\u0020coach\u0020=\u0020coaches.get(i);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020List\u003CCourseDto\u003E\u0020courseDtoList\u0020=\u0020courseDtoFutures.get(i).join();\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020boolean\u0020isWorking\u0020=\u0020courseDtoList.stream()\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020.anyMatch(c\u0020-\u003E\u0020LocalTime.now().isAfter(c.getStartReservationTime().toLocalTime())\u0020&&\u0020LocalTime.now().isBefore(c.getEndReservationTime().toLocalTime()));\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020CoachDto\u0020coachDto\u0020=\u0020new\u0020CoachDto();\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020BeanUtils.copyProperties(coach,\u0020coachDto);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachDto.setWorkStatus(isWorking);\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachDto.setCarCount(queryAllDeviceEntitiesByCoachIdByCount(coach.getCoachId()));\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020coachList.add(coachDto);\n\u0020\u0020\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020} catch (InterruptedException | ExecutionException e) {\n\u0020\u0020\u0020\u0020\u0020\u0020// Handle exception\n\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020}\n\n\u0020\u0020Comparator\u003CCoachDto\u003E\u0020comparator\u0020=\u0020Comparator.comparing(CoachDto::getWorkStatus,\u0020Comparator.reverseOrder())\n\u0020\u0020\u0020\u0020.thenComparing((c1,\u0020c2)\u0020-\u003E\u0020Collator.getInstance(Locale.CHINESE).compare(c1.getRealName(),\u0020c2.getRealName()));\n\n\u0020\u0020coachList.sort(comparator);\n\u0020\u0020int\u0020total\u0020=\u0020coachList.size();\n\u0020\u0020int\u0020startIndex\u0020=\u0020(queryRequest.getPageNum()\u0020-\u00201)\u0020*\u0020queryRequest.getPageSize();\n\u0020\u0020int\u0020endIndex\u0020=\u0020Math.min(startIndex\u0020+\u0020queryRequest.getPageSize(),\u0020total);\n\u0020\u0020List\u003CCoachDto\u003E\u0020coachDtoList\u0020=\u0020coachList.subList(startIndex,\u0020endIndex);\n\n\u0020\u0020map.put("coaches",\u0020coachDtoList);\n\u0020\u0020map.put("total",\u0020total);\n\n\u0020\u0020return\u0020map;\n}\n
原文地址: http://www.cveoy.top/t/topic/qgmL 著作权归作者所有。请勿转载和采集!