Forráskód Böngészése

添加根据企业分页查询用户信息操作

huxz 8 éve
szülő
commit
94fb222ed4

+ 4 - 16
sso-manage-console/src/main/java/com/uas/sso/sso/backend/api/UserManageController.java

@@ -1,15 +1,12 @@
 package com.uas.sso.sso.backend.api;
 
 import com.uas.sso.entity.User;
-import com.uas.sso.entity.Userspace;
 import com.uas.sso.sso.backend.dto.UpdateUserInfo;
 import com.uas.sso.sso.backend.service.UserService;
 import com.uas.sso.sso.backend.support.ResultBean;
-import java.util.Set;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Pageable;
 import org.springframework.http.MediaType;
-import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -39,24 +36,15 @@ public class UserManageController {
             @RequestParam(required = false) Short mobileValidCode,
             @RequestParam(required = false) String key,
             @RequestParam(required = false) String keyword) {
-
-        return new ResultBean<>(userService.showUserByPagination(page, fromApp, mobileValidCode, key, keyword));
-    }
-
-    @RequestMapping(method = RequestMethod.GET, path = "//showUserByPaginationOld",
-            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public ResultBean<org.springframework.data.domain.Page<User>> showUserByPaginationOld(Pageable page,
-            @RequestParam(required = false) String name,
-            @RequestParam(required = false) String phone) {
         // Controller中的Pageable类型参数默认根据查询参数 page 和 size 注入并实例化
-        return new ResultBean<>(userService.showUserByPaginationOld(page, name, phone));
+        return new ResultBean<>(userService.showUserByPagination(page, fromApp, mobileValidCode, key, keyword));
     }
 
-    @RequestMapping(method = RequestMethod.GET, path = "/{userUU}/findSpacesByUser",
+    @RequestMapping(method = RequestMethod.GET, path = "//showEnUserByPagination",
             produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public ResultBean<Set<Userspace>> findSpacesByUser(@PathVariable("userUU") Long userUu) {
+    public ResultBean<org.springframework.data.domain.Page<User>> showEnUserByPagination(Pageable page, Long spaceUU) {
 
-        return new ResultBean<>(userService.findSpacesByUser(userUu));
+        return new ResultBean<>(userService.showEnUserByPagination(page, spaceUU));
     }
 
     @RequestMapping(method = RequestMethod.PUT, path = "//modifyUserInfo",

+ 27 - 9
sso-manage-console/src/main/java/com/uas/sso/sso/backend/service/UserService.java

@@ -1,10 +1,8 @@
 package com.uas.sso.sso.backend.service;
 
 import com.uas.sso.entity.User;
-import com.uas.sso.entity.Userspace;
 import com.uas.sso.sso.backend.dto.UpdateUserInfo;
-import java.util.List;
-import java.util.Set;
+import javax.validation.constraints.NotNull;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 
@@ -15,14 +13,34 @@ import org.springframework.data.domain.Pageable;
  */
 public interface UserService {
 
-    List<User> showAllUsers();
-
-    Page<User> showUserByPaginationOld(Pageable page, String name, String phone);
-
-    Set<Userspace> findSpacesByUser(Long userUu);
-
+    /**
+     * 分页获取用户信息.
+     *
+     * @param page  分页参数
+     * @param fromApp   注册来源
+     * @param mobileValidCode   收集认证状态
+     * @param key   搜索字段
+     * @param keyword   搜索关键字
+     * @return  获取用户分页数据
+     */
     Page<User> showUserByPagination(Pageable page, String fromApp, Short mobileValidCode, String key,
             String keyword);
 
+    /**
+     * 分页获取企业用户信息.
+     *
+     * @param page  分页参数
+     * @param spaceUU   企业UU
+     * @return  获取后的分页数据
+     */
+    Page<User> showEnUserByPagination(@NotNull Pageable page, @NotNull Long spaceUU);
+
+    /**
+     * 管理员收到客户时,后台修改用户的信息.
+     *
+     * @param userInfo  用户信息
+     * @return  操作状态
+     */
     Boolean modifyUserInfo(UpdateUserInfo userInfo);
+
 }

+ 55 - 70
sso-manage-console/src/main/java/com/uas/sso/sso/backend/service/impl/UserServiceImpl.java

@@ -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);
     }