Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问一下这种一个字段记录其他表多个id的情况,通过apijson如何关联查询id对应的名称 #763

Open
afumu opened this issue Sep 19, 2024 · 2 comments

Comments

@afumu
Copy link
Contributor

afumu commented Sep 19, 2024

Description

请问一下这种一个字段记录其他表多个id的情况,通过apijson如何关联查询id对应的名称

image

@afumu
Copy link
Contributor Author

afumu commented Sep 19, 2024

目前我的解决方案是,写了一个远程函数来解决

    public String getFieldName(@NotNull JSONObject current, @NotNull String fieldName,@NotNull String tableName,@NotNull String displayName) throws Exception{
        log.info("参数列表:{},{},{}",fieldName,tableName,displayName);
        String fileValue = current.getString(fieldName);
        log.info("参数{}的值:{}",fieldName,fileValue);
        if (StringUtils.isBlank(fileValue)){
            return "";
        }
        String[] split = fileValue.split(",");
        List<String> fileValueIdList = Arrays.asList(split);
        JSONObject jsonObjectList = buildJsonObject(tableName, displayName, fileValueIdList);
        log.info("构建查询条件:{}",jsonObjectList);

        APIJSONParser<Long> parser = new APIJSONParser<>();
        parser.setNeedVerify(false);
        parser.setMethod(RequestMethod.GET);
        JSONObject result = parser.parseResponse(jsonObjectList.toJSONString());
        log.info("查询结果:{}",result);

        JSONArray jsonArray = result.getJSONArray(tableName+"[]");
        List<Object> displayNameList = new ArrayList<>();
        jsonArray.forEach(item -> {
            JSONObject itemObj = (JSONObject) item;
            String name = itemObj.getString(displayName);
            displayNameList.add(name);
        });

        String displayNames = StringUtils.join(displayNameList, ",");
        log.info("返回显示值:{}",displayNames);
        return displayNames;
    }

image

@TommyLemon
Copy link
Collaborator

TommyLemon commented Sep 19, 2024

这是一个方式,也可以提供一个 字符串转列表 的远程函数,转换后,再通过传副表 "id{}@": "/Z_teacher2/stu_ids" 引用赋值。
或者找下 SQL 中 逗号分隔数组 转 JSON Array 的方案( concat 和 replace 虽然能做但比较麻烦 )。

也可以为 APIJSON 新增支持这种 “数组”,AbstractSQLConfig getRangeString 判断 String 格式自动 split 逗号转成数组,但会和已有的 key{}:"conditon1;conditon2" 条件范围冲突,可能还需要额外字段来标识:
https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java#L3996-L4095

不如远程函数方式,或者扩充 @json:"a,b,c",当 JSON.parse 未解析成非字符串类型值,则 split 逗号转成数组 arr,再 new ArrayList(arr) 转成列表(可能这个才是最佳方式)
https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java#L1055-L1061

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants