feat: 命令分组.

This commit is contained in:
lijiahang
2024-01-24 17:39:16 +08:00
parent 49931ba227
commit 7d614f0bdc
30 changed files with 795 additions and 55 deletions

View File

@@ -0,0 +1,13 @@
package com.orion.ops.framework.common.validator.group;
import javax.validation.groups.Default;
/**
* 批量验证分组
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/9/1 19:13
*/
public interface Batch extends Default {
}

View File

@@ -45,9 +45,9 @@ public class CodeGenerators {
// .color("blue", "gray", "red", "green", "white")
// .valueUseFields()
// .build(),
Template.create("command_snippet", "命令片段", "command")
Template.create("command_snippet_group", "命令片段分组", "command")
.disableUnitTest()
.cache("command:snippet:list:{}", "命令片段列表 ${userId}")
.cache("command:snippet:group:{}", "命令片段分组 ${userId}")
.expire(1, TimeUnit.DAYS)
.vue("host", "command-snippet")
.build(),

View File

@@ -5,7 +5,7 @@ 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.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import com.orion.ops.module.asset.entity.vo.CommandSnippetWrapperVO;
import com.orion.ops.module.asset.service.CommandSnippetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@@ -15,7 +15,6 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 命令片段 api
@@ -51,8 +50,8 @@ public class CommandSnippetController {
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/list")
@Operation(summary = "查询全部命令片段")
public List<CommandSnippetVO> getCommandSnippetList() {
return commandSnippetService.getCommandSnippetList();
public CommandSnippetWrapperVO getCommandSnippetList() {
return commandSnippetService.getCommandSnippet();
}
@DeleteMapping("/delete")

View File

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

View File

@@ -0,0 +1,66 @@
package com.orion.ops.module.asset.controller;
import com.orion.ops.framework.common.validator.group.Id;
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.CommandSnippetGroupCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupDeleteRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetGroupVO;
import com.orion.ops.module.asset.service.CommandSnippetGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 命令片段分组 api
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Tag(name = "asset - 命令片段分组服务")
@Slf4j
@Validated
@RestWrapper
@RestController
@RequestMapping("/asset/command-snippet-group")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class CommandSnippetGroupController {
@Resource
private CommandSnippetGroupService commandSnippetGroupService;
@PostMapping("/create")
@Operation(summary = "创建命令片段分组")
public Long createCommandSnippetGroup(@Validated @RequestBody CommandSnippetGroupCreateRequest request) {
return commandSnippetGroupService.createCommandSnippetGroup(request);
}
@PutMapping("/update")
@Operation(summary = "更新命令片段分组")
public Integer updateCommandSnippetGroup(@Validated @RequestBody CommandSnippetGroupUpdateRequest request) {
return commandSnippetGroupService.updateCommandSnippetGroupById(request);
}
@IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/list")
@Operation(summary = "查询全部命令片段分组")
public List<CommandSnippetGroupVO> getCommandSnippetGroupList() {
return commandSnippetGroupService.getCommandSnippetGroupList();
}
@DeleteMapping("/delete")
@Operation(summary = "删除命令片段分组")
public Integer deleteCommandSnippetGroup(@Validated(Id.class) @RequestBody CommandSnippetGroupDeleteRequest request) {
return commandSnippetGroupService.deleteCommandSnippetGroup(request);
}
}

View File

@@ -0,0 +1,37 @@
package com.orion.ops.module.asset.convert;
import com.orion.ops.module.asset.entity.domain.CommandSnippetGroupDO;
import com.orion.ops.module.asset.entity.dto.CommandSnippetGroupCacheDTO;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetGroupVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 命令片段分组 内部对象转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Mapper
public interface CommandSnippetGroupConvert {
CommandSnippetGroupConvert MAPPER = Mappers.getMapper(CommandSnippetGroupConvert.class);
CommandSnippetGroupDO to(CommandSnippetGroupCreateRequest request);
CommandSnippetGroupDO to(CommandSnippetGroupUpdateRequest request);
CommandSnippetGroupVO to(CommandSnippetGroupDO domain);
List<CommandSnippetGroupVO> to(List<CommandSnippetGroupDO> list);
CommandSnippetGroupVO to(CommandSnippetGroupCacheDTO cache);
CommandSnippetGroupCacheDTO toCache(CommandSnippetGroupDO domain);
}

View File

@@ -1,5 +1,8 @@
package com.orion.ops.module.asset.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
import com.orion.ops.module.asset.entity.domain.CommandSnippetDO;
import org.apache.ibatis.annotations.Mapper;
@@ -14,4 +17,29 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CommandSnippetDAO extends IMapper<CommandSnippetDO> {
/**
* 设置 groupId 为 null
*
* @param groupId groupId
* @return effect
*/
default int setGroupIdWithNull(Long groupId) {
LambdaUpdateWrapper<CommandSnippetDO> wrapper = Wrappers.<CommandSnippetDO>lambdaUpdate()
.set(CommandSnippetDO::getGroupId, null)
.eq(CommandSnippetDO::getGroupId, groupId);
return this.update(null, wrapper);
}
/**
* 通过 groupId 删除
*
* @param groupId groupId
* @return effect
*/
default int deleteByGroupId(Long groupId) {
LambdaQueryWrapper<CommandSnippetDO> wrapper = this.lambda()
.eq(CommandSnippetDO::getGroupId, groupId);
return this.delete(wrapper);
}
}

View File

@@ -0,0 +1,17 @@
package com.orion.ops.module.asset.dao;
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
import com.orion.ops.module.asset.entity.domain.CommandSnippetGroupDO;
import org.apache.ibatis.annotations.Mapper;
/**
* 命令片段分组 Mapper 接口
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Mapper
public interface CommandSnippetGroupDAO extends IMapper<CommandSnippetGroupDO> {
}

View File

@@ -4,6 +4,7 @@ import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import com.orion.ops.module.asset.entity.dto.CommandSnippetCacheDTO;
import com.orion.ops.module.asset.entity.dto.CommandSnippetGroupCacheDTO;
import java.util.concurrent.TimeUnit;
@@ -16,7 +17,7 @@ import java.util.concurrent.TimeUnit;
*/
public interface CommandSnippetCacheKeyDefine {
CacheKeyDefine COMMAND_SNIPPET = new CacheKeyBuilder()
CacheKeyDefine SNIPPET = new CacheKeyBuilder()
.key("command:snippet:list:{}")
.desc("命令片段列表 ${userId}")
.type(CommandSnippetCacheDTO.class)
@@ -24,4 +25,12 @@ public interface CommandSnippetCacheKeyDefine {
.timeout(1, TimeUnit.DAYS)
.build();
CacheKeyDefine SNIPPET_GROUP = new CacheKeyBuilder()
.key("command:snippet:group:{}")
.desc("命令片段分组 ${userId}")
.type(CommandSnippetGroupCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)
.build();
}

View File

@@ -1,13 +1,13 @@
package com.orion.ops.module.asset.entity.domain;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.math.*;
/**
* 命令片段 实体对象
*
@@ -34,6 +34,10 @@ public class CommandSnippetDO extends BaseDO {
@TableField("user_id")
private Long userId;
@Schema(description = "分组id")
@TableField("group_id")
private Long groupId;
@Schema(description = "名称")
@TableField("name")
private String name;

View File

@@ -0,0 +1,41 @@
package com.orion.ops.module.asset.entity.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
/**
* 命令片段分组 实体对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(value = "command_snippet_group", autoResultMap = true)
@Schema(name = "CommandSnippetGroupDO", description = "命令片段分组 实体对象")
public class CommandSnippetGroupDO extends BaseDO {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@Schema(description = "用户id")
@TableField("user_id")
private Long userId;
@Schema(description = "分组名称")
@TableField("name")
private String name;
}

View File

@@ -8,7 +8,6 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* 命令片段 缓存对象
@@ -29,6 +28,9 @@ public class CommandSnippetCacheDTO implements LongCacheIdModel, Serializable {
@Schema(description = "id")
private Long id;
@Schema(description = "分组id")
private Long groupId;
@Schema(description = "名称")
private String name;

View File

@@ -0,0 +1,34 @@
package com.orion.ops.module.asset.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
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-24 12:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetGroupCacheDTO", description = "命令片段分组 缓存对象")
public class CommandSnippetGroupCacheDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "分组名称")
private String name;
}

View File

@@ -31,7 +31,7 @@ public class CommandSnippetCreateRequest implements Serializable {
@Schema(description = "名称")
private String name;
@Schema(description = "groupId")
@Schema(description = "分组id")
private Long groupId;
@NotBlank

View File

@@ -0,0 +1,34 @@
package com.orion.ops.module.asset.entity.request.command;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 命令片段分组 创建请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetGroupCreateRequest", description = "命令片段分组 创建请求对象")
public class CommandSnippetGroupCreateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotBlank
@Size(max = 64)
@Schema(description = "分组名称")
private String name;
}

View File

@@ -0,0 +1,37 @@
package com.orion.ops.module.asset.entity.request.command;
import com.orion.ops.framework.common.validator.group.Id;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 命令片段分组 删除请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetGroupDeleteRequest", description = "命令片段分组 删除请求对象")
public class CommandSnippetGroupDeleteRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull(groups = Id.class)
@Schema(description = "id")
private Long id;
@NotNull
@Schema(description = "是否删除组内数据")
private Boolean deleteItem;
}

View File

@@ -0,0 +1,39 @@
package com.orion.ops.module.asset.entity.request.command;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 命令片段分组 更新请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetGroupUpdateRequest", description = "命令片段分组 更新请求对象")
public class CommandSnippetGroupUpdateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Schema(description = "id")
private Long id;
@NotBlank
@Size(max = 64)
@Schema(description = "分组名称")
private String name;
}

View File

@@ -31,14 +31,14 @@ public class CommandSnippetUpdateRequest implements Serializable {
@Schema(description = "id")
private Long id;
@Schema(description = "分组id")
private Long groupId;
@NotBlank
@Size(max = 64)
@Schema(description = "名称")
private String name;
@Schema(description = "groupId")
private Long groupId;
@NotBlank
@Schema(description = "代码片段")
private String command;

View File

@@ -0,0 +1,33 @@
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-24 12:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetGroupVO", description = "命令片段分组 视图响应对象")
public class CommandSnippetGroupVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "分组名称")
private String name;
}

View File

@@ -27,6 +27,9 @@ public class CommandSnippetVO implements Serializable {
@Schema(description = "id")
private Long id;
@Schema(description = "分组id")
private Long groupId;
@Schema(description = "名称")
private String name;

View File

@@ -0,0 +1,34 @@
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;
import java.util.List;
/**
* 命令片段组合 视图响应对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/1/24 17:30
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetWrapperVO", description = "命令片段组合 视图响应对象")
public class CommandSnippetWrapperVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "分组")
private List<CommandSnippetGroupVO> groups;
@Schema(description = "命令片段")
private List<CommandSnippetVO> items;
}

View File

@@ -0,0 +1,50 @@
package com.orion.ops.module.asset.service;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupDeleteRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetGroupVO;
import java.util.List;
/**
* 命令片段分组 服务类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
public interface CommandSnippetGroupService {
/**
* 创建命令片段分组
*
* @param request request
* @return id
*/
Long createCommandSnippetGroup(CommandSnippetGroupCreateRequest request);
/**
* 更新命令片段分组
*
* @param request request
* @return effect
*/
Integer updateCommandSnippetGroupById(CommandSnippetGroupUpdateRequest request);
/**
* 查询全部命令片段分组
*
* @return rows
*/
List<CommandSnippetGroupVO> getCommandSnippetGroupList();
/**
* 删除命令片段分组
*
* @param request request
* @return effect
*/
Integer deleteCommandSnippetGroup(CommandSnippetGroupDeleteRequest request);
}

View File

@@ -3,6 +3,7 @@ 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.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import com.orion.ops.module.asset.entity.vo.CommandSnippetWrapperVO;
import java.util.List;
@@ -31,6 +32,13 @@ public interface CommandSnippetService {
*/
Integer updateCommandSnippetById(CommandSnippetUpdateRequest request);
/**
* 查询命令片段
*
* @return rows
*/
CommandSnippetWrapperVO getCommandSnippet();
/**
* 查询全部命令片段
*
@@ -46,4 +54,22 @@ public interface CommandSnippetService {
*/
Integer deleteCommandSnippetById(Long id);
/**
* 设置分组为 null
*
* @param userId userId
* @param groupId groupId
* @return effect
*/
Integer setGroupNull(Long userId, Long groupId);
/**
* 通过 groupId 删除
*
* @param userId userId
* @param groupId groupId
* @return effect
*/
Integer deleteByGroupId(Long userId, Long groupId);
}

View File

@@ -0,0 +1,156 @@
package com.orion.ops.module.asset.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.utils.Booleans;
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.CommandSnippetGroupConvert;
import com.orion.ops.module.asset.dao.CommandSnippetGroupDAO;
import com.orion.ops.module.asset.define.cache.CommandSnippetCacheKeyDefine;
import com.orion.ops.module.asset.entity.domain.CommandSnippetGroupDO;
import com.orion.ops.module.asset.entity.dto.CommandSnippetGroupCacheDTO;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupDeleteRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetGroupVO;
import com.orion.ops.module.asset.service.CommandSnippetGroupService;
import com.orion.ops.module.asset.service.CommandSnippetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* 命令片段分组 服务实现类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-24 12:28
*/
@Slf4j
@Service
public class CommandSnippetGroupServiceImpl implements CommandSnippetGroupService {
@Resource
private CommandSnippetGroupDAO commandSnippetGroupDAO;
@Resource
private CommandSnippetService commandSnippetService;
@Override
public Long createCommandSnippetGroup(CommandSnippetGroupCreateRequest request) {
Long userId = SecurityUtils.getLoginUserId();
log.info("CommandSnippetGroupService-createCommandSnippetGroup request: {}", JSON.toJSONString(request));
// 转换
CommandSnippetGroupDO record = CommandSnippetGroupConvert.MAPPER.to(request);
record.setUserId(userId);
// 查询数据是否冲突
this.checkCommandSnippetGroupPresent(record);
// 插入
int effect = commandSnippetGroupDAO.insert(record);
Long id = record.getId();
log.info("CommandSnippetGroupService-createCommandSnippetGroup id: {}, effect: {}", id, effect);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET_GROUP.format(userId);
RedisMaps.delete(cacheKey);
return id;
}
@Override
public Integer updateCommandSnippetGroupById(CommandSnippetGroupUpdateRequest request) {
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
log.info("CommandSnippetGroupService-updateCommandSnippetGroupById id: {}, request: {}", id, JSON.toJSONString(request));
// 查询
CommandSnippetGroupDO record = commandSnippetGroupDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
CommandSnippetGroupDO updateRecord = CommandSnippetGroupConvert.MAPPER.to(request);
updateRecord.setUserId(record.getUserId());
// 查询数据是否冲突
this.checkCommandSnippetGroupPresent(updateRecord);
// 更新
int effect = commandSnippetGroupDAO.updateById(updateRecord);
log.info("CommandSnippetGroupService-updateCommandSnippetGroupById effect: {}", effect);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET_GROUP.format(record.getUserId());
RedisMaps.delete(cacheKey);
return effect;
}
@Override
public List<CommandSnippetGroupVO> getCommandSnippetGroupList() {
Long userId = SecurityUtils.getLoginUserId();
// 查询缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET_GROUP.format(userId);
List<CommandSnippetGroupCacheDTO> list = RedisMaps.valuesJson(cacheKey, CommandSnippetCacheKeyDefine.SNIPPET_GROUP);
if (list.isEmpty()) {
// 查询数据库
list = commandSnippetGroupDAO.of()
.createWrapper()
.eq(CommandSnippetGroupDO::getUserId, userId)
.then()
.list(CommandSnippetGroupConvert.MAPPER::toCache);
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(list, CommandSnippetGroupCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(cacheKey, s -> s.getId().toString(), list);
}
// 删除屏障
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(CommandSnippetGroupConvert.MAPPER::to)
.collect(Collectors.toList());
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer deleteCommandSnippetGroup(CommandSnippetGroupDeleteRequest request) {
Long id = request.getId();
log.info("CommandSnippetGroupService-deleteCommandSnippetGroupById id: {}", id);
// 检查数据是否存在
CommandSnippetGroupDO record = commandSnippetGroupDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
Long userId = record.getUserId();
// 删除
int effect = commandSnippetGroupDAO.deleteById(id);
log.info("CommandSnippetGroupService-deleteCommandSnippetGroupById id: {}, effect: {}", id, effect);
if (Booleans.isTrue(request.getDeleteItem())) {
// 删除组内数据
commandSnippetService.deleteByGroupId(userId, id);
} else {
// 移动到根节点
commandSnippetService.setGroupNull(userId, id);
}
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET_GROUP.format(userId);
RedisMaps.delete(cacheKey, id);
return effect;
}
/**
* 检查对象是否存在
*
* @param domain domain
*/
private void checkCommandSnippetGroupPresent(CommandSnippetGroupDO domain) {
// 构造条件
LambdaQueryWrapper<CommandSnippetGroupDO> wrapper = commandSnippetGroupDAO.wrapper()
// 更新时忽略当前记录
.ne(CommandSnippetGroupDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(CommandSnippetGroupDO::getUserId, domain.getUserId())
.eq(CommandSnippetGroupDO::getName, domain.getName());
// 检查是否存在
boolean present = commandSnippetGroupDAO.of(wrapper).present();
Valid.isFalse(present, ErrorMessage.DATA_PRESENT);
}
}

View File

@@ -1,6 +1,8 @@
package com.orion.ops.module.asset.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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;
@@ -13,10 +15,11 @@ 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.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetGroupVO;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import com.orion.ops.module.asset.entity.vo.CommandSnippetWrapperVO;
import com.orion.ops.module.asset.service.CommandSnippetGroupService;
import com.orion.ops.module.asset.service.CommandSnippetService;
import com.orion.ops.module.infra.api.DataGroupRelApi;
import com.orion.ops.module.infra.enums.DataGroupTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -40,7 +43,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
private CommandSnippetDAO commandSnippetDAO;
@Resource
private DataGroupRelApi dataGroupRelApi;
private CommandSnippetGroupService commandSnippetGroupService;
@Override
public Long createCommandSnippet(CommandSnippetCreateRequest request) {
@@ -53,12 +56,8 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
int effect = commandSnippetDAO.insert(record);
Long id = record.getId();
log.info("CommandSnippetService-createCommandSnippet id: {}, effect: {}", id, effect);
// 设置分组引用
if (request.getGroupId() != null) {
dataGroupRelApi.addGroupRel(request.getGroupId(), id);
}
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
return id;
}
@@ -72,31 +71,39 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 查询
CommandSnippetDO record = commandSnippetDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
CommandSnippetDO updateRecord = CommandSnippetConvert.MAPPER.to(request);
// 更新
int effect = commandSnippetDAO.updateById(updateRecord);
LambdaUpdateWrapper<CommandSnippetDO> update = Wrappers.<CommandSnippetDO>lambdaUpdate()
.set(CommandSnippetDO::getGroupId, request.getGroupId())
.set(CommandSnippetDO::getName, request.getName())
.set(CommandSnippetDO::getCommand, request.getCommand())
.eq(CommandSnippetDO::getId, id)
.eq(CommandSnippetDO::getUserId, userId);
int effect = commandSnippetDAO.update(null, update);
log.info("CommandSnippetService-updateCommandSnippetById effect: {}", effect);
// fixme 删除分组引用
dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id);
// 设置分组引用
if (request.getGroupId() != null) {
dataGroupRelApi.addGroupRel(request.getGroupId(), id);
}
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
return effect;
}
@Override
public CommandSnippetWrapperVO getCommandSnippet() {
// 查询分组
List<CommandSnippetGroupVO> groups = commandSnippetGroupService.getCommandSnippetGroupList();
// 查询命令片段
List<CommandSnippetVO> items = this.getCommandSnippetList();
return CommandSnippetWrapperVO.builder()
.groups(groups)
.items(items)
.build();
}
@Override
public List<CommandSnippetVO> getCommandSnippetList() {
Long userId = SecurityUtils.getLoginUserId();
String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
// fixme 查询分组
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
// 查询缓存
List<CommandSnippetCacheDTO> list = RedisMaps.valuesJson(cacheKey, CommandSnippetCacheKeyDefine.COMMAND_SNIPPET);
List<CommandSnippetCacheDTO> list = RedisMaps.valuesJson(cacheKey, CommandSnippetCacheKeyDefine.SNIPPET);
if (list.isEmpty()) {
// 查询数据库
list = commandSnippetDAO.of()
@@ -107,7 +114,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(list, CommandSnippetCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET, s -> s.getId().toString(), list);
RedisMaps.putAllJson(CommandSnippetCacheKeyDefine.SNIPPET, s -> s.getId().toString(), list);
}
// 删除屏障
CacheBarriers.removeBarrier(list);
@@ -128,12 +135,28 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 删除
int effect = commandSnippetDAO.deleteById(id);
log.info("CommandSnippetService-deleteCommandSnippetById id: {}, effect: {}", id, effect);
// 删除分组引用
dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.COMMAND_SNIPPET.format(userId);
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey, id);
return effect;
}
@Override
public Integer setGroupNull(Long userId, Long groupId) {
int effect = commandSnippetDAO.setGroupIdWithNull(groupId);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
return effect;
}
@Override
public Integer deleteByGroupId(Long userId, Long groupId) {
int effect = commandSnippetDAO.deleteByGroupId(groupId);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
return effect;
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.orion.ops.module.asset.dao.CommandSnippetGroupDAO">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.orion.ops.module.asset.entity.domain.CommandSnippetGroupDO">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="name" property="name"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="creator" property="creator"/>
<result column="updater" property="updater"/>
<result column="deleted" property="deleted"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, user_id, name, create_time, update_time, creator, updater, deleted
</sql>
</mapper>

View File

@@ -6,6 +6,7 @@
<resultMap id="BaseResultMap" type="com.orion.ops.module.asset.entity.domain.CommandSnippetDO">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="group_id" property="groupId"/>
<result column="name" property="name"/>
<result column="prefix" property="prefix"/>
<result column="command" property="command"/>
@@ -18,7 +19,7 @@
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, user_id, name, prefix, command, create_time, update_time, creator, updater, deleted
id, user_id, group_id, name, prefix, command, create_time, update_time, creator, updater, deleted
</sql>
</mapper>

View File

@@ -1,13 +1,13 @@
package com.orion.ops.module.infra.entity.domain;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.math.*;
/**
* 数据拓展信息 实体对象
*

View File

@@ -1,13 +1,13 @@
package com.orion.ops.module.infra.entity.domain;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.math.*;
/**
* 数据权限 实体对象
*

View File

@@ -1,13 +1,13 @@
package com.orion.ops.module.infra.entity.domain;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.math.*;
/**
* 标签引用 实体对象
*