【dict】更新一个获取字典树的接口

dev-7.6.0
fengshuonan 2023-11-15 19:15:32 +08:00
parent e9ff5d2e9a
commit 039cbdc748
4 changed files with 115 additions and 0 deletions

View File

@ -24,6 +24,7 @@
*/
package cn.stylefeng.roses.kernel.dict.api;
import cn.stylefeng.roses.kernel.dict.api.pojo.DictTreeDto;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import java.util.List;
@ -66,4 +67,15 @@ public interface DictApi {
*/
void deleteByDictId(Long dictId);
/**
*
* <p>
*
*
* @author fengshuonan
* @since 2023/11/15 18:55
*/
List<DictTreeDto> buildDictTreeStructure();
}

View File

@ -0,0 +1,31 @@
package cn.stylefeng.roses.kernel.dict.api.pojo;
import lombok.Data;
import java.util.List;
/**
*
*
* @author fengshuonan
* @since 2023/11/15 18:44
*/
@Data
public class DictTreeDto {
/**
*
*/
private String dictLabel;
/**
*
*/
private String dictValue;
/**
*
*/
private List<DictTreeDto> children;
}

View File

@ -25,7 +25,9 @@
package cn.stylefeng.roses.kernel.dict.modular.controller;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dict.api.DictApi;
import cn.stylefeng.roses.kernel.dict.api.constants.DictConstants;
import cn.stylefeng.roses.kernel.dict.api.pojo.DictTreeDto;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictTypeRequest;
import cn.stylefeng.roses.kernel.dict.modular.service.DictTypeService;
@ -57,6 +59,9 @@ public class DictTypeController {
@Resource
private DictTypeService dictTypeService;
@Resource
private DictApi dictApi;
/**
*
*
@ -169,4 +174,15 @@ public class DictTypeController {
return new SuccessResponseData<>(detail);
}
/**
*
*
* @author fengshuonan
* @since 2023/11/15 19:08
*/
@GetResource(name = "获取所有的字典类型树", path = "/dictType/dictTreeBuild", requiredPermission = false)
public ResponseData<List<DictTreeDto>> dictTreeBuild() {
return new SuccessResponseData<>(dictApi.buildDictTreeStructure());
}
}

View File

@ -25,6 +25,7 @@
package cn.stylefeng.roses.kernel.dict.modular.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
@ -34,6 +35,7 @@ import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dict.api.constants.DictConstants;
import cn.stylefeng.roses.kernel.dict.api.exception.DictException;
import cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum;
import cn.stylefeng.roses.kernel.dict.api.pojo.DictTreeDto;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import cn.stylefeng.roses.kernel.dict.modular.mapper.DictMapper;
@ -55,7 +57,9 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -224,6 +228,58 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
this.removeById(dictId);
}
@Override
public List<DictTreeDto> buildDictTreeStructure() {
// 获取所有字典类型
LambdaQueryWrapper<SysDictType> dictTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
dictTypeLambdaQueryWrapper.select(SysDictType::getDictTypeCode, SysDictType::getDictTypeName);
dictTypeLambdaQueryWrapper.orderByAsc(SysDictType::getDictTypeSort);
List<SysDictType> dictTypeList = this.dictTypeService.list(dictTypeLambdaQueryWrapper);
// 获取所有字典信息
LambdaQueryWrapper<SysDict> sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysDictLambdaQueryWrapper.select(SysDict::getDictCode, SysDict::getDictName, SysDict::getDictTypeCode);
sysDictLambdaQueryWrapper.orderByAsc(SysDict::getDictSort);
List<SysDict> dictList = this.list(sysDictLambdaQueryWrapper);
// 构建字典类型基本信息
Map<String, DictTreeDto> typeCodeMap = new HashMap<>();
for (SysDictType sysDictType : dictTypeList) {
DictTreeDto dictTreeDto = new DictTreeDto();
dictTreeDto.setDictLabel(sysDictType.getDictTypeName());
dictTreeDto.setDictValue(sysDictType.getDictTypeCode());
typeCodeMap.put(sysDictType.getDictTypeCode(), dictTreeDto);
}
// 遍历所有字典,把字典信息装配到字典类型信息里
for (SysDict sysDict : dictList) {
String dictTypeCode = sysDict.getDictTypeCode();
if (StrUtil.isBlank(dictTypeCode)) {
continue;
}
DictTreeDto dictTreeDto = typeCodeMap.get(dictTypeCode);
if (dictTreeDto == null) {
continue;
}
List<DictTreeDto> children = dictTreeDto.getChildren();
if (ObjectUtil.isEmpty(children)) {
children = new ArrayList<>();
}
DictTreeDto dictValue = new DictTreeDto();
dictValue.setDictLabel(sysDict.getDictName());
dictValue.setDictValue(sysDict.getDictCode());
children.add(dictValue);
dictTreeDto.setChildren(children);
}
// map转化为树形结构
return ListUtil.list(true, typeCodeMap.values());
}
/**
*
*