|
|
@@ -11,11 +11,14 @@ import com.uas.sso.sso.backend.service.UserService;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
@@ -36,82 +39,64 @@ public class UserServiceImpl implements UserService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<User> showAllUsers() {
|
|
|
- List<User> all = userDao.findAll();
|
|
|
-
|
|
|
- if (CollectionUtils.isEmpty(all)) {
|
|
|
- all = Collections.emptyList();
|
|
|
- }
|
|
|
- return all;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Page<User> showUserByPaginationOld(final Pageable page, String name, String phone) {
|
|
|
-
|
|
|
- return userDao.findAll((root, query, builder) -> {
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
- if (!StringUtils.isEmpty(name)) {
|
|
|
- Predicate predicate = builder.like(root.get("realName"), "%" + name + "%");
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
+ public Page<User> showUserByPagination(Pageable page, String fromApp, Short mobileValidCode,
|
|
|
+ String key, String keyword) {
|
|
|
|
|
|
- if (!StringUtils.isEmpty(phone)) {
|
|
|
- Predicate predicate = builder.like(root.get("mobile"), phone + "%");
|
|
|
- predicates.add(predicate);
|
|
|
+ return userDao.findAll(new Specification<User>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query,
|
|
|
+ CriteriaBuilder builder) {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
+ if (!StringUtils.isEmpty(fromApp)) {
|
|
|
+ Predicate predicate = builder.equal(root.get("fromApp"), fromApp);
|
|
|
+ predicates.add(predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(mobileValidCode)) {
|
|
|
+ Predicate predicate = builder.equal(root.get("mobileValidCode"), mobileValidCode);
|
|
|
+ predicates.add(predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(keyword)) {
|
|
|
+ Predicate predicate = builder.like(root.get(key), "%" + keyword + "%");
|
|
|
+ predicates.add(predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ predicates.removeAll(Collections.singletonList(null));
|
|
|
+
|
|
|
+ Predicate[] array = new Predicate[predicates.size()];
|
|
|
+ predicates.toArray(array);
|
|
|
+ Predicate predicate = builder.and(array);
|
|
|
+ query.where(predicate);
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
- predicates.removeAll(Collections.singletonList(null));
|
|
|
-
|
|
|
- Predicate[] array = new Predicate[predicates.size()];
|
|
|
- predicates.toArray(array);
|
|
|
- Predicate predicate = builder.and(array);
|
|
|
- query.where(predicate);
|
|
|
- return null;
|
|
|
}, page);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Set<Userspace> findSpacesByUser(Long userUu) {
|
|
|
- if (userUu == null) {
|
|
|
- throw new ValidationFailedException("用户UU不能为空");
|
|
|
- }
|
|
|
-
|
|
|
- final User user = userDao.findOne(userUu);
|
|
|
- if (user == null) {
|
|
|
- throw new ValidationFailedException(String.format("UU %d 对应用户不存在", userUu));
|
|
|
- }
|
|
|
-
|
|
|
- return user.getUserSpaces();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Page<User> showUserByPagination(Pageable page, String fromApp, Short mobileValidCode,
|
|
|
- String key, String keyword) {
|
|
|
-
|
|
|
- return userDao.findAll((root, query, builder) -> {
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
- if (!StringUtils.isEmpty(fromApp)) {
|
|
|
- Predicate predicate = builder.equal(root.get("fromApp"), fromApp);
|
|
|
- predicates.add(predicate);
|
|
|
+ public Page<User> showEnUserByPagination(Pageable page, Long spaceUU) {
|
|
|
+
|
|
|
+ return userDao.findAll(new Specification<User>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query,
|
|
|
+ CriteriaBuilder builder) {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
+ if (spaceUU != null) {
|
|
|
+ Userspace space = new Userspace();
|
|
|
+ space.setSpaceUU(spaceUU);
|
|
|
+ Predicate predicate = builder.isMember(space, root.get("userSpaces"));
|
|
|
+ predicates.add(predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除无效条件表达式
|
|
|
+ predicates.removeAll(Collections.singletonList(null));
|
|
|
+
|
|
|
+ Predicate[] array = new Predicate[predicates.size()];
|
|
|
+ predicates.toArray(array);
|
|
|
+ Predicate predicate = builder.and(array);
|
|
|
+ query.where(predicate);
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
- if (!StringUtils.isEmpty(mobileValidCode)) {
|
|
|
- Predicate predicate = builder.equal(root.get("mobileValidCode"), mobileValidCode);
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
-
|
|
|
- if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(keyword)) {
|
|
|
- Predicate predicate = builder.like(root.get(key), "%" + keyword + "%");
|
|
|
- predicates.add(predicate);
|
|
|
- }
|
|
|
-
|
|
|
- predicates.removeAll(Collections.singletonList(null));
|
|
|
-
|
|
|
- Predicate[] array = new Predicate[predicates.size()];
|
|
|
- predicates.toArray(array);
|
|
|
- Predicate predicate = builder.and(array);
|
|
|
- query.where(predicate);
|
|
|
- return null;
|
|
|
}, page);
|
|
|
}
|
|
|
|