| | |
| | | package com.example.utils; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.*; |
| | |
| | | @Slf4j |
| | | @Component |
| | | public class DictionaryUtil { |
| | | private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis_sjpt/src/main/resources/config.properties"; |
| | | // private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis_sjpt/src/main/resources/config.properties"; |
| | | // private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis_sjpt/src/main/resources/ltkj.properties"; |
| | | // private static final String CONFIG_PATH = "D:\\ltkjprojectconf\\config.properties"; |
| | | private static final String CONFIG_PATH = "D:\\ltkjprojectconf\\config.properties"; |
| | | private static String BASE_API_URL = ""; |
| | | // 请求代码总集合 |
| | | private static List<String> methods = new ArrayList<>(); |
| | |
| | | |
| | | /** |
| | | * 暂时不做 |
| | | * 1.18 计费明细项目 MXJFXMZD |
| | | * 1.90 门诊明细计费及组合计费项目 MZJFZHMXXM |
| | | * |
| | | */ |
| | |
| | | methods.add("MZJCJYSQXMFL"); |
| | | methods.add("MZJCJYSQDDYXM"); |
| | | methods.add("BRFB"); |
| | | methods.add("MXJFXMZD"); |
| | | methods.add("MZJFZHMXXM"); |
| | | |
| | | limits.add("ZDZYZ"); |
| | | limits.add("KSZD"); |
| | |
| | | limits.add("TJZHGLMXZD"); |
| | | limits.add("TJTCZD"); |
| | | limits.add("TJTCGLTJZHZD"); |
| | | limits.add("MXJFXMZD"); |
| | | limits.add("MZJFZHMXXM"); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // DictionaryUtil util = new DictionaryUtil(); |
| | | // try { |
| | | // util.exec1(); |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // log.error("异常"); |
| | | // } |
| | | DictionaryUtil util = new DictionaryUtil(); |
| | | try { |
| | | util.exec1(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | log.error("异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public JSONObject addInputVal(JSONObject jsonObject){ |
| | | JSONObject obj = JSONUtil.createObj(); |
| | | String method = jsonObject.getStr("method"); |
| | | if (method.equals("MZJFZHMXXM") || method.equals("MXJFXMZD")){ |
| | | obj.putOpt("mode","1"); |
| | | } |
| | | obj.putOpt("jgbm",""); |
| | | obj.putOpt("bm",""); |
| | | obj.putOpt("flbm",""); |
| | |
| | | String apiPort = props.getProperty("his_api_port"); |
| | | BASE_API_URL = apiUrl+":"+apiPort+"/api/His/HisRequest"; |
| | | Connection connection = getConnection(user,password,url); |
| | | methods.forEach(method -> { |
| | | log.info("开始请求代码 ->{}",method); |
| | | for (String method : methods) { |
| | | log.info("开始请求代码 ->{}", method); |
| | | String tabName = "ltkj_" + method.toLowerCase(); |
| | | boolean isLimit = false; |
| | | int page = 1; |
| | |
| | | input.putOpt("pagecount", size); |
| | | input.putOpt("page", page); |
| | | } |
| | | log.info("请求接口 ->{},请求参数 ->{}",BASE_API_URL,object.toString()); |
| | | log.info("请求接口 ->{}, 请求参数 ->{}", BASE_API_URL, object.toString()); |
| | | JSONObject entries = execRequest(object.toString()); |
| | | // log.info("请求返回 ->{}",entries.toString()); |
| | | assert entries != null; |
| | | // log.info("请求返回 ->{}", entries.toString()); |
| | | JSONObject response = entries.getJSONObject("Response"); |
| | | if ("0".equals(response.getStr("ResultCode"))) { |
| | | try { |
| | | dropTable(tabName,connection); |
| | | dropTable(tabName, connection); |
| | | } catch (SQLException e) { |
| | | log.error("删除表异常 ->{}",e.getMessage()); |
| | | log.error("删除表异常 ->{}", e.getMessage()); |
| | | } |
| | | if (isLimit) { |
| | | maxPage = LimitInsertData(connection, tabName, response,name,page); |
| | | maxPage = LimitInsertData(connection, tabName, response,name, page); |
| | | log.info("计算页码为 ->{}", maxPage); |
| | | if (maxPage > 1) { |
| | | page += 1; |
| | | for (int i = page; i <= maxPage; i++) { |
| | | for (page = 2; page <= maxPage; page++) { |
| | | object.clear(); |
| | | object.putOpt("method", method); |
| | | object = addInputVal(object); |
| | | JSONObject input = object.getJSONObject("input"); |
| | | input.putOpt("pagecount", size); |
| | | input.putOpt("page", page); |
| | | log.info("请求接口 ->{},请求参数 ->{}",BASE_API_URL,object.toString()); |
| | | log.info("请求接口 ->{}, 请求参数 ->{}", BASE_API_URL, object.toString()); |
| | | entries = execRequest(object.toString()); |
| | | // log.info("请求返回 ->{}",entries.toString()); |
| | | // log.info("请求返回 ->{}", entries.toString()); |
| | | response = entries.getJSONObject("Response"); |
| | | if ("0".equals(response.getStr("ResultCode"))) { |
| | | LimitInsertData(connection, tabName, response,name,page); |
| | | LimitInsertData(connection, tabName, response,name, page); |
| | | } else { |
| | | log.error("{} 请求失败:{}",method,object.toString()); |
| | | log.error("{} 请求失败:{}", method, object.toString()); |
| | | } |
| | | page++; |
| | | } |
| | | } |
| | | } else { |
| | | JSONArray resultData = response.getJSONArray("ResultData"); |
| | | JSONObject jsonObject = JSONUtil.toList(resultData, JSONObject.class).get(0); |
| | | editDataBase(connection, tabName, jsonObject,name); |
| | | List<JSONObject> list = JSONUtil.toList(resultData, JSONObject.class); |
| | | for (JSONObject jsonObject : list) { |
| | | editDataBase(connection, tabName, jsonObject,name); |
| | | } |
| | | } |
| | | } else { |
| | | log.error("{}请求失败 - 请求状态码不为0,请求参数 ->{},请求返回 ->{}", method,object.toString(),response.toString()); |
| | | log.error("{}请求失败 - 请求状态码不为0, 请求参数 ->{}, 请求返回 ->{}", method, object.toString(), response.toString()); |
| | | } |
| | | }); |
| | | } |
| | | log.info("执行同步完毕"); |
| | | } |
| | | |
| | |
| | | // 创建表 |
| | | creatTable(obj, tabName, connection); |
| | | } |
| | | // 对比字段 并插入数据 |
| | | operationTable(obj, tabName, connection); |
| | | } catch (SQLException throwables) { |
| | | log.error("数据库操作异常"); |
| | | log.error("创建表异常"); |
| | | log.error(throwables.getSQLState()); |
| | | log.error(throwables.getMessage()); |
| | | } |
| | | // 对比字段 并插入数据 |
| | | try { |
| | | operationTable(obj, tabName, connection); |
| | | } catch (SQLException e) { |
| | | log.error("对比字段插入数据异常"); |
| | | log.error(e.getSQLState()); |
| | | log.error(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | // log.info("当前表字段为 ->{}", columns); |
| | | ArrayList<String> responseColums = new ArrayList<>(); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | responseColums.add(entry.getKey()); |
| | | String key = entry.getKey().trim().toLowerCase(); |
| | | responseColums.add(key); |
| | | } |
| | | // log.info("请求返回的字段为 ->{}", responseColums); |
| | | // log.info("请求返回的字段为 ->{}", responseColums); |
| | | responseColums.removeAll(columns); |
| | | // log.info("需要增加的字段 ->{}",responseColums); |
| | | if (!responseColums.isEmpty()) { |
| | | // 需要增加字段 并插入数据 |
| | | for (String colum : responseColums) { |
| | |
| | | insertSqlBuilder.append("insert into ").append(tabName).append(" ("); |
| | | // selectSqlBuilder.append("select count(1) as count from ").append(tabName).append(" where "); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | String str = entry.getValue().toString().replaceAll("\\\\", ""); |
| | | if (str.equals("null")) |
| | | String defaultVal = entry.getValue().toString(); |
| | | String key = entry.getKey().trim().toLowerCase(); |
| | | if (StrUtil.isBlank(defaultVal) || defaultVal.equals("null")) |
| | | continue; |
| | | insertSqlBuilder.append(entry.getKey()).append(", "); |
| | | valueBuilder.append("'").append(str.replaceAll("'","‘")).append("', "); |
| | | String val = defaultVal.trim().replaceAll("\\s+", "").replace("\\",""); |
| | | insertSqlBuilder.append(key).append(", "); |
| | | valueBuilder.append("'").append(val.replaceAll("'","‘")).append("', "); |
| | | // selectSqlBuilder.append(entry.getKey()).append(" = '").append(entry.getValue().toString().replaceAll("'","‘")).append("' and "); |
| | | } |
| | | insertSqlBuilder.append("insert_time, "); |
| | |
| | | // } |
| | | // 插入数据 |
| | | Statement statement = connection.createStatement(); |
| | | // log.info("插入数据 sql-> {}", insertSqlBuilder.toString()); |
| | | log.info("插入数据 sql-> {}", insertSqlBuilder.toString()); |
| | | statement.execute(insertSqlBuilder.toString()); |
| | | statement.close(); |
| | | } |
| | |
| | | private void creatTable(JSONObject resultDataIndex1, String tabName, Connection connection) throws SQLException { |
| | | StringBuilder sql = new StringBuilder("CREATE TABLE " + tabName + " ("); |
| | | for (Map.Entry<String, Object> entry : resultDataIndex1.entrySet()) { |
| | | sql.append(entry.getKey()).append(" VARCHAR(200) null,"); |
| | | String key = entry.getKey().trim().toLowerCase(); |
| | | sql.append(key).append(" VARCHAR(200) null,"); |
| | | } |
| | | sql.append("insert_time").append(" VARCHAR(100) null,"); |
| | | sql = new StringBuilder(sql.substring(0, sql.length() - 1)); |
| | | sql.append(");"); |
| | | // log.info("创建表格 -> {}",sql.toString()); |
| | | log.info("创建表格 -> {}",sql.toString()); |
| | | Statement statement = connection.createStatement(); |
| | | statement.execute(sql.toString()); |
| | | } |