| | |
| | | import com.ltkj.common.core.domain.AjaxResult; |
| | | import com.ltkj.hosp.domain.HisApiConfig; |
| | | 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.stereotype.Component; |
| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.Date; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Company: 西安路泰科技有限公司 |
| | |
| | | public class HisApiGetMethodService { |
| | | // 数据库配置文件路径 |
| | | private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis/ltkj-admin/src/main/resources/config.properties"; |
| | | // private static final String CONFIG_PATH = "D:\\ltkjprojectconf\\config.properties"; |
| | | // 数据库名 |
| | | private static String DB_NAME = ""; |
| | | private static String HIS_URL = ""; |
| | | private static String DATA_URL = ""; |
| | | private static String DATA_USER =""; |
| | | private static String DATA_PASS = ""; |
| | | @Autowired |
| | | private HisApiMethodService controller; |
| | | @Autowired |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | FileInputStream inputStream = new FileInputStream(CONFIG_PATH); |
| | | InputStreamReader reader = new InputStreamReader(new FileInputStream(CONFIG_PATH), StandardCharsets.UTF_8); |
| | | Properties props = new Properties(); |
| | | props.load(inputStream); |
| | | String url = props.getProperty("his_api_url"); |
| | | props.load(reader); |
| | | String api_url = props.getProperty("his_api_url"); |
| | | String port = props.getProperty("his_api_port"); |
| | | HIS_URL=url+":"+port+"/api/His/"; |
| | | DB_NAME = props.getProperty("name"); |
| | | DATA_URL = "jdbc:mysql://" + props.getProperty("ip") + ":" + props.getProperty("prot") + "/" + DB_NAME + |
| | | "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"; |
| | | DATA_USER = props.getProperty("username"); |
| | | DATA_PASS = props.getProperty("password"); |
| | | HIS_URL=api_url+":"+port; |
| | | } catch (IOException throwables) { |
| | | throwables.printStackTrace(); |
| | | } |
| | |
| | | */ |
| | | @Transactional |
| | | public AjaxResult getHISData(String type, Map<String, Object> params) { |
| | | // LambdaQueryWrapper<HisApiConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | // lambdaQueryWrapper.eq(HisApiConfig::getApiMethod, type); |
| | | // HisApiConfig hisApiConfig = hisApiConfigService.getOne(lambdaQueryWrapper); |
| | | |
| | | AjaxResult result = null; |
| | | // TODO 创建表 弄成数据库动态获取 接口地址、代码、是否分页、主键id、主键字段 |
| | | // TODO 从2.4.2获取到数据 根据his_registration_id删除旧数据在插入新数据 下层业务均一样根据返回的JYBGID删除旧数据在插入新数据 |
| | |
| | | List<Map<String, String>> list = new ArrayList<>(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | ajaxResult = save(object, type); |
| | | ajaxResult = save(object, type,null); |
| | | Map<String, String> map = new HashMap<>(); |
| | | for (String key : object.keySet()) { |
| | | String defaultVal = object.get(key).toString(); |
| | |
| | | } |
| | | } |
| | | |
| | | @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); |
| | | JSONObject response = JSONUtil.parseObj(responseJson).getJSONObject("Response"); |
| | | if (response.getStr("ResultCode").equals("0")) { |
| | | AjaxResult ajaxResult = AjaxResult.success(); |
| | | JSONArray resultData = response.getJSONArray("ResultData"); |
| | | List<Map<String, String>> list = new ArrayList<>(); |
| | | for (Object resultDatum : resultData) { |
| | | JSONObject object = (JSONObject) resultDatum; |
| | | ajaxResult = save(object, type,hisApiConfig); |
| | | 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)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ResultData为对象 |
| | | * 根据his接口返回值 插入数据 |
| | |
| | | * @param code 接口代码 |
| | | * @return 执行是否成功 |
| | | */ |
| | | public AjaxResult save(JSONObject object, 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")) { |
| | | // JSONObject object = response.getJSONObject("ResultData"); |
| | | public AjaxResult save(JSONObject object, String code,HisApiConfig hisApiConfig) { |
| | | Connection connection = getConnection(); |
| | | // for (Object resultDatum : resultData) { |
| | | // JSONObject object = (JSONObject) resultDatum; |
| | | Boolean isExists = tabIsExists(connection, tabName); |
| | | Boolean isExists = tabIsExists(connection, hisApiConfig.getTabName()); |
| | | if (null == isExists) |
| | | return AjaxResult.error(); |
| | | if (!isExists) { |
| | | try { |
| | | creatTable(object, tabName, connection); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | creatTable(object, hisApiConfig.getTabName(), connection); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | return AjaxResult.error(); |
| | | } |
| | | } |
| | | //插入数据 |
| | | try { |
| | | operationTable(object, tabName, connection); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | operationTable(object,connection,hisApiConfig); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | return AjaxResult.error(); |
| | | } |
| | | // } |
| | | try { |
| | | if (connection != null) |
| | | connection.close(); |
| | | } catch (SQLException throwables) { |
| | | } catch (SQLException ignored) { |
| | | } |
| | | // } |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | |
| | | } |
| | | //插入数据 |
| | | try { |
| | | operationTable(object, tabName, connection); |
| | | operationTable(object,connection,null); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | return AjaxResult.error(); |
| | |
| | | /** |
| | | * 操作表 |
| | | * |
| | | * @param tabName |
| | | * @param connection |
| | | * @throws SQLException |
| | | */ |
| | | private void operationTable(JSONObject jsonObject, String tabName, Connection connection) throws SQLException { |
| | | List<String> columns = getColumns(tabName, connection); |
| | | private void operationTable(JSONObject jsonObject, Connection connection,HisApiConfig hisApiConfig) 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.removeAll(columns); |
| | | if (!responseColums.isEmpty()) { |
| | | for (String colum : responseColums) { |
| | | String sql = "alter table " + tabName + " add column " + colum + " VARCHAR(200) null"; |
| | | String sql = "alter table " + hisApiConfig.getTabName() + " add column " + colum + " VARCHAR(200) null"; |
| | | Statement statement = connection.createStatement(); |
| | | statement.executeUpdate(sql); |
| | | statement.close(); |
| | | } |
| | | insertData(tabName, connection, jsonObject); |
| | | if (hisApiConfig == null) |
| | | insertData(hisApiConfig.getTabName(), connection, jsonObject); |
| | | else insertDataNew(connection, jsonObject,hisApiConfig); |
| | | } else { |
| | | insertData(tabName, connection, jsonObject); |
| | | if (hisApiConfig == null) |
| | | insertData(hisApiConfig.getTabName(), connection, jsonObject); |
| | | else insertDataNew(connection, jsonObject,hisApiConfig); |
| | | } |
| | | } |
| | | |
| | |
| | | insertSqlBuilder.delete(insertSqlBuilder.length() - 2, insertSqlBuilder.length()); |
| | | valueBuilder.delete(valueBuilder.length() - 2, valueBuilder.length()); |
| | | insertSqlBuilder.append(") values (").append(valueBuilder).append(")"); |
| | | // 插入数据 |
| | | statement = connection.createStatement(); |
| | | statement.execute(insertSqlBuilder.toString()); |
| | | statement.close(); |
| | | } |
| | | |
| | | /** |
| | | * 插入数据 |
| | | * |
| | | * @param connection |
| | | * @param jsonObject |
| | | * @throws SQLException |
| | | */ |
| | | private void insertDataNew(Connection connection, JSONObject jsonObject,HisApiConfig hisApiConfig) throws SQLException { |
| | | List<String> primaryKesList = Arrays.stream(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 "); |
| | | 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+", ""); |
| | | if (primaryKesList.contains(key)) { |
| | | deleteSqlBuilder.append(key).append(" = '").append(val).append("' and "); |
| | | } |
| | | insertSqlBuilder.append(key).append(", "); |
| | | valueBuilder.append("'").append(val).append("', "); |
| | | } |
| | | deleteSqlBuilder.delete(deleteSqlBuilder.length() - 5, deleteSqlBuilder.length()); |
| | | Statement statement = connection.prepareStatement(deleteSqlBuilder.toString()); |
| | | // log.info("执行删除 ->{}",deleteSqlBuilder.toString()); |
| | | int i = statement.executeUpdate(deleteSqlBuilder.toString()); |
| | | // log.info("删除条数 ->{}",i); |
| | | statement.close(); |
| | | insertSqlBuilder.append("insert_time, "); |
| | | String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); |
| | | valueBuilder.append("'").append(time).append("'").append(", "); |
| | | insertSqlBuilder.delete(insertSqlBuilder.length() - 2, insertSqlBuilder.length()); |
| | | valueBuilder.delete(valueBuilder.length() - 2, valueBuilder.length()); |
| | | insertSqlBuilder.append(") values (").append(valueBuilder).append(")"); |
| | | // log.info("执行插入 ->{}",insertSqlBuilder.toString()); |
| | | // 插入数据 |
| | | statement = connection.createStatement(); |
| | | statement.execute(insertSqlBuilder.toString()); |
| | |
| | | */ |
| | | private Connection getConnection() { |
| | | try { |
| | | FileInputStream inputStream = new FileInputStream(CONFIG_PATH); |
| | | InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); |
| | | Properties props = new Properties(); |
| | | props.load(reader); |
| | | String name = props.getProperty("name"); |
| | | String url = "jdbc:mysql://" + props.getProperty("ip") + ":" + props.getProperty("prot") + "/" + name + "" + |
| | | "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"; |
| | | String user = props.getProperty("username"); |
| | | String password = props.getProperty("password"); |
| | | log.info("数据库配置信息 ->数据库名-{},user-{},password-{},url-{}", name, user, password, url); |
| | | Connection connection = DriverManager.getConnection(url, user, password); |
| | | DB_NAME = name; |
| | | return connection; |
| | | } catch (SQLException | IOException throwables) { |
| | | return DriverManager.getConnection(DATA_URL, DATA_USER, DATA_PASS); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | } |
| | | return null; |