parent
781463417c
commit
328b61b252
@ -414,12 +414,15 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
dictTypeList.addAll(dictTypeMapper.selectList());
|
dictTypeList.addAll(dictTypeMapper.selectList());
|
||||||
dictDataList.addAll(dictDataMapper.selectList());
|
dictDataList.addAll(dictDataMapper.selectList());
|
||||||
});
|
});
|
||||||
Map<String, List<SysDictType>> typeMap = StreamUtils.groupByKey(dictTypeList, TenantEntity::getTenantId);
|
// 所有租户字典类型
|
||||||
Map<String, Map<String, List<SysDictData>>> typeDataMap = StreamUtils.groupBy2Key(
|
Map<String, List<SysDictType>> dictTypeMap = StreamUtils.groupByKey(dictTypeList, TenantEntity::getTenantId);
|
||||||
dictDataList, TenantEntity::getTenantId, SysDictData::getDictType);
|
// 所有租户字典数据
|
||||||
// 管理租户字典数据
|
Map<String, Map<String, List<SysDictData>>> dictDataMap = StreamUtils.groupBy2Key(dictDataList, TenantEntity::getTenantId, SysDictData::getDictType);
|
||||||
List<SysDictType> defaultTypeMap = typeMap.get(TenantConstants.DEFAULT_TENANT_ID);
|
|
||||||
Map<String, List<SysDictData>> defaultTypeDataMap = typeDataMap.get(TenantConstants.DEFAULT_TENANT_ID);
|
// 默认租户字典类型列表
|
||||||
|
List<SysDictType> defaultDictTypeList = dictTypeMap.get(TenantConstants.DEFAULT_TENANT_ID);
|
||||||
|
// 默认租户字典数据
|
||||||
|
Map<String, List<SysDictData>> defaultDictDataMap = dictDataMap.get(TenantConstants.DEFAULT_TENANT_ID);
|
||||||
|
|
||||||
// 获取所有租户编号
|
// 获取所有租户编号
|
||||||
List<String> tenantIds = baseMapper.selectObjs(
|
List<String> tenantIds = baseMapper.selectObjs(
|
||||||
@ -427,57 +430,67 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
.eq(SysTenant::getStatus, SystemConstants.NORMAL), x -> {
|
.eq(SysTenant::getStatus, SystemConstants.NORMAL), x -> {
|
||||||
return Convert.toStr(x);
|
return Convert.toStr(x);
|
||||||
});
|
});
|
||||||
|
// 待入库的字典类型和字典数据
|
||||||
List<SysDictType> saveTypeList = new ArrayList<>();
|
List<SysDictType> saveTypeList = new ArrayList<>();
|
||||||
List<SysDictData> saveDataList = new ArrayList<>();
|
List<SysDictData> saveDataList = new ArrayList<>();
|
||||||
Set<String> set = new HashSet<>();
|
// 待同步的租户编号(用于清除对于租户的字典缓存)
|
||||||
|
Set<String> syncTenantIds = new HashSet<>();
|
||||||
|
// 循环所有租户,处理需要同步的数据
|
||||||
for (String tenantId : tenantIds) {
|
for (String tenantId : tenantIds) {
|
||||||
|
// 排除默认租户
|
||||||
if (TenantConstants.DEFAULT_TENANT_ID.equals(tenantId)) {
|
if (TenantConstants.DEFAULT_TENANT_ID.equals(tenantId)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (SysDictType dictType : defaultTypeMap) {
|
// 根据默认租户的字典类型进行数据同步
|
||||||
List<String> typeList = StreamUtils.toList(typeMap.get(tenantId), SysDictType::getDictType);
|
for (SysDictType dictType : defaultDictTypeList) {
|
||||||
List<SysDictData> dataList = defaultTypeDataMap.get(dictType.getDictType());
|
// 获取当前租户的字典类型列表
|
||||||
|
List<String> typeList = StreamUtils.toList(dictTypeMap.get(tenantId), SysDictType::getDictType);
|
||||||
|
// 根据字典类型获取默认租户的字典数据
|
||||||
|
List<SysDictData> defaultDictDataList = defaultDictDataMap.get(dictType.getDictType());
|
||||||
|
// 排除不需要同步的字典数据
|
||||||
|
Set<String> excludeDictDataSet = CollUtil.newHashSet();
|
||||||
|
// 处理 存在type不存在data 的情况
|
||||||
if (typeList.contains(dictType.getDictType())) {
|
if (typeList.contains(dictType.getDictType())) {
|
||||||
List<SysDictData> dataListTenant = typeDataMap.get(tenantId).get(dictType.getDictType());
|
// 获取租户字典数据
|
||||||
Map<String, SysDictData> map = StreamUtils.toIdentityMap(dataListTenant, SysDictData::getDictValue);
|
Optional.ofNullable(dictDataMap.get(tenantId))
|
||||||
for (SysDictData dictData : dataList) {
|
// 获取租户当前字典类型的字典数据
|
||||||
if (!map.containsKey(dictData.getDictValue())) {
|
.map(tenantDictDataMap -> tenantDictDataMap.get(dictType.getDictType()))
|
||||||
SysDictData data = BeanUtil.toBean(dictData, SysDictData.class);
|
// 保存字典数据项的字典键值,用于判断数据是否需要同步
|
||||||
// 设置字典编码为 null
|
.map(data -> StreamUtils.toSet(data, SysDictData::getDictValue))
|
||||||
data.setDictCode(null);
|
// 添加到排除集合中
|
||||||
data.setTenantId(tenantId);
|
.ifPresent(excludeDictDataSet::addAll);
|
||||||
data.setCreateTime(null);
|
|
||||||
data.setUpdateTime(null);
|
|
||||||
data.setCreateDept(null);
|
|
||||||
data.setCreateBy(null);
|
|
||||||
data.setUpdateBy(null);
|
|
||||||
set.add(tenantId);
|
|
||||||
saveDataList.add(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
// 同步字典类型
|
||||||
SysDictType type = BeanUtil.toBean(dictType, SysDictType.class);
|
SysDictType type = BeanUtil.toBean(dictType, SysDictType.class);
|
||||||
type.setDictId(null);
|
type.setDictId(null);
|
||||||
type.setTenantId(tenantId);
|
type.setTenantId(tenantId);
|
||||||
type.setCreateTime(null);
|
type.setCreateTime(null);
|
||||||
type.setUpdateTime(null);
|
type.setUpdateTime(null);
|
||||||
set.add(tenantId);
|
syncTenantIds.add(tenantId);
|
||||||
saveTypeList.add(type);
|
saveTypeList.add(type);
|
||||||
if (CollUtil.isNotEmpty(dataList)) {
|
}
|
||||||
// 筛选出 dictType 对应的 data
|
|
||||||
for (SysDictData dictData : dataList) {
|
// 默认租户字典数据不为空再去处理
|
||||||
SysDictData data = BeanUtil.toBean(dictData, SysDictData.class);
|
if (CollUtil.isNotEmpty(defaultDictDataList)) {
|
||||||
// 设置字典编码为 null
|
// 提前优化排除判断if条件语句,对于 && 并联条件,该优化可以避免不必要的 excludeDictDataSet.contains() 函数调用
|
||||||
data.setDictCode(null);
|
boolean isExclude = CollUtil.isNotEmpty(excludeDictDataSet);
|
||||||
data.setTenantId(tenantId);
|
// 筛选出 dictType 对应的 data
|
||||||
data.setCreateTime(null);
|
for (SysDictData dictData : defaultDictDataList) {
|
||||||
data.setUpdateTime(null);
|
// 排除不需要同步的字典数据
|
||||||
data.setCreateDept(null);
|
if (isExclude && excludeDictDataSet.contains(dictData.getDictValue())) {
|
||||||
data.setCreateBy(null);
|
continue;
|
||||||
data.setUpdateBy(null);
|
|
||||||
set.add(tenantId);
|
|
||||||
saveDataList.add(data);
|
|
||||||
}
|
}
|
||||||
|
SysDictData data = BeanUtil.toBean(dictData, SysDictData.class);
|
||||||
|
// 设置字典编码为 null
|
||||||
|
data.setDictCode(null);
|
||||||
|
data.setTenantId(tenantId);
|
||||||
|
data.setCreateTime(null);
|
||||||
|
data.setUpdateTime(null);
|
||||||
|
data.setCreateDept(null);
|
||||||
|
data.setCreateBy(null);
|
||||||
|
data.setUpdateBy(null);
|
||||||
|
syncTenantIds.add(tenantId);
|
||||||
|
saveDataList.add(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -490,7 +503,7 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
dictDataMapper.insertBatch(saveDataList);
|
dictDataMapper.insertBatch(saveDataList);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (String tenantId : set) {
|
for (String tenantId : syncTenantIds) {
|
||||||
TenantHelper.dynamic(tenantId, () -> CacheUtils.clear(CacheNames.SYS_DICT));
|
TenantHelper.dynamic(tenantId, () -> CacheUtils.clear(CacheNames.SYS_DICT));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user