|
@@ -2,6 +2,9 @@ package com.yaoxiang.diagnosis.service;
|
|
|
|
|
|
import com.yaoxiang.diagnosis.dao.AuthorityRepo;
|
|
|
import com.yaoxiang.diagnosis.dao.RoleRepo;
|
|
|
+import com.yaoxiang.diagnosis.dao.UserRepo;
|
|
|
+import com.yaoxiang.diagnosis.entity.UserInfo;
|
|
|
+import com.yaoxiang.diagnosis.model.Result;
|
|
|
import com.yaoxiang.diagnosis.util.CommonUtil;
|
|
|
import com.yaoxiang.diagnosis.entity.Authority;
|
|
|
import com.yaoxiang.diagnosis.entity.Role;
|
|
@@ -10,6 +13,9 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Supplier;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -19,12 +25,108 @@ public class RoleService {
|
|
|
private RoleRepo roleRepo;
|
|
|
@Resource
|
|
|
private AuthorityRepo authorityRepo;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public Result addRole(String name, String type, String code, String authorityIds) {
|
|
|
+ if (StringUtils.isBlank(name) || hasRole(name)) {
|
|
|
+ return Result.fail("该角色已存在");
|
|
|
+ }
|
|
|
+ Role role = new Role();
|
|
|
+ role.setName(name);
|
|
|
+ role.setType(type);
|
|
|
+ role.setCode(code);
|
|
|
+ role.setAuthorityIds(authorityIds);
|
|
|
+ roleRepo.save(role);
|
|
|
+ return Result.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result updateRole(Long id, String name, String type, String code, String authorityIds) {
|
|
|
+ Role role = roleRepo.findById(id).orElse(null);
|
|
|
+ if (role == null) {
|
|
|
+ return Result.fail("未找到角色");
|
|
|
+ }
|
|
|
+ role.setName(name);
|
|
|
+ role.setType(type);
|
|
|
+ role.setCode(code);
|
|
|
+ role.setAuthorityIds(authorityIds);
|
|
|
+ roleRepo.save(role);
|
|
|
+ return Result.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean deleteRole(Long id, Consumer<Role> consumer) {
|
|
|
+ Role role = roleRepo.findById(id).orElse(null);
|
|
|
+ if (role == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ consumer.accept(role);
|
|
|
+ roleRepo.delete(role);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean deleteAuthority(Long id) {
|
|
|
+ if (id == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ List<Role> roles = roleRepo.findByAuthorityIdsContaining("%" + id + "%");
|
|
|
+ List<Role> result = roles.stream().filter(u -> Arrays.stream(u.getAuthorityIds().split(","))
|
|
|
+ .anyMatch(r -> r.equals(String.valueOf(id)))).collect(Collectors.toList());
|
|
|
+ return deleteAuthority(id, authority -> result.forEach(role -> {
|
|
|
+ String authorityIds = role.getAuthorityIds();
|
|
|
+ String newAuthorityIds = Arrays.stream(authorityIds.split(",")).filter(a -> !a.equals(String.valueOf(id))).collect(Collectors.joining(","));
|
|
|
+ role.setAuthorityIds(newAuthorityIds);
|
|
|
+ roleRepo.save(role);
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean deleteAuthority(Long id, Consumer<Authority> consumer) {
|
|
|
+ Authority authority = authorityRepo.findById(id).orElse(null);
|
|
|
+ if (authority == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ consumer.accept(authority);
|
|
|
+ authorityRepo.delete(authority);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result updateAuthority(Long id, String name, String type, String des) {
|
|
|
+ Authority authority = authorityRepo.findById(id).orElse(null);
|
|
|
+ if (authority == null) {
|
|
|
+ return Result.fail("未找到权限");
|
|
|
+ }
|
|
|
+ authority.setName(name);
|
|
|
+ authority.setType(type);
|
|
|
+ authority.setDes(des);
|
|
|
+ authorityRepo.save(authority);
|
|
|
+ return Result.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Result addAuthority(String name, String type, String des) {
|
|
|
+ if (StringUtils.isBlank(name) || hasAuthority(name)) {
|
|
|
+ return Result.fail("该权限已存在");
|
|
|
+ }
|
|
|
+ Authority authority = new Authority();
|
|
|
+ authority.setName(name);
|
|
|
+ authority.setType(type);
|
|
|
+ authority.setDes(des);
|
|
|
+ authorityRepo.save(authority);
|
|
|
+ return Result.ok();
|
|
|
+ }
|
|
|
|
|
|
public Role get(Long id) {
|
|
|
return roleRepo.getOne(id);
|
|
|
}
|
|
|
|
|
|
- public List<Role> list() {
|
|
|
+ public boolean hasRole(String role) {
|
|
|
+ return roleRepo.findByName(role) != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean hasAuthority(String authority) {
|
|
|
+ return authorityRepo.findByName(authority) != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Role> listRoles() {
|
|
|
return roleRepo.findAll();
|
|
|
}
|
|
|
|
|
@@ -36,6 +138,15 @@ public class RoleService {
|
|
|
return listRoles(Arrays.asList(ids.split(",")));
|
|
|
}
|
|
|
|
|
|
+ public List<String> listRoleNames(List<String> ids) {
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+ if (CommonUtil.isEmpty(ids)) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ List<Long> roleIds = ids.stream().map(Long::valueOf).collect(Collectors.toList());
|
|
|
+ return roleRepo.findAllById(roleIds).stream().map(Role::getName).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
public List<Role> listRoles(List<String> ids) {
|
|
|
List<Role> result = new ArrayList<>();
|
|
|
if (CommonUtil.isEmpty(ids)) {
|