|
|
@@ -4,12 +4,10 @@ import cn.reghao.autodop.dmaster.rbac.model.po.UserAuthority;
|
|
|
import cn.reghao.autodop.dmaster.rbac.model.po.Role;
|
|
|
import cn.reghao.autodop.dmaster.rbac.model.po.User;
|
|
|
import cn.reghao.autodop.dmaster.rbac.model.vo.RoleVO;
|
|
|
-import cn.reghao.autodop.dmaster.rbac.db.repository.RoleRepository;
|
|
|
-import cn.reghao.autodop.dmaster.rbac.db.repository.UserRepository;
|
|
|
+import cn.reghao.util.db.BaseQuery;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import javax.persistence.criteria.SetJoin;
|
|
|
@@ -22,46 +20,11 @@ import java.util.stream.Collectors;
|
|
|
* @author reghao
|
|
|
* @date 2021-07-12 15:32:26
|
|
|
*/
|
|
|
-@Service
|
|
|
-public class RoleQuery {
|
|
|
- private final RoleRepository roleRepository;
|
|
|
- private final UserRepository userRepository;
|
|
|
-
|
|
|
- public RoleQuery(RoleRepository roleRepository, UserRepository userRepository) {
|
|
|
- this.roleRepository = roleRepository;
|
|
|
- this.userRepository = userRepository;
|
|
|
- }
|
|
|
-
|
|
|
- public Set<Role> findAll() {
|
|
|
- return new HashSet<>(roleRepository.findAll());
|
|
|
- }
|
|
|
-
|
|
|
- public Role findById(Integer id) {
|
|
|
- return roleRepository.findById(id).orElse(null);
|
|
|
- }
|
|
|
-
|
|
|
- public Role findByTitle(String title) {
|
|
|
- return roleRepository.findByTitle(title);
|
|
|
- }
|
|
|
-
|
|
|
- public Page<RoleVO> getRoleVOByPage(PageRequest pageRequest) {
|
|
|
- return roleRepository.findAll(pageRequest).map(RoleVO::new);
|
|
|
- }
|
|
|
-
|
|
|
- public List<RoleVO> getByMatchName(String name) {
|
|
|
- Specification<Role> specification = (root, query, cb) -> {
|
|
|
- String likeQuery = String.format("%%%s%%", name);
|
|
|
- Predicate predicate = cb.like(root.get("name"), likeQuery);
|
|
|
- return cb.and(predicate);
|
|
|
- };
|
|
|
- return roleRepository.findAll(specification).stream().map(RoleVO::new).collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
- public RoleVO getRoleVOById(Integer id) {
|
|
|
- Role role = findById(id);
|
|
|
- return role != null ? new RoleVO(role) : null;
|
|
|
- }
|
|
|
-
|
|
|
+public interface RoleQuery extends BaseQuery<Role> {
|
|
|
+ Role findByTitle(String title);
|
|
|
+ Page<RoleVO> getRoleVOByPage(PageRequest pageRequest);
|
|
|
+ List<RoleVO> getByMatchName(String name);
|
|
|
+ RoleVO getRoleVOById(Integer id);
|
|
|
/**
|
|
|
* 获取拥有角色的用户
|
|
|
*
|
|
|
@@ -69,18 +32,6 @@ public class RoleQuery {
|
|
|
* @return
|
|
|
* @date 2021-07-14 下午2:57
|
|
|
*/
|
|
|
- public List<User> getUsersByRoleId(Integer id) {
|
|
|
- Role role = findById(id);
|
|
|
- return getUsersByRole(role);
|
|
|
- }
|
|
|
-
|
|
|
- public List<User> getUsersByRole(Role role) {
|
|
|
- String title = role.getTitle();
|
|
|
- Specification<User> specification = ((root, query, cb) -> {
|
|
|
- SetJoin<User, UserAuthority> setJoin = root.joinSet("authorities");
|
|
|
- Predicate predicate = cb.equal(setJoin.get("role"), title);
|
|
|
- return cb.and(predicate);
|
|
|
- });
|
|
|
- return userRepository.findAll(specification);
|
|
|
- }
|
|
|
+ List<User> getUsersByRoleId(Integer id);
|
|
|
+ List<User> getUsersByRole(Role role);
|
|
|
}
|