🚧 命令书签.

This commit is contained in:
lijiahangmax
2024-04-24 00:13:42 +08:00
parent e04e14a6e4
commit cdf10770d6
19 changed files with 721 additions and 22 deletions

View File

@@ -10,6 +10,7 @@ import com.orion.ops.framework.mybatis.core.generator.template.Table;
import com.orion.ops.framework.mybatis.core.generator.template.Template;
import java.io.File;
import java.util.concurrent.TimeUnit;
/**
* 代码生成器
@@ -33,7 +34,7 @@ public class CodeGenerators {
// .enableProviderApi()
// .disableUnitTest()
// .cache("dict:keys", "字典配置项")
// .expire(1, TimeUnit.DAYS)
// .expire(8, TimeUnit.HOURS)
// .vue("system", "dict-key")
// .enableRowSelection()
// .enableCardView()
@@ -48,8 +49,10 @@ public class CodeGenerators {
// .disableUnitTest()
// .vue("exec", "exec-template-host")
// .build(),
Template.create("path_bookmark", "路径标签", "host")
Template.create("path_bookmark", "路径标签", "path")
.disableUnitTest()
.cache("path:bookmark:list:{}", "路径标签列表 ${userId}")
.expire(8, TimeUnit.HOURS)
.vue("host", "path-bookmark")
.build(),
};

View File

@@ -0,0 +1,34 @@
### 创建路径标签
POST {{baseUrl}}/asset/path-bookmark/create
Content-Type: application/json
Authorization: {{token}}
{
"name": "",
"path": ""
}
### 更新路径标签
PUT {{baseUrl}}/asset/path-bookmark/update
Content-Type: application/json
Authorization: {{token}}
{
"id": "",
"name": "",
"path": ""
}
### 查询全部路径标签
GET {{baseUrl}}/asset/path-bookmark/list
Authorization: {{token}}
### 删除路径标签
DELETE {{baseUrl}}/asset/path-bookmark/delete?id=1
Authorization: {{token}}
###

View File

@@ -0,0 +1,63 @@
package com.orion.ops.module.asset.controller;
import com.orion.ops.framework.web.core.annotation.RestWrapper;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkCreateRequest;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkUpdateRequest;
import com.orion.ops.module.asset.entity.vo.PathBookmarkVO;
import com.orion.ops.module.asset.service.PathBookmarkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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.6
* @since 2024-4-23 23:15
*/
@Tag(name = "asset - 路径标签服务")
@Slf4j
@Validated
@RestWrapper
@RestController
@RequestMapping("/asset/path-bookmark")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class PathBookmarkController {
@Resource
private PathBookmarkService pathBookmarkService;
@PostMapping("/create")
@Operation(summary = "创建路径标签")
public Long createPathBookmark(@Validated @RequestBody PathBookmarkCreateRequest request) {
return pathBookmarkService.createPathBookmark(request);
}
@PutMapping("/update")
@Operation(summary = "更新路径标签")
public Integer updatePathBookmark(@Validated @RequestBody PathBookmarkUpdateRequest request) {
return pathBookmarkService.updatePathBookmarkById(request);
}
@GetMapping("/list")
@Operation(summary = "查询全部路径标签")
public List<PathBookmarkVO> getPathBookmarkList() {
return pathBookmarkService.getPathBookmarkList();
}
@DeleteMapping("/delete")
@Operation(summary = "删除路径标签")
@Parameter(name = "id", description = "id", required = true)
public Integer deletePathBookmark(@RequestParam("id") Long id) {
return pathBookmarkService.deletePathBookmarkById(id);
}
}

View File

@@ -0,0 +1,37 @@
package com.orion.ops.module.asset.convert;
import com.orion.ops.module.asset.entity.domain.PathBookmarkDO;
import com.orion.ops.module.asset.entity.dto.PathBookmarkCacheDTO;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkCreateRequest;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkUpdateRequest;
import com.orion.ops.module.asset.entity.vo.PathBookmarkVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 路径标签 内部对象转换器
*
* @author Jiahang Li
* @version 1.0.6
* @since 2024-4-23 23:15
*/
@Mapper
public interface PathBookmarkConvert {
PathBookmarkConvert MAPPER = Mappers.getMapper(PathBookmarkConvert.class);
PathBookmarkDO to(PathBookmarkCreateRequest request);
PathBookmarkDO to(PathBookmarkUpdateRequest request);
PathBookmarkVO to(PathBookmarkDO domain);
List<PathBookmarkVO> to(List<PathBookmarkDO> list);
PathBookmarkVO to(PathBookmarkCacheDTO cache);
PathBookmarkCacheDTO toCache(PathBookmarkDO domain);
}

View File

@@ -0,0 +1,45 @@
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.PathBookmarkDO;
import org.apache.ibatis.annotations.Mapper;
/**
* 路径标签 Mapper 接口
*
* @author Jiahang Li
* @version 1.0.6
* @since 2024-4-23 23:15
*/
@Mapper
public interface PathBookmarkDAO extends IMapper<PathBookmarkDO> {
/**
* 设置 groupId 为 null
*
* @param groupId groupId
* @return effect
*/
default int setGroupIdWithNull(Long groupId) {
LambdaUpdateWrapper<PathBookmarkDO> wrapper = Wrappers.<PathBookmarkDO>lambdaUpdate()
.set(PathBookmarkDO::getGroupId, null)
.eq(PathBookmarkDO::getGroupId, groupId);
return this.update(null, wrapper);
}
/**
* 通过 groupId 删除
*
* @param groupId groupId
* @return effect
*/
default int deleteByGroupId(Long groupId) {
LambdaQueryWrapper<PathBookmarkDO> wrapper = this.lambda()
.eq(PathBookmarkDO::getGroupId, groupId);
return this.delete(wrapper);
}
}

View File

@@ -4,7 +4,6 @@ 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;

View File

@@ -0,0 +1,27 @@
package com.orion.ops.module.asset.define.cache;
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.PathBookmarkCacheDTO;
import java.util.concurrent.TimeUnit;
/**
* 路径标签缓存 key
*
* @author Jiahang Li
* @version 1.0.6
* @since 2024-4-23 23:15
*/
public interface PathBookmarkCacheKeyDefine {
CacheKeyDefine PATH_BOOKMARK = new CacheKeyBuilder()
.key("path:bookmark:list:{}")
.desc("路径标签列表 ${userId}")
.type(PathBookmarkCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(8, TimeUnit.HOURS)
.build();
}

View File

@@ -0,0 +1,49 @@
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.6
* @since 2024-4-23 23:15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(value = "path_bookmark", autoResultMap = true)
@Schema(name = "PathBookmarkDO", description = "路径标签 实体对象")
public class PathBookmarkDO 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 = "分组id")
@TableField("group_id")
private Long groupId;
@Schema(description = "名称")
@TableField("name")
private String name;
@Schema(description = "路径")
@TableField("path")
private String path;
}

View File

@@ -0,0 +1,40 @@
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.6
* @since 2024-4-23 23:15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "PathBookmarkCacheDTO", description = "路径标签 缓存对象")
public class PathBookmarkCacheDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "分组id")
private Long groupId;
@Schema(description = "名称")
private String name;
@Schema(description = "路径")
private String path;
}

View File

@@ -26,14 +26,14 @@ public class CommandSnippetCreateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "分组id")
private Long groupId;
@NotBlank
@Size(max = 64)
@Schema(description = "名称")
private String name;
@Schema(description = "分组id")
private Long groupId;
@NotBlank
@Schema(description = "代码片段")
private String command;

View File

@@ -0,0 +1,42 @@
package com.orion.ops.module.asset.entity.request.path;
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.6
* @since 2024-4-23 23:15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "PathBookmarkCreateRequest", description = "路径标签 创建请求对象")
public class PathBookmarkCreateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "分组id")
private Long groupId;
@NotBlank
@Size(max = 64)
@Schema(description = "名称")
private String name;
@NotBlank
@Size(max = 1024)
@Schema(description = "路径")
private String path;
}

View File

@@ -0,0 +1,47 @@
package com.orion.ops.module.asset.entity.request.path;
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.6
* @since 2024-4-23 23:15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "PathBookmarkUpdateRequest", description = "路径标签 更新请求对象")
public class PathBookmarkUpdateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Schema(description = "id")
private Long id;
@Schema(description = "分组id")
private Long groupId;
@NotBlank
@Size(max = 64)
@Schema(description = "名称")
private String name;
@NotBlank
@Size(max = 1024)
@Schema(description = "路径")
private String path;
}

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.6
* @since 2024-4-23 23:15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "PathBookmarkVO", description = "路径标签 视图响应对象")
public class PathBookmarkVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "用户id")
private Long userId;
@Schema(description = "名称")
private String name;
@Schema(description = "路径")
private String path;
}

View File

@@ -6,6 +6,7 @@ import com.orion.lang.exception.ConnectionRuntimeException;
import com.orion.lang.exception.SftpException;
import com.orion.lang.exception.argument.InvalidArgumentException;
import com.orion.lang.support.timeout.TimeoutChecker;
import com.orion.lang.support.timeout.TimeoutEndpoint;
import com.orion.lang.utils.Booleans;
import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.Strings;
@@ -52,7 +53,7 @@ public class ExecCommandHandler implements IExecCommandHandler {
private final ExecCommandHostDTO execHostCommand;
private final TimeoutChecker timeoutChecker;
private final TimeoutChecker<TimeoutEndpoint> timeoutChecker;
@Getter
private ExecHostStatusEnum status;
@@ -69,7 +70,7 @@ public class ExecCommandHandler implements IExecCommandHandler {
public ExecCommandHandler(ExecCommandDTO execCommand,
ExecCommandHostDTO execHostCommand,
TimeoutChecker timeoutChecker) {
TimeoutChecker<TimeoutEndpoint> timeoutChecker) {
this.status = ExecHostStatusEnum.WAITING;
this.execCommand = execCommand;
this.execHostCommand = execHostCommand;

View File

@@ -0,0 +1,24 @@
<?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.PathBookmarkDAO">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.orion.ops.module.asset.entity.domain.PathBookmarkDO">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="group_id" property="groupId"/>
<result column="name" property="name"/>
<result column="path" property="path"/>
<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, group_id, name, path, create_time, update_time, creator, updater, deleted
</sql>
</mapper>

View File

@@ -0,0 +1,67 @@
package com.orion.ops.module.asset.service;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkCreateRequest;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkUpdateRequest;
import com.orion.ops.module.asset.entity.vo.PathBookmarkVO;
import java.util.List;
/**
* 路径标签 服务类
*
* @author Jiahang Li
* @version 1.0.6
* @since 2024-4-23 23:15
*/
public interface PathBookmarkService {
/**
* 创建路径标签
*
* @param request request
* @return id
*/
Long createPathBookmark(PathBookmarkCreateRequest request);
/**
* 更新路径标签
*
* @param request request
* @return effect
*/
Integer updatePathBookmarkById(PathBookmarkUpdateRequest request);
/**
* 查询全部路径标签
*
* @return rows
*/
List<PathBookmarkVO> getPathBookmarkList();
/**
* 删除路径标签
*
* @param id id
* @return effect
*/
Integer deletePathBookmarkById(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

@@ -88,10 +88,10 @@ public class CommandSnippetGroupServiceImpl implements CommandSnippetGroupServic
Integer effect = dataGroupApi.deleteDataGroupById(id);
if (Booleans.isTrue(request.getDeleteItem())) {
// 删除组内数据
commandSnippetService.deleteByGroupId(userId, id);
effect += commandSnippetService.deleteByGroupId(userId, id);
} else {
// 移动到根节点
commandSnippetService.setGroupNull(userId, id);
effect += commandSnippetService.setGroupNull(userId, id);
}
return effect;
}

View File

@@ -1,6 +1,7 @@
package com.orion.ops.module.asset.service.impl;
import com.alibaba.fastjson.JSON;
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.common.constant.ErrorMessage;
@@ -55,18 +56,18 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 转换
CommandSnippetDO record = CommandSnippetConvert.MAPPER.to(request);
record.setUserId(userId);
// 查询数据是否冲突
this.checkCommandSnippetPresent(record);
// 插入
int effect = commandSnippetDAO.insert(record);
Long id = record.getId();
log.info("CommandSnippetService-createCommandSnippet id: {}, effect: {}", id, effect);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
RedisMaps.delete(CommandSnippetCacheKeyDefine.SNIPPET.format(userId));
return id;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer updateCommandSnippetById(CommandSnippetUpdateRequest request) {
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
Long userId = SecurityUtils.getLoginUserId();
@@ -74,6 +75,9 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 查询
CommandSnippetDO record = commandSnippetDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 查询数据是否冲突
CommandSnippetDO updateRecord = CommandSnippetConvert.MAPPER.to(request);
this.checkCommandSnippetPresent(updateRecord);
// 更新
LambdaUpdateWrapper<CommandSnippetDO> update = Wrappers.<CommandSnippetDO>lambdaUpdate()
.set(CommandSnippetDO::getGroupId, request.getGroupId())
@@ -84,8 +88,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
int effect = commandSnippetDAO.update(null, update);
log.info("CommandSnippetService-updateCommandSnippetById effect: {}", effect);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
RedisMaps.delete(CommandSnippetCacheKeyDefine.SNIPPET.format(userId));
return effect;
}
@@ -130,7 +133,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(list, CommandSnippetCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(CommandSnippetCacheKeyDefine.SNIPPET, s -> s.getId().toString(), list);
RedisMaps.putAllJson(cacheKey, s -> s.getId().toString(), list);
}
// 删除屏障
CacheBarriers.removeBarrier(list);
@@ -153,8 +156,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
int effect = commandSnippetDAO.deleteById(id);
log.info("CommandSnippetService-deleteCommandSnippetById id: {}, effect: {}", id, effect);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey, id);
RedisMaps.delete(CommandSnippetCacheKeyDefine.SNIPPET.format(userId), id);
return effect;
}
@@ -162,8 +164,7 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
public Integer setGroupNull(Long userId, Long groupId) {
int effect = commandSnippetDAO.setGroupIdWithNull(groupId);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
RedisMaps.delete(CommandSnippetCacheKeyDefine.SNIPPET.format(userId));
return effect;
}
@@ -171,9 +172,27 @@ public class CommandSnippetServiceImpl implements CommandSnippetService {
public Integer deleteByGroupId(Long userId, Long groupId) {
int effect = commandSnippetDAO.deleteByGroupId(groupId);
// 删除缓存
String cacheKey = CommandSnippetCacheKeyDefine.SNIPPET.format(userId);
RedisMaps.delete(cacheKey);
RedisMaps.delete(CommandSnippetCacheKeyDefine.SNIPPET.format(userId));
return effect;
}
/**
* 检查对象是否存在
*
* @param domain domain
*/
private void checkCommandSnippetPresent(CommandSnippetDO domain) {
// 构造条件
LambdaQueryWrapper<CommandSnippetDO> wrapper = commandSnippetDAO.wrapper()
// 更新时忽略当前记录
.ne(CommandSnippetDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(CommandSnippetDO::getUserId, domain.getUserId())
.eq(CommandSnippetDO::getGroupId, domain.getGroupId())
.eq(CommandSnippetDO::getName, domain.getName());
// 检查是否存在
boolean present = commandSnippetDAO.of(wrapper).present();
Valid.isFalse(present, ErrorMessage.DATA_PRESENT);
}
}

View File

@@ -0,0 +1,163 @@
package com.orion.ops.module.asset.service.impl;
import com.alibaba.fastjson.JSON;
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.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.PathBookmarkConvert;
import com.orion.ops.module.asset.dao.PathBookmarkDAO;
import com.orion.ops.module.asset.define.cache.PathBookmarkCacheKeyDefine;
import com.orion.ops.module.asset.entity.domain.PathBookmarkDO;
import com.orion.ops.module.asset.entity.dto.PathBookmarkCacheDTO;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkCreateRequest;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkUpdateRequest;
import com.orion.ops.module.asset.entity.vo.PathBookmarkVO;
import com.orion.ops.module.asset.service.PathBookmarkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* 路径标签 服务实现类
*
* @author Jiahang Li
* @version 1.0.6
* @since 2024-4-23 23:15
*/
@Slf4j
@Service
public class PathBookmarkServiceImpl implements PathBookmarkService {
@Resource
private PathBookmarkDAO pathBookmarkDAO;
@Override
public Long createPathBookmark(PathBookmarkCreateRequest request) {
Long userId = SecurityUtils.getLoginUserId();
log.info("PathBookmarkService-createPathBookmark request: {}", JSON.toJSONString(request));
// 转换
PathBookmarkDO record = PathBookmarkConvert.MAPPER.to(request);
record.setUserId(userId);
// 查询数据是否冲突
this.checkPathBookmarkPresent(record);
// 插入
int effect = pathBookmarkDAO.insert(record);
Long id = record.getId();
log.info("PathBookmarkService-createPathBookmark id: {}, effect: {}", id, effect);
// 删除缓存
RedisMaps.delete(PathBookmarkCacheKeyDefine.PATH_BOOKMARK.format(userId));
return id;
}
@Override
public Integer updatePathBookmarkById(PathBookmarkUpdateRequest request) {
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
Long userId = SecurityUtils.getLoginUserId();
log.info("PathBookmarkService-updatePathBookmarkById id: {}, request: {}", id, JSON.toJSONString(request));
// 查询
PathBookmarkDO record = pathBookmarkDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 查询数据是否冲突
PathBookmarkDO updateRecord = PathBookmarkConvert.MAPPER.to(request);
this.checkPathBookmarkPresent(updateRecord);
// 更新
LambdaUpdateWrapper<PathBookmarkDO> update = Wrappers.<PathBookmarkDO>lambdaUpdate()
.set(PathBookmarkDO::getGroupId, request.getGroupId())
.set(PathBookmarkDO::getName, request.getName())
.set(PathBookmarkDO::getPath, request.getPath())
.eq(PathBookmarkDO::getId, id)
.eq(PathBookmarkDO::getUserId, userId);
int effect = pathBookmarkDAO.update(null, update);
log.info("PathBookmarkService-updatePathBookmarkById effect: {}", effect);
// 删除缓存
RedisMaps.delete(PathBookmarkCacheKeyDefine.PATH_BOOKMARK.format(userId));
return effect;
}
@Override
public List<PathBookmarkVO> getPathBookmarkList() {
Long userId = SecurityUtils.getLoginUserId();
String cacheKey = PathBookmarkCacheKeyDefine.PATH_BOOKMARK.format(userId);
// 查询缓存
List<PathBookmarkCacheDTO> list = RedisMaps.valuesJson(cacheKey, PathBookmarkCacheKeyDefine.PATH_BOOKMARK);
if (list.isEmpty()) {
// 查询数据库
list = pathBookmarkDAO.of()
.createWrapper()
.eq(PathBookmarkDO::getUserId, userId)
.then()
.list(PathBookmarkConvert.MAPPER::toCache);
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(list, PathBookmarkCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(cacheKey, s -> s.getId().toString(), list);
}
// 删除屏障
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(PathBookmarkConvert.MAPPER::to)
.sorted(Comparator.comparing(PathBookmarkVO::getId))
.collect(Collectors.toList());
}
@Override
public Integer deletePathBookmarkById(Long id) {
Long userId = SecurityUtils.getLoginUserId();
log.info("PathBookmarkService-deletePathBookmarkById id: {}", id);
// 检查数据是否存在
PathBookmarkDO record = pathBookmarkDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 删除
int effect = pathBookmarkDAO.deleteById(id);
log.info("PathBookmarkService-deletePathBookmarkById id: {}, effect: {}", id, effect);
// 删除缓存
RedisMaps.delete(PathBookmarkCacheKeyDefine.PATH_BOOKMARK.format(userId), id);
return effect;
}
@Override
public Integer setGroupNull(Long userId, Long groupId) {
int effect = pathBookmarkDAO.setGroupIdWithNull(groupId);
// 删除缓存
RedisMaps.delete(PathBookmarkCacheKeyDefine.PATH_BOOKMARK.format(userId));
return effect;
}
@Override
public Integer deleteByGroupId(Long userId, Long groupId) {
int effect = pathBookmarkDAO.deleteByGroupId(groupId);
// 删除缓存
RedisMaps.delete(PathBookmarkCacheKeyDefine.PATH_BOOKMARK.format(userId));
return effect;
}
/**
* 检查对象是否存在
*
* @param domain domain
*/
private void checkPathBookmarkPresent(PathBookmarkDO domain) {
// 构造条件
LambdaQueryWrapper<PathBookmarkDO> wrapper = pathBookmarkDAO.wrapper()
// 更新时忽略当前记录
.ne(PathBookmarkDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(PathBookmarkDO::getUserId, domain.getUserId())
.eq(PathBookmarkDO::getGroupId, domain.getGroupId())
.eq(PathBookmarkDO::getName, domain.getName());
// 检查是否存在
boolean present = pathBookmarkDAO.of(wrapper).present();
Valid.isFalse(present, ErrorMessage.DATA_PRESENT);
}
}