|
@@ -2,21 +2,32 @@ package cn.iocoder.yudao.module.system.service.dictTenant;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
|
|
|
+import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
|
|
+import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
|
|
+import cn.iocoder.yudao.module.system.api.dicttenant.dto.DictDataTenantRespDTO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.dictTenant.vo.type.DictTypeTenantPageReqVO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.dictTenant.vo.type.DictTypeTenantSaveReqVO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.dictTenant.DictDataTenantDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.dictTenant.DictTypeTenantDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.mysql.dictTenant.DictTypeTenantMapper;
|
|
|
+import cn.iocoder.yudao.module.system.enums.dicttenant.DictTypeTenantEnum;
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
+
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
@@ -94,6 +105,66 @@ public class DictTypeTenantServiceImpl implements DictTypeTenantService {
|
|
|
return dictTypeTenantMapper.selectList();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Map<String, String> initDictTypeAndDataForTenant(String dictType) {
|
|
|
+
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+
|
|
|
+ // 所有租户字典类型枚举
|
|
|
+ List<DictTypeTenantEnum> dictTypeTenantEnumList = Stream.of(DictTypeTenantEnum.values()).collect(Collectors.toList());
|
|
|
+ // 所有租户字典类型值
|
|
|
+ List<String> dictTypeList = dictTypeTenantEnumList.stream().map(DictTypeTenantEnum::getType).collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(dictType)) {
|
|
|
+ if (dictTypeList.contains(dictType)) {
|
|
|
+ // 查询当前租户有没有添加该类型
|
|
|
+ DictTypeTenantDO dictTypeTenantDO = dictTypeTenantMapper.selectByType(dictType);
|
|
|
+ if (dictTypeTenantDO == null) {
|
|
|
+ // 添加类型和数据
|
|
|
+ DictTypeTenantDO build = DictTypeTenantDO.builder()
|
|
|
+ .type(dictType)
|
|
|
+ .name(DictTypeTenantEnum.getLabel(dictType))
|
|
|
+ .build();
|
|
|
+ dictTypeTenantMapper.insert(build);
|
|
|
+
|
|
|
+ List<DictDataTenantRespDTO> dictData = DictTypeTenantEnum.getDictData(dictType);
|
|
|
+ List<DictDataTenantDO> dictDataTenantDOList = BeanUtils.toBean(dictData, DictDataTenantDO.class);
|
|
|
+ dictDataTenantService.insertBatch(dictDataTenantDOList);
|
|
|
+ result.put(dictType + ":" + TenantContextHolder.getTenantId(), "添加成功");
|
|
|
+ } else {
|
|
|
+ result.put(dictType + ":" + TenantContextHolder.getTenantId(), "已存在");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result.put(dictType + ":" + TenantContextHolder.getTenantId(), "不是预定义的类型");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (DictTypeTenantEnum dictTypeTenantEnum : dictTypeTenantEnumList) {
|
|
|
+ dictType = dictTypeTenantEnum.getType();
|
|
|
+ // 查询当前租户有没有添加该类型
|
|
|
+ DictTypeTenantDO dictTypeTenantDO = dictTypeTenantMapper.selectByType(dictType);
|
|
|
+ if (dictTypeTenantDO == null) {
|
|
|
+ // 添加类型和数据
|
|
|
+ DictTypeTenantDO build = DictTypeTenantDO.builder()
|
|
|
+ .type(dictType)
|
|
|
+ .name(DictTypeTenantEnum.getLabel(dictType))
|
|
|
+ .build();
|
|
|
+ dictTypeTenantMapper.insert(build);
|
|
|
+
|
|
|
+ List<DictDataTenantRespDTO> dictData = DictTypeTenantEnum.getDictData(dictType);
|
|
|
+ List<DictDataTenantDO> dictDataTenantDOList = BeanUtils.toBean(dictData, DictDataTenantDO.class);
|
|
|
+ dictDataTenantService.insertBatch(dictDataTenantDOList);
|
|
|
+
|
|
|
+ result.put(dictTypeTenantEnum.getType() + ":" + TenantContextHolder.getTenantId(), "添加成功");
|
|
|
+ } else {
|
|
|
+ result.put(dictTypeTenantEnum.getType() + ":" + TenantContextHolder.getTenantId(), "已存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
@VisibleForTesting
|
|
|
void validateDictTypeNameUnique(Long id, String name) {
|