| | |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.ltkj.common.core.domain.AjaxResult; |
| | | import com.ltkj.hosp.domain.ApiConfig; |
| | | import com.ltkj.hosp.domain.HisApiConfig; |
| | | import com.ltkj.hosp.service.ApiConfigService; |
| | | import com.ltkj.hosp.service.HisApiConfigService; |
| | | import com.ltkj.web.wxUtils.HttpClientUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.FileInputStream; |
| | |
| | | private static String DATA_URL = ""; |
| | | private static String DATA_USER =""; |
| | | private static String DATA_PASS = ""; |
| | | private static String LIS_URL = ""; |
| | | private static String PACS_URL = ""; |
| | | @Autowired |
| | | private HisApiMethodService controller; |
| | | @Autowired |
| | | private HisApiConfigService hisApiConfigService; |
| | | @Autowired |
| | | private ApiConfigService apiConfigService; |
| | | |
| | | private static String url; |
| | | |
| | |
| | | DATA_PASS = props.getProperty("password"); |
| | | String apiUrl = props.getProperty("his_api_url"); |
| | | String apiPort = props.getProperty("his_api_port"); |
| | | HIS_URL = apiUrl+":"+apiPort+"/api/His/HisRequest"; |
| | | HIS_URL = apiUrl+":"+apiPort; |
| | | String lisApiUrl = props.getProperty("lis_api_url"); |
| | | String lisApiPort = props.getProperty("lis_api_port"); |
| | | LIS_URL = lisApiUrl+":"+lisApiPort; |
| | | String pacsApiUrl = props.getProperty("pacs_api_url"); |
| | | String pacsApiPort = props.getProperty("pacs_api_port"); |
| | | PACS_URL = pacsApiUrl+":"+pacsApiPort; |
| | | } catch (IOException e) { |
| | | System.out.println("初始化数据库异常 ->"+e.getMessage()); |
| | | } |
| | |
| | | try { |
| | | Class.forName("com.mysql.cj.jdbc.Driver"); |
| | | } catch (ClassNotFoundException e) { |
| | | e.printStackTrace(); |
| | | log.error(String.valueOf(e),e.getMessage()); |
| | | } |
| | | // try { |
| | | // InputStreamReader reader = new InputStreamReader(new FileInputStream(url), StandardCharsets.UTF_8); |
| | |
| | | } |
| | | } |
| | | |
| | | @Transactional |
| | | // @Transactional |
| | | public AjaxResult getHISDataNew(String type, Map<String, Object> params){ |
| | | LambdaQueryWrapper<HisApiConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | lambdaQueryWrapper.eq(HisApiConfig::getApiMethod, type); |
| | | HisApiConfig hisApiConfig = hisApiConfigService.getOne(lambdaQueryWrapper); |
| | | String responseJson = HttpClientUtils.sendPost(HIS_URL + hisApiConfig.getApiUrl(), params); |
| | | Integer isResponse = hisApiConfig.getIsResponse(); |
| | | try { |
| | | LambdaQueryWrapper<HisApiConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | lambdaQueryWrapper.eq(HisApiConfig::getApiMethod, type); |
| | | HisApiConfig hisApiConfig = hisApiConfigService.getOne(lambdaQueryWrapper); |
| | | String responseJson = HttpClientUtils.sendPost(HIS_URL + hisApiConfig.getApiUrl(), params); |
| | | 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(hisApiConfig.getResultDataKey()); |
| | | List<Map<String, String>> list = new ArrayList<>(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | ajaxResult = save(object, type,hisApiConfig,JSONUtil.toJsonStr(params)); |
| | | // save(object, type,hisApiConfig,JSONUtil.toJsonStr(params)); |
| | | Map<String, String> map = new HashMap<>(); |
| | | for (String key : object.keySet()) { |
| | | String defaultVal = object.get(key).toString(); |
| | | String val = defaultVal.trim().replaceAll("\\s+", ""); |
| | | map.put(key, val); |
| | | } |
| | | list.add(map); |
| | | } |
| | | ajaxResult.put("data", list); |
| | | return ajaxResult; |
| | | } else { |
| | | return AjaxResult.error().put("result", JSONUtil.parseObj(responseJson)); |
| | | } |
| | | } catch (Exception e) { |
| | | // throw new RuntimeException(e); |
| | | log.error(String.valueOf(e),e.getMessage()); |
| | | return AjaxResult.error(); |
| | | } |
| | | } |
| | | |
| | | public AjaxResult getLisData(String type,Map<String ,Object> params){ |
| | | LambdaQueryWrapper<ApiConfig> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(ApiConfig::getType,"lis"); |
| | | wrapper.eq(ApiConfig::getApiMethod,type); |
| | | return getAjaxResult("lis", params, wrapper); |
| | | } |
| | | |
| | | public AjaxResult getPacsData(String type,Map<String ,Object> params){ |
| | | LambdaQueryWrapper<ApiConfig> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(ApiConfig::getType,"pacs"); |
| | | wrapper.eq(ApiConfig::getApiMethod,type); |
| | | return getAjaxResult("pacs", params, wrapper); |
| | | } |
| | | |
| | | private AjaxResult getAjaxResult(String type, Map<String, Object> params, LambdaQueryWrapper<ApiConfig> wrapper) { |
| | | ApiConfig apiConfig = apiConfigService.getOne(wrapper); |
| | | String responseJson; |
| | | switch (type){ |
| | | case "pacs": |
| | | responseJson = HttpClientUtils.sendPost(PACS_URL + apiConfig.getApiUrl(), params); |
| | | break; |
| | | default: |
| | | responseJson = HttpClientUtils.sendPost(LIS_URL + apiConfig.getApiUrl(), params); |
| | | break; |
| | | } |
| | | Integer isResponse = apiConfig.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")) { |
| | | if (response.getStr(apiConfig.getResultCodeKey()).equals("1")) { |
| | | AjaxResult ajaxResult = AjaxResult.success(); |
| | | JSONArray resultData = response.getJSONArray(hisApiConfig.getResultDataKey()); |
| | | List<Map<String, String>> list = new ArrayList<>(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | 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(); |
| | | String val = defaultVal.trim().replaceAll("\\s+", ""); |
| | | map.put(key, val); |
| | | if (response.getStr(apiConfig.getResultDataKey()) != null && StrUtil.isNotBlank(response.getStr(apiConfig.getResultDataKey()))){ |
| | | JSONArray resultData = response.getJSONArray(apiConfig.getResultDataKey()); |
| | | List<Map<String, String>> list = new ArrayList<>(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | ajaxResult = save(object, type,apiConfig,JSONUtil.toJsonStr(params)); |
| | | Map<String, String> map = new HashMap<>(); |
| | | for (String key : object.keySet()) { |
| | | String defaultVal = object.get(key).toString(); |
| | | String val = defaultVal.trim().replaceAll("\\s+", ""); |
| | | map.put(key, val); |
| | | } |
| | | list.add(map); |
| | | } |
| | | list.add(map); |
| | | ajaxResult.put("data", list); |
| | | }else { |
| | | ajaxResult.put("data",response); |
| | | } |
| | | ajaxResult.put("data", list); |
| | | return ajaxResult; |
| | | } else { |
| | | return AjaxResult.error().put("result", JSONUtil.parseObj(responseJson)); |
| | |
| | | * @param code 接口代码 |
| | | * @return 执行是否成功 |
| | | */ |
| | | public AjaxResult save(JSONObject object, String code,HisApiConfig hisApiConfig,String paramsJson) { |
| | | public AjaxResult save(JSONObject object, String code,Object hisApiConfig,String paramsJson) { |
| | | String tabName = ""; |
| | | if (code.equals("lis") || code.equals("pacs")) |
| | | tabName = ((ApiConfig)hisApiConfig).getTabName(); |
| | | else tabName = ((HisApiConfig) hisApiConfig).getTabName(); |
| | | Connection connection = getConnection(); |
| | | Boolean isExists = tabIsExists(connection, hisApiConfig.getTabName()); |
| | | Boolean isExists = tabIsExists(connection, tabName); |
| | | if (null == isExists) |
| | | return AjaxResult.error(); |
| | | if (!isExists) { |
| | | try { |
| | | creatTable(object, hisApiConfig.getTabName(), connection); |
| | | creatTable(object, tabName, connection); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | return AjaxResult.error(); |
| | | log.error(String.valueOf(e),e.getMessage()); |
| | | // return AjaxResult.error(); |
| | | } |
| | | } |
| | | //插入数据 |
| | | try { |
| | | operationTable(object,connection,hisApiConfig,paramsJson); |
| | | operationTable(object,connection,hisApiConfig,paramsJson,code); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | return AjaxResult.error(); |
| | | } |
| | | try { |
| | | connection.close(); |
| | | } catch (SQLException ignored) { |
| | | log.error(String.valueOf(e),e.getMessage()); |
| | | // return AjaxResult.error(); |
| | | }finally { |
| | | try { |
| | | connection.close(); |
| | | } catch (SQLException ignored) { |
| | | } |
| | | } |
| | | return AjaxResult.success(); |
| | | } |
| | |
| | | * @param connection |
| | | * @throws SQLException |
| | | */ |
| | | private void operationTable(JSONObject jsonObject, Connection connection,HisApiConfig hisApiConfig,String paramsJson) throws SQLException { |
| | | List<String> columns = getColumns(hisApiConfig.getTabName(), connection); |
| | | private void operationTable(JSONObject jsonObject, Connection connection,Object hisApiConfig,String paramsJson,String code) throws SQLException { |
| | | String tabName = ""; |
| | | if (code.equals("lis") || code.equals("pacs")) |
| | | tabName = ((ApiConfig)hisApiConfig).getTabName(); |
| | | else tabName = ((HisApiConfig) hisApiConfig).getTabName(); |
| | | List<String> columns = getColumns(tabName, connection); |
| | | ArrayList<String> responseColums = new ArrayList<>(); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | String key = entry.getKey().trim().toLowerCase(); |
| | |
| | | responseColums.removeAll(columns); |
| | | if (!responseColums.isEmpty()) { |
| | | for (String colum : responseColums) { |
| | | String sql = "alter table " + hisApiConfig.getTabName() + " add column " + colum + " VARCHAR(200) null"; |
| | | String sql = "alter table " + tabName + " add column " + colum + " VARCHAR(200) null"; |
| | | Statement statement = connection.createStatement(); |
| | | statement.executeUpdate(sql); |
| | | statement.close(); |
| | | } |
| | | insertDataNew(connection, jsonObject,hisApiConfig,paramsJson); |
| | | insertDataNew(connection, jsonObject,hisApiConfig,paramsJson,code); |
| | | } else { |
| | | insertDataNew(connection, jsonObject,hisApiConfig,paramsJson); |
| | | insertDataNew(connection, jsonObject,hisApiConfig,paramsJson,code); |
| | | } |
| | | } |
| | | |
| | |
| | | * @param jsonObject |
| | | * @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()); |
| | | @Transactional |
| | | public void insertDataNew(Connection connection, JSONObject jsonObject, Object hisApiConfig, String paramsJson, String code) throws SQLException { |
| | | String tabName = ""; |
| | | List<String> primaryKesList; |
| | | if (code.equals("lis") || code.equals("pacs")) { |
| | | tabName = ((ApiConfig) hisApiConfig).getTabName(); |
| | | primaryKesList = Arrays.stream(((ApiConfig)hisApiConfig).getPrimaryKeys().split(",")).map(String::toLowerCase).collect(Collectors.toList()); |
| | | } else { |
| | | tabName = ((HisApiConfig) hisApiConfig).getTabName(); |
| | | primaryKesList = Arrays.stream(((HisApiConfig)hisApiConfig).getPrimaryKeys().split(",")).map(String::toLowerCase).collect(Collectors.toList()); |
| | | } |
| | | StringBuilder deleteSqlBuilder = new StringBuilder(); |
| | | StringBuilder insertSqlBuilder = new StringBuilder(); |
| | | StringBuilder valueBuilder = new StringBuilder(); |
| | | insertSqlBuilder.append("insert into ").append(hisApiConfig.getTabName()).append(" ("); |
| | | deleteSqlBuilder.append("delete from ").append(hisApiConfig.getTabName()).append(" where "); |
| | | insertSqlBuilder.append("insert into ").append(tabName).append(" ("); |
| | | deleteSqlBuilder.append("delete from ").append(tabName).append(" where "); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | String defaultVal = entry.getValue().toString(); |
| | | String key = entry.getKey().trim().toLowerCase(); |
| | | if (StrUtil.isBlank(defaultVal) || defaultVal.equals("null")) |
| | | continue; |
| | | String val = defaultVal.trim().replaceAll("\\s+", ""); |
| | | String val = defaultVal.trim().replaceAll("\\s+", "").replaceAll("[^\\x00-\\x7F]", ""); |
| | | if (primaryKesList.contains(key)) { |
| | | deleteSqlBuilder.append(key).append(" = '").append(val).append("' and "); |
| | | } |
| | | insertSqlBuilder.append(key).append(", "); |
| | | valueBuilder.append("'").append(val).append("', "); |
| | | // 判断数据是否大于字段创建值如果大于则增加超过阈值则使用text |
| | | String columnType = getColumnType(connection, hisApiConfig.getTabName(), key); |
| | | String columnType = getColumnType(connection, tabName, key); |
| | | if (!"text".equalsIgnoreCase(columnType)){ |
| | | int currentSize = getColumnSize(connection, hisApiConfig.getTabName(), key); |
| | | int currentSize = getColumnSize(connection, tabName, key); |
| | | if (currentSize < val.length()){ |
| | | if (val.length()>= 1000){ |
| | | alterColumnTypeToText(connection, hisApiConfig.getTabName(), key); |
| | | alterColumnTypeToText(connection, tabName, key); |
| | | }else { |
| | | int newSize = ((val.length() / 100) + 1) * 100; |
| | | alterColumnSize(connection, hisApiConfig.getTabName(), key, newSize); |
| | | alterColumnSize(connection, tabName, key, newSize); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | deleteSqlBuilder.delete(deleteSqlBuilder.length() - 5, deleteSqlBuilder.length()); |
| | | Statement statement = connection.prepareStatement(deleteSqlBuilder.toString()); |
| | | // log.info("执行删除 ->{}",deleteSqlBuilder.toString()); |
| | | log.info("HIS数据保存类 执行删除 ->{}",deleteSqlBuilder.toString()); |
| | | int i = statement.executeUpdate(deleteSqlBuilder.toString()); |
| | | // log.info("删除条数 ->{}",i); |
| | | log.info("HIS数据保存类 删除条数 ->{}",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")) { |
| | | try (ResultSet resultSet = metaData.getColumns(null, null, tabName, "request_params")) { |
| | | if (!resultSet.next()){ |
| | | String alterTableSQL = String.format("ALTER TABLE %s ADD COLUMN %s %s", hisApiConfig.getTabName(), "request_params", "text"); |
| | | String alterTableSQL = String.format("ALTER TABLE %s ADD COLUMN %s %s", tabName, "request_params", "text"); |
| | | statement = connection.createStatement(); |
| | | statement.execute(alterTableSQL); |
| | | } |
| | |
| | | insertSqlBuilder.append(") values (").append(valueBuilder).append(")"); |
| | | // log.info("执行插入 ->{}",insertSqlBuilder.toString()); |
| | | // 插入数据 |
| | | // boolean execute = statement.execute(insertSqlBuilder.toString()); |
| | | // for (int j = 0; j < 10; j++) { |
| | | // statement = connection.createStatement(); |
| | | // log.info("HIS数据保存类 执行插入 ->{}",insertSqlBuilder.toString()); |
| | | // boolean execute = statement.execute(insertSqlBuilder.toString()); |
| | | // log.info("HIS数据保存类 执行插入状态 ->{},次数->{}",execute,(j+1)); |
| | | // statement.close(); |
| | | // if (execute) |
| | | // break; |
| | | // } |
| | | statement = connection.createStatement(); |
| | | statement.execute(insertSqlBuilder.toString()); |
| | | log.info("HIS数据保存类 执行插入 ->{}",insertSqlBuilder.toString()); |
| | | boolean execute = statement.execute(insertSqlBuilder.toString()); |
| | | if (!execute){ |
| | | int updateCount = statement.getUpdateCount(); |
| | | log.info("HIS数据保存类 执行插入成功 影响行数->{}",updateCount); |
| | | } |
| | | statement.close(); |
| | | } |
| | | |
| | |
| | | statement.close(); |
| | | return next; |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | log.error(String.valueOf(throwables),throwables.getMessage()); |
| | | } |
| | | log.error("查询表执行sql返回为null ->{},{},{}",tabSql,DB_NAME,tableName); |
| | | return null; |
| | | } |
| | | |