refactor: 分组添加 userId.

This commit is contained in:
lijiahang
2024-01-23 18:33:37 +08:00
parent ddd603c957
commit 914359505e
23 changed files with 305 additions and 99 deletions

View File

@@ -21,6 +21,8 @@ public interface Const extends com.orion.lang.constant.Const, FieldConst, CnCons
String INTRANET_IP = "内网IP"; String INTRANET_IP = "内网IP";
Long SYSTEM_USER_ID = 0L;
Long ROOT_PARENT_ID = 0L; Long ROOT_PARENT_ID = 0L;
Integer DEFAULT_SORT = 10; Integer DEFAULT_SORT = 10;

View File

@@ -581,6 +581,18 @@ public class RedisMaps extends RedisUtils {
return valuesJson(key.getKey(), (Class<V>) key.getType()); return valuesJson(key.getKey(), (Class<V>) key.getType());
} }
/**
* 获取所有值 json
*
* @param key key
* @param define define
* @param <V> V
* @return values
*/
public static <V> List<V> valuesJson(String key, CacheKeyDefine define) {
return valuesJson(key, (Class<V>) define.getType());
}
/** /**
* 获取所有值 json * 获取所有值 json
* *

View File

@@ -0,0 +1,38 @@
### 创建命令片段
POST {{baseUrl}}/asset/command-snippet/create
Content-Type: application/json
Authorization: {{token}}
{
"groupId": "",
"name": "",
"prefix": "",
"command": ""
}
### 更新命令片段
PUT {{baseUrl}}/asset/command-snippet/update
Content-Type: application/json
Authorization: {{token}}
{
"id": "",
"groupId": "",
"name": "",
"prefix": "",
"command": ""
}
### 查询全部命令片段
GET {{baseUrl}}/asset/command-snippet/list
Authorization: {{token}}
### 删除命令片段
DELETE {{baseUrl}}/asset/command-snippet/delete?id=1
Authorization: {{token}}
###

View File

@@ -4,7 +4,6 @@ import com.orion.ops.framework.log.core.annotation.IgnoreLog;
import com.orion.ops.framework.log.core.enums.IgnoreLogMode; import com.orion.ops.framework.log.core.enums.IgnoreLogMode;
import com.orion.ops.framework.web.core.annotation.RestWrapper; import com.orion.ops.framework.web.core.annotation.RestWrapper;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO; import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import com.orion.ops.module.asset.service.CommandSnippetService; import com.orion.ops.module.asset.service.CommandSnippetService;
@@ -50,10 +49,10 @@ public class CommandSnippetController {
} }
@IgnoreLog(IgnoreLogMode.RET) @IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/list") @GetMapping("/list")
@Operation(summary = "查询全部命令片段") @Operation(summary = "查询全部命令片段")
public List<CommandSnippetVO> getCommandSnippetList(@Validated @RequestBody CommandSnippetQueryRequest request) { public List<CommandSnippetVO> getCommandSnippetList() {
return commandSnippetService.getCommandSnippetList(request); return commandSnippetService.getCommandSnippetList();
} }
@DeleteMapping("/delete") @DeleteMapping("/delete")

View File

@@ -3,7 +3,6 @@ package com.orion.ops.module.asset.convert;
import com.orion.ops.module.asset.entity.domain.CommandSnippetDO; import com.orion.ops.module.asset.entity.domain.CommandSnippetDO;
import com.orion.ops.module.asset.entity.dto.CommandSnippetCacheDTO; import com.orion.ops.module.asset.entity.dto.CommandSnippetCacheDTO;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO; import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
@@ -27,8 +26,6 @@ public interface CommandSnippetConvert {
CommandSnippetDO to(CommandSnippetUpdateRequest request); CommandSnippetDO to(CommandSnippetUpdateRequest request);
CommandSnippetDO to(CommandSnippetQueryRequest request);
CommandSnippetVO to(CommandSnippetDO domain); CommandSnippetVO to(CommandSnippetDO domain);
List<CommandSnippetVO> to(List<CommandSnippetDO> list); List<CommandSnippetVO> to(List<CommandSnippetDO> list);

View File

@@ -1,28 +0,0 @@
package com.orion.ops.module.asset.entity.request.command;
import com.orion.ops.framework.common.entity.PageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import javax.validation.constraints.Size;
/**
* 命令片段 查询请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(name = "CommandSnippetQueryRequest", description = "命令片段 查询请求对象")
public class CommandSnippetQueryRequest extends PageRequest {
@Size(max = 64)
@Schema(description = "名称")
private String name;
}

View File

@@ -0,0 +1,39 @@
package com.orion.ops.module.asset.entity.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 命令片段 视图响应对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetVO", description = "命令片段 视图响应对象")
public class CommandSnippetVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "名称")
private String name;
@Schema(description = "触发前缀")
private String prefix;
@Schema(description = "代码片段")
private String command;
}

View File

@@ -1,7 +1,6 @@
package com.orion.ops.module.asset.service; package com.orion.ops.module.asset.service;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO; import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
@@ -35,10 +34,9 @@ public interface CommandSnippetService {
/** /**
* 查询全部命令片段 * 查询全部命令片段
* *
* @param request request
* @return rows * @return rows
*/ */
List<CommandSnippetVO> getCommandSnippetList(CommandSnippetQueryRequest request); List<CommandSnippetVO> getCommandSnippetList();
/** /**
* 删除命令片段 * 删除命令片段

View File

@@ -1,16 +1,17 @@
package com.orion.ops.module.asset.service.impl; package com.orion.ops.module.asset.service.impl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.ops.framework.common.constant.ErrorMessage; import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid; import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps; import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.asset.convert.CommandSnippetConvert; import com.orion.ops.module.asset.convert.CommandSnippetConvert;
import com.orion.ops.module.asset.dao.CommandSnippetDAO; import com.orion.ops.module.asset.dao.CommandSnippetDAO;
import com.orion.ops.module.asset.define.cache.CommandSnippetCacheKeyDefine; import com.orion.ops.module.asset.define.cache.CommandSnippetCacheKeyDefine;
import com.orion.ops.module.asset.entity.domain.CommandSnippetDO; import com.orion.ops.module.asset.entity.domain.CommandSnippetDO;
import com.orion.ops.module.asset.entity.dto.CommandSnippetCacheDTO;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO; import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import com.orion.ops.module.asset.service.CommandSnippetService; import com.orion.ops.module.asset.service.CommandSnippetService;
@@ -22,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* 命令片段 服务实现类 * 命令片段 服务实现类
@@ -42,9 +44,11 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
@Override @Override
public Long createCommandSnippet(CommandSnippetCreateRequest request) { public Long createCommandSnippet(CommandSnippetCreateRequest request) {
Long userId = SecurityUtils.getLoginUserId();
log.info("CommandSnippetService-createCommandSnippet request: {}", JSON.toJSONString(request)); log.info("CommandSnippetService-createCommandSnippet request: {}", JSON.toJSONString(request));
// 转换 // 转换
CommandSnippetDO record = CommandSnippetConvert.MAPPER.to(request); CommandSnippetDO record = CommandSnippetConvert.MAPPER.to(request);
record.setUserId(userId);
// 插入 // 插入
int effect = commandSnippetDAO.insert(record); int effect = commandSnippetDAO.insert(record);
Long id = record.getId(); Long id = record.getId();
@@ -54,7 +58,8 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
dataGroupRelApi.addGroupRel(request.getGroupId(), id); dataGroupRelApi.addGroupRel(request.getGroupId(), id);
} }
// 删除缓存 // 删除缓存
RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET); String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
return id; return id;
} }
@@ -62,6 +67,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer updateCommandSnippetById(CommandSnippetUpdateRequest request) { public Integer updateCommandSnippetById(CommandSnippetUpdateRequest request) {
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING); Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
Long userId = SecurityUtils.getLoginUserId();
log.info("CommandSnippetService-updateCommandSnippetById id: {}, request: {}", id, JSON.toJSONString(request)); log.info("CommandSnippetService-updateCommandSnippetById id: {}, request: {}", id, JSON.toJSONString(request));
// 查询 // 查询
CommandSnippetDO record = commandSnippetDAO.selectById(id); CommandSnippetDO record = commandSnippetDAO.selectById(id);
@@ -71,30 +77,50 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 更新 // 更新
int effect = commandSnippetDAO.updateById(updateRecord); int effect = commandSnippetDAO.updateById(updateRecord);
log.info("CommandSnippetService-updateCommandSnippetById effect: {}", effect); log.info("CommandSnippetService-updateCommandSnippetById effect: {}", effect);
// 删除分组引用 // fixme 删除分组引用
dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id); dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id);
// 设置分组引用 // 设置分组引用
if (request.getGroupId() != null) { if (request.getGroupId() != null) {
dataGroupRelApi.addGroupRel(request.getGroupId(), id); dataGroupRelApi.addGroupRel(request.getGroupId(), id);
} }
// 删除缓存 // 删除缓存
RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET); String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
return effect; return effect;
} }
@Override @Override
public List<CommandSnippetVO> getCommandSnippetList(CommandSnippetQueryRequest request) { public List<CommandSnippetVO> getCommandSnippetList() {
// FIXME 查询缓存 Long userId = SecurityUtils.getLoginUserId();
String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
// fixme 查询分组 // fixme 查询分组
// 条件
LambdaQueryWrapper<CommandSnippetDO> wrapper = this.buildQueryWrapper(request); // 查询缓存
// 查询 List<CommandSnippetCacheDTO> list = RedisMaps.valuesJson(cacheKey, CommandSnippetCacheKeyDefine.COMMAND_SNIPPET);
return commandSnippetDAO.of(wrapper).list(CommandSnippetConvert.MAPPER::to); if (list.isEmpty()) {
// 查询数据库
list = commandSnippetDAO.of()
.createWrapper()
.eq(CommandSnippetDO::getUserId, userId)
.then()
.list(CommandSnippetConvert.MAPPER::toCache);
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(list, CommandSnippetCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET, s -> s.getId().toString(), list);
}
// 删除屏障
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(CommandSnippetConvert.MAPPER::to)
.collect(Collectors.toList());
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer deleteCommandSnippetById(Long id) { public Integer deleteCommandSnippetById(Long id) {
Long userId = SecurityUtils.getLoginUserId();
log.info("CommandSnippetService-deleteCommandSnippetById id: {}", id); log.info("CommandSnippetService-deleteCommandSnippetById id: {}", id);
// 检查数据是否存在 // 检查数据是否存在
CommandSnippetDO record = commandSnippetDAO.selectById(id); CommandSnippetDO record = commandSnippetDAO.selectById(id);
@@ -105,19 +131,9 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 删除分组引用 // 删除分组引用
dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id); dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id);
// 删除缓存 // 删除缓存
RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET, id); String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
RedisMaps.delete(cacheKey, id);
return effect; return effect;
} }
/**
* 构建查询 wrapper
*
* @param request request
* @return wrapper
*/
private LambdaQueryWrapper<CommandSnippetDO> buildQueryWrapper(CommandSnippetQueryRequest request) {
return commandSnippetDAO.wrapper()
.eq(CommandSnippetDO::getName, request.getName());
}
} }

View File

@@ -53,11 +53,30 @@ public interface DataGroupApi {
/** /**
* 通过缓存查询数据分组 * 通过缓存查询数据分组
* *
* @param type type * @param type type
* @param userId userId
* @return rows
*/
List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type, Long userId);
/**
* 通过缓存查询数据分组
*
* @param type type
* @param userId userId
* @return rows * @return rows
*/ */
List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type); List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type);
/**
* 通过缓存查询数据分组
*
* @param type type
* @param userId userId
* @return rows
*/
List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type, Long userId);
/** /**
* 通过 id 查询 * 通过 id 查询
* *

View File

@@ -25,6 +25,9 @@ import java.io.Serializable;
@Schema(name = "DataGroupCreateDTO", description = "数据分组 创建请求业务对象") @Schema(name = "DataGroupCreateDTO", description = "数据分组 创建请求业务对象")
public class DataGroupCreateDTO implements Serializable { public class DataGroupCreateDTO implements Serializable {
@Schema(description = "userId")
private Long userId;
@NotNull @NotNull
@Schema(description = "父id") @Schema(description = "父id")
private Long parentId; private Long parentId;

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.infra.api.impl; package com.orion.ops.module.infra.api.impl;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.utils.Valid; import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.module.infra.api.DataGroupApi; import com.orion.ops.module.infra.api.DataGroupApi;
import com.orion.ops.module.infra.convert.DataGroupProviderConvert; import com.orion.ops.module.infra.convert.DataGroupProviderConvert;
@@ -42,6 +43,9 @@ public class DataGroupApiImpl implements DataGroupApi {
@Override @Override
public Long createDataGroup(DataGroupTypeEnum type, DataGroupCreateDTO dto) { public Long createDataGroup(DataGroupTypeEnum type, DataGroupCreateDTO dto) {
Valid.valid(dto); Valid.valid(dto);
if (dto.getUserId() == null) {
dto.setUserId(Const.SYSTEM_USER_ID);
}
DataGroupCreateRequest request = DataGroupProviderConvert.MAPPER.toRequest(dto); DataGroupCreateRequest request = DataGroupProviderConvert.MAPPER.toRequest(dto);
request.setType(type.name()); request.setType(type.name());
return dataGroupService.createDataGroup(request); return dataGroupService.createDataGroup(request);
@@ -63,13 +67,23 @@ public class DataGroupApiImpl implements DataGroupApi {
@Override @Override
public List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type) { public List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupListByCache(type.name()); return this.getDataGroupList(type, Const.SYSTEM_USER_ID);
}
@Override
public List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type, Long userId) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupListByCache(type.name(), userId);
return DataGroupProviderConvert.MAPPER.toList(rows); return DataGroupProviderConvert.MAPPER.toList(rows);
} }
@Override @Override
public List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type) { public List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupTreeByCache(type.name()); return this.getDataGroupTree(type, Const.SYSTEM_USER_ID);
}
@Override
public List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type, Long userId) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupTreeByCache(type.name(), userId);
return DataGroupProviderConvert.MAPPER.toList(rows); return DataGroupProviderConvert.MAPPER.toList(rows);
} }

View File

@@ -53,4 +53,16 @@ public interface DataGroupDAO extends IMapper<DataGroupDO> {
return this.selectList(wrapper); return this.selectList(wrapper);
} }
/**
* 通过 userId 查询
*
* @param userId userId
* @return rows
*/
default List<DataGroupDO> selectByUserId(Long userId) {
LambdaQueryWrapper<DataGroupDO> wrapper = this.lambda()
.eq(DataGroupDO::getUserId, userId);
return this.selectList(wrapper);
}
} }

View File

@@ -18,16 +18,16 @@ import java.util.concurrent.TimeUnit;
public interface DataGroupCacheKeyDefine { public interface DataGroupCacheKeyDefine {
CacheKeyDefine DATA_GROUP_LIST = new CacheKeyBuilder() CacheKeyDefine DATA_GROUP_LIST = new CacheKeyBuilder()
.key("data:group-list:{}") .key("data:group-list:{}:{}")
.desc("数据分组列表结构 ${type}") .desc("数据分组列表结构 ${type} ${userId}")
.type(DataGroupCacheDTO.class) .type(DataGroupCacheDTO.class)
.struct(RedisCacheStruct.STRING) .struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS) .timeout(1, TimeUnit.DAYS)
.build(); .build();
CacheKeyDefine DATA_GROUP_TREE = new CacheKeyBuilder() CacheKeyDefine DATA_GROUP_TREE = new CacheKeyBuilder()
.key("data:group-tree:{}") .key("data:group-tree:{}:{}")
.desc("数据分组树结构 ${type}") .desc("数据分组树结构 ${type} ${userId}")
.type(DataGroupCacheDTO.class) .type(DataGroupCacheDTO.class)
.struct(RedisCacheStruct.STRING) .struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS) .timeout(1, TimeUnit.DAYS)
@@ -42,8 +42,8 @@ public interface DataGroupCacheKeyDefine {
.build(); .build();
CacheKeyDefine DATA_GROUP_REL_TYPE = new CacheKeyBuilder() CacheKeyDefine DATA_GROUP_REL_TYPE = new CacheKeyBuilder()
.key("data:group-rel:type:{}") .key("data:group-rel:type:{}:{}")
.desc("数据分组数据关联-类型 ${type}") .desc("数据分组数据关联-类型 ${type} ${userId}")
.type(DataGroupRelCacheDTO.class) .type(DataGroupRelCacheDTO.class)
.struct(RedisCacheStruct.STRING) .struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS) .timeout(1, TimeUnit.DAYS)

View File

@@ -34,6 +34,10 @@ public class DataGroupDO extends BaseDO {
@TableField("parent_id") @TableField("parent_id")
private Long parentId; private Long parentId;
@Schema(description = "用户id")
@TableField("user_id")
private Long userId;
@Schema(description = "组名称") @Schema(description = "组名称")
@TableField("name") @TableField("name")
private String name; private String name;

View File

@@ -34,6 +34,10 @@ public class DataGroupRelDO extends BaseDO {
@TableField("group_id") @TableField("group_id")
private Long groupId; private Long groupId;
@Schema(description = "用户id")
@TableField("user_id")
private Long userId;
@Schema(description = "引用id") @Schema(description = "引用id")
@TableField("rel_id") @TableField("rel_id")
private Long relId; private Long relId;

View File

@@ -25,6 +25,10 @@ import java.io.Serializable;
@Schema(name = "DataGroupCreateRequest", description = "数据分组 创建请求对象") @Schema(name = "DataGroupCreateRequest", description = "数据分组 创建请求对象")
public class DataGroupCreateRequest implements Serializable { public class DataGroupCreateRequest implements Serializable {
@NotNull
@Schema(description = "userId")
private Long userId;
@NotNull @NotNull
@Schema(description = "父id") @Schema(description = "父id")
private Long parentId; private Long parentId;

View File

@@ -27,25 +27,28 @@ public interface DataGroupRelService {
* 设置关联 * 设置关联
* *
* @param type type * @param type type
* @param userId userId
* @param groupIdList groupIdList * @param groupIdList groupIdList
* @param relId relId * @param relId relId
*/ */
void updateGroupRel(String type, List<Long> groupIdList, Long relId); void updateGroupRel(String type, Long userId, List<Long> groupIdList, Long relId);
/** /**
* 添加关联 * 添加关联
* *
* @param userId userId
* @param groupId groupId * @param groupId groupId
* @param relId relId * @param relId relId
*/ */
void addGroupRel(Long groupId, Long relId); void addGroupRel(Long userId, Long groupId, Long relId);
/** /**
* 添加关联 * 添加关联
* *
* @param list list * @param userId userId
* @param list list
*/ */
void addGroupRel(List<DataGroupRelCreateRequest> list); void addGroupRel(Long userId, List<DataGroupRelCreateRequest> list);
/** /**
* 通过 type 查询 relId 缓存 * 通过 type 查询 relId 缓存

View File

@@ -43,25 +43,35 @@ public interface DataGroupService {
/** /**
* 通过缓存查询数据分组 - 列表 * 通过缓存查询数据分组 - 列表
* *
* @param type type * @param type type
* @param userId userId
* @return rows * @return rows
*/ */
List<DataGroupCacheDTO> getDataGroupListByCache(String type); List<DataGroupCacheDTO> getDataGroupListByCache(String type, Long userId);
/** /**
* 通过缓存查询数据分组 - 树结构 * 通过缓存查询数据分组 - 树结构
* *
* @param type type * @param type type
* @param userId userId
* @return rows * @return rows
*/ */
List<DataGroupCacheDTO> getDataGroupTreeByCache(String type); List<DataGroupCacheDTO> getDataGroupTreeByCache(String type, Long userId);
/** /**
* 删除数据分组 * 通过 id 删除数据分组
* *
* @param id id * @param id id
* @return effect * @return effect
*/ */
Integer deleteDataGroupById(Long id); Integer deleteDataGroupById(Long id);
/**
* 通过 userId 删除数据分组
*
* @param userId userId
* @return effect
*/
Integer deleteDataGroupByUserId(Long userId);
} }

View File

@@ -84,6 +84,7 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
List<DataGroupRelDO> insertRecords = relIdList.stream() List<DataGroupRelDO> insertRecords = relIdList.stream()
.map(s -> DataGroupRelDO.builder() .map(s -> DataGroupRelDO.builder()
.groupId(groupId) .groupId(groupId)
.userId(group.getUserId())
.type(type) .type(type)
.relId(s) .relId(s)
.build()) .build())
@@ -92,12 +93,12 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
} }
} }
// 删除缓存 // 删除缓存
this.deleteCache(type, Lists.of(groupId)); this.deleteCache(type, group.getUserId(), Lists.of(groupId));
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateGroupRel(String type, List<Long> groupIdList, Long relId) { public void updateGroupRel(String type, Long userId, List<Long> groupIdList, Long relId) {
Valid.notNull(relId); Valid.notNull(relId);
// 删除引用 // 删除引用
this.deleteByRelId(type, relId); this.deleteByRelId(type, relId);
@@ -108,11 +109,12 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
.type(type) .type(type)
.groupId(s) .groupId(s)
.relId(relId) .relId(relId)
.userId(userId)
.build()) .build())
.collect(Collectors.toList()); .collect(Collectors.toList());
dataGroupRelDAO.insertBatch(relList); dataGroupRelDAO.insertBatch(relList);
// 删除缓存 // 删除缓存
this.deleteCache(type, groupIdList); this.deleteCache(type, userId, groupIdList);
} }
} }
@@ -125,7 +127,7 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
.build(); .build();
// 插入 // 插入
SpringHolder.getBean(DataGroupRelService.class) SpringHolder.getBean(DataGroupRelService.class)
.addGroupRel(Lists.singleton(record)); .addGroupRel(groupId,Lists.singleton(record));
} }
@Override @Override
@@ -286,11 +288,12 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
* 删除缓存 * 删除缓存
* *
* @param type type * @param type type
* @param userId userId
* @param groupIdList groupIdList * @param groupIdList groupIdList
*/ */
private void deleteCache(String type, Collection<Long> groupIdList) { private void deleteCache(String type, Long userId, Collection<Long> groupIdList) {
// 类型缓存 // 类型缓存
List<String> keyList = Lists.of(DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type)); List<String> keyList = Lists.of(DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type, userId));
// 分组缓存 // 分组缓存
if (!Lists.isEmpty(groupIdList)) { if (!Lists.isEmpty(groupIdList)) {
groupIdList.stream() groupIdList.stream()

View File

@@ -26,7 +26,10 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -61,7 +64,7 @@ public class DataGroupServiceImpl implements DataGroupService {
Long id = record.getId(); Long id = record.getId();
log.info("DataGroupService-createDataGroup id: {}, effect: {}", id, effect); log.info("DataGroupService-createDataGroup id: {}, effect: {}", id, effect);
// 删除缓存 // 删除缓存
this.deleteCache(request.getType()); this.deleteCache(record);
return id; return id;
} }
@@ -85,7 +88,7 @@ public class DataGroupServiceImpl implements DataGroupService {
// 更新 // 更新
int effect = dataGroupDAO.updateById(updateRecord); int effect = dataGroupDAO.updateById(updateRecord);
// 删除缓存 // 删除缓存
this.deleteCache(record.getType()); this.deleteCache(record);
return effect; return effect;
} }
@@ -139,7 +142,7 @@ public class DataGroupServiceImpl implements DataGroupService {
effect = dataGroupDAO.updateById(update); effect = dataGroupDAO.updateById(update);
} }
// 删除缓存 // 删除缓存
this.deleteCache(type); this.deleteCache(moveRecord);
// 添加日志参数 // 添加日志参数
OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName()); OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName());
OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName()); OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName());
@@ -148,15 +151,16 @@ public class DataGroupServiceImpl implements DataGroupService {
} }
@Override @Override
public List<DataGroupCacheDTO> getDataGroupListByCache(String type) { public List<DataGroupCacheDTO> getDataGroupListByCache(String type, Long userId) {
// 查询缓存 // 查询缓存
String key = DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type); String key = DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type, userId);
List<DataGroupCacheDTO> list = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_LIST); List<DataGroupCacheDTO> list = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_LIST);
if (Lists.isEmpty(list)) { if (Lists.isEmpty(list)) {
// 查询数据库 // 查询数据库
list = dataGroupDAO.of() list = dataGroupDAO.of()
.createWrapper() .createWrapper()
.eq(DataGroupDO::getType, type) .eq(DataGroupDO::getType, type)
.eq(DataGroupDO::getUserId, userId)
.then() .then()
.list(DataGroupConvert.MAPPER::toCache); .list(DataGroupConvert.MAPPER::toCache);
// 设置屏障 防止穿透 // 设置屏障 防止穿透
@@ -170,13 +174,13 @@ public class DataGroupServiceImpl implements DataGroupService {
} }
@Override @Override
public List<DataGroupCacheDTO> getDataGroupTreeByCache(String type) { public List<DataGroupCacheDTO> getDataGroupTreeByCache(String type, Long userId) {
// 查询缓存 // 查询缓存
String key = DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type); String key = DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type, userId);
List<DataGroupCacheDTO> treeData = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_TREE); List<DataGroupCacheDTO> treeData = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_TREE);
if (Lists.isEmpty(treeData)) { if (Lists.isEmpty(treeData)) {
// 查询列表缓存 // 查询列表缓存
List<DataGroupCacheDTO> rows = this.getDataGroupListByCache(type); List<DataGroupCacheDTO> rows = this.getDataGroupListByCache(type, userId);
// 设置屏障 防止穿透 // 设置屏障 防止穿透
CacheBarriers.checkBarrier(rows, DataGroupCacheDTO::new); CacheBarriers.checkBarrier(rows, DataGroupCacheDTO::new);
if (!Lists.isEmpty(rows)) { if (!Lists.isEmpty(rows)) {
@@ -213,12 +217,39 @@ public class DataGroupServiceImpl implements DataGroupService {
dataGroupRelService.deleteByGroupIdList(type, deleteIdList); dataGroupRelService.deleteByGroupIdList(type, deleteIdList);
log.info("DataGroupService-deleteDataGroupById id: {}, effect: {}", id, effect); log.info("DataGroupService-deleteDataGroupById id: {}, effect: {}", id, effect);
// 删除缓存 // 删除缓存
this.deleteCache(type); this.deleteCache(record);
// 添加日志参数 // 添加日志参数
OperatorLogs.add(OperatorLogs.GROUP_NAME, record.getName()); OperatorLogs.add(OperatorLogs.GROUP_NAME, record.getName());
return effect; return effect;
} }
@Override
public Integer deleteDataGroupByUserId(Long userId) {
// 查询数据
List<DataGroupDO> rows = dataGroupDAO.selectByUserId(userId);
if (rows.isEmpty()) {
return rows.size();
}
// 删除分组
List<Long> idList = rows.stream()
.map(DataGroupDO::getId)
.collect(Collectors.toList());
dataGroupDAO.deleteBatchIds(idList);
// 删除分组内数据
Map<String, List<Long>> groupRelGroup = rows.stream()
.collect(Collectors.groupingBy(
DataGroupDO::getType,
Collectors.mapping(
DataGroupDO::getId,
Collectors.toList()
)
));
groupRelGroup.forEach((k, v) -> dataGroupRelService.deleteByGroupIdList(k, v));
// 删除缓存
this.deleteCache(userId, groupRelGroup.keySet());
return rows.size();
}
/** /**
* 获取所有子节点 id * 获取所有子节点 id
* *
@@ -252,6 +283,7 @@ public class DataGroupServiceImpl implements DataGroupService {
.ne(DataGroupDO::getId, domain.getId()) .ne(DataGroupDO::getId, domain.getId())
// 用其他字段做重复校验 // 用其他字段做重复校验
.eq(DataGroupDO::getParentId, domain.getParentId()) .eq(DataGroupDO::getParentId, domain.getParentId())
.eq(DataGroupDO::getUserId, domain.getUserId())
.eq(DataGroupDO::getName, domain.getName()) .eq(DataGroupDO::getName, domain.getName())
.eq(DataGroupDO::getType, domain.getType()); .eq(DataGroupDO::getType, domain.getType());
// 检查是否存在 // 检查是否存在
@@ -262,11 +294,30 @@ public class DataGroupServiceImpl implements DataGroupService {
/** /**
* 删除缓存 * 删除缓存
* *
* @param type type * @param type type
* @param userId userId
*/ */
private void deleteCache(String type) { private void deleteCache(DataGroupDO record) {
RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type), String type = record.getType();
DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type)); Long userId = record.getUserId();
RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type, userId),
DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type, userId));
}
/**
* 删除缓存
*
* @param userId userId
* @param types types
* @param userId userId
*/
private void deleteCache(Long userId, Collection<String> types) {
List<String> deleteKeys = new ArrayList<>();
for (String type : types) {
deleteKeys.add(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type, userId));
deleteKeys.add(DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type, userId));
}
RedisStrings.delete(deleteKeys);
} }
} }

View File

@@ -70,6 +70,9 @@ public class SystemUserServiceImpl implements SystemUserService {
@Resource @Resource
private DataPermissionService dataPermissionService; private DataPermissionService dataPermissionService;
@Resource
private DataGroupService dataGroupService;
@Resource @Resource
private DataExtraService dataExtraService; private DataExtraService dataExtraService;
@@ -251,6 +254,8 @@ public class SystemUserServiceImpl implements SystemUserService {
preferenceService.deletePreferenceByUserId(id); preferenceService.deletePreferenceByUserId(id);
// 删除用户数据权限 // 删除用户数据权限
dataPermissionService.deleteByUserId(id); dataPermissionService.deleteByUserId(id);
// 删除用户分组信息
dataGroupService.deleteDataGroupByUserId(id);
// 删除用户拓展数据 // 删除用户拓展数据
dataExtraService.deleteByUserId(id); dataExtraService.deleteByUserId(id);
} }

View File

@@ -6,6 +6,7 @@
<resultMap id="BaseResultMap" type="com.orion.ops.module.infra.entity.domain.DataGroupDO"> <resultMap id="BaseResultMap" type="com.orion.ops.module.infra.entity.domain.DataGroupDO">
<id column="id" property="id"/> <id column="id" property="id"/>
<result column="parent_id" property="parentId"/> <result column="parent_id" property="parentId"/>
<result column="user_id" property="userId"/>
<result column="name" property="name"/> <result column="name" property="name"/>
<result column="type" property="type"/> <result column="type" property="type"/>
<result column="sort" property="sort"/> <result column="sort" property="sort"/>
@@ -18,7 +19,7 @@
<!-- 通用查询结果列 --> <!-- 通用查询结果列 -->
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id, parent_id, name, type, sort, create_time, update_time, creator, updater, deleted id, parent_id, user_id, name, type, sort, create_time, update_time, creator, updater, deleted
</sql> </sql>
<update id="updateSort"> <update id="updateSort">