🔨 命令分组替换为通用分组模型.

This commit is contained in:
lijiahang
2024-04-23 19:10:15 +08:00
parent f146989a1a
commit e04e14a6e4
11 changed files with 92 additions and 184 deletions

View File

@@ -1,10 +1,12 @@
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 com.orion.ops.module.infra.entity.dto.data.DataGroupCreateDTO;
import com.orion.ops.module.infra.entity.dto.data.DataGroupDTO;
import com.orion.ops.module.infra.entity.dto.data.DataGroupRenameDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@@ -22,16 +24,12 @@ public interface CommandSnippetGroupConvert {
CommandSnippetGroupConvert MAPPER = Mappers.getMapper(CommandSnippetGroupConvert.class);
CommandSnippetGroupDO to(CommandSnippetGroupCreateRequest request);
DataGroupCreateDTO to(CommandSnippetGroupCreateRequest request);
CommandSnippetGroupDO to(CommandSnippetGroupUpdateRequest request);
DataGroupRenameDTO to(CommandSnippetGroupUpdateRequest request);
CommandSnippetGroupVO to(CommandSnippetGroupDO domain);
CommandSnippetGroupVO to(DataGroupDTO domain);
List<CommandSnippetGroupVO> to(List<CommandSnippetGroupDO> list);
CommandSnippetGroupVO to(CommandSnippetGroupCacheDTO cache);
CommandSnippetGroupCacheDTO toCache(CommandSnippetGroupDO domain);
}

View File

@@ -1,17 +0,0 @@
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

@@ -25,12 +25,4 @@ public interface CommandSnippetCacheKeyDefine {
.timeout(8, TimeUnit.HOURS)
.build();
CacheKeyDefine SNIPPET_GROUP = new CacheKeyBuilder()
.key("command:snippet:group:{}")
.desc("命令片段分组 ${userId}")
.type(CommandSnippetGroupCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(8, TimeUnit.HOURS)
.build();
}

View File

@@ -1,34 +0,0 @@
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

@@ -1,24 +1,24 @@
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.Const;
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 com.orion.ops.module.infra.api.DataGroupApi;
import com.orion.ops.module.infra.api.DataGroupUserApi;
import com.orion.ops.module.infra.entity.dto.data.DataGroupCreateDTO;
import com.orion.ops.module.infra.entity.dto.data.DataGroupDTO;
import com.orion.ops.module.infra.entity.dto.data.DataGroupRenameDTO;
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 +40,10 @@ import java.util.stream.Collectors;
public class CommandSnippetGroupServiceImpl implements CommandSnippetGroupService {
@Resource
private CommandSnippetGroupDAO commandSnippetGroupDAO;
private DataGroupApi dataGroupApi;
@Resource
private DataGroupUserApi dataGroupUserApi;
@Resource
private CommandSnippetService commandSnippetService;
@@ -49,81 +52,40 @@ public class CommandSnippetGroupServiceImpl implements CommandSnippetGroupServic
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;
// 创建
DataGroupCreateDTO create = CommandSnippetGroupConvert.MAPPER.to(request);
create.setParentId(Const.ROOT_PARENT_ID);
return dataGroupUserApi.createDataGroup(DataGroupTypeEnum.COMMAND_SNIPPET, userId, create);
}
@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;
// 重命名
DataGroupRenameDTO rename = CommandSnippetGroupConvert.MAPPER.to(request);
return dataGroupApi.renameDataGroup(rename);
}
@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()
// 查询分组
return dataGroupUserApi.getDataGroupList(DataGroupTypeEnum.COMMAND_SNIPPET, userId)
.stream()
.sorted(Comparator.comparing(DataGroupDTO::getSort))
.map(CommandSnippetGroupConvert.MAPPER::to)
.sorted(Comparator.comparing(CommandSnippetGroupVO::getId))
.collect(Collectors.toList());
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer deleteCommandSnippetGroup(CommandSnippetGroupDeleteRequest request) {
Long userId = SecurityUtils.getLoginUserId();
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);
// 删除分组
Integer effect = dataGroupApi.deleteDataGroupById(id);
if (Booleans.isTrue(request.getDeleteItem())) {
// 删除组内数据
commandSnippetService.deleteByGroupId(userId, id);
@@ -131,28 +93,7 @@ public class CommandSnippetGroupServiceImpl implements CommandSnippetGroupServic
// 移动到根节点
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,22 +0,0 @@
<?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>