add 增加 同步租户字典功能

2.X
疯狂的狮子Li 5 months ago
parent 79bec85e27
commit 0ebfcf1edd

@ -177,4 +177,18 @@ public class SysTenantController extends BaseController {
return toAjax(TenantHelper.ignore(() -> tenantService.syncTenantPackage(tenantId, packageId))); return toAjax(TenantHelper.ignore(() -> tenantService.syncTenantPackage(tenantId, packageId)));
} }
/**
*
*/
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@Log(title = "同步租户字典", businessType = BusinessType.INSERT)
@GetMapping("/syncTenantDict")
public R<Void> syncTenantDict() {
if (!TenantHelper.isEnable()) {
return R.fail("当前未开启租户模式");
}
tenantService.syncTenantDict();
return R.ok("同步租户字典成功");
}
} }

@ -79,4 +79,9 @@ public interface ISysTenantService {
* *
*/ */
Boolean syncTenantPackage(String tenantId, Long packageId); Boolean syncTenantPackage(String tenantId, Long packageId);
/**
*
*/
void syncTenantDict();
} }

@ -1,6 +1,7 @@
package org.dromara.system.service.impl; package org.dromara.system.service.impl;
import cn.dev33.satoken.secure.BCrypt; import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
@ -14,9 +15,13 @@ import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.SpringUtils; import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery; import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo; import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.redis.utils.CacheUtils;
import org.dromara.common.tenant.core.TenantEntity;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.*; import org.dromara.system.domain.*;
import org.dromara.system.domain.bo.SysTenantBo; import org.dromara.system.domain.bo.SysTenantBo;
import org.dromara.system.domain.vo.SysTenantVo; import org.dromara.system.domain.vo.SysTenantVo;
@ -27,10 +32,7 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/** /**
* Service * Service
@ -369,4 +371,86 @@ public class SysTenantServiceImpl implements ISysTenantService {
} }
return true; return true;
} }
/**
*
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void syncTenantDict() {
// 查询超管 所有字典数据
List<SysDictType> dictTypeList = new ArrayList<>();
List<SysDictData> dictDataList = new ArrayList<>();
TenantHelper.ignore(() -> {
dictTypeList.addAll(dictTypeMapper.selectList());
dictDataList.addAll(dictDataMapper.selectList());
});
Map<String, List<SysDictType>> typeMap = StreamUtils.groupByKey(dictTypeList, TenantEntity::getTenantId);
Map<String, Map<String, List<SysDictData>>> typeDataMap = 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<String> tenantIds = baseMapper.selectObjs(
new LambdaQueryWrapper<SysTenant>().select(SysTenant::getTenantId)
.eq(SysTenant::getStatus, TenantConstants.NORMAL), x -> {return Convert.toStr(x);});
List<SysDictType> saveTypeList = new ArrayList<>();
List<SysDictData> saveDataList = new ArrayList<>();
Set<String> set = new HashSet<>();
for (String tenantId : tenantIds) {
if (TenantConstants.DEFAULT_TENANT_ID.equals(tenantId)) {
continue;
}
for (SysDictType dictType : defaultTypeMap) {
List<String> typeList = StreamUtils.toList(typeMap.get(tenantId), SysDictType::getDictType);
List<SysDictData> dataList = defaultTypeDataMap.get(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);
for (SysDictData dictData : dataList) {
if (!map.containsKey(dictData.getDictValue())) {
// 设置字典编码为 null
dictData.setDictCode(null);
dictData.setTenantId(tenantId);
dictData.setCreateTime(null);
dictData.setUpdateTime(null);
set.add(tenantId);
saveDataList.add(dictData);
}
}
} else {
dictType.setDictId(null);
dictType.setTenantId(tenantId);
dictType.setCreateTime(null);
dictType.setUpdateTime(null);
set.add(tenantId);
saveTypeList.add(dictType);
if (CollUtil.isNotEmpty(dataList)) {
// 筛选出 dictType 对应的 data
for (SysDictData dictData : dataList) {
// 设置字典编码为 null
dictData.setDictCode(null);
dictData.setTenantId(tenantId);
dictData.setCreateTime(null);
dictData.setUpdateTime(null);
set.add(tenantId);
}
saveDataList.addAll(dataList);
}
}
}
}
if (CollUtil.isNotEmpty(saveTypeList)) {
dictTypeMapper.insertBatch(saveTypeList);
}
if (CollUtil.isNotEmpty(saveDataList)) {
dictDataMapper.insertBatch(saveDataList);
}
for (String tenantId : set) {
TenantHelper.dynamic(tenantId, () -> CacheUtils.clear(CacheNames.SYS_DICT));
}
}
} }

Loading…
Cancel
Save