自定义分页核心:
new PageImpl<MasterUserAuth>(result, usersPage.getPageable(), usersPage.getTotalElements());
@JsonView(TreeWithCheckedView.class)
@PostMapping("/authUser/users")
@ApiOperation(value = "加载员工信息", notes = "加载员工信息")
public ResponseEntity<Page<MasterUserAuth>> findAuthUser(
@RequestParam(required = false) String name,
@RequestParam(required = false) String code,
@RequestBody COPUserBean userBean,
@RequestParam String pageNum,
@RequestParam String pageSize) {
List<MasterUser> selectedUsers = new ArrayList<>();
if ("MasterCompany".equals(userBean.getOrgType())) {
MasterCompany company = companyService.findByIdAndDeletedFalse(userBean.getOrgId());
if (company == null) {
throw new ControllerException("所属公司没有找到");
}
// 获取已选用户
List<MasterCompanyUser> selectedCompanyUsers = companyUserService.findByCompany(company);
selectedUsers = selectedCompanyUsers.stream()
.map(MasterCompanyUser::getUser)
.collect(Collectors.toList());
} else if ("MasterOrganization".equals(userBean.getOrgType())) {
MasterOrganization organization = masterOrganizationService.findByIdAndDeletedFalse(userBean.getOrgId());
if (organization == null) {
throw new ControllerException("所属机构没有找到");
}
// 获取已选用户
List<MasterOrganizationUser> selectedOrganizationUser = masterOrganizationUserService.findByOrg(organization);
selectedUsers = selectedOrganizationUser.stream()
.map(MasterOrganizationUser::getUser)
.collect(Collectors.toList());
}
// 获取所有用户
List<SearchFilter> userFilter = new ArrayList<>();
userFilter.add(SearchFilter.equal("deleted", Boolean.FALSE));
userFilter.add(SearchFilter.contains("userName", name));
userFilter.add(SearchFilter.contains("userCode", code));
Specification<MasterUser> spec = buildFilterRequest(userFilter);
int pn = Integer.parseInt(pageNum);
int ps = Integer.parseInt(pageSize);
PageRequest pageRequest = PageRequest.of(pn - 1, ps);
Page<MasterUser> usersPage = masterUserService.findAll(spec, pageRequest);
List<MasterUser> users=usersPage.getContent();
List<MasterUserAuth> result=setCheckedUser(selectedUsers, users);
Page<MasterUserAuth> resultPage=new PageImpl<MasterUserAuth>(result, usersPage.getPageable(), usersPage.getTotalElements());
return ResponseEntity.ok(resultPage);
}
/**
* 设置已选的用户
*/
private List<MasterUserAuth> setCheckedUser(List<MasterUser> selectedUsers, List<MasterUser> users) {
List<MasterUserAuth> result = new ArrayList<>();
for (MasterUser user : users) {
MasterUserAuth ua = new MasterUserAuth();
ua.setUser(user);
if (selectedUsers.contains(user)) {
ua.setChecked(true);
}
result.add(ua);
}
return result;
}