| | |
| | | package com.example.utils; |
| | | |
| | | import cn.hutool.core.util.RandomUtil; |
| | | 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.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.OutputStream; |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.sql.*; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * @Company: 西安路泰科技有限公司 |
| | | * @Author: zhaowenxuan |
| | | * @Date: 2024/5/21 16:41 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class DictionaryUtil { |
| | | private static final Logger log = LoggerFactory.getLogger(DictionaryUtil.class); |
| | | private static final String BASE_API_URL = "https://mock.mengxuegu.com/mock/664d3dbce45d2156fa209c80/example/api/His/HisRequest"; |
| | | // 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 String BASE_API_URL = ""; |
| | | // 请求代码总集合 |
| | | private static List<String> methods = new ArrayList<>(); |
| | | private static final String url = "jdbc:mysql://localhost:3306/test_urlreq"; |
| | | private static final String user = "root"; |
| | | private static final String password = "Root_ltkj123"; |
| | | // 需要分页的代码集合 |
| | | private static List<String> limits = new ArrayList<>(); |
| | | |
| | | /** |
| | | * 暂时不做 |
| | | * 1.18 计费明细项目 MXJFXMZD |
| | | * 1.90 门诊明细计费及组合计费项目 MZJFZHMXXM |
| | | * |
| | | */ |
| | | |
| | | // 分页每次1000条 |
| | | static { |
| | | methods.add("JGDM"); |
| | | methods.add("ZDZYZ"); |
| | | methods.add("YQZD"); |
| | | methods.add("KSFLZD"); |
| | | methods.add("BQZD"); |
| | | methods.add("ZKKSZD"); |
| | |
| | | methods.add("ZYZZZFZD"); |
| | | methods.add("YJJFXMZD"); |
| | | methods.add("EJJFXMZD"); |
| | | methods.add("MXJFXMZD"); |
| | | methods.add("ZHFLXMZD"); |
| | | methods.add("JFZHXMZD"); |
| | | methods.add("JFZHGLMXJFXMZD"); |
| | |
| | | methods.add("TJZHGLMXZD"); |
| | | methods.add("TJTCZD"); |
| | | methods.add("TJTCGLTJZHZD"); |
| | | |
| | | methods.add("HTDWZD"); |
| | | methods.add("ZFLXZD"); |
| | | methods.add("MZJCJYSQD"); |
| | | methods.add("MZJCJYSQXMFL"); |
| | | methods.add("MZJCJYSQDDYXM"); |
| | | methods.add("MZJFZHMXXM"); |
| | | methods.add("BRFB"); |
| | | |
| | | limits.add("ZDZYZ"); |
| | | limits.add("KSZD"); |
| | | limits.add("YHZD"); |
| | | limits.add("XYZDZD"); |
| | | limits.add("ZYZDZD"); |
| | | limits.add("SSZD"); |
| | | limits.add("ZLXTXZD"); |
| | | limits.add("ZYZZZFZD"); |
| | | limits.add("JFZHGLMXJFXMZD"); |
| | | limits.add("YPZD"); |
| | | limits.add("JYKSZD"); |
| | | limits.add("JYYSZD"); |
| | | limits.add("JYXMZD"); |
| | | limits.add("JYZBZD"); |
| | | limits.add("JYXMZBZD"); |
| | | limits.add("JFXMGLJYXMZD"); |
| | | limits.add("JFZHGLJYXMZD"); |
| | | limits.add("JFXMGLJCBWZD"); |
| | | limits.add("TJKSZD"); |
| | | limits.add("TJYSZD"); |
| | | limits.add("TJXMZD"); |
| | | limits.add("TJZHXMZD"); |
| | | limits.add("TJZHGLMXZD"); |
| | | limits.add("TJTCZD"); |
| | | limits.add("TJTCGLTJZHZD"); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | DictionaryUtil util = new DictionaryUtil(); |
| | | util.exec(); |
| | | } |
| | | |
| | | private void exec() { |
| | | JSONObject entries = execRequest(); |
| | | if (entries != null) { |
| | | JSONObject response = entries.getJSONObject("Response"); |
| | | // 0 成功 -1失败 |
| | | if ("0".equals(response.getStr("ResultCode"))) { |
| | | JSONArray jsonArray = response.getJSONArray("ResultData"); |
| | | JSONObject resultDataIndex1 = jsonArray.getJSONObject(0); |
| | | String method = entries.getStr("method"); |
| | | String tabName = "ltkj_" + method; |
| | | Connection connection = getConnection(); |
| | | try { |
| | | if (!tabIsExists(connection, tabName)) { |
| | | // 创建表 |
| | | creatTable(resultDataIndex1, tabName, connection); |
| | | } |
| | | // 对比字段 并插入数据 |
| | | operationTable(jsonArray, tabName, connection); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | } |
| | | } |
| | | try { |
| | | util.exec1(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | log.error("异常"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 执行http请求 |
| | | * |
| | | * 增加默认参数 |
| | | * @param jsonObject |
| | | * @return |
| | | */ |
| | | private JSONObject execRequest() { |
| | | public JSONObject addInputVal(JSONObject jsonObject){ |
| | | JSONObject obj = JSONUtil.createObj(); |
| | | obj.putOpt("jgbm",""); |
| | | obj.putOpt("bm",""); |
| | | obj.putOpt("flbm",""); |
| | | jsonObject.putOpt("input",obj); |
| | | return jsonObject; |
| | | } |
| | | |
| | | public void exec1() throws IOException { |
| | | int size = 1000; |
| | | FileInputStream inputStream = new FileInputStream(CONFIG_PATH); |
| | | Properties props = new Properties(); |
| | | props.load(inputStream); |
| | | 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"); |
| | | String apiUrl = props.getProperty("his_api_url"); |
| | | 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); |
| | | String tabName = "ltkj_" + method.toLowerCase(); |
| | | boolean isLimit = false; |
| | | int page = 1; |
| | | int maxPage = 1; |
| | | JSONObject object = JSONUtil.createObj(); |
| | | object.putOpt("method", method); |
| | | object = addInputVal(object); |
| | | if (limits.contains(method)) { |
| | | isLimit = true; |
| | | JSONObject input = object.getJSONObject("input"); |
| | | input.putOpt("pagecount", size); |
| | | input.putOpt("page", page); |
| | | } |
| | | log.info("请求接口 ->{},请求参数 ->{}",BASE_API_URL,object.toString()); |
| | | JSONObject entries = execRequest(object.toString()); |
| | | // log.info("请求返回 ->{}",entries.toString()); |
| | | assert entries != null; |
| | | JSONObject response = entries.getJSONObject("Response"); |
| | | if ("0".equals(response.getStr("ResultCode"))) { |
| | | if (isLimit) { |
| | | maxPage = LimitInsertData(connection, tabName, response,name,page); |
| | | log.info("计算页码为 ->{}", maxPage); |
| | | if (maxPage > 1) { |
| | | page += 1; |
| | | for (int i = page; i <= maxPage; i++) { |
| | | 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()); |
| | | entries = execRequest(object.toString()); |
| | | // log.info("请求返回 ->{}",entries.toString()); |
| | | response = entries.getJSONObject("Response"); |
| | | if ("0".equals(response.getStr("ResultCode"))) { |
| | | LimitInsertData(connection, tabName, response,name,page); |
| | | } else { |
| | | 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); |
| | | } |
| | | } else { |
| | | log.error("{}请求失败 - 请求状态码不为0,请求参数 ->{},请求返回 ->{}", method,object.toString(),response.toString()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 针对于分页接口 创建表、插入数据 |
| | | * |
| | | * @param connection |
| | | * @param tabName |
| | | * @param response |
| | | * @return |
| | | */ |
| | | private int LimitInsertData(Connection connection, String tabName, JSONObject response,String name,Integer page) { |
| | | JSONArray jsonArray = response.getJSONArray("ResultData"); |
| | | JSONObject entries = (JSONObject) jsonArray.get(0); |
| | | int maxPage; |
| | | // 行数 数据返回的第几行 |
| | | Integer rowNumber = entries.getInt("RowNumber"); |
| | | // 总条数 |
| | | Integer totalCount = entries.getInt("totalCount"); |
| | | maxPage = (totalCount + 1000 - 1) / 1000; |
| | | log.info("请求返回总条数 ->{},当前页 ->{},总页数 ->{}",totalCount,page,maxPage); |
| | | jsonArray.forEach(obj -> { |
| | | editDataBase(connection, tabName, (JSONObject) obj,name); |
| | | }); |
| | | return maxPage; |
| | | } |
| | | |
| | | /** |
| | | * 操作数据库 |
| | | * |
| | | * @param connection |
| | | * @param tabName |
| | | * @param obj |
| | | */ |
| | | private void editDataBase(Connection connection, String tabName, JSONObject obj,String name) { |
| | | try { |
| | | if (!tabIsExists(connection, tabName,name)) { |
| | | // 创建表 |
| | | creatTable(obj, tabName, connection); |
| | | } |
| | | // 对比字段 并插入数据 |
| | | operationTable(obj, tabName, connection); |
| | | } catch (SQLException throwables) { |
| | | log.error("数据库操作异常"); |
| | | log.error(throwables.getSQLState()); |
| | | log.error(throwables.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 执行post请求 |
| | | * |
| | | * @param params 请求参数 例如: param1=val1¶m2=val2 |
| | | * @return 请求返回的json转换后的JSONObject对象 |
| | | */ |
| | | private JSONObject execRequest(String params) { |
| | | // System.out.println(params); |
| | | URL url = null; |
| | | HttpURLConnection connection = null; |
| | | try { |
| | | url = new URL(BASE_API_URL); |
| | | connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("POST"); |
| | | connection.setDoOutput(true); |
| | | connection.setRequestProperty("Accept", "application/json"); |
| | | connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| | | // connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| | | connection.setRequestProperty("Content-Type", "application/json;utf-8"); |
| | | connection.setDoOutput(true); |
| | | OutputStream stream = connection.getOutputStream(); |
| | | int index = 0; |
| | | if (methods.size() > 1) { |
| | | index = RandomUtil.randomInt(0, methods.size() - 1); |
| | | } |
| | | String method = methods.get(index); |
| | | String param = "method=" + method; |
| | | stream.write(param.getBytes(StandardCharsets.UTF_8)); |
| | | stream.write(params.getBytes(StandardCharsets.UTF_8)); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | StringBuilder builder = new StringBuilder(); |
| | | String str; |
| | | while ((str = reader.readLine()) != null) { |
| | | builder.append(str); |
| | | } |
| | | log.info("执行请求响应:{}", builder.toString()); |
| | | JSONObject entries = JSONUtil.parseObj(builder.toString()); |
| | | entries.set("method", method); |
| | | return entries; |
| | | return JSONUtil.parseObj(builder.toString()); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | |
| | | return null; |
| | | } |
| | | |
| | | private Connection getConnection() { |
| | | private Connection getConnection(String user,String password,String url) { |
| | | try { |
| | | log.info("数据库信息 ->{},{},{}",user,password,url); |
| | | return DriverManager.getConnection(url, user, password); |
| | | } catch (SQLException throwables) { |
| | | } catch (Exception throwables) { |
| | | log.error("获取sql连接失败"); |
| | | throwables.printStackTrace(); |
| | | } |
| | | return null; |
| | |
| | | * @return |
| | | * @throws SQLException |
| | | */ |
| | | private Boolean tabIsExists(Connection connection, String tableName) throws SQLException { |
| | | private Boolean tabIsExists(Connection connection, String tableName,String name) throws SQLException { |
| | | String tabSql = "SELECT table_name FROM information_schema.tables WHERE table_schema = ? AND table_name = ?"; |
| | | PreparedStatement statement = connection.prepareStatement(tabSql); |
| | | statement.setString(1, "test_urlreq"); |
| | | statement.setString(1, name); |
| | | statement.setString(2, tableName); |
| | | log.info("判断表是否存在 sql-> {}", statement.toString()); |
| | | // log.info("判断表是否存在 sql-> {}", statement.toString()); |
| | | ResultSet resultSet = statement.executeQuery(); |
| | | boolean next = resultSet.next(); |
| | | statement.close(); |
| | |
| | | /** |
| | | * 操作表 |
| | | * |
| | | * @param jsonArray |
| | | * @param tabName |
| | | * @param connection |
| | | * @throws SQLException |
| | | */ |
| | | private void operationTable(JSONArray jsonArray, String tabName, Connection connection) throws SQLException { |
| | | private void operationTable(JSONObject jsonObject, String tabName, Connection connection) throws SQLException { |
| | | List<String> columns = getColumns(tabName, connection); |
| | | log.info("当前表字段为 ->{}", columns); |
| | | // log.info("当前表字段为 ->{}", columns); |
| | | ArrayList<String> responseColums = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject jsonObject = jsonArray.getJSONObject(i); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | responseColums.add(entry.getKey()); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | responseColums.add(entry.getKey()); |
| | | } |
| | | // log.info("请求返回的字段为 ->{}", responseColums); |
| | | responseColums.removeAll(columns); |
| | | if (!responseColums.isEmpty()) { |
| | | // 需要增加字段 并插入数据 |
| | | for (String colum : responseColums) { |
| | | String sql = "alter table " + tabName + " add column " + colum + " text null"; |
| | | // log.info("修改字段 ->{}",sql.toString()); |
| | | Statement statement = connection.createStatement(); |
| | | statement.executeUpdate(sql); |
| | | statement.close(); |
| | | } |
| | | log.info("请求返回的字段为 ->{}", responseColums); |
| | | responseColums.removeAll(columns); |
| | | if (!responseColums.isEmpty()) { |
| | | // 需要增加字段 并插入数据 |
| | | for (String colum : responseColums) { |
| | | String sql = "alter table " + tabName + " add column " + colum + " text null"; |
| | | Statement statement = connection.createStatement(); |
| | | statement.executeUpdate(sql); |
| | | statement.close(); |
| | | } |
| | | insertData(tabName, connection, jsonObject); |
| | | } else { |
| | | insertData(tabName, connection, jsonObject); |
| | | } |
| | | insertData(tabName, connection, jsonObject); |
| | | } else { |
| | | insertData(tabName, connection, jsonObject); |
| | | } |
| | | } |
| | | |
| | |
| | | selectSqlBuilder.append("select count(1) as count from ").append(tabName).append(" where "); |
| | | for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { |
| | | insertSqlBuilder.append(entry.getKey()).append(", "); |
| | | valueBuilder.append("'").append(entry.getValue()).append("', "); |
| | | selectSqlBuilder.append(entry.getKey()).append(" = '").append(entry.getValue()).append("' and "); |
| | | valueBuilder.append("'").append(entry.getValue().toString().replaceAll("'","‘")).append("', "); |
| | | selectSqlBuilder.append(entry.getKey()).append(" = '").append(entry.getValue().toString().replaceAll("'","‘")).append("' and "); |
| | | } |
| | | 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()); |
| | | selectSqlBuilder.delete(selectSqlBuilder.length() - 5, selectSqlBuilder.length()); |
| | | insertSqlBuilder.append(") values (").append(valueBuilder).append(")"); |
| | | log.info("插入前查询 sql ->{}", selectSqlBuilder.toString()); |
| | | // log.info("插入前查询 sql ->{}", selectSqlBuilder.toString()); |
| | | Statement statement = connection.prepareStatement(selectSqlBuilder.toString()); |
| | | ResultSet resultSet = statement.executeQuery(selectSqlBuilder.toString()); |
| | | resultSet.next(); |
| | |
| | | statement.close(); |
| | | // 如果不为0 则这条数据存在 不进行插入 |
| | | if (!"0".equals(string)) { |
| | | log.info("数据存在不需要插入 {}", jsonObject); |
| | | // log.info("数据存在不需要插入 {}", jsonObject); |
| | | return; |
| | | } |
| | | // 插入数据 |
| | | statement = connection.createStatement(); |
| | | log.info("插入数据 sql-> {}", insertSqlBuilder.toString()); |
| | | // log.info("插入数据 sql-> {}", insertSqlBuilder.toString()); |
| | | statement.execute(insertSqlBuilder.toString()); |
| | | statement.close(); |
| | | } |
| | |
| | | for (Map.Entry<String, Object> entry : resultDataIndex1.entrySet()) { |
| | | sql.append(entry.getKey()).append(" text null,"); |
| | | } |
| | | sql.append("insert_time").append(" text null,"); |
| | | sql = new StringBuilder(sql.substring(0, sql.length() - 1)); |
| | | sql.append(");"); |
| | | // log.info("创建表格 -> {}",sql.toString()); |
| | | Statement statement = connection.createStatement(); |
| | | statement.execute(sql.toString()); |
| | | } |