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

@@ -53,11 +53,30 @@ public interface DataGroupApi {
/**
* 通过缓存查询数据分组
*
* @param type type
* @param type type
* @param userId userId
* @return rows
*/
List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type, Long userId);
/**
* 通过缓存查询数据分组
*
* @param type type
* @param userId userId
* @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,6 +25,9 @@ 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,5 +1,6 @@
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;
@@ -42,6 +43,9 @@ 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);
@@ -63,13 +67,23 @@ public class DataGroupApiImpl implements DataGroupApi {
@Override
public List<DataGroupDTO> getDataGroupList(DataGroupTypeEnum type) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupListByCache(type.name());
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);
return DataGroupProviderConvert.MAPPER.toList(rows);
}
@Override
public List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type) {
List<DataGroupCacheDTO> rows = dataGroupService.getDataGroupTreeByCache(type.name());
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);
return DataGroupProviderConvert.MAPPER.toList(rows);
}

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -6,6 +6,7 @@
<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"/>
@@ -18,7 +19,7 @@
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, parent_id, name, type, sort, create_time, update_time, creator, updater, deleted
id, parent_id, user_id, name, type, sort, create_time, update_time, creator, updater, deleted
</sql>
<update id="updateSort">