赵文轩
2024-06-12 cb36319a6de6a18b00465e87c04e21faf4b0be94
根据字段值判断是否扩增字段或超过阈值更改为text
1个文件已修改
121 ■■■■■ 已修改文件
ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisApiGetMethodService.java 121 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisApiGetMethodService.java
@@ -177,10 +177,16 @@
        lambdaQueryWrapper.eq(HisApiConfig::getApiMethod, type);
        HisApiConfig hisApiConfig = hisApiConfigService.getOne(lambdaQueryWrapper);
        String responseJson = HttpClientUtils.sendPost(HIS_URL + hisApiConfig.getApiUrl(), params);
        JSONObject response = JSONUtil.parseObj(responseJson).getJSONObject("Response");
        if (response.getStr("ResultCode").equals("0")) {
        Integer isResponse = hisApiConfig.getIsResponse();
        JSONObject response = null;
        if (isResponse == 1) {
            response = JSONUtil.parseObj(responseJson).getJSONObject("Response");
        } else{
            response = JSONUtil.parseObj(responseJson);
        }
        if (response.getStr(hisApiConfig.getResultCodeKey()).equals("0")) {
            AjaxResult ajaxResult = AjaxResult.success();
            JSONArray resultData = response.getJSONArray("ResultData");
            JSONArray resultData = response.getJSONArray(hisApiConfig.getResultDataKey());
            List<Map<String, String>> list = new ArrayList<>();
            for (Object resultDatum : resultData) {
                JSONObject object = (JSONObject) resultDatum;
@@ -303,11 +309,11 @@
                statement.close();
            }
            if (hisApiConfig == null)
            insertData(hisApiConfig.getTabName(), connection, jsonObject);
            insertData(hisApiConfig, connection, jsonObject);
            else insertDataNew(connection, jsonObject,hisApiConfig);
        } else {
            if (hisApiConfig == null)
            insertData(hisApiConfig.getTabName(), connection, jsonObject);
            insertData(hisApiConfig, connection, jsonObject);
            else insertDataNew(connection, jsonObject,hisApiConfig);
        }
    }
@@ -315,17 +321,17 @@
    /**
     * 插入数据
     *
     * @param tabName
     * @param hisApiConfig
     * @param connection
     * @param jsonObject
     * @throws SQLException
     */
    private void insertData(String tabName, Connection connection, JSONObject jsonObject) throws SQLException {
    private void insertData(HisApiConfig hisApiConfig, Connection connection, JSONObject jsonObject) throws SQLException {
        StringBuilder insertSqlBuilder = new StringBuilder();
        StringBuilder valueBuilder = new StringBuilder();
        StringBuilder selectSqlBuilder = new StringBuilder();
        insertSqlBuilder.append("insert into ").append(tabName).append(" (");
        selectSqlBuilder.append("select count(1) as count from ").append(tabName).append(" where ");
        insertSqlBuilder.append("insert into ").append(hisApiConfig.getTabName()).append(" (");
        selectSqlBuilder.append("select count(1) as count from ").append(hisApiConfig.getTabName()).append(" where ");
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            String defaultVal = entry.getValue().toString();
            String key = entry.getKey().trim().toLowerCase();
@@ -335,6 +341,19 @@
            insertSqlBuilder.append(key).append(", ");
            valueBuilder.append("'").append(val).append("', ");
            selectSqlBuilder.append(key).append(" = '").append(val).append("' and ");
            // 判断数据是否大于字段创建值如果大于则增加超过阈值则使用text
            String columnType = getColumnType(connection, hisApiConfig.getTabName(), key);
            if (!"text".equalsIgnoreCase(columnType)){
                int currentSize = getColumnSize(connection, hisApiConfig.getTabName(), key);
                if (currentSize < val.length()){
                    if (val.length()>= 1000){
                        alterColumnTypeToText(connection, hisApiConfig.getTabName(), key);
                    }else {
                        int newSize = ((val.length() / 100) + 1) * 100;
                        alterColumnSize(connection, hisApiConfig.getTabName(), key, newSize);
                    }
                }
            }
        }
        selectSqlBuilder.delete(selectSqlBuilder.length() - 5, selectSqlBuilder.length());
        Statement statement = connection.prepareStatement(selectSqlBuilder.toString());
@@ -384,6 +403,19 @@
            }
            insertSqlBuilder.append(key).append(", ");
            valueBuilder.append("'").append(val).append("', ");
            // 判断数据是否大于字段创建值如果大于则增加超过阈值则使用text
            String columnType = getColumnType(connection, hisApiConfig.getTabName(), key);
            if (!"text".equalsIgnoreCase(columnType)){
                int currentSize = getColumnSize(connection, hisApiConfig.getTabName(), key);
                if (currentSize < val.length()){
                    if (val.length()>= 1000){
                        alterColumnTypeToText(connection, hisApiConfig.getTabName(), key);
                    }else {
                        int newSize = ((val.length() / 100) + 1) * 100;
                        alterColumnSize(connection, hisApiConfig.getTabName(), key, newSize);
                    }
                }
            }
        }
        deleteSqlBuilder.delete(deleteSqlBuilder.length() - 5, deleteSqlBuilder.length());
        Statement statement = connection.prepareStatement(deleteSqlBuilder.toString());
@@ -405,6 +437,77 @@
    }
    /**
     * 获取列类型
     * @param connection
     * @param tableName
     * @param columnName
     * @return
     * @throws SQLException
     */
    private static String getColumnType(Connection connection, String tableName, String columnName) throws SQLException {
        String query = "SELECT DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME = ?";
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, tableName);
            statement.setString(2, columnName);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getString("DATA_TYPE");
            }
        }
        return "";
    }
    /**
     * 获取列长度
     * @param connection
     * @param tableName
     * @param columnName
     * @return
     * @throws SQLException
     */
    private static int getColumnSize(Connection connection, String tableName, String columnName) throws SQLException {
        String query = "SELECT CHARACTER_MAXIMUM_LENGTH FROM information_schema.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME = ?";
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, tableName);
            statement.setString(2, columnName);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt("CHARACTER_MAXIMUM_LENGTH");
            }
        }
        return 0;
    }
    /**
     * 修改列长度
     * @param connection
     * @param tableName
     * @param columnName
     * @param newSize
     * @throws SQLException
     */
    private static void alterColumnSize(Connection connection, String tableName, String columnName, int newSize) throws SQLException {
        String alterQuery = String.format("ALTER TABLE %s MODIFY %s VARCHAR(%d)", tableName, columnName, newSize);
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate(alterQuery);
        }
    }
    /**
     * 超过阈值则修改为text类型
     * @param connection
     * @param tableName
     * @param columnName
     * @throws SQLException
     */
    private static void alterColumnTypeToText(Connection connection, String tableName, String columnName) throws SQLException {
        String alterQuery = String.format("ALTER TABLE %s MODIFY %s TEXT", tableName, columnName);
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate(alterQuery);
        }
    }
    /**
     * 获取表的列
     *
     * @param tabName