2025年4月10日16:25:46
parent
9fe20c9fcc
commit
b19988672a
|
|
@ -963,4 +963,17 @@ public class InitController {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/test8")
|
||||
public void test8() {
|
||||
|
||||
List<Permission> all = this.permissionDao.findAll();
|
||||
|
||||
Role role = this.roleDao.findById(1L).get();
|
||||
role.getPermissions().clear();
|
||||
role.getPermissions().addAll(all);
|
||||
roleDao.save(role);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,27 +34,28 @@ public class PermissionController {
|
|||
/**
|
||||
* // 基本分页(无排序)
|
||||
* Pageable pageable = PageRequest.of(0, 10);
|
||||
*
|
||||
* <p>
|
||||
* // 分页+排序(单字段)
|
||||
* Pageable pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "createTime");
|
||||
*
|
||||
* <p>
|
||||
* // 分页+多字段排序
|
||||
* Sort sort = Sort.by(Sort.Order.asc("age"), Sort.Order.desc("name"));
|
||||
* Pageable pageable = PageRequest.of(0, 10, sort);
|
||||
*
|
||||
* @param pageNum
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/list")
|
||||
public Result list(Integer pageNum, Integer pageSize,boolean isTop) {
|
||||
public Result list(Integer pageNum, Integer pageSize, boolean isTop) {
|
||||
|
||||
Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize, Sort.Direction.DESC, "addTime");
|
||||
|
||||
Page<Permission> all;
|
||||
if (isTop){
|
||||
if (isTop) {
|
||||
Specification<Permission> spec = isDescriptionNull(); // 仅查询description为null的情况
|
||||
all = permissionDao.findAll(spec, pageable);
|
||||
}else {
|
||||
} else {
|
||||
all = permissionDao.findAll(pageable);
|
||||
}
|
||||
|
||||
|
|
@ -67,7 +68,7 @@ public class PermissionController {
|
|||
map.put("desc", permission.getDescription());
|
||||
map.put("url", permission.getUrl());
|
||||
map.put("sort", permission.getSort());
|
||||
map.put("parentId", permission.getParent()==null?0L:permission.getParent().getId());
|
||||
map.put("parentId", permission.getParent() == null ? 0L : permission.getParent().getId());
|
||||
|
||||
resultList.add(map);
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ public class PermissionController {
|
|||
}
|
||||
|
||||
@RequestMapping("/save")
|
||||
public Result save(@RequestParam @RequestBody Map<String, Object> params, HttpServletRequest request) {
|
||||
public Result save(@RequestParam @RequestBody Map<String, Object> params, HttpServletRequest request) {
|
||||
Permission permission;
|
||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||
|
||||
|
|
@ -85,15 +86,15 @@ public class PermissionController {
|
|||
String url = params.get("url").toString();
|
||||
String parent_id = params.get("parent_id").toString();
|
||||
|
||||
if (id != null && !id.isEmpty()){
|
||||
if (id != null && !id.isEmpty()) {
|
||||
permission = permissionDao.getById(Long.parseLong(id));
|
||||
}else {
|
||||
} else {
|
||||
permission = new Permission();
|
||||
}
|
||||
permission.setName(name);
|
||||
permission.setDescription(description);
|
||||
permission.setUrl(url);
|
||||
if (parent_id != null && !parent_id.isEmpty()){
|
||||
if (parent_id != null && !parent_id.isEmpty()) {
|
||||
permission.setParent(permissionDao.getById(Long.parseLong(parent_id)));
|
||||
}
|
||||
permissionDao.save(permission);
|
||||
|
|
@ -104,4 +105,56 @@ public class PermissionController {
|
|||
public Specification<Permission> isDescriptionNull() {
|
||||
return (root, query, cb) -> cb.isNull(root.get("parent"));
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/getPermissionById")
|
||||
public Result getPermissionById(Long id) {
|
||||
Permission permission = permissionDao.getById(id);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", permission.getId());
|
||||
map.put("name", permission.getName());
|
||||
map.put("desc", permission.getDescription());
|
||||
map.put("url", permission.getUrl());
|
||||
map.put("sort", permission.getSort());
|
||||
map.put("parentId", permission.getParent() == null ? 0L : permission.getParent().getId());
|
||||
|
||||
return Result.success(map);
|
||||
}
|
||||
|
||||
@RequestMapping("/getRootPermissions")
|
||||
public Result getRootPermissions() {
|
||||
Specification<Permission> spec = isDescriptionNull(); // 仅查询description为null的情况
|
||||
List<Permission> all = permissionDao.findAll(spec);
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
for (Permission permission : all) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", permission.getId());
|
||||
map.put("name", permission.getName());
|
||||
map.put("desc", permission.getDescription());
|
||||
map.put("url", permission.getUrl());
|
||||
map.put("sort", permission.getSort());
|
||||
|
||||
List<Map<String, Object>> childList = new ArrayList<>();
|
||||
for (Permission child : permission.getChildren()) {
|
||||
Map<String, Object> childMap = new HashMap<>();
|
||||
childMap.put("id", child.getId());
|
||||
childMap.put("name", child.getName());
|
||||
childMap.put("desc", child.getDescription());
|
||||
childMap.put("url", child.getUrl());
|
||||
childMap.put("sort", child.getSort());
|
||||
childMap.put("parentId", child.getParent() == null ? 0L : child.getParent().getId());
|
||||
childList.add(childMap);
|
||||
}
|
||||
map.put("children", childList);
|
||||
|
||||
resultList.add(map);
|
||||
}
|
||||
return Result.success(resultList);
|
||||
}
|
||||
|
||||
@RequestMapping("/delete")
|
||||
public Result delete(Long id) {
|
||||
permissionDao.deleteById(id);
|
||||
return Result.success(null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package com.zhangmeng.online.exam.controller;
|
||||
|
||||
import com.zhangmeng.online.exam.dao.PermissionDao;
|
||||
import com.zhangmeng.online.exam.dao.RoleDao;
|
||||
import com.zhangmeng.online.exam.dto.Result;
|
||||
import com.zhangmeng.online.exam.entity.Permission;
|
||||
import com.zhangmeng.online.exam.entity.Question;
|
||||
import com.zhangmeng.online.exam.entity.Role;
|
||||
import com.zhangmeng.online.exam.entity.User;
|
||||
|
|
@ -11,13 +13,12 @@ import org.springframework.data.domain.Page;
|
|||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
|
|
@ -31,6 +32,9 @@ public class RoleController {
|
|||
@Autowired
|
||||
private RoleDao roleDao;
|
||||
|
||||
@Autowired
|
||||
private PermissionDao permissionDao;
|
||||
|
||||
@RequestMapping("/list")
|
||||
public Result list(Integer pageNum, Integer pageSize) {
|
||||
Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize, Sort.Direction.DESC, "addTime");
|
||||
|
|
@ -48,4 +52,118 @@ public class RoleController {
|
|||
return Result.success(pageNum, pageSize, all.getTotalElements(), resultList);
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/getRoleById")
|
||||
public Result getRoleById(Long id) {
|
||||
Role role = roleDao.findById(id).get();
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", role.getId());
|
||||
map.put("name", role.getName());
|
||||
map.put("desc", role.getDescription());
|
||||
map.put("type_name", role.getType().name());
|
||||
|
||||
return Result.success(map);
|
||||
}
|
||||
|
||||
@RequestMapping("/getRoleTypes")
|
||||
public Result getRoleTypes() {
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
for (Role.Type type : Role.Type.values()) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("type_name", type.name());
|
||||
map.put("type_desc", type.getDesc());
|
||||
resultList.add(map);
|
||||
}
|
||||
return Result.success(resultList);
|
||||
}
|
||||
|
||||
@RequestMapping("/saveRole")
|
||||
public Result saveRole(@RequestBody @RequestParam Map<String, Object> params) {
|
||||
|
||||
String id = params.get("id").toString();
|
||||
String name = params.get("name").toString();
|
||||
String desc = params.get("desc").toString();
|
||||
String type = params.get("type").toString();
|
||||
|
||||
Role role ;
|
||||
|
||||
if (id != null && !id.isEmpty()){
|
||||
role = roleDao.findById(Long.parseLong(id)).get();
|
||||
}else {
|
||||
role = new Role();
|
||||
}
|
||||
|
||||
role.setName(name);
|
||||
role.setDescription(desc);
|
||||
role.setType(Role.Type.valueOf(type));
|
||||
roleDao.save(role);
|
||||
|
||||
return Result.success(null);
|
||||
}
|
||||
|
||||
@RequestMapping("/delete")
|
||||
public Result delete(Long id) {
|
||||
roleDao.deleteById(id);
|
||||
return Result.success(null);
|
||||
}
|
||||
|
||||
@RequestMapping("/updatePermission")
|
||||
public Result updatePermission(@RequestBody @RequestParam Map<String, Object> params) {
|
||||
Long roleId = Long.parseLong(params.get("id").toString());
|
||||
String permissionIds = params.get("permissionIds").toString();
|
||||
List<Long> permissionIdList = new ArrayList<>();
|
||||
for (String permissionId : permissionIds.split(",")) {
|
||||
permissionIdList.add(Long.parseLong(permissionId));
|
||||
}
|
||||
List<Permission> permissionList = permissionDao.findAllById(permissionIdList);
|
||||
Role role = roleDao.getById(roleId);
|
||||
role.setPermissions(new HashSet<>(permissionList));
|
||||
roleDao.save(role);
|
||||
return Result.success(null);
|
||||
}
|
||||
|
||||
@RequestMapping("/getPermissionsByRole")
|
||||
public Result getPermissionsByRole(Long roleId) {
|
||||
Role role = roleDao.getById(roleId);
|
||||
Set<Permission> permissionList = role.getPermissions();
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
|
||||
for (Permission permission : permissionList) {
|
||||
if (permission.getParent() == null){
|
||||
List<Permission> children = permission.getChildren();
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", permission.getId());
|
||||
map.put("name", permission.getName());
|
||||
map.put("desc", permission.getDescription());
|
||||
map.put("url", permission.getUrl());
|
||||
map.put("sort", permission.getSort());
|
||||
map.put("parentId", permission.getParent() == null ? 0L : permission.getParent().getId());
|
||||
|
||||
List<Map<String, Object>> childList = new ArrayList<>();
|
||||
|
||||
if (!children.isEmpty()){
|
||||
for (Permission child : children) {
|
||||
if (permissionList.contains(child)){
|
||||
Map<String, Object> childMap = new HashMap<>();
|
||||
childMap.put("id", child.getId());
|
||||
childMap.put("name", child.getName());
|
||||
childMap.put("desc", child.getDescription());
|
||||
childMap.put("url", child.getUrl());
|
||||
childMap.put("sort", child.getSort());
|
||||
childMap.put("parentId", child.getParent() == null ? 0L : child.getParent().getId());
|
||||
childList.add(childMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
map.put("children", childList);
|
||||
|
||||
resultList.add(map);
|
||||
}
|
||||
}
|
||||
|
||||
return Result.success(resultList);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ import com.zhangmeng.online.exam.entity.baseEntity.BaseEntity;
|
|||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
|
@ -41,6 +43,7 @@ public class Permission extends BaseEntity<Long> {
|
|||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Permission parent;
|
||||
|
||||
|
||||
@OneToMany(mappedBy = "parent",fetch = FetchType.LAZY)
|
||||
private List<Permission> children = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,4 +44,4 @@ jwt:
|
|||
- /doc.html
|
||||
- /webjars/**
|
||||
- /favicon.ico # ??FAVICON
|
||||
- /init/data
|
||||
- /init/**
|
||||
Loading…
Reference in New Issue