<p>@GetMapping(&quot;/queryCoachByPageByParam&quot;)
public Result<Object> queryCoachByPageByParam(QueryRequest queryRequest, Admin admin,
String text, String drivingProjectType,
String workStatus, HttpServletRequest request) {</p>
<pre><code>//权限验证
String token = (String) request.getAttribute(&quot;claims_admin&quot;);

if (token == null || &quot;&quot;.equals(token)) {
    throw new RuntimeException(&quot;权限不足!&quot;);
}

Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();

//查出当前管理员
Admin admin1 = adminService.queryOneAdminByID(admin.getAdminId());


//1.当前管理员为总管理
if (admin1.getTotalAdminId() != null &amp;&amp; admin1.getTotalAdminId() == 0) {
    //找到当前总管理名下所有管理员
    List&lt;Admin&gt; admins = adminService.queryAdminsByTotalAdminId(admin.getAdminId());

    //(1)通过姓名和电话查询-总管理
    if (!StringUtils.isEmpty(text)) {
        List&lt;Coach&gt; coaches = new ArrayList&lt;&gt;();
        for (Admin admin2 : admins) {
            coaches.addAll(adminService.queryAllCoachByAdminId(admin2.getAdminId()));
        }

        List&lt;CoachDto&gt; coachDtoListByText = new ArrayList&lt;&gt;();
        List&lt;Coach&gt; coachList = new ArrayList&lt;&gt;();
        for (Coach coach : coaches) {
            if (coach.getRealName() != null) {
                if (coach.getRealName().contains(text) || coach.getCoachPhone().contains(text)) {
                    coachList.add(coach);
                }
            }
        }

        if (!CollectionUtils.isEmpty(coachList)) {
            List&lt;Long&gt; coachIds = coachList.stream()
                    .map(Coach::getCoachId)
                    .collect(Collectors.toList());
            Map&lt;Long, Coach&gt; coachMap = coaches.stream().collect(Collectors.toMap(Coach::getCoachId, Function.identity()));

            // 查询当前教练课程
            Map&lt;Long, List&lt;CourseDto&gt;&gt; courseDtoMap = courseInfoService.queryCourseByCoachIds(coachIds, RcqtUtils.getDay());

            coachDtoListByText = coachIds.stream()
                    .map(coachId -&gt; {
                        Coach coach = coachMap.get(coachId);

                        if (coach != null) {
                            CoachDto coachDto = new CoachDto();
                            BeanUtils.copyProperties(coach, coachDto);

                            // 判断教练是否在工作中
                            List&lt;CourseDto&gt; courseDtoList = courseDtoMap.get(coachId);
                            boolean isWorking = courseDtoList != null &amp;&amp; courseDtoList.stream()
                                    .anyMatch(c -&gt; {
                                        LocalTime currentTime = LocalTime.now();
                                        return currentTime.isAfter(c.getStartReservationTime().toLocalTime()) &amp;&amp; currentTime.isBefore(c.getEndReservationTime().toLocalTime());
                                    });
                            coachDto.setWorkStatus(isWorking);
                            return coachDto;
                        }
                        return null;
                    })
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());

        }

        //按照姓名与工作状态排序
        Comparator&lt;CoachDto&gt; comparator = Comparator.comparing(CoachDto::getWorkStatus, Comparator.reverseOrder())
                .thenComparing((c1, c2) -&gt; Collator.getInstance(Locale.CHINESE).compare(c1.getRealName(), c2.getRealName()));
        coachDtoListByText.sort(comparator);

        //进行分页
        List&lt;CoachDto&gt; coachDtoListByPage = coachDtoListByText.stream()
                .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
                .limit(queryRequest.getPageSize())
                .collect(Collectors.toList());

        map.put(&quot;coaches&quot;, coachDtoListByPage);
        map.put(&quot;total&quot;, coachDtoListByText.size());

        return new Result&lt;&gt;(ResultCode.SUCCESS, map);
    }

    //(2)查全部教练-总管理
    List&lt;Coach&gt; coaches = new ArrayList&lt;&gt;();
    for (Admin admin2 : admins) {
        coaches.addAll(adminService.queryAllCoachByAdminId(admin2.getAdminId()));
    }

    List&lt;CoachDto&gt; coachDtoListByAll = new ArrayList&lt;&gt;();
    if (!CollectionUtils.isEmpty(coaches)) {
        List&lt;Long&gt; coachIds = coaches.stream()
                .map(Coach::getCoachId)
                .collect(Collectors.toList());
        Map&lt;Long, Coach&gt; coachMap = coaches.stream().collect(Collectors.toMap(Coach::getCoachId, Function.identity()));
        // 查询当前教练课程
        Map&lt;Long, List&lt;CourseDto&gt;&gt; courseDtoMap = courseInfoService.queryCourseByCoachIds(coachIds, RcqtUtils.getDay());

        coachDtoListByAll = coachIds.stream()
                .map(coachId -&gt; {
                    Coach coach = coachMap.get(coachId);

                    if (coach != null) {
                        CoachDto coachDto = new CoachDto();
                        BeanUtils.copyProperties(coach, coachDto);

                        // 判断教练是否在工作中
                        List&lt;CourseDto&gt; courseDtoList = courseDtoMap.get(coachId);
                        boolean isWorking = courseDtoList != null &amp;&amp; courseDtoList.stream()
                                .anyMatch(c -&gt; {
                                    LocalTime currentTime = LocalTime.now();
                                    return currentTime.isAfter(c.getStartReservationTime().toLocalTime()) &amp;&amp; currentTime.isBefore(c.getEndReservationTime().toLocalTime());
                                });
                        coachDto.setWorkStatus(isWorking);
                        return coachDto;
                    }
                    return null;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
    
    //按照姓名与工作状态排序
    Comparator&lt;CoachDto&gt; comparator = Comparator.comparing(CoachDto::getWorkStatus, Comparator.reverseOrder())
            .thenComparing((c1, c2) -&gt; Collator.getInstance(Locale.CHINESE).compare(c1.getRealName(), c2.getRealName()));
    coachDtoListByAll.sort(comparator);


    //(3)通过科目和工作状态-总管理
    if (!StringUtils.isEmpty(workStatus) &amp;&amp; !StringUtils.isEmpty(drivingProjectType)) {
        //前端传递工作状态为0、1
        Boolean workStatusB = Integer.parseInt(workStatus) == 1;
        List&lt;CoachDto&gt; filteredList = coachDtoListByAll.stream()
                .filter(coachDto -&gt; coachDto.getWorkStatus() == workStatusB &amp;&amp; coachDto.getDrivingProjectType() != null &amp;&amp; coachDto.getDrivingProjectType().contains(drivingProjectType))
                .toList();
        //分页查询
        List&lt;CoachDto&gt; coachDtos = filteredList.stream()
                .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
                .limit(queryRequest.getPageSize())
                .collect(Collectors.toList());

        map.put(&quot;coachList&quot;, coachDtos);
        map.put(&quot;total&quot;, filteredList.size());
        return new Result&lt;&gt;(ResultCode.SUCCESS, map);

    } else if (StringUtils.isEmpty(workStatus) &amp;&amp; !StringUtils.isEmpty(drivingProjectType)) {
        List&lt;CoachDto&gt; filteredList = coachDtoListByAll.stream()
                .filter(coachDto -&gt; coachDto.getDrivingProjectType() != null &amp;&amp; coachDto.getDrivingProjectType().contains(drivingProjectType))
                .toList();
        //分页查询
        List&lt;CoachDto&gt; coachDtos = filteredList.stream()
                .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
                .limit(queryRequest.getPageSize())
                .collect(Collectors.toList());

        map.put(&quot;coachList&quot;, coachDtos);
        map.put(&quot;total&quot;, filteredList.size());
        return new Result&lt;&gt;(ResultCode.SUCCESS, map);
    } else if (!StringUtils.isEmpty(workStatus) &amp;&amp; StringUtils.isEmpty(drivingProjectType)) {
        //前端传递工作状态为0、1
        Boolean workStatusB = Integer.parseInt(workStatus) == 1;
        List&lt;CoachDto&gt; filteredList = coachDtoListByAll.stream()
                .filter(coachDto -&gt; coachDto.getWorkStatus() == workStatusB)
                .toList();
        //分页查询
        List&lt;CoachDto&gt; coachDtos = filteredList.stream()
                .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
                .limit(queryRequest.getPageSize())
                .collect(Collectors.toList());

        map.put(&quot;coachList&quot;, coachDtos);
        map.put(&quot;total&quot;, filteredList.size());
        return new Result&lt;&gt;(ResultCode.SUCCESS, map);
    }

    //进行分页
    List&lt;CoachDto&gt; coachDtoListByPage = coachDtoListByAll.stream()
            .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
            .limit(queryRequest.getPageSize())
            .collect(Collectors.toList());

    map.put(&quot;coaches&quot;, coachDtoListByPage);
    map.put(&quot;total&quot;, coachDtoListByAll.size());

    return new Result&lt;&gt;(ResultCode.SUCCESS, map);
}

//2.管理员不是总管理
//查询当前管理员所有教练
List&lt;Coach&gt; coaches = adminService.queryAllCoachByAdminId(admin.getAdminId());


//(1)通过姓名和电话查询-普通管理员
if (!StringUtils.isEmpty(text)) {
    //存放所有管理员名下教练
    List&lt;CoachDto&gt; coachDtoList = new ArrayList&lt;&gt;();
    List&lt;Coach&gt; coachList = new ArrayList&lt;&gt;();
    for (Coach coach : coaches) {
        if (coach.getRealName() != null) {
            if (coach.getRealName().contains(text) || coach.getCoachPhone().contains(text)) {
                coachList.add(coach);
            }
        }
    }

    if (!CollectionUtils.isEmpty(coachList)) {
        List&lt;Long&gt; coachIds = coachList.stream()
                .map(Coach::getCoachId)
                .collect(Collectors.toList());
        Map&lt;Long, Coach&gt; coachMap = coaches.stream().collect(Collectors.toMap(Coach::getCoachId, Function.identity()));

        // 查询当前教练课程
        Map&lt;Long, List&lt;CourseDto&gt;&gt; courseDtoMap = courseInfoService.queryCourseByCoachIds(coachIds, RcqtUtils.getDay());

        coachDtoList = coachIds.stream()
                .map(coachId -&gt; {
                    Coach coach = coachMap.get(coachId);

                    if (coach != null) {
                        CoachDto coachDto = new CoachDto();
                        BeanUtils.copyProperties(coach, coachDto);

                        // 判断教练是否在工作中
                        List&lt;CourseDto&gt; courseDtoList = courseDtoMap.get(coachId);
                        boolean isWorking = courseDtoList != null &amp;&amp; courseDtoList.stream()
                                .anyMatch(c -&gt; {
                                    LocalTime currentTime = LocalTime.now();
                                    return currentTime.isAfter(c.getStartReservationTime().toLocalTime()) &amp;&amp; currentTime.isBefore(c.getEndReservationTime().toLocalTime());
                                });
                        coachDto.setWorkStatus(isWorking);
                        return coachDto;
                    }
                    return null;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

    }

    //按照姓名与工作状态排序
    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);

    //进行分页
    List&lt;CoachDto&gt; coachDtoListByPage = coachDtoList.stream()
            .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
            .limit(queryRequest.getPageSize())
            .collect(Collectors.toList());

    map.put(&quot;coaches&quot;, coachDtoListByPage);
    map.put(&quot;total&quot;, coachDtoList.size());

    return new Result&lt;&gt;(ResultCode.SUCCESS, map);
}

//(2)查询所有教练-普通管理员
List&lt;CoachDto&gt; coachDtoList = new ArrayList&lt;&gt;();
List&lt;Long&gt; coachIds = coaches.stream()
        .map(Coach::getCoachId)
        .collect(Collectors.toList());
Map&lt;Long, Coach&gt; coachMap = coaches.stream().collect(Collectors.toMap(Coach::getCoachId, Function.identity()));

if (!CollectionUtils.isEmpty(coaches)) {
    // 查询当前教练课程
    Map&lt;Long, List&lt;CourseDto&gt;&gt; courseDtoMap = courseInfoService.queryCourseByCoachIds(coachIds, RcqtUtils.getDay());

    coachDtoList = coachIds.stream()
            .map(coachId -&gt; {
                Coach coach = coachMap.get(coachId);

                if (coach != null) {
                    CoachDto coachDto = new CoachDto();
                    BeanUtils.copyProperties(coach, coachDto);

                    // 判断教练是否在工作中
                    List&lt;CourseDto&gt; courseDtoList = courseDtoMap.get(coachId);
                    boolean isWorking = courseDtoList != null &amp;&amp; courseDtoList.stream()
                            .anyMatch(c -&gt; {
                                LocalTime currentTime = LocalTime.now();
                                return currentTime.isAfter(c.getStartReservationTime().toLocalTime()) &amp;&amp; currentTime.isBefore(c.getEndReservationTime().toLocalTime());
                            });
                    coachDto.setWorkStatus(isWorking);
                    return coachDto;
                }
                return null;
            })
            .filter(Objects::nonNull)
            .collect(Collectors.toList());

    //按照姓名与工作状态排序
    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);
}


//(3)通过科目和工作状态-普通管理员
if (!StringUtils.isEmpty(workStatus) &amp;&amp; !StringUtils.isEmpty(drivingProjectType)) {
    //前端传递工作状态为0、1
    Boolean workStatusB = Integer.parseInt(workStatus) == 1;
    List&lt;CoachDto&gt; filteredList = coachDtoList.stream()
            .filter(coachDto -&gt; coachDto.getWorkStatus() == workStatusB &amp;&amp; coachDto.getDrivingProjectType() != null &amp;&amp; coachDto.getDrivingProjectType().contains(drivingProjectType))
            .toList();
    //分页查询
    List&lt;CoachDto&gt; coachDtos = filteredList.stream()
            .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
            .limit(queryRequest.getPageSize())
            .collect(Collectors.toList());

    map.put(&quot;coachList&quot;, coachDtos);
    map.put(&quot;total&quot;, filteredList.size());
    return new Result&lt;&gt;(ResultCode.SUCCESS, map);

} else if (StringUtils.isEmpty(workStatus) &amp;&amp; !StringUtils.isEmpty(drivingProjectType)) {
    List&lt;CoachDto&gt; filteredList = coachDtoList.stream()
            .filter(coachDto -&gt; coachDto.getDrivingProjectType() != null &amp;&amp; coachDto.getDrivingProjectType().contains(drivingProjectType))
            .toList();
    //分页查询
    List&lt;CoachDto&gt; coachDtos = filteredList.stream()
            .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
            .limit(queryRequest.getPageSize())
            .collect(Collectors.toList());

    map.put(&quot;coachList&quot;, coachDtos);
    map.put(&quot;total&quot;, filteredList.size());
    return new Result&lt;&gt;(ResultCode.SUCCESS, map);
} else if (!StringUtils.isEmpty(workStatus) &amp;&amp; StringUtils.isEmpty(drivingProjectType)) {
    //前端传递工作状态为0、1
    Boolean workStatusB = Integer.parseInt(workStatus) == 1;
    List&lt;CoachDto&gt; filteredList = coachDtoList.stream()
            .filter(coachDto -&gt; coachDto.getWorkStatus() == workStatusB)
            .toList();
    //分页查询
    List&lt;CoachDto&gt; coachDtos = filteredList.stream()
            .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
            .limit(queryRequest.getPageSize())
            .collect(Collectors.toList());

    map.put(&quot;coachList&quot;, coachDtos);
    map.put(&quot;total&quot;, filteredList.size());
    return new Result&lt;&gt;(ResultCode.SUCCESS, map);
}

//进行分页
List&lt;CoachDto&gt; coachDtoListByPage = coachDtoList.stream()
        .skip((long) (queryRequest.getPageNum() - 1) * queryRequest.getPageSize())
        .limit(queryRequest.getPageSize())
        .collect(Collectors.toList());

map.put(&quot;coaches&quot;, coachDtoListByPage);
map.put(&quot;total&quot;, coachDtoList.size());

return new Result&lt;&gt;(ResultCode.SUCCESS, map);
</code></pre>
<p>}</p>
查询教练分页列表-参数查询

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

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