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

@@ -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.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<CommandSnippetVO> getCommandSnippetList(@Validated @RequestBody CommandSnippetQueryRequest request) {
return commandSnippetService.getCommandSnippetList(request);
public List<CommandSnippetVO> getCommandSnippetList() {
return commandSnippetService.getCommandSnippetList();
}
@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.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<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;
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<CommandSnippetVO> getCommandSnippetList(CommandSnippetQueryRequest request);
List<CommandSnippetVO> getCommandSnippetList();
/**
* 删除命令片段

View File

@@ -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<CommandSnippetVO> getCommandSnippetList(CommandSnippetQueryRequest request) {
// FIXME 查询缓存
public List<CommandSnippetVO> getCommandSnippetList() {
Long userId = SecurityUtils.getLoginUserId();
String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
// fixme 查询分组
// 条件
LambdaQueryWrapper<CommandSnippetDO> wrapper = this.buildQueryWrapper(request);
// 查询
return commandSnippetDAO.of(wrapper).list(CommandSnippetConvert.MAPPER::to);
// 查询缓存
List<CommandSnippetCacheDTO> 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<CommandSnippetDO> buildQueryWrapper(CommandSnippetQueryRequest request) {
return commandSnippetDAO.wrapper()
.eq(CommandSnippetDO::getName, request.getName());
}
}