This commit is contained in:
lijiahang
2024-01-24 11:08:32 +08:00
parent 914359505e
commit 49931ba227
16 changed files with 48 additions and 187 deletions

View File

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

View File

@@ -146,12 +146,8 @@ public class HostServiceImpl implements HostService {
Valid.notNull(record, ErrorMessage.HOST_ABSENT);
// 转换
HostVO vo = HostConvert.MAPPER.to(record);
// 设置 tag 信息
List<TagDTO> tags = tagFuture.get();
// 设置分组信息
vo.setTags(tags);
Set<Long> groupIdList = groupIdFuture.get();
vo.setGroupIdList(groupIdList);
vo.setTags(tagFuture.get());
vo.setGroupIdList(groupIdFuture.get());
return vo;
}

View File

@@ -53,30 +53,11 @@ public interface DataGroupApi {
/**
* 通过缓存查询数据分组
*
* @param type type
* @param userId userId
* @return rows
*/
List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type, Long userId);
/**
* 通过缓存查询数据分组
*
* @param type type
* @param userId userId
* @param type type
* @return rows
*/
List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type);
/**
* 通过缓存查询数据分组
*
* @param type type
* @param userId userId
* @return rows
*/
List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type, Long userId);
/**
* 通过 id 查询
*

View File

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

View File

@@ -1,6 +1,5 @@
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;
@@ -43,9 +42,6 @@ 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);
@@ -67,23 +63,13 @@ public class DataGroupApiImpl implements DataGroupApi {
@Override
public List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type) {
return this.getDataGroupList(type, Const.SYSTEM_USER_ID);
}
@Override
public List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type, Long userId) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupListByCache(type.name(), userId);
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupListByCache(type.name());
return DataGroupProviderConvert.MAPPER.toList(rows);
}
@Override
public List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type) {
return this.getDataGroupTree(type, Const.SYSTEM_USER_ID);
}
@Override
public List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type, Long userId) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupTreeByCache(type.name(), userId);
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupTreeByCache(type.name());
return DataGroupProviderConvert.MAPPER.toList(rows);
}

View File

@@ -53,16 +53,4 @@ public interface DataGroupDAO extends IMapper<DataGroupDO> {
return this.selectList(wrapper);
}
/**
* 通过 userId 查询
*
* @param userId userId
* @return rows
*/
default List<DataGroupDO> selectByUserId(Long userId) {
LambdaQueryWrapper<DataGroupDO> wrapper = this.lambda()
.eq(DataGroupDO::getUserId, userId);
return this.selectList(wrapper);
}
}

View File

@@ -18,16 +18,16 @@ import java.util.concurrent.TimeUnit;
public interface DataGroupCacheKeyDefine {
CacheKeyDefine DATA_GROUP_LIST = new CacheKeyBuilder()
.key("data:group-list:{}:{}")
.desc("数据分组列表结构 ${type} ${userId}")
.key("data:group-list:{}")
.desc("数据分组列表结构 ${type}")
.type(DataGroupCacheDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();
CacheKeyDefine DATA_GROUP_TREE = new CacheKeyBuilder()
.key("data:group-tree:{}:{}")
.desc("数据分组树结构 ${type} ${userId}")
.key("data:group-tree:{}")
.desc("数据分组树结构 ${type}")
.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} ${userId}")
.key("data:group-rel:type:{}")
.desc("数据分组数据关联-类型 ${type}")
.type(DataGroupRelCacheDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)

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.*;
/**
* 数据分组 实体对象
*
@@ -34,10 +34,6 @@ 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;

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.*;
/**
* 数据分组关联 实体对象
*
@@ -34,10 +34,6 @@ 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;

View File

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

View File

@@ -27,28 +27,25 @@ public interface DataGroupRelService {
* 设置关联
*
* @param type type
* @param userId userId
* @param groupIdList groupIdList
* @param relId relId
*/
void updateGroupRel(String type, Long userId, List<Long> groupIdList, Long relId);
void updateGroupRel(String type, List<Long> groupIdList, Long relId);
/**
* 添加关联
*
* @param userId userId
* @param groupId groupId
* @param relId relId
*/
void addGroupRel(Long userId, Long groupId, Long relId);
void addGroupRel(Long groupId, Long relId);
/**
* 添加关联
*
* @param userId userId
* @param list list
* @param list list
*/
void addGroupRel(Long userId, List<DataGroupRelCreateRequest> list);
void addGroupRel(List<DataGroupRelCreateRequest> list);
/**
* 通过 type 查询 relId 缓存

View File

@@ -43,35 +43,25 @@ public interface DataGroupService {
/**
* 通过缓存查询数据分组 - 列表
*
* @param type type
* @param userId userId
* @param type type
* @return rows
*/
List<DataGroupCacheDTO> getDataGroupListByCache(String type, Long userId);
List<DataGroupCacheDTO> getDataGroupListByCache(String type);
/**
* 通过缓存查询数据分组 - 树结构
*
* @param type type
* @param userId userId
* @param type type
* @return rows
*/
List<DataGroupCacheDTO> getDataGroupTreeByCache(String type, Long userId);
List<DataGroupCacheDTO> getDataGroupTreeByCache(String type);
/**
* 通过 id 删除数据分组
* 删除数据分组
*
* @param id id
* @return effect
*/
Integer deleteDataGroupById(Long id);
/**
* 通过 userId 删除数据分组
*
* @param userId userId
* @return effect
*/
Integer deleteDataGroupByUserId(Long userId);
}

View File

@@ -84,7 +84,6 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
List<DataGroupRelDO> insertRecords = relIdList.stream()
.map(s -> DataGroupRelDO.builder()
.groupId(groupId)
.userId(group.getUserId())
.type(type)
.relId(s)
.build())
@@ -93,12 +92,12 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
}
}
// 删除缓存
this.deleteCache(type, group.getUserId(), Lists.of(groupId));
this.deleteCache(type, Lists.of(groupId));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateGroupRel(String type, Long userId, List<Long> groupIdList, Long relId) {
public void updateGroupRel(String type, List<Long> groupIdList, Long relId) {
Valid.notNull(relId);
// 删除引用
this.deleteByRelId(type, relId);
@@ -109,12 +108,11 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
.type(type)
.groupId(s)
.relId(relId)
.userId(userId)
.build())
.collect(Collectors.toList());
dataGroupRelDAO.insertBatch(relList);
// 删除缓存
this.deleteCache(type, userId, groupIdList);
this.deleteCache(type, groupIdList);
}
}
@@ -127,7 +125,7 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
.build();
// 插入
SpringHolder.getBean(DataGroupRelService.class)
.addGroupRel(groupId,Lists.singleton(record));
.addGroupRel(Lists.singleton(record));
}
@Override
@@ -288,12 +286,11 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
* 删除缓存
*
* @param type type
* @param userId userId
* @param groupIdList groupIdList
*/
private void deleteCache(String type, Long userId, Collection<Long> groupIdList) {
private void deleteCache(String type, Collection<Long> groupIdList) {
// 类型缓存
List<String> keyList = Lists.of(DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type, userId));
List<String> keyList = Lists.of(DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type));
// 分组缓存
if (!Lists.isEmpty(groupIdList)) {
groupIdList.stream()

View File

@@ -26,10 +26,7 @@ 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;
/**
@@ -64,7 +61,7 @@ public class DataGroupServiceImpl implements DataGroupService {
Long id = record.getId();
log.info("DataGroupService-createDataGroup id: {}, effect: {}", id, effect);
// 删除缓存
this.deleteCache(record);
this.deleteCache(request.getType());
return id;
}
@@ -88,7 +85,7 @@ public class DataGroupServiceImpl implements DataGroupService {
// 更新
int effect = dataGroupDAO.updateById(updateRecord);
// 删除缓存
this.deleteCache(record);
this.deleteCache(record.getType());
return effect;
}
@@ -142,7 +139,7 @@ public class DataGroupServiceImpl implements DataGroupService {
effect = dataGroupDAO.updateById(update);
}
// 删除缓存
this.deleteCache(moveRecord);
this.deleteCache(type);
// 添加日志参数
OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName());
OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName());
@@ -151,16 +148,15 @@ public class DataGroupServiceImpl implements DataGroupService {
}
@Override
public List<DataGroupCacheDTO> getDataGroupListByCache(String type, Long userId) {
public List<DataGroupCacheDTO> getDataGroupListByCache(String type) {
// 查询缓存
String key = DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type, userId);
String key = DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type);
List<DataGroupCacheDTO> 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);
// 设置屏障 防止穿透
@@ -174,13 +170,13 @@ public class DataGroupServiceImpl implements DataGroupService {
}
@Override
public List<DataGroupCacheDTO> getDataGroupTreeByCache(String type, Long userId) {
public List<DataGroupCacheDTO> getDataGroupTreeByCache(String type) {
// 查询缓存
String key = DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type, userId);
String key = DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type);
List<DataGroupCacheDTO> treeData = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_TREE);
if (Lists.isEmpty(treeData)) {
// 查询列表缓存
List<DataGroupCacheDTO> rows = this.getDataGroupListByCache(type, userId);
List<DataGroupCacheDTO> rows = this.getDataGroupListByCache(type);
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(rows, DataGroupCacheDTO::new);
if (!Lists.isEmpty(rows)) {
@@ -217,39 +213,12 @@ public class DataGroupServiceImpl implements DataGroupService {
dataGroupRelService.deleteByGroupIdList(type, deleteIdList);
log.info("DataGroupService-deleteDataGroupById id: {}, effect: {}", id, effect);
// 删除缓存
this.deleteCache(record);
this.deleteCache(type);
// 添加日志参数
OperatorLogs.add(OperatorLogs.GROUP_NAME, record.getName());
return effect;
}
@Override
public Integer deleteDataGroupByUserId(Long userId) {
// 查询数据
List<DataGroupDO> rows = dataGroupDAO.selectByUserId(userId);
if (rows.isEmpty()) {
return rows.size();
}
// 删除分组
List<Long> idList = rows.stream()
.map(DataGroupDO::getId)
.collect(Collectors.toList());
dataGroupDAO.deleteBatchIds(idList);
// 删除分组内数据
Map<String, List<Long>> 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
*
@@ -283,7 +252,6 @@ 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());
// 检查是否存在
@@ -294,30 +262,11 @@ public class DataGroupServiceImpl implements DataGroupService {
/**
* 删除缓存
*
* @param type type
* @param userId userId
* @param type 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<String> types) {
List<String> 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);
private void deleteCache(String type) {
RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type),
DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type));
}
}

View File

@@ -70,9 +70,6 @@ public class SystemUserServiceImpl implements SystemUserService {
@Resource
private DataPermissionService dataPermissionService;
@Resource
private DataGroupService dataGroupService;
@Resource
private DataExtraService dataExtraService;
@@ -254,8 +251,6 @@ public class SystemUserServiceImpl implements SystemUserService {
preferenceService.deletePreferenceByUserId(id);
// 删除用户数据权限
dataPermissionService.deleteByUserId(id);
// 删除用户分组信息
dataGroupService.deleteDataGroupByUserId(id);
// 删除用户拓展数据
dataExtraService.deleteByUserId(id);
}

View File

@@ -6,7 +6,6 @@
<resultMap id="BaseResultMap" type="com.orion.ops.module.infra.entity.domain.DataGroupDO">
<id column="id" property="id"/>
<result column="parent_id" property="parentId"/>
<result column="user_id" property="userId"/>
<result column="name" property="name"/>
<result column="type" property="type"/>
<result column="sort" property="sort"/>
@@ -19,7 +18,7 @@
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, parent_id, user_id, name, type, sort, create_time, update_time, creator, updater, deleted
id, parent_id, name, type, sort, create_time, update_time, creator, updater, deleted
</sql>
<update id="updateSort">