diff --git a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/constant/Const.java b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/constant/Const.java index 310b86ff..012cfcee 100644 --- a/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/constant/Const.java +++ b/orion-ops-framework/orion-ops-framework-common/src/main/java/com/orion/ops/framework/common/constant/Const.java @@ -21,6 +21,8 @@ public interface Const extends com.orion.lang.constant.Const, FieldConst, CnCons String INTRANET_IP = "内网IP"; + Long SYSTEM_USER_ID = 0L; + Long ROOT_PARENT_ID = 0L; Integer DEFAULT_SORT = 10; diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisMaps.java b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisMaps.java index 426102a8..62fda47f 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisMaps.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisMaps.java @@ -581,6 +581,18 @@ public class RedisMaps extends RedisUtils { return valuesJson(key.getKey(), (Class) key.getType()); } + /** + * 获取所有值 json + * + * @param key key + * @param define define + * @param V + * @return values + */ + public static List valuesJson(String key, CacheKeyDefine define) { + return valuesJson(key, (Class) define.getType()); + } + /** * 获取所有值 json * diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/controller/CommandSnippetController.http b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/controller/CommandSnippetController.http new file mode 100644 index 00000000..6c10a419 --- /dev/null +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/controller/CommandSnippetController.http @@ -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}} + + +### diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/controller/CommandSnippetController.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/controller/CommandSnippetController.java index 4e360050..a4113859 100644 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/controller/CommandSnippetController.java +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/controller/CommandSnippetController.java @@ -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.web.core.annotation.RestWrapper; 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.vo.CommandSnippetVO; import com.orion.ops.module.asset.service.CommandSnippetService; @@ -50,10 +49,10 @@ public class CommandSnippetController { } @IgnoreLog(IgnoreLogMode.RET) - @PostMapping("/list") + @GetMapping("/list") @Operation(summary = "查询全部命令片段") - public List getCommandSnippetList(@Validated @RequestBody CommandSnippetQueryRequest request) { - return commandSnippetService.getCommandSnippetList(request); + public List getCommandSnippetList() { + return commandSnippetService.getCommandSnippetList(); } @DeleteMapping("/delete") diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/convert/CommandSnippetConvert.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/convert/CommandSnippetConvert.java index f1637e3a..d0804636 100644 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/convert/CommandSnippetConvert.java +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/convert/CommandSnippetConvert.java @@ -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.dto.CommandSnippetCacheDTO; 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.vo.CommandSnippetVO; import org.mapstruct.Mapper; @@ -27,8 +26,6 @@ public interface CommandSnippetConvert { CommandSnippetDO to(CommandSnippetUpdateRequest request); - CommandSnippetDO to(CommandSnippetQueryRequest request); - CommandSnippetVO to(CommandSnippetDO domain); List to(List list); diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/command/CommandSnippetQueryRequest.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/command/CommandSnippetQueryRequest.java deleted file mode 100644 index c39f1b83..00000000 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/command/CommandSnippetQueryRequest.java +++ /dev/null @@ -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; - -} diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/vo/CommandSnippetVO.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/vo/CommandSnippetVO.java new file mode 100644 index 00000000..1e22c9ea --- /dev/null +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/vo/CommandSnippetVO.java @@ -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; + +} diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/CommandSnippetService.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/CommandSnippetService.java index 6a2b0a33..db654bf8 100644 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/CommandSnippetService.java +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/CommandSnippetService.java @@ -1,7 +1,6 @@ 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.CommandSnippetQueryRequest; import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest; import com.orion.ops.module.asset.entity.vo.CommandSnippetVO; @@ -35,10 +34,9 @@ public interface CommandSnippetService { /** * 查询全部命令片段 * - * @param request request * @return rows */ - List getCommandSnippetList(CommandSnippetQueryRequest request); + List getCommandSnippetList(); /** * 删除命令片段 diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/CommandSnippetServiceImpl.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/CommandSnippetServiceImpl.java index 3c40601c..9dff62eb 100644 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/CommandSnippetServiceImpl.java +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/CommandSnippetServiceImpl.java @@ -1,16 +1,17 @@ package com.orion.ops.module.asset.service.impl; 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.utils.Valid; 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.dao.CommandSnippetDAO; 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.dto.CommandSnippetCacheDTO; 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.vo.CommandSnippetVO; import com.orion.ops.module.asset.service.CommandSnippetService; @@ -22,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; +import java.util.stream.Collectors; /** * 命令片段 服务实现类 @@ -42,9 +44,11 @@ public class CommandSnippetServiceImpl implements CommandSnippetService { @Override public Long createCommandSnippet(CommandSnippetCreateRequest request) { + Long userId = SecurityUtils.getLoginUserId(); log.info("CommandSnippetService-createCommandSnippet request: {}", JSON.toJSONString(request)); // 转换 CommandSnippetDO record = CommandSnippetConvert.MAPPER.to(request); + record.setUserId(userId); // 插入 int effect = commandSnippetDAO.insert(record); Long id = record.getId(); @@ -54,7 +58,8 @@ public class CommandSnippetServiceImpl implements CommandSnippetService { dataGroupRelApi.addGroupRel(request.getGroupId(), id); } // 删除缓存 - RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET); + String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId); + RedisMaps.delete(cacheKey); return id; } @@ -62,6 +67,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService { @Transactional(rollbackFor = Exception.class) public Integer updateCommandSnippetById(CommandSnippetUpdateRequest request) { Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING); + Long userId = SecurityUtils.getLoginUserId(); log.info("CommandSnippetService-updateCommandSnippetById id: {}, request: {}", id, JSON.toJSONString(request)); // 查询 CommandSnippetDO record = commandSnippetDAO.selectById(id); @@ -71,30 +77,50 @@ public class CommandSnippetServiceImpl implements CommandSnippetService { // 更新 int effect = commandSnippetDAO.updateById(updateRecord); log.info("CommandSnippetService-updateCommandSnippetById effect: {}", effect); - // 删除分组引用 + // fixme 删除分组引用 dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id); // 设置分组引用 if (request.getGroupId() != null) { dataGroupRelApi.addGroupRel(request.getGroupId(), id); } // 删除缓存 - RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET); + String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId); + RedisMaps.delete(cacheKey); return effect; } @Override - public List getCommandSnippetList(CommandSnippetQueryRequest request) { - // FIXME 查询缓存 + public List getCommandSnippetList() { + Long userId = SecurityUtils.getLoginUserId(); + String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId); // fixme 查询分组 - // 条件 - LambdaQueryWrapper wrapper = this.buildQueryWrapper(request); - // 查询 - return commandSnippetDAO.of(wrapper).list(CommandSnippetConvert.MAPPER::to); + + // 查询缓存 + List list = RedisMaps.valuesJson(cacheKey, CommandSnippetCacheKeyDefine.COMMAND_SNIPPET); + 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 @Transactional(rollbackFor = Exception.class) public Integer deleteCommandSnippetById(Long id) { + Long userId = SecurityUtils.getLoginUserId(); log.info("CommandSnippetService-deleteCommandSnippetById id: {}", id); // 检查数据是否存在 CommandSnippetDO record = commandSnippetDAO.selectById(id); @@ -105,19 +131,9 @@ public class CommandSnippetServiceImpl implements CommandSnippetService { // 删除分组引用 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; } - /** - * 构建查询 wrapper - * - * @param request request - * @return wrapper - */ - private LambdaQueryWrapper buildQueryWrapper(CommandSnippetQueryRequest request) { - return commandSnippetDAO.wrapper() - .eq(CommandSnippetDO::getName, request.getName()); - } - } diff --git a/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/api/DataGroupApi.java b/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/api/DataGroupApi.java index 74158af2..d13879a6 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/api/DataGroupApi.java +++ b/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/api/DataGroupApi.java @@ -53,11 +53,30 @@ public interface DataGroupApi { /** * 通过缓存查询数据分组 * - * @param type type + * @param type type + * @param userId userId + * @return rows + */ + List getDataGroupList(DataGroupTypeEnum type, Long userId); + + /** + * 通过缓存查询数据分组 + * + * @param type type + * @param userId userId * @return rows */ List getDataGroupTree(DataGroupTypeEnum type); + /** + * 通过缓存查询数据分组 + * + * @param type type + * @param userId userId + * @return rows + */ + List getDataGroupTree(DataGroupTypeEnum type, Long userId); + /** * 通过 id 查询 * diff --git a/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/entity/dto/data/DataGroupCreateDTO.java b/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/entity/dto/data/DataGroupCreateDTO.java index 8cd9a3de..e70b29ea 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/entity/dto/data/DataGroupCreateDTO.java +++ b/orion-ops-module-infra/orion-ops-module-infra-provider/src/main/java/com/orion/ops/module/infra/entity/dto/data/DataGroupCreateDTO.java @@ -25,6 +25,9 @@ import java.io.Serializable; @Schema(name = "DataGroupCreateDTO", description = "数据分组 创建请求业务对象") public class DataGroupCreateDTO implements Serializable { + @Schema(description = "userId") + private Long userId; + @NotNull @Schema(description = "父id") private Long parentId; diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupApiImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupApiImpl.java index f7dc6e24..b83c6b78 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupApiImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupApiImpl.java @@ -1,5 +1,6 @@ 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.module.infra.api.DataGroupApi; import com.orion.ops.module.infra.convert.DataGroupProviderConvert; @@ -42,6 +43,9 @@ public class DataGroupApiImpl implements DataGroupApi { @Override public Long createDataGroup(DataGroupTypeEnum type, DataGroupCreateDTO dto) { Valid.valid(dto); + if (dto.getUserId() == null) { + dto.setUserId(Const.SYSTEM_USER_ID); + } DataGroupCreateRequest request = DataGroupProviderConvert.MAPPER.toRequest(dto); request.setType(type.name()); return dataGroupService.createDataGroup(request); @@ -63,13 +67,23 @@ public class DataGroupApiImpl implements DataGroupApi { @Override public List getDataGroupList(DataGroupTypeEnum type) { - List rows = dataGroupService.getDataGroupListByCache(type.name()); + return this.getDataGroupList(type, Const.SYSTEM_USER_ID); + } + + @Override + public List getDataGroupList(DataGroupTypeEnum type, Long userId) { + List rows = dataGroupService.getDataGroupListByCache(type.name(), userId); return DataGroupProviderConvert.MAPPER.toList(rows); } @Override public List getDataGroupTree(DataGroupTypeEnum type) { - List rows = dataGroupService.getDataGroupTreeByCache(type.name()); + return this.getDataGroupTree(type, Const.SYSTEM_USER_ID); + } + + @Override + public List getDataGroupTree(DataGroupTypeEnum type, Long userId) { + List rows = dataGroupService.getDataGroupTreeByCache(type.name(), userId); return DataGroupProviderConvert.MAPPER.toList(rows); } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/dao/DataGroupDAO.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/dao/DataGroupDAO.java index 806a38e7..4f2c2dc0 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/dao/DataGroupDAO.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/dao/DataGroupDAO.java @@ -53,4 +53,16 @@ public interface DataGroupDAO extends IMapper { return this.selectList(wrapper); } + /** + * 通过 userId 查询 + * + * @param userId userId + * @return rows + */ + default List selectByUserId(Long userId) { + LambdaQueryWrapper wrapper = this.lambda() + .eq(DataGroupDO::getUserId, userId); + return this.selectList(wrapper); + } + } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java index 27905062..dbc45094 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java @@ -18,16 +18,16 @@ import java.util.concurrent.TimeUnit; public interface DataGroupCacheKeyDefine { CacheKeyDefine DATA_GROUP_LIST = new CacheKeyBuilder() - .key("data:group-list:{}") - .desc("数据分组列表结构 ${type}") + .key("data:group-list:{}:{}") + .desc("数据分组列表结构 ${type} ${userId}") .type(DataGroupCacheDTO.class) .struct(RedisCacheStruct.STRING) .timeout(1, TimeUnit.DAYS) .build(); CacheKeyDefine DATA_GROUP_TREE = new CacheKeyBuilder() - .key("data:group-tree:{}") - .desc("数据分组树结构 ${type}") + .key("data:group-tree:{}:{}") + .desc("数据分组树结构 ${type} ${userId}") .type(DataGroupCacheDTO.class) .struct(RedisCacheStruct.STRING) .timeout(1, TimeUnit.DAYS) @@ -42,8 +42,8 @@ public interface DataGroupCacheKeyDefine { .build(); CacheKeyDefine DATA_GROUP_REL_TYPE = new CacheKeyBuilder() - .key("data:group-rel:type:{}") - .desc("数据分组数据关联-类型 ${type}") + .key("data:group-rel:type:{}:{}") + .desc("数据分组数据关联-类型 ${type} ${userId}") .type(DataGroupRelCacheDTO.class) .struct(RedisCacheStruct.STRING) .timeout(1, TimeUnit.DAYS) diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupDO.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupDO.java index cb8101f1..de297115 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupDO.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupDO.java @@ -34,6 +34,10 @@ public class DataGroupDO extends BaseDO { @TableField("parent_id") private Long parentId; + @Schema(description = "用户id") + @TableField("user_id") + private Long userId; + @Schema(description = "组名称") @TableField("name") private String name; diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupRelDO.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupRelDO.java index 462d5cc3..efbc7386 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupRelDO.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/domain/DataGroupRelDO.java @@ -34,6 +34,10 @@ public class DataGroupRelDO extends BaseDO { @TableField("group_id") private Long groupId; + @Schema(description = "用户id") + @TableField("user_id") + private Long userId; + @Schema(description = "引用id") @TableField("rel_id") private Long relId; diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/data/DataGroupCreateRequest.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/data/DataGroupCreateRequest.java index aba4bd6e..f69fe61f 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/data/DataGroupCreateRequest.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/entity/request/data/DataGroupCreateRequest.java @@ -25,6 +25,10 @@ import java.io.Serializable; @Schema(name = "DataGroupCreateRequest", description = "数据分组 创建请求对象") public class DataGroupCreateRequest implements Serializable { + @NotNull + @Schema(description = "userId") + private Long userId; + @NotNull @Schema(description = "父id") private Long parentId; diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java index f8b6c7a4..2a2dde79 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java @@ -27,25 +27,28 @@ public interface DataGroupRelService { * 设置关联 * * @param type type + * @param userId userId * @param groupIdList groupIdList * @param relId relId */ - void updateGroupRel(String type, List groupIdList, Long relId); + void updateGroupRel(String type, Long userId, List groupIdList, Long relId); /** * 添加关联 * + * @param userId userId * @param groupId groupId * @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 list); + void addGroupRel(Long userId, List list); /** * 通过 type 查询 relId 缓存 diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupService.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupService.java index 2dd7dba8..b2d6297d 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupService.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupService.java @@ -43,25 +43,35 @@ public interface DataGroupService { /** * 通过缓存查询数据分组 - 列表 * - * @param type type + * @param type type + * @param userId userId * @return rows */ - List getDataGroupListByCache(String type); + List getDataGroupListByCache(String type, Long userId); /** * 通过缓存查询数据分组 - 树结构 * - * @param type type + * @param type type + * @param userId userId * @return rows */ - List getDataGroupTreeByCache(String type); + List getDataGroupTreeByCache(String type, Long userId); /** - * 删除数据分组 + * 通过 id 删除数据分组 * * @param id id * @return effect */ Integer deleteDataGroupById(Long id); + /** + * 通过 userId 删除数据分组 + * + * @param userId userId + * @return effect + */ + Integer deleteDataGroupByUserId(Long userId); + } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java index b527156f..96a4a871 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java @@ -84,6 +84,7 @@ public class DataGroupRelServiceImpl implements DataGroupRelService { List insertRecords = relIdList.stream() .map(s -> DataGroupRelDO.builder() .groupId(groupId) + .userId(group.getUserId()) .type(type) .relId(s) .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 @Transactional(rollbackFor = Exception.class) - public void updateGroupRel(String type, List groupIdList, Long relId) { + public void updateGroupRel(String type, Long userId, List groupIdList, Long relId) { Valid.notNull(relId); // 删除引用 this.deleteByRelId(type, relId); @@ -108,11 +109,12 @@ public class DataGroupRelServiceImpl implements DataGroupRelService { .type(type) .groupId(s) .relId(relId) + .userId(userId) .build()) .collect(Collectors.toList()); dataGroupRelDAO.insertBatch(relList); // 删除缓存 - this.deleteCache(type, groupIdList); + this.deleteCache(type, userId, groupIdList); } } @@ -125,7 +127,7 @@ public class DataGroupRelServiceImpl implements DataGroupRelService { .build(); // 插入 SpringHolder.getBean(DataGroupRelService.class) - .addGroupRel(Lists.singleton(record)); + .addGroupRel(groupId,Lists.singleton(record)); } @Override @@ -286,11 +288,12 @@ public class DataGroupRelServiceImpl implements DataGroupRelService { * 删除缓存 * * @param type type + * @param userId userId * @param groupIdList groupIdList */ - private void deleteCache(String type, Collection groupIdList) { + private void deleteCache(String type, Long userId, Collection groupIdList) { // 类型缓存 - List keyList = Lists.of(DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type)); + List keyList = Lists.of(DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type, userId)); // 分组缓存 if (!Lists.isEmpty(groupIdList)) { groupIdList.stream() diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java index 1fbd85fc..45f966bb 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java @@ -26,7 +26,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -61,7 +64,7 @@ public class DataGroupServiceImpl implements DataGroupService { Long id = record.getId(); log.info("DataGroupService-createDataGroup id: {}, effect: {}", id, effect); // 删除缓存 - this.deleteCache(request.getType()); + this.deleteCache(record); return id; } @@ -85,7 +88,7 @@ public class DataGroupServiceImpl implements DataGroupService { // 更新 int effect = dataGroupDAO.updateById(updateRecord); // 删除缓存 - this.deleteCache(record.getType()); + this.deleteCache(record); return effect; } @@ -139,7 +142,7 @@ public class DataGroupServiceImpl implements DataGroupService { effect = dataGroupDAO.updateById(update); } // 删除缓存 - this.deleteCache(type); + this.deleteCache(moveRecord); // 添加日志参数 OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName()); OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName()); @@ -148,15 +151,16 @@ public class DataGroupServiceImpl implements DataGroupService { } @Override - public List getDataGroupListByCache(String type) { + public List getDataGroupListByCache(String type, Long userId) { // 查询缓存 - String key = DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type); + String key = DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type, userId); List list = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_LIST); if (Lists.isEmpty(list)) { // 查询数据库 list = dataGroupDAO.of() .createWrapper() .eq(DataGroupDO::getType, type) + .eq(DataGroupDO::getUserId, userId) .then() .list(DataGroupConvert.MAPPER::toCache); // 设置屏障 防止穿透 @@ -170,13 +174,13 @@ public class DataGroupServiceImpl implements DataGroupService { } @Override - public List getDataGroupTreeByCache(String type) { + public List getDataGroupTreeByCache(String type, Long userId) { // 查询缓存 - String key = DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type); + String key = DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type, userId); List treeData = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_TREE); if (Lists.isEmpty(treeData)) { // 查询列表缓存 - List rows = this.getDataGroupListByCache(type); + List rows = this.getDataGroupListByCache(type, userId); // 设置屏障 防止穿透 CacheBarriers.checkBarrier(rows, DataGroupCacheDTO::new); if (!Lists.isEmpty(rows)) { @@ -213,12 +217,39 @@ public class DataGroupServiceImpl implements DataGroupService { dataGroupRelService.deleteByGroupIdList(type, deleteIdList); log.info("DataGroupService-deleteDataGroupById id: {}, effect: {}", id, effect); // 删除缓存 - this.deleteCache(type); + this.deleteCache(record); // 添加日志参数 OperatorLogs.add(OperatorLogs.GROUP_NAME, record.getName()); return effect; } + @Override + public Integer deleteDataGroupByUserId(Long userId) { + // 查询数据 + List rows = dataGroupDAO.selectByUserId(userId); + if (rows.isEmpty()) { + return rows.size(); + } + // 删除分组 + List idList = rows.stream() + .map(DataGroupDO::getId) + .collect(Collectors.toList()); + dataGroupDAO.deleteBatchIds(idList); + // 删除分组内数据 + Map> 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 * @@ -252,6 +283,7 @@ public class DataGroupServiceImpl implements DataGroupService { .ne(DataGroupDO::getId, domain.getId()) // 用其他字段做重复校验 .eq(DataGroupDO::getParentId, domain.getParentId()) + .eq(DataGroupDO::getUserId, domain.getUserId()) .eq(DataGroupDO::getName, domain.getName()) .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) { - RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type), - DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type)); + private void deleteCache(DataGroupDO record) { + String type = record.getType(); + 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 types) { + List 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); } } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/SystemUserServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/SystemUserServiceImpl.java index bbd537ce..0303bd31 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/SystemUserServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/SystemUserServiceImpl.java @@ -70,6 +70,9 @@ public class SystemUserServiceImpl implements SystemUserService { @Resource private DataPermissionService dataPermissionService; + @Resource + private DataGroupService dataGroupService; + @Resource private DataExtraService dataExtraService; @@ -251,6 +254,8 @@ public class SystemUserServiceImpl implements SystemUserService { preferenceService.deletePreferenceByUserId(id); // 删除用户数据权限 dataPermissionService.deleteByUserId(id); + // 删除用户分组信息 + dataGroupService.deleteDataGroupByUserId(id); // 删除用户拓展数据 dataExtraService.deleteByUserId(id); } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/resources/mapper/DataGroupMapper.xml b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/resources/mapper/DataGroupMapper.xml index cea2ba06..40055b1e 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/resources/mapper/DataGroupMapper.xml +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/resources/mapper/DataGroupMapper.xml @@ -6,6 +6,7 @@ + @@ -18,7 +19,7 @@ - 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