From e460e434fe3975a4ae33466710d8292245ad8e07 Mon Sep 17 00:00:00 2001
From: zhaowenxuan <chacca165@163.com>
Date: 星期四, 19 六月 2025 18:09:33 +0800
Subject: [PATCH] 2025-06-19

---
 src/main/java/com/example/utils/DictionaryUtil.java |  417 +++++++++++++++++++++++++++++------------------------------
 1 files changed, 204 insertions(+), 213 deletions(-)

diff --git a/src/main/java/com/example/utils/DictionaryUtil.java b/src/main/java/com/example/utils/DictionaryUtil.java
index 08cd5c7..6d7521c 100644
--- a/src/main/java/com/example/utils/DictionaryUtil.java
+++ b/src/main/java/com/example/utils/DictionaryUtil.java
@@ -1,10 +1,11 @@
 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 org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
 
 import java.io.*;
 import java.net.HttpURLConnection;
@@ -20,91 +21,95 @@
  * @Author: zhaowenxuan
  * @Date: 2024/5/21 16:41
  */
+@Slf4j
+@Component
 public class DictionaryUtil {
+//    private static final String CONFIG_PATH = "/Users/chacca/寮�鍙戠浉鍏�/浠g爜/ltkj_peis_sjpt/src/main/resources/config.properties";
+//    private static final String CONFIG_PATH = "/Users/chacca/寮�鍙戠浉鍏�/浠g爜/ltkj_peis_sjpt/src/main/resources/ltkj.properties";
     private static final String CONFIG_PATH = "D:\\ltkjprojectconf\\config.properties";
-    private static final Logger log = LoggerFactory.getLogger(DictionaryUtil.class);
     private static String BASE_API_URL = "";
     // 璇锋眰浠g爜鎬婚泦鍚�
     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";
     // 闇�瑕佸垎椤电殑浠g爜闆嗗悎
     private static List<String> limits = new ArrayList<>();
 
     /**
      * 鏆傛椂涓嶅仛
-     * 1.18 璁¤垂鏄庣粏椤圭洰 MXJFXMZD
      * 1.90 闂ㄨ瘖鏄庣粏璁¤垂鍙婄粍鍚堣璐归」鐩� MZJFZHMXXM
      *
      */
 
 // 鍒嗛〉姣忔1000鏉�
     static {
+        try {
+            Class.forName("com.mysql.cj.jdbc.Driver");
+        } catch (ClassNotFoundException e) {}
         methods.add("JGDM");
         methods.add("ZDZYZ");
-//        methods.add("YQZD");
-//        methods.add("KSFLZD");
-//        methods.add("BQZD");
-//        methods.add("ZKKSZD");
-//        methods.add("KSZD");
-//        methods.add("YWCKZD");
-//        methods.add("GHZLZD");
-//        methods.add("YHZD");
-//        methods.add("XYZDZD");
-//        methods.add("ZYZDZD");
-//        methods.add("SSZD");
+        methods.add("YQZD");
+        methods.add("KSFLZD");
+        methods.add("BQZD");
+        methods.add("ZKKSZD");
+        methods.add("KSZD");
+        methods.add("YWCKZD");
+        methods.add("GHZLZD");
+        methods.add("YHZD");
+        methods.add("XYZDZD");
+        methods.add("ZYZDZD");
+        methods.add("SSZD");
         methods.add("ZLXTXZD");
-//        methods.add("ZYZZZFZD");
-//        methods.add("YJJFXMZD");
-//        methods.add("EJJFXMZD");
-//        methods.add("ZHFLXMZD");
-//        methods.add("JFZHXMZD");
-//        methods.add("JFZHGLMXJFXMZD");
-//        methods.add("CFLXZD");
-//        methods.add("YPZLZD");
-//        methods.add("YPJXZD");
-//        methods.add("YPGXZD");
-//        methods.add("YPLXZD");
-//        methods.add("JLDWZD");
-//        methods.add("YFZD");
-//        methods.add("YPZD");
-//        methods.add("JYKSZD");
-//        methods.add("JYYSZD");
-//        methods.add("JYFZZD");
-//        methods.add("JYFLZD");
-//        methods.add("JYFFZD");
-//        methods.add("JYBBZD");
-//        methods.add("JYXMZD");
-//        methods.add("JYZBZD");
-//        methods.add("JYXMZBZD");
-//        methods.add("JFXMGLJYXMZD");
-//        methods.add("JFZHGLJYXMZD");
-//        methods.add("JCSBZD");
-//        methods.add("JCBWFLZD");
-//        methods.add("JCBWZD");
-//        methods.add("JCBWMSZD");
+        methods.add("ZYZZZFZD");
+        methods.add("YJJFXMZD");
+        methods.add("EJJFXMZD");
+        methods.add("ZHFLXMZD");
+        methods.add("JFZHXMZD");
+        methods.add("JFZHGLMXJFXMZD");
+        methods.add("CFLXZD");
+        methods.add("YPZLZD");
+        methods.add("YPJXZD");
+        methods.add("YPGXZD");
+        methods.add("YPLXZD");
+        methods.add("JLDWZD");
+        methods.add("YFZD");
+        methods.add("YPZD");
+        methods.add("JYKSZD");
+        methods.add("JYYSZD");
+        methods.add("JYFZZD");
+        methods.add("JYFLZD");
+        methods.add("JYFFZD");
+        methods.add("JYBBZD");
+        methods.add("JYXMZD");
+        methods.add("JYZBZD");
+        methods.add("JYXMZBZD");
+        methods.add("JFXMGLJYXMZD");
+        methods.add("JFZHGLJYXMZD");
+        methods.add("JCSBZD");
+        methods.add("JCBWFLZD");
+        methods.add("JCBWZD");
+        methods.add("JCBWMSZD");
         methods.add("JFXMGLJCBWZD");
-//        methods.add("JFZHXMGLJCBWZD");
-//
-//        methods.add("TJDWXZZD");
-//        methods.add("TJRYLBZD");
-//        methods.add("TJLBZD");
-//        methods.add("TJFQZD");
-//        methods.add("TJKSZD");
-//        methods.add("TJYSZD");
-//        methods.add("TJXMZD");
-//        methods.add("TJZHXMZD");
-//        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("BRFB");
+        methods.add("JFZHXMGLJCBWZD");
+
+        methods.add("TJDWXZZD");
+        methods.add("TJRYLBZD");
+        methods.add("TJLBZD");
+        methods.add("TJFQZD");
+        methods.add("TJKSZD");
+        methods.add("TJYSZD");
+        methods.add("TJXMZD");
+        methods.add("TJZHXMZD");
+        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("BRFB");
+        methods.add("MXJFXMZD");
+        methods.add("MZJFZHMXXM");
 
         limits.add("ZDZYZ");
         limits.add("KSZD");
@@ -131,20 +136,31 @@
         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();
-//        String json = "{\"Response\":{\"ResultCode\":\"0\",\"ResultContent\":\"鏌ヨ鎴愬姛\",\"ResultData\":[{\"RowNumber\":1,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0000000002\",\"KSMC\":\"璐ㄦ帶鑲",\"KSPY\":\"ZKG\",\"FLKSDM\":\"0000000001\",\"FLKSMC\":\"鍔炲叕\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":\"\",\"KSMS\":null,\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":2,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0001\",\"KSMC\":\"鎬诲姟搴撴埧\",\"KSPY\":\"ZWKF\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":\"\",\"KSMS\":\"\",\"KSDZ\":null,\"LXDH\":\"\",\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":3,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0002\",\"KSMC\":\"瑗胯嵂搴撴埧\",\"KSPY\":\"XYKF\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":null,\"KSMS\":null,\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":4,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0003\",\"KSMC\":\"闂ㄨ瘖瑗胯嵂鎴縗",\"KSPY\":\"MZXYF\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":\"\",\"KSMS\":\"闂ㄨ瘖涓�妤艰タ鑽埧鍙栬嵂\",\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":5,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0004\",\"KSMC\":\"闂ㄨ瘖涓嵂鎴縗",\"KSPY\":\"MZZYF\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":\"\",\"KSMS\":\"闂ㄨ瘖涓�妤间腑鑽埧鍙栬嵂\",\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":6,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0005\",\"KSMC\":\"浣忛櫌鑽埧\",\"KSPY\":\"ZYYF\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":\"\",\"KSMS\":\"浣忛櫌閮ㄤ簩妤艰嵂鎴垮彇鑽痋",\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":7,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0006\",\"KSMC\":\"鍔炲叕瀹",\"KSPY\":\"BGS\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":null,\"KSMS\":null,\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":8,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0007\",\"KSMC\":\"鎬诲姟鑲",\"KSPY\":\"ZWG\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":null,\"KSMS\":null,\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":9,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0008\",\"KSMC\":\"鍖诲姟鑲",\"KSPY\":\"YWG\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":null,\"KSMS\":null,\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"},{\"RowNumber\":10,\"totalCount\":171,\"YLJGDM\":\"43899007-X\",\"YLJGMC\":\"娉惧窛鍘夸汉姘戝尰闄",\"KSDM\":\"0009\",\"KSMC\":\"闂ㄨ瘖鏀惰垂瀹",\"KSPY\":\"MZSFS\",\"FLKSDM\":\"01\",\"FLKSMC\":\"鍖荤枟\",\"KSLXDM\":\"0\",\"KSLXMC\":\"鍏敤\",\"BZNCW\":null,\"BZWCW\":null,\"KSJJ\":\"\",\"KSMS\":\"闂ㄨ瘖妤间竴妤笺�侀棬璇婃ゼ浜屾ゼ缂磋垂\",\"KSDZ\":null,\"LXDH\":null,\"ZKDM\":null,\"BQDM\":null,\"YQDM\":null,\"CJRQ\":\"2022-04-15T10:01:06.77\",\"JLZT\":\"0\",\"BZ\":\"\"}]}}";
-        util.exec1();
-//        Connection connection = util.getConnection();
-//        String tabName = "ltkj_kszd";
-//        JSONObject entries = JSONUtil.parseObj(json);
-//        util.LimitInsertData(connection,tabName,entries.getJSONObject("Response"));
+        try {
+            util.exec1();
+        } catch (IOException e) {
+            e.printStackTrace();
+            log.error("寮傚父");
+        }
     }
 
+    /**
+     * 澧炲姞榛樿鍙傛暟
+     * @param jsonObject
+     * @return
+     */
     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","");
@@ -152,61 +168,88 @@
         return jsonObject;
     }
 
-    public void exec1() {
+    public void execSync(){
+
+    }
+
+    /**
+     * 鍗曠嚎绋嬫墽琛�
+     * @throws IOException
+     */
+    public void exec1() throws IOException {
+        log.info("寮�濮嬫墽琛屽悓姝�");
         int size = 1000;
-        Connection connection = getConnection();
-        methods.forEach(method -> {
-            log.info("寮�濮嬭姹備唬鐮� ->{}",method);
+        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);
+        for (String method : methods) {
+            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;
-                object.putOpt("pagecount", size);
-                object.putOpt("page", page);
+                JSONObject input = object.getJSONObject("input");
+                input.putOpt("pagecount", size);
+                input.putOpt("page", page);
             }
-            object = addInputVal(object);
-            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);
+                } catch (SQLException e) {
+                    log.error("鍒犻櫎琛ㄥ紓甯� ->{}", e.getMessage());
+                }
                 if (isLimit) {
-                    maxPage = LimitInsertData(connection, tabName, response);
+                    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.putOpt("pagecount", size);
-                            object.putOpt("page", page);
                             object = addInputVal(object);
-                            log.info("璇锋眰鎺ュ彛 ->{},璇锋眰鍙傛暟 ->{}",BASE_API_URL,object.toString());
+                            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());
+//                            log.info("璇锋眰杩斿洖 ->{}", entries.toString());
                             response = entries.getJSONObject("Response");
                             if ("0".equals(response.getStr("ResultCode"))) {
-                                LimitInsertData(connection, tabName, response);
+                                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, resultData, jsonObject);
+                    List<JSONObject> list = JSONUtil.toList(resultData, JSONObject.class);
+                    for (JSONObject jsonObject : list) {
+                        editDataBase(connection, tabName, jsonObject,name);
+                    }
                 }
             } else {
-                log.error("{}璇锋眰澶辫触 - 璇锋眰鐘舵�佺爜涓嶄负0锛歿}", method,object.toString());
+                log.error("{}璇锋眰澶辫触 - 璇锋眰鐘舵�佺爜涓嶄负0, 璇锋眰鍙傛暟 ->{}, 璇锋眰杩斿洖 ->{}", method, object.toString(), response.toString());
             }
-        });
+        }
+        log.info("鎵ц鍚屾瀹屾瘯");
     }
 
     /**
@@ -217,7 +260,7 @@
      * @param response
      * @return
      */
-    private int LimitInsertData(Connection connection, String tabName, JSONObject response) {
+    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;
@@ -226,9 +269,9 @@
         // 鎬绘潯鏁�
         Integer totalCount = entries.getInt("totalCount");
         maxPage = (totalCount + 1000 - 1) / 1000;
-        log.info("璇锋眰杩斿洖闆嗗悎澶у皬 ->{},鎬婚〉鏁� ->{}",jsonArray.size(),maxPage);
+        log.info("璇锋眰杩斿洖鎬绘潯鏁� ->{},褰撳墠椤� ->{},鎬婚〉鏁� ->{}",totalCount,page,maxPage);
         jsonArray.forEach(obj -> {
-            editDataBase(connection, tabName, jsonArray, (JSONObject) obj);
+            editDataBase(connection, tabName, (JSONObject) obj,name);
         });
         return maxPage;
     }
@@ -238,48 +281,28 @@
      *
      * @param connection
      * @param tabName
-     * @param jsonArray
      * @param obj
      */
-    private void editDataBase(Connection connection, String tabName, JSONArray jsonArray, JSONObject obj) {
+    private void editDataBase(Connection connection, String tabName, JSONObject obj,String name) {
         try {
-            if (!tabIsExists(connection, tabName)) {
+            if (!tabIsExists(connection, tabName,name)) {
                 // 鍒涘缓琛�
                 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());
+        }
     }
-
-//    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((JSONObject) jsonArray.get(0), tabName, connection);
-//                } catch (SQLException throwables) {
-//                    throwables.printStackTrace();
-//                }
-//            }
-//        }
-//    }
 
     /**
      * 鎵цpost璇锋眰
@@ -297,11 +320,11 @@
             connection.setRequestMethod("POST");
             connection.setRequestProperty("Accept", "application/json");
 //            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
-            connection.setRequestProperty("Content-Type", "application/json;utf-8");
+            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
             connection.setDoOutput(true);
             OutputStream stream = connection.getOutputStream();
             stream.write(params.getBytes(StandardCharsets.UTF_8));
-            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),StandardCharsets.UTF_8));
             StringBuilder builder = new StringBuilder();
             String str;
             while ((str = reader.readLine()) != null) {
@@ -318,62 +341,9 @@
         return null;
     }
 
-    /**
-     * 鎵цhttp璇锋眰
-     *
-     * @return
-     */
-//    private JSONObject execRequest() {
-//        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");
-//            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));
-//            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;
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        } finally {
-//            if (connection != null) {
-//                connection.disconnect();
-//            }
-//        }
-//        return null;
-//    }
-
-    private Connection getConnection() {
+    private Connection getConnection(String user,String password,String url) {
         try {
-            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";
+            log.info("鏁版嵁搴撲俊鎭� ->{},{},{}",user,password,url);
             return DriverManager.getConnection(url, user, password);
         } catch (Exception throwables) {
             log.error("鑾峰彇sql杩炴帴澶辫触");
@@ -390,10 +360,10 @@
      * @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());
         ResultSet resultSet = statement.executeQuery();
@@ -414,15 +384,17 @@
 //        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) {
-                String sql = "alter table " + tabName + " add column " + colum + " text null";
-//                    log.info("淇敼瀛楁 ->{}",sql.toString());
+                String sql = "alter table " + tabName + " add column " + colum + " VARCHAR(200) null";
+//                log.info("淇敼瀛楁 ->{}",sql.toString());
                 Statement statement = connection.createStatement();
                 statement.executeUpdate(sql);
                 statement.close();
@@ -443,37 +415,42 @@
      */
     private void insertData(String tabName, Connection connection, JSONObject jsonObject) throws SQLException {
         // 鎻掑叆鏁版嵁鍓� 鍏堟煡璇㈡暟鎹槸鍚﹀瓨鍦�
-        StringBuilder selectSqlBuilder = new StringBuilder();
+//        StringBuilder selectSqlBuilder = new StringBuilder();
         StringBuilder insertSqlBuilder = new StringBuilder();
         StringBuilder valueBuilder = new StringBuilder();
         insertSqlBuilder.append("insert into ").append(tabName).append(" (");
-        selectSqlBuilder.append("select count(1) as count from ").append(tabName).append(" where ");
+//        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 ");
+            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+", "").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, ");
         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());
+//        selectSqlBuilder.delete(selectSqlBuilder.length() - 5, selectSqlBuilder.length());
         insertSqlBuilder.append(") values (").append(valueBuilder).append(")");
 //        log.info("鎻掑叆鍓嶆煡璇� sql ->{}", selectSqlBuilder.toString());
-        Statement statement = connection.prepareStatement(selectSqlBuilder.toString());
-        ResultSet resultSet = statement.executeQuery(selectSqlBuilder.toString());
-        resultSet.next();
-        String string = resultSet.getString("count");
-        statement.close();
+//        Statement statement = connection.prepareStatement(selectSqlBuilder.toString());
+//        ResultSet resultSet = statement.executeQuery(selectSqlBuilder.toString());
+//        resultSet.next();
+//        String string = resultSet.getString("count");
+//        statement.close();
         // 濡傛灉涓嶄负0 鍒欒繖鏉℃暟鎹瓨鍦� 涓嶈繘琛屾彃鍏�
-        if (!"0".equals(string)) {
+//        if (!"0".equals(string)) {
 //            log.info("鏁版嵁瀛樺湪涓嶉渶瑕佹彃鍏� {}", jsonObject);
-            return;
-        }
+//            return;
+//        }
         // 鎻掑叆鏁版嵁
-        statement = connection.createStatement();
-//        log.info("鎻掑叆鏁版嵁 sql-> {}", insertSqlBuilder.toString());
+        Statement statement = connection.createStatement();
+        log.info("鎻掑叆鏁版嵁 sql-> {}", insertSqlBuilder.toString());
         statement.execute(insertSqlBuilder.toString());
         statement.close();
     }
@@ -508,13 +485,27 @@
     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(" text null,");
+            String key = entry.getKey().trim().toLowerCase();
+            sql.append(key).append(" VARCHAR(200) null,");
         }
-        sql.append("insert_time").append(" text 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());
     }
+
+    /**
+     * 鍒犻櫎琛�
+     * @param tabName
+     * @param connection
+     * @throws SQLException
+     */
+    private void dropTable(String tabName,Connection connection) throws SQLException {
+        String sql = "DROP TABLE IF EXISTS " + tabName;
+        Statement statement = connection.createStatement();
+        statement.executeUpdate(sql);
+        statement.close();
+    }
 }

--
Gitblit v1.8.0