| | |
| | | package com.ltkj.web.controller.his; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | |
| | | * @param type 接口代码 |
| | | * @param params 请求参数 |
| | | * @return |
| | | * @deprecated |
| | | * 方法已弃用,将switch更改为数据库匹配 使用新方法{@link #getHISDataNew(String, Map)} |
| | | */ |
| | | @Deprecated |
| | | @Transactional |
| | | public AjaxResult getHISData(String type, Map<String, Object> params) { |
| | | AjaxResult result = null; |
| | |
| | | List<Map<String, String>> list = new ArrayList<>(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | ajaxResult = save(object, type,null); |
| | | ajaxResult = save(object, type,null,JSONUtil.toJsonStr(params)); |
| | | Map<String, String> map = new HashMap<>(); |
| | | for (String key : object.keySet()) { |
| | | String defaultVal = object.get(key).toString(); |
| | |
| | | return AjaxResult.error().put("result", JSONUtil.parseObj(json)); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Transactional |
| | | public AjaxResult getHISDataNew(String type, Map<String, Object> params){ |
| | | LambdaQueryWrapper<HisApiConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | |
| | | List<Map<String, String>> list = new ArrayList<>(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | ajaxResult = save(object, type,hisApiConfig); |
| | | ajaxResult = save(object, type,hisApiConfig,JSONUtil.toJsonStr(params)); |
| | | Map<String, String> map = new HashMap<>(); |
| | | for (String key : object.keySet()) { |
| | | String defaultVal = object.get(key).toString(); |
| | |
| | | * @param code 接口代码 |
| | | * @return 执行是否成功 |
| | | */ |
| | | public AjaxResult save(JSONObject object, String code,HisApiConfig hisApiConfig) { |
| | | public AjaxResult save(JSONObject object, String code,HisApiConfig hisApiConfig,String paramsJson) { |
| | | Connection connection = getConnection(); |
| | | Boolean isExists = tabIsExists(connection, hisApiConfig.getTabName()); |
| | | if (null == isExists) |
| | |
| | | } |
| | | //插入数据 |
| | | try { |
| | | operationTable(object,connection,hisApiConfig); |
| | | operationTable(object,connection,hisApiConfig,paramsJson); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | return AjaxResult.error(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * ResultData为集合 |
| | | * 根据his接口返回值 插入数据 |
| | | * 如果没有表则创建表 接口增加了字段则为表增加字段 |
| | | * |
| | | * @param code 接口代码 |
| | | * @return 执行是否成功 |
| | | */ |
| | | public AjaxResult saveArray(String json, String code) { |
| | | code = code.toLowerCase(); |
| | | JSONObject jsonObject = JSONUtil.parseObj(json); |
| | | JSONObject response = jsonObject.getJSONObject("Response"); |
| | | String tabName = "ltkj_" + code; |
| | | if (response.getStr("ResultCode").equals("0")) { |
| | | JSONArray resultData = response.getJSONArray("ResultData"); |
| | | Connection connection = getConnection(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | Boolean isExists = tabIsExists(connection, tabName); |
| | | if (null == isExists) |
| | | return AjaxResult.error(); |
| | | if (!isExists) { |
| | | try { |
| | | creatTable(object, tabName, connection); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | return AjaxResult.error(); |
| | | } |
| | | } |
| | | //插入数据 |
| | | try { |
| | | operationTable(object,connection,null); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | return AjaxResult.error(); |
| | | } |
| | | } |
| | | try { |
| | | if (connection != null) |
| | | connection.close(); |
| | | } catch (SQLException throwables) { |
| | | } |
| | | } |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | /** |
| | | * 操作表 |
| | | * |
| | | * @param connection |
| | | * @throws SQLException |
| | | */ |
| | | private void operationTable(JSONObject jsonObject, Connection connection,HisApiConfig hisApiConfig) throws SQLException { |
| | | private void operationTable(JSONObject jsonObject, Connection connection,HisApiConfig hisApiConfig,String paramsJson) throws SQLException { |
| | | List<String> columns = getColumns(hisApiConfig.getTabName(), connection); |
| | | ArrayList<String> responseColums = new ArrayList<>(); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | String key = entry.getKey().trim().toLowerCase(); |
| | | responseColums.add(key); |
| | | } |
| | | Map<String, Object> beanToMap = BeanUtil.beanToMap(JSONUtil.parseObj(paramsJson)); |
| | | Set<Map.Entry<String, Object>> entries = beanToMap.entrySet(); |
| | | for (Map.Entry<String, Object> entry : entries) { |
| | | String key = "request_params_"+entry.getKey().trim().toLowerCase(); |
| | | if (!columns.contains(key)) |
| | | responseColums.add(key); |
| | | } |
| | | responseColums.removeAll(columns); |
| | | if (!responseColums.isEmpty()) { |
| | |
| | | statement.executeUpdate(sql); |
| | | statement.close(); |
| | | } |
| | | if (hisApiConfig == null) |
| | | insertData(hisApiConfig, connection, jsonObject); |
| | | else insertDataNew(connection, jsonObject,hisApiConfig); |
| | | insertDataNew(connection, jsonObject,hisApiConfig,paramsJson); |
| | | } else { |
| | | if (hisApiConfig == null) |
| | | insertData(hisApiConfig, connection, jsonObject); |
| | | else insertDataNew(connection, jsonObject,hisApiConfig); |
| | | insertDataNew(connection, jsonObject,hisApiConfig,paramsJson); |
| | | } |
| | | } |
| | | |
| | |
| | | * @param jsonObject |
| | | * @throws SQLException |
| | | */ |
| | | private void insertDataNew(Connection connection, JSONObject jsonObject,HisApiConfig hisApiConfig) throws SQLException { |
| | | private void insertDataNew(Connection connection, JSONObject jsonObject,HisApiConfig hisApiConfig,String paramsJson) throws SQLException { |
| | | List<String> primaryKesList = Arrays.stream(hisApiConfig.getPrimaryKeys().split(",")).map(String::toLowerCase).collect(Collectors.toList()); |
| | | StringBuilder deleteSqlBuilder = new StringBuilder(); |
| | | StringBuilder insertSqlBuilder = new StringBuilder(); |
| | |
| | | // log.info("删除条数 ->{}",i); |
| | | statement.close(); |
| | | insertSqlBuilder.append("insert_time, "); |
| | | insertSqlBuilder.append("request_params, "); |
| | | DatabaseMetaData metaData = connection.getMetaData(); |
| | | try (ResultSet resultSet = metaData.getColumns(null, null, hisApiConfig.getTabName(), "request_params")) { |
| | | if (!resultSet.next()){ |
| | | String alterTableSQL = String.format("ALTER TABLE %s ADD COLUMN %s %s", hisApiConfig.getTabName(), "request_params", "text"); |
| | | statement = connection.createStatement(); |
| | | statement.execute(alterTableSQL); |
| | | } |
| | | } |
| | | String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); |
| | | valueBuilder.append("'").append(time).append("'").append(", "); |
| | | valueBuilder.append("'").append(paramsJson).append("'").append(", "); |
| | | Map<String, Object> beanToMap = BeanUtil.beanToMap(JSONUtil.parseObj(paramsJson)); |
| | | Set<Map.Entry<String, Object>> entries = beanToMap.entrySet(); |
| | | for (Map.Entry<String, Object> entry : entries) { |
| | | String key = "request_params_"+entry.getKey().trim().toLowerCase(); |
| | | insertSqlBuilder.append(key).append(", "); |
| | | valueBuilder.append("'").append(entry.getValue().toString()).append("', "); |
| | | } |
| | | insertSqlBuilder.delete(insertSqlBuilder.length() - 2, insertSqlBuilder.length()); |
| | | valueBuilder.delete(valueBuilder.length() - 2, valueBuilder.length()); |
| | | insertSqlBuilder.append(") values (").append(valueBuilder).append(")"); |
| | |
| | | sql.append(key).append(" VARCHAR(200) null,"); |
| | | } |
| | | sql.append("insert_time").append(" VARCHAR(200) null,"); |
| | | sql.append("request_params").append(" text null COMMENT '请求参数',"); |
| | | sql = new StringBuilder(sql.substring(0, sql.length() - 1)); |
| | | sql.append(");"); |
| | | Statement statement = connection.createStatement(); |