1 总述

栏目 说明 备注
请求方式 POST
请求数据格式 JSON
返回数据格式 JSON

2 入参说明

参数名 数据类型 是否必填 说明 备注
apiName String 数据字典分类编码
name String 数据字典项名称 用于模糊查询,仅此情况需要分页,其他情况勿分页
nameList List< String> 数据字典项名称列表 用于反向翻译获取字典值,不能分页
code String 数据字典编码 用于精确匹配翻译,支持多个用英文逗号隔开,不能分页
extMap Map<String, Object> 额外扩展参数 用于级联多表单字段做输入参数情况下字典接口
pageFlag String 是否分页标识,树形无效不分页 1:分页 其他不分页
page Integer 当前页码 默认是1
pageSize Integer 一页行数 默认是20

3 出参说明RestResponse中data为Map<String, List>

参数名 数据类型 是否必填 说明 备注
id String 树形数据字典项ID 树形时候需要返回
code String 数据字典编码
name String 数据字典项名称
isEnable String 是否有效 1:有效;0:无效
description String 数据字典项的详情描述 场景1:树形全路径展示;场景2:数据字典项的详细说明;
parentId String 树形数据字典项父级ID 树形时候需要返回
children List< DictItemVo> 子数据字典项集合 树形时候需要返回
extData List<Map<String, Object>> 额外扩展数据:英文名称、别名等 用于赋值表单其他属性或者明细表行,Map中的key为属性API_NAME,value为对应的值(PS:字典为code)

3.2 出参示例

3.2.1 无搜索出参示例
{
    "status": 0,
    "message": "请求成功",
    "data": {
        "pagination": { // 分页信息(树形可以忽略,全部返回)
            "total": 4, // 总数
            "totalPages": 1, // 总页数
            "pageSize": 2000, // 一页行数
            "page": 1 // 当前页码
        },
        "content": {
            // key为搜索条件:name值,或者code逗号分隔后精确值(即每个code作为key)    
            "": [{ //不能写死为空字符串,要根据实际查询值作为key,方便前端快速获取
                "id": "04e65ec8764c4286b1f0bae242b88cc1",
                "code": "04e65ec8764c4286b1f0bae242b88cc1",
                "parentId": "0",
                "name": "杜比",
                "description": "杜比",
                "isEnabled": "1",
                "children": [{
                        "id": "9f1135ef38a047d782c09d5984310c2a",
                        "code": "9f1135ef38a047d782c09d5984310c2a",
                        "parentId": "04e65ec8764c4286b1f0bae242b88cc1",
                        "name": "Dolby_MS12V2",
                        "children": null,
                        "description": "杜比/Dolby_MS12V2",
                        "isEnabled": "1"
                    },
                    {
                        "id": "2800630230a941bbbf77cb52b0431451",
                        "code": "2800630230a941bbbf77cb52b0431451",
                        "parentId": "04e65ec8764c4286b1f0bae242b88cc1",
                        "name": "Dolby_MS12V2NH",
                        "children": null,
                        "description": "杜比/Dolby_MS12V2NH",
                        "isEnabled": "1"
                    }
                ]
            }]
        }
    }
}
3.2.2 模糊搜索出参示例
{
    "status": 0,
    "message": "请求成功",
    "data": {
        "pagination": { // 分页信息(树形可以忽略,全部返回)
            "total": 4, // 总数
            "totalPages": 1, // 总页数
            "pageSize": 2000, // 一页行数
            "page": 1 // 当前页码
        },
        "content": {
            // key为搜索条件:name值,或者code逗号分隔后精确值(即每个code作为key)    
            "杜比": [{ //不能写死为空字符串,要根据实际查询值作为key,方便前端快速获取
                "id": "04e65ec8764c4286b1f0bae242b88cc1",
                "code": "04e65ec8764c4286b1f0bae242b88cc1",
                "parentId": "0",
                "name": "杜比",
                "description": "杜比",
                "isEnabled": "1",
                "children": [{
                        "id": "9f1135ef38a047d782c09d5984310c2a",
                        "code": "9f1135ef38a047d782c09d5984310c2a",
                        "parentId": "04e65ec8764c4286b1f0bae242b88cc1",
                        "name": "Dolby_MS12V2",
                        "children": null,
                        "description": "杜比/Dolby_MS12V2",
                        "isEnabled": "1"
                    },
                    {
                        "id": "2800630230a941bbbf77cb52b0431451",
                        "code": "2800630230a941bbbf77cb52b0431451",
                        "parentId": "04e65ec8764c4286b1f0bae242b88cc1",
                        "name": "Dolby_MS12V2NH",
                        "children": null,
                        "description": "杜比/Dolby_MS12V2NH",
                        "isEnabled": "1"
                    }
                ]
            }]
        }
    }
}
3.2.3 精确匹配code搜索出参示例
{
    "status": 0,
    "message": "请求成功",
    "data": {
        "pagination": { // 分页信息(树形可以忽略,全部返回)
            "total": 4, // 总数
            "totalPages": 1, // 总页数
            "pageSize": 2000, // 一页行数
            "page": 1 // 当前页码
        },
        "content": {
            // key为搜索条件:name值,或者code逗号分隔后精确值(即每个code作为key)    
            "04e65ec8764c4286b1f0bae242b88cc1": [{ //不能写死为空字符串,要根据实际查询值作为key,方便前端快速获取
                "id": "04e65ec8764c4286b1f0bae242b88cc1",
                "code": "04e65ec8764c4286b1f0bae242b88cc1",
                "parentId": "0",
                "name": "杜比",
                "description": "杜比",
                "isEnabled": "1",
                "children": [{
                        "id": "9f1135ef38a047d782c09d5984310c2a",
                        "code": "9f1135ef38a047d782c09d5984310c2a",
                        "parentId": "04e65ec8764c4286b1f0bae242b88cc1",
                        "name": "Dolby_MS12V2",
                        "children": null,
                        "description": "杜比/Dolby_MS12V2",
                        "isEnabled": "1"
                    },
                    {
                        "id": "2800630230a941bbbf77cb52b0431451",
                        "code": "2800630230a941bbbf77cb52b0431451",
                        "parentId": "04e65ec8764c4286b1f0bae242b88cc1",
                        "name": "Dolby_MS12V2NH",
                        "children": null,
                        "description": "杜比/Dolby_MS12V2NH",
                        "isEnabled": "1"
                    }
                ]
            }]
        }
    }
}
3.2.3 nameList反向翻译出参示例

入参示例

{
    "apiName": "DOBLY_TEST",
    "nameList": ["杜比","杜比2"],
    "extMap": {}
}

出参示例

{
    "status": 0,
    "message": "请求成功",
    "data": {
        "pagination": { // 分页信息(树形可以忽略,全部返回)
            "total": 4, // 总数
            "totalPages": 1, // 总页数
            "pageSize": 2000, // 一页行数
            "page": 1 // 当前页码
        },
        "content": {
            // key为nameList中单个值(即每个name作为key)    
            "杜比": [{ //不能写死为空字符串,要根据实际查询值作为key,方便前端快速获取
                "id": "04e65ec8764c4286b1f0bae242b88cc1",
                "code": "04e65ec8764c4286b1f0bae242b88cc1",
                "parentId": "0",
                "name": "杜比",
                "description": "杜比",
                "isEnabled": "1"
            }],
           "杜比2": [{ //不能写死为空字符串,要根据实际查询值作为key,方便前端快速获取
                "id": "04e65ec8764c4286b1f0bae242b88cc2",
                "code": "04e65ec8764c4286b1f0bae242b88cc2",
                "parentId": "0",
                "name": "杜比2",
                "description": "杜比2",
                "isEnabled": "1"
            }]   }
    }
}

4 示例代码

@PostMapping("/demo")
    @ApiOperation(value = "/demo", notes = "动态数据字典demo")
    public RestResponse dynamicDictDemo(@ApiParam("数据字典查询DTO") @RequestBody DictQueryDTO dto) {

        ObjPageResp<Map<String, List<DictionaryDetailDTO>>> data = null;
        String name = dto.getName(), code = dto.getCode();
        Map<String, List<DictionaryDetailDTO>> content = Maps.newHashMap();
        // 没有搜索和翻译时候默认返回第一页数据,key为空字符串,此处为分页
        if (StringUtilsEx.isEmpty(name) && StringUtilsEx.isEmpty(code)) {
            DictionaryDetailDTO dictDto = DictionaryDetailDTO.init("1", "1", "name1", "1", null, null);
            dictDto.putExtData(1, "telephone", "13188990234");
            dictDto.putExtData(1, "TELEPHONE", "13188990234");
            dictDto.putExtData(1, "email", "hehe1@cvte.com");
            dictDto.putExtData(1, "EMAIL", "hehe1@cvte.com");
            content.put("", Lists.newArrayList(dictDto));
            data = ObjPageResp.init(dto.getPage(), dto.getPageSize(), 1L, content);
            return ResponseFactory.getOkResponse(data);
        }
        // 存在模糊搜索时候,key为模糊搜索内容即name值是key,此处才需要开启分页逻辑,其他不允许分页
        if (StringUtilsEx.isNotEmpty(name)) {
            DictionaryDetailDTO dictDto = DictionaryDetailDTO.init("1", "1", String.format("%s_1", name), "1", null, null);
            dictDto.putExtData(1, "telephone", "13188990234");
            dictDto.putExtData(1, "TELEPHONE", "13188990234");
            dictDto.putExtData(1, "email", "hehe1@cvte.com");
            dictDto.putExtData(1, "EMAIL", "hehe1@cvte.com");
            DictionaryDetailDTO dictDto2 = DictionaryDetailDTO.init("2", "2", String.format("%s_2", name), "1", null, null);
            dictDto.putExtData(1, "telephone", "13288990234");
            dictDto.putExtData(1, "TELEPHONE", "13288990234");
            dictDto.putExtData(1, "email", "hehe2@cvte.com");
            dictDto.putExtData(1, "EMAIL", "hehe2@cvte.com");
            content.put(name, Lists.newArrayList(dictDto, dictDto2));
            data = ObjPageResp.init(dto.getPage(), dto.getPageSize(), 2L, content);
            return ResponseFactory.getOkResponse(data);
        }
        // 最后再处理精确翻译,key为code分隔后的值,此逻辑不允许分页
        String[] codes = StringUtilsEx.split(code, BizConstants.SEP_COMMA);
        for (Integer i = 0; i < codes.length; i++) {
            String c = codes[i];
            DictionaryDetailDTO dictDto = DictionaryDetailDTO.init(i.toString(), c, String.format("name%s", i), "1", null, null);
            dictDto.putExtData(1, "telephone", String.format("1318899023%s", i));
            dictDto.putExtData(1, "TELEPHONE", String.format("1318899023%s", i));
            dictDto.putExtData(1, "email", String.format("hehe%s@cvte.com", i));
            dictDto.putExtData(1, "EMAIL", String.format("hehe%s@cvte.com", i));
            content.put(c, Lists.newArrayList(dictDto));
        }
        data = ObjPageResp.init(dto.getPage(), dto.getPageSize(), Long.parseLong(codes.length + ""), content);
        return ResponseFactory.getOkResponse(data);
    }

public class DictQueryDTO {
    /** 数据字典apiName,改为body传参,因为特殊字符的问题 **/
    private String apiName;
    private String name;
    private String code;
    private List<String> nameList;
    private String pageFlag;
    private Integer pageSize;
    private Integer page;
    /** 额外扩展参数 **/
    private Map<String, Object> extMap;
}

@Data
public class DictionaryDetailDTO {
    private String id;
    private String code;
    private String name;
    private String parentId;
    private String isEnabled;
    private String description;
    private List<DictionaryDetailDTO> children;
    private List<Map<String, Object>> extData;

    public static DictionaryDetailDTO init(String id, String code, String name, String isEnabled, String parentId, String description){
        DictionaryDetailDTO dto = new DictionaryDetailDTO();
        dto.setId(id);
        dto.setCode(code);
        dto.setName(name);
        dto.setIsEnabled(isEnabled);
        dto.setParentId(parentId);
        dto.setDescription(description);
        return dto;
    }

    public DictionaryDetailDTO addExtData(Map<String, Object> data) {
        if (null == extData) {
            extData = Lists.newArrayList();
        }
        extData.add(data);
        return this;
    }

    public DictionaryDetailDTO putExtData(Integer index, String key, Object value) {
        if (null == extData) {
            extData = Lists.newArrayList();
        }
        int size = extData.size();
        if (null == index || index < 1) {
            index = 1;
        }
        if (size < index) {
            int space = index - size;
            for (int i = 0; i < space; i++) {
                extData.add(Maps.newHashMap());
            }
        }
        extData.get(index - 1).put(key, value);
        return this;
    }
}
作者:聂维  创建时间:2023-04-14 09:47
最后编辑:叶端旺  更新时间:2024-12-11 10:09