| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 插入数据 |
| | | * |
| | | * @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(); |
| | |
| | | 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()); |
| | |
| | | } |
| | | 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()); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取列类型 |
| | | * @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 |