2025-03-07 10:19:30 +00:00
|
|
|
package com.zhangmeng.online.exam.controller;
|
|
|
|
|
|
|
|
|
|
import com.zhangmeng.online.exam.dao.PermissionDao;
|
|
|
|
|
import com.zhangmeng.online.exam.dto.Result;
|
|
|
|
|
import com.zhangmeng.online.exam.entity.Permission;
|
|
|
|
|
import com.zhangmeng.online.exam.entity.Role;
|
2025-03-14 08:54:45 +00:00
|
|
|
import com.zhangmeng.online.exam.utils.PageUtils;
|
2025-03-07 10:19:30 +00:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2025-04-09 10:19:39 +00:00
|
|
|
import org.springframework.data.domain.*;
|
|
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
2025-03-07 10:19:30 +00:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2025-04-09 10:19:39 +00:00
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
2025-03-07 10:19:30 +00:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
2025-04-09 10:19:39 +00:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2025-03-07 10:19:30 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author zm
|
|
|
|
|
* @date 2025/3/7 9:46
|
|
|
|
|
* @version: 1.0
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/permission")
|
|
|
|
|
public class PermissionController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private PermissionDao permissionDao;
|
|
|
|
|
|
2025-03-14 08:54:45 +00:00
|
|
|
/**
|
|
|
|
|
* // 基本分页(无排序)
|
|
|
|
|
* Pageable pageable = PageRequest.of(0, 10);
|
|
|
|
|
*
|
|
|
|
|
* // 分页+排序(单字段)
|
|
|
|
|
* Pageable pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "createTime");
|
|
|
|
|
*
|
|
|
|
|
* // 分页+多字段排序
|
|
|
|
|
* Sort sort = Sort.by(Sort.Order.asc("age"), Sort.Order.desc("name"));
|
|
|
|
|
* Pageable pageable = PageRequest.of(0, 10, sort);
|
|
|
|
|
* @param pageNum
|
|
|
|
|
* @param pageSize
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2025-03-07 10:19:30 +00:00
|
|
|
@RequestMapping("/list")
|
2025-04-09 10:19:39 +00:00
|
|
|
public Result list(Integer pageNum, Integer pageSize,boolean isTop) {
|
2025-03-14 08:54:45 +00:00
|
|
|
|
|
|
|
|
Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize, Sort.Direction.DESC, "addTime");
|
2025-04-09 10:19:39 +00:00
|
|
|
|
|
|
|
|
Page<Permission> all;
|
|
|
|
|
if (isTop){
|
|
|
|
|
Specification<Permission> spec = isDescriptionNull(); // 仅查询description为null的情况
|
|
|
|
|
all = permissionDao.findAll(spec, pageable);
|
|
|
|
|
}else {
|
|
|
|
|
all = permissionDao.findAll(pageable);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 08:54:45 +00:00
|
|
|
List<Permission> list = all.getContent();
|
2025-03-07 10:19:30 +00:00
|
|
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
|
|
for (Permission permission : list) {
|
|
|
|
|
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());
|
2025-04-09 10:19:39 +00:00
|
|
|
map.put("sort", permission.getSort());
|
|
|
|
|
map.put("parentId", permission.getParent()==null?0L:permission.getParent().getId());
|
|
|
|
|
|
2025-03-07 10:19:30 +00:00
|
|
|
resultList.add(map);
|
|
|
|
|
}
|
2025-03-14 08:54:45 +00:00
|
|
|
return Result.success(pageNum, pageSize, all.getTotalElements(), resultList);
|
2025-03-07 10:19:30 +00:00
|
|
|
}
|
2025-04-09 10:19:39 +00:00
|
|
|
|
|
|
|
|
@RequestMapping("/save")
|
|
|
|
|
public Result save(@RequestParam @RequestBody Map<String, Object> params, HttpServletRequest request) {
|
|
|
|
|
Permission permission;
|
|
|
|
|
Map<String, String[]> parameterMap = request.getParameterMap();
|
|
|
|
|
|
|
|
|
|
String id = params.get("id").toString();
|
|
|
|
|
String name = params.get("name").toString();
|
|
|
|
|
String description = params.get("description").toString();
|
|
|
|
|
String url = params.get("url").toString();
|
|
|
|
|
String parent_id = params.get("parent_id").toString();
|
|
|
|
|
|
|
|
|
|
if (id != null && !id.isEmpty()){
|
|
|
|
|
permission = permissionDao.getById(Long.parseLong(id));
|
|
|
|
|
}else {
|
|
|
|
|
permission = new Permission();
|
|
|
|
|
}
|
|
|
|
|
permission.setName(name);
|
|
|
|
|
permission.setDescription(description);
|
|
|
|
|
permission.setUrl(url);
|
|
|
|
|
if (parent_id != null && !parent_id.isEmpty()){
|
|
|
|
|
permission.setParent(permissionDao.getById(Long.parseLong(parent_id)));
|
|
|
|
|
}
|
|
|
|
|
permissionDao.save(permission);
|
|
|
|
|
|
|
|
|
|
return Result.success(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Specification<Permission> isDescriptionNull() {
|
|
|
|
|
return (root, query, cb) -> cb.isNull(root.get("parent"));
|
|
|
|
|
}
|
2025-03-07 10:19:30 +00:00
|
|
|
}
|