学校组
This commit is contained in:
@@ -2,17 +2,23 @@ package com.yunhai.bschclassmanage.controller.api;
|
|||||||
|
|
||||||
import com.yunhai.bschclassmanage.config.Config;
|
import com.yunhai.bschclassmanage.config.Config;
|
||||||
import com.yunhai.bschclassmanage.controller.BaseController;
|
import com.yunhai.bschclassmanage.controller.BaseController;
|
||||||
|
import com.yunhai.bschclassmanage.enums.UserType;
|
||||||
import com.yunhai.bschclassmanage.utils.RedisUtil;
|
import com.yunhai.bschclassmanage.utils.RedisUtil;
|
||||||
|
import com.yunhai.bschclassmanage.utils.UserContext;
|
||||||
|
import com.yunhai.platform.utils.exception.DException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存查看接口
|
* 缓存查看接口
|
||||||
@@ -23,12 +29,25 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@CrossOrigin(originPatterns = {"*"}, allowCredentials = "true")
|
@CrossOrigin(originPatterns = {"*"}, allowCredentials = "true")
|
||||||
public class CacheController extends BaseController {
|
public class CacheController extends BaseController {
|
||||||
|
|
||||||
|
/** 匹配全部 key 的通配符,禁止用于清空全部 */
|
||||||
|
private static final String ALL_KEYS_PATTERN = "*";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisTemplate redisTemplate;
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisUtil redisUtil;
|
private RedisUtil redisUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 超管校验:非超级管理员直接抛异常阻断
|
||||||
|
*/
|
||||||
|
private void denyIfNotSuperAdmin() {
|
||||||
|
boolean isSuperAdmin = UserType.SUPER_ADMINISTRATOR.getId().equals(UserContext.getType());
|
||||||
|
if (!isSuperAdmin) {
|
||||||
|
throw new DException("无权限:仅超级管理员可执行缓存操作");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 key 查看缓存内容
|
* 根据 key 查看缓存内容
|
||||||
* 返回 key 是否存在、数据类型、剩余过期时间(秒)以及对应的值
|
* 返回 key 是否存在、数据类型、剩余过期时间(秒)以及对应的值
|
||||||
@@ -38,22 +57,18 @@ public class CacheController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping("get")
|
@GetMapping("get")
|
||||||
public Map<String, Object> getByKey(@RequestParam String key) {
|
public Map<String, Object> getByKey(@RequestParam String key) {
|
||||||
Map<String, Object> result = new HashMap<>();
|
denyIfNotSuperAdmin();
|
||||||
result.put("key", key);
|
|
||||||
|
|
||||||
if (StringUtils.isBlank(key)) {
|
if (StringUtils.isBlank(key)) {
|
||||||
result.put("exists", false);
|
throw new DException("key不能为空");
|
||||||
result.put("msg", "key不能为空");
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Boolean exists = redisTemplate.hasKey(key);
|
Boolean exists = redisTemplate.hasKey(key);
|
||||||
if (exists == null || !exists) {
|
if (exists == null || !exists) {
|
||||||
result.put("exists", false);
|
throw new DException("缓存不存在或已过期");
|
||||||
result.put("msg", "缓存不存在或已过期");
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
result.put("key", key);
|
||||||
result.put("exists", true);
|
result.put("exists", true);
|
||||||
String type = redisTemplate.type(key).name().toLowerCase();
|
String type = redisTemplate.type(key).name().toLowerCase();
|
||||||
result.put("type", type);
|
result.put("type", type);
|
||||||
@@ -90,7 +105,8 @@ public class CacheController extends BaseController {
|
|||||||
* @param pattern 匹配模式,默认 * 表示所有 key
|
* @param pattern 匹配模式,默认 * 表示所有 key
|
||||||
*/
|
*/
|
||||||
@GetMapping("keys")
|
@GetMapping("keys")
|
||||||
public Object keys(@RequestParam(required = false, defaultValue = "*") String pattern) {
|
public Set<String> keys(@RequestParam(required = false, defaultValue = "*") String pattern) {
|
||||||
|
denyIfNotSuperAdmin();
|
||||||
return redisUtil.keys(pattern);
|
return redisUtil.keys(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,14 +119,19 @@ public class CacheController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("delete")
|
@PostMapping("delete")
|
||||||
public Map<String, Object> delete(@RequestParam String key) {
|
public Map<String, Object> delete(@RequestParam String key) {
|
||||||
Map<String, Object> result = new HashMap<>();
|
denyIfNotSuperAdmin();
|
||||||
if (StringUtils.isBlank(key)) {
|
if (StringUtils.isBlank(key)) {
|
||||||
result.put("success", false);
|
throw new DException("key不能为空");
|
||||||
result.put("msg", "key不能为空");
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
String[] keys = key.split(",");
|
List<String> keys = Arrays.stream(key.split(","))
|
||||||
long count = redisTemplate.delete(java.util.Arrays.asList(keys));
|
.map(String::trim)
|
||||||
|
.filter(StringUtils::isNotBlank)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
if (keys.isEmpty()) {
|
||||||
|
throw new DException("key不能为空");
|
||||||
|
}
|
||||||
|
long count = redisTemplate.delete(keys);
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
result.put("success", true);
|
result.put("success", true);
|
||||||
result.put("deleted", count);
|
result.put("deleted", count);
|
||||||
result.put("msg", "已清除 " + count + " 个 key");
|
result.put("msg", "已清除 " + count + " 个 key");
|
||||||
@@ -125,43 +146,19 @@ public class CacheController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("deletePattern")
|
@PostMapping("deletePattern")
|
||||||
public Map<String, Object> deletePattern(@RequestParam String pattern) {
|
public Map<String, Object> deletePattern(@RequestParam String pattern) {
|
||||||
Map<String, Object> result = new HashMap<>();
|
denyIfNotSuperAdmin();
|
||||||
if (StringUtils.isBlank(pattern) || "*".equals(pattern.trim())) {
|
if (StringUtils.isBlank(pattern) || ALL_KEYS_PATTERN.equals(pattern.trim())) {
|
||||||
result.put("success", false);
|
throw new DException("禁止用 * 清空全部,请指定明确的 key 前缀");
|
||||||
result.put("msg", "禁止用 * 清空全部,请使用 clearAll 接口或指定明确前缀");
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
Set<String> keys = redisUtil.keys(pattern);
|
Set<String> keys = redisUtil.keys(pattern.trim());
|
||||||
Map<String, Object> ret = new HashMap<>();
|
|
||||||
if (keys == null || keys.isEmpty()) {
|
if (keys == null || keys.isEmpty()) {
|
||||||
result.put("success", true);
|
throw new DException("未匹配到任何 key");
|
||||||
result.put("deleted", 0);
|
|
||||||
result.put("msg", "未匹配到任何 key");
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
redisTemplate.delete(keys);
|
redisTemplate.delete(keys);
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
result.put("success", true);
|
result.put("success", true);
|
||||||
result.put("deleted", keys.size());
|
result.put("deleted", keys.size());
|
||||||
result.put("msg", "已清除 " + keys.size() + " 个 key");
|
result.put("msg", "已清除 " + keys.size() + " 个 key");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 清空当前 Redis 数据库(仅当前 db,不影响其他 db)
|
|
||||||
* ⚠️ 高危操作:会删除当前库下所有缓存,请谨慎调用
|
|
||||||
*/
|
|
||||||
@PostMapping("clearAll")
|
|
||||||
public Map<String, Object> clearAll() {
|
|
||||||
Map<String, Object> result = new HashMap<>();
|
|
||||||
try {
|
|
||||||
redisTemplate.getConnectionFactory().getConnection().flushDb();
|
|
||||||
result.put("success", true);
|
|
||||||
result.put("msg", "当前数据库已清空");
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("清空缓存失败", e);
|
|
||||||
result.put("success", false);
|
|
||||||
result.put("msg", "清空失败:" + e.getMessage());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -22,7 +22,7 @@ public class SyncGroupController {
|
|||||||
|
|
||||||
@PostMapping("AddGroup")
|
@PostMapping("AddGroup")
|
||||||
public void AddGroup(@RequestBody String jsonString) {
|
public void AddGroup(@RequestBody String jsonString) {
|
||||||
log.info("jsonString="+jsonString);
|
log.info("AddGroup jsonString="+jsonString);
|
||||||
//判断 2,"学校组" 4, "班级组"
|
//判断 2,"学校组" 4, "班级组"
|
||||||
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
||||||
syncGroupService.syncAddOrUpdate(group);
|
syncGroupService.syncAddOrUpdate(group);
|
||||||
@@ -30,19 +30,21 @@ public class SyncGroupController {
|
|||||||
|
|
||||||
@PostMapping("UpdateGroup")
|
@PostMapping("UpdateGroup")
|
||||||
public void UpdateGroup(@RequestBody String jsonString) {
|
public void UpdateGroup(@RequestBody String jsonString) {
|
||||||
log.info("jsonString="+jsonString);
|
log.info("UpdateGroup jsonString="+jsonString);
|
||||||
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
||||||
syncGroupService.syncAddOrUpdate(group);
|
syncGroupService.syncAddOrUpdate(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("DeleteGroup")
|
@PostMapping("DeleteGroup")
|
||||||
public void DeleteGroup(@RequestBody String jsonString) {
|
public void DeleteGroup(@RequestBody String jsonString) {
|
||||||
log.info("jsonString="+jsonString);
|
log.info("DeleteGroup jsonString="+jsonString);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("SyncGroup")
|
@PostMapping("SyncGroup")
|
||||||
public void SyncGroup(@RequestBody String jsonString) {
|
public void SyncGroup(@RequestBody String jsonString) {
|
||||||
log.info("jsonString="+jsonString);
|
log.info("SyncGroup jsonString="+jsonString);
|
||||||
|
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
||||||
|
syncGroupService.syncAddOrUpdate(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,5 +43,7 @@ public class SyncUserController {
|
|||||||
@PostMapping("SyncUser")
|
@PostMapping("SyncUser")
|
||||||
public void SyncUser(@RequestBody String jsonString) {
|
public void SyncUser(@RequestBody String jsonString) {
|
||||||
log.info("SyncUser jsonString="+jsonString);
|
log.info("SyncUser jsonString="+jsonString);
|
||||||
|
List<SyncUserDTO> userList = OtherUtil.jsonParseList(jsonString, SyncUserDTO.class);
|
||||||
|
syncUserService.syncAddOrUpdate(userList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,10 +107,12 @@ public class ComSchTreeService implements IComSchTreeService {
|
|||||||
|
|
||||||
|
|
||||||
public List<BoaGroup> getSchTree(Integer schId) {
|
public List<BoaGroup> getSchTree(Integer schId) {
|
||||||
String cacheKey = RedisConstants.SCHOOL_TREE + "_" + schId;
|
BoaGroup group = YschBaseOpenApi.getGroupById(schId,true);
|
||||||
String json = redisUtil.get(cacheKey,String.class);
|
return Collections.singletonList(group);
|
||||||
List<BoaGroup> treeList = OtherUtil.jsonParseArrayTree(json, BoaGroup.class);
|
// String cacheKey = RedisConstants.SCHOOL_TREE + "_" + schId;
|
||||||
return treeList;
|
// String json = redisUtil.get(cacheKey,String.class);
|
||||||
|
// List<BoaGroup> treeList = OtherUtil.jsonParseArrayTree(json, BoaGroup.class);
|
||||||
|
// return treeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user