From d3e9ae0b5f000e52bd54962644be8e9feec0df4a Mon Sep 17 00:00:00 2001
From: zhaowenxuan <chacca165@163.com>
Date: 星期四, 19 十二月 2024 18:04:42 +0800
Subject: [PATCH] 20241219

---
 src/main/java/com/example/service/shanxiqin/xian/MeiJiHisService.java |    2 
 src/main/java/com/example/utils/DictionaryUtilCommon.java             |  397 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/main/java/com/example/scheudleds/DictSync.java                    |    6 
 config.properties                                                     |   47 +++++
 4 files changed, 451 insertions(+), 1 deletions(-)

diff --git a/config.properties b/config.properties
new file mode 100644
index 0000000..02b0527
--- /dev/null
+++ b/config.properties
@@ -0,0 +1,47 @@
+
+#101.42.27.146
+ip = 192.168.1.99
+prot = 3306
+name = ltkjpeis10weinan
+#name = ltkjpeis10weinan1212
+username = root
+password =Root_ltkj123
+
+
+
+his_api_url=http://192.168.0.123
+his_api_port=11004
+
+hisenabled = true
+hisip = localhost
+hisprot = 1433
+hisname = Testlg
+hisusername = sa
+hispassword =admin123
+hisapiappend = /OAPI/
+
+
+pacsenabled = false
+pacsip = 192.168.1.99
+pacsprot = 1521
+pacsname = ORCL
+pacsusername = ltemr
+pacspassword = ltkjemr
+
+
+lisenabled = true
+lisip =  101.42.27.146
+lisprot = 3306
+lisname = ltkjpeis10weinan
+lisusername = root
+lispassword =Root_ltkj123
+
+#101.42.27.146
+redisIp = 192.168.1.99
+redisIpDatabase =0
+redisProt = 6379
+redisPassword = 654321
+
+jmurl = http://localhost:5011
+hosp_service = ShanXi_Qin_XiAn_MeiJi
+sjpt_open = false
\ No newline at end of file
diff --git a/src/main/java/com/example/scheudleds/DictSync.java b/src/main/java/com/example/scheudleds/DictSync.java
index 5de19a9..a66ba58 100644
--- a/src/main/java/com/example/scheudleds/DictSync.java
+++ b/src/main/java/com/example/scheudleds/DictSync.java
@@ -1,5 +1,7 @@
 package com.example.scheudleds;
 
+import cn.hutool.core.util.StrUtil;
+import com.example.config.ConfigValue;
 import com.example.utils.DictionaryUtil;
 import com.example.utils.DictionaryUtilNew;
 import lombok.extern.slf4j.Slf4j;
@@ -21,12 +23,16 @@
     private DictionaryUtil dictionaryUtil;
     @Autowired
     private DictionaryUtilNew dictionaryUtilNew;
+    @Autowired
+    private ConfigValue configValue;
 
     /**
      * 瀛楀吀鍚屾
      */
     @Scheduled(cron = "0 0 22 * * ? ")
     public void execDict(){
+        String open = configValue.getConfigValue("sjpt_open");
+        if (StrUtil.isNotBlank(open) && open.equals("false")) return;
         FileWriter fileWriter = null;
         try {
             fileWriter = new FileWriter("src/main/resources/log.log");
diff --git a/src/main/java/com/example/service/shanxiqin/xian/MeiJiHisService.java b/src/main/java/com/example/service/shanxiqin/xian/MeiJiHisService.java
index edd40f9..44176ac 100644
--- a/src/main/java/com/example/service/shanxiqin/xian/MeiJiHisService.java
+++ b/src/main/java/com/example/service/shanxiqin/xian/MeiJiHisService.java
@@ -38,7 +38,7 @@
     public MeiJiHisService(ApplicationContext applicationContext, ConfigValue configValue) {
         this.applicationContext = applicationContext;
         this.configValue = configValue;
-        HIS_URL = configValue.getConfigValue("his_api_url") + ":" + configValue.getConfigValue("his_api_port") +"/OAPI/";
+        HIS_URL = configValue.getConfigValue("his_api_url") + ":" + configValue.getConfigValue("his_api_port") + configValue.getConfigValue("hisapiappend");
     }
 
     @Autowired
diff --git a/src/main/java/com/example/utils/DictionaryUtilCommon.java b/src/main/java/com/example/utils/DictionaryUtilCommon.java
new file mode 100644
index 0000000..a2c156b
--- /dev/null
+++ b/src/main/java/com/example/utils/DictionaryUtilCommon.java
@@ -0,0 +1,397 @@
+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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.example.config.ConfigValue;
+import com.example.domain.HisSyncDict;
+import com.example.service.HisSyncDictService;
+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 java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.sql.*;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.*;
+
+/**
+ * 瀛楀吀鍏叡瑙f瀽绫�
+ * @Company: 瑗垮畨璺嘲绉戞妧鏈夐檺鍏徃
+ * @Author: zhaowenxuan
+ * @Date: 2024/12/19 16:41
+ */
+@Slf4j
+@Component
+public class DictionaryUtilCommon {
+
+    @Autowired
+    private HisSyncDictService hisSyncDictService;
+    @Autowired
+    private ConfigValue configValue;
+
+    private static String CONFIG_PATH;
+    private static String BASE_API_URL = "";
+    private static final int SIZE = 1000;
+    private static String USER = "";
+    private static String PASSWORD = "";
+    private static String URL = "";
+    private static String NAME = "";
+    @Value("${config.path}")
+    public void setConfigPath(String configPath) {
+        CONFIG_PATH = configPath;
+        NAME = configValue.getConfigValue("name");
+        URL = "jdbc:mysql://" + configValue.getConfigValue("ip") + ":" + configValue.getConfigValue("prot") + "/" + NAME +
+                "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        USER = configValue.getConfigValue("username");
+        PASSWORD = configValue.getConfigValue("password");
+        String apiUrl = configValue.getConfigValue("his_api_url");
+        String apiPort = configValue.getConfigValue("his_api_port");
+        BASE_API_URL = apiUrl+":"+apiPort+configValue.getConfigValue("hisapiappend");
+    }
+
+
+    /**
+     * 鏆傛椂涓嶅仛
+     * 1.90 闂ㄨ瘖鏄庣粏璁¤垂鍙婄粍鍚堣璐归」鐩� MZJFZHMXXM
+     *
+     */
+
+// 鍒嗛〉姣忔1000鏉�
+    static {
+        try {
+            Class.forName("com.mysql.cj.jdbc.Driver");
+        } catch (Exception e) {
+            System.out.println("闈欐�佷唬鐮佸潡寮傚父 ->"+e.getMessage());
+        }
+    }
+
+    public static void main(String[] args) {
+        DictionaryUtilNew util = new DictionaryUtilNew();
+        util.exec1();
+    }
+
+    /**
+     * 杩涜鍚屾
+     */
+    public void exec1(){
+        log.info("寮�濮嬫墽琛屽悓姝�");
+        Connection connection = getConnection();
+        if (connection == null)
+            return;
+        List<HisSyncDict> hisSyncDicts = hisSyncDictService.list();
+        for (HisSyncDict hisSyncDict : hisSyncDicts) {
+            extracted(NAME, connection, hisSyncDict);
+        }
+        try {
+            connection.close();
+        } catch (SQLException ignored) { }
+        log.info("鎵ц鍚屾瀹屾瘯");
+    }
+
+    private void extracted(String name, Connection connection, HisSyncDict hisSyncDict) {
+        String method = hisSyncDict.getDictName();
+        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);
+        if (hisSyncDict.getIsLimit() == 1) {
+            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());
+        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, name, page);
+                log.info("璁$畻椤电爜涓� ->{}", maxPage);
+                if (maxPage > 1) {
+                    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());
+                        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());
+                        }
+                    }
+                }
+            } else {
+                JSONArray resultData = response.getJSONArray("ResultData");
+                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());
+        }
+    }
+
+    /**
+     * 閽堝浜庡垎椤垫帴鍙� 鍒涘缓琛ㄣ�佹彃鍏ユ暟鎹�
+     *
+     * @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);
+            }
+        } catch (SQLException throwables) {
+            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());
+        }
+    }
+
+    /**
+     * 鎵цpost璇锋眰
+     *
+     * @param params 璇锋眰鍙傛暟 渚嬪锛� param1=val1&param2=val2
+     * @return 璇锋眰杩斿洖鐨刯son杞崲鍚庣殑JSONObject瀵硅薄
+     */
+    private JSONObject execRequest(String params) {
+        URL url = null;
+        HttpURLConnection connection = null;
+        try {
+            url = new URL(BASE_API_URL);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("POST");
+            connection.setRequestProperty("Accept", "application/json");
+            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(),StandardCharsets.UTF_8));
+            StringBuilder builder = new StringBuilder();
+            String str;
+            while ((str = reader.readLine()) != null) {
+                builder.append(str);
+            }
+            return JSONUtil.parseObj(builder.toString());
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+        return null;
+    }
+
+    public Connection getConnection() {
+        try {
+            log.info("鏁版嵁搴撲俊鎭� ->{},{},{}",USER,PASSWORD,URL);
+            return DriverManager.getConnection(URL,USER,PASSWORD);
+        } catch (Exception throwables) {
+            log.error("鑾峰彇sql杩炴帴澶辫触");
+            throwables.printStackTrace();
+        }
+        return null;
+    }
+
+    /**
+     * 琛ㄦ槸鍚﹀瓨鍦�
+     *
+     * @param connection
+     * @param tableName
+     * @return
+     * @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, name);
+        statement.setString(2, tableName);
+//        log.info("鍒ゆ柇琛ㄦ槸鍚﹀瓨鍦� sql-> {}", statement.toString());
+        ResultSet resultSet = statement.executeQuery();
+        boolean next = resultSet.next();
+        statement.close();
+        return next;
+    }
+
+    /**
+     * 鎿嶄綔琛�
+     *
+     * @param tabName
+     * @param connection
+     * @throws SQLException
+     */
+    private void operationTable(JSONObject jsonObject, String tabName, Connection connection) throws SQLException {
+        List<String> columns = getColumns(tabName, connection);
+//        log.info("褰撳墠琛ㄥ瓧娈典负 ->{}", columns);
+        ArrayList<String> responseColums = new ArrayList<>();
+        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
+            String key = entry.getKey().trim().toLowerCase();
+            responseColums.add(key);
+        }
+//        log.info("璇锋眰杩斿洖鐨勫瓧娈典负 ->{}", responseColums);
+        responseColums.removeAll(columns);
+//        log.info("闇�瑕佸鍔犵殑瀛楁 ->{}",responseColums);
+        if (!responseColums.isEmpty()) {
+            // 闇�瑕佸鍔犲瓧娈� 骞舵彃鍏ユ暟鎹�
+            for (String colum : responseColums) {
+                String sql = "alter table " + tabName + " add column " + colum + " VARCHAR(200) null";
+//                log.info("淇敼瀛楁 ->{}",sql.toString());
+                Statement statement = connection.createStatement();
+                statement.executeUpdate(sql);
+                statement.close();
+            }
+            insertData(tabName, connection, jsonObject);
+        } else {
+            insertData(tabName, connection, jsonObject);
+        }
+    }
+
+    /**
+     * 鎻掑叆鏁版嵁
+     *
+     * @param tabName
+     * @param connection
+     * @param jsonObject
+     * @throws SQLException
+     */
+    private void insertData(String tabName, Connection connection, JSONObject jsonObject) throws SQLException {
+        // 鎻掑叆鏁版嵁鍓� 鍏堟煡璇㈡暟鎹槸鍚﹀瓨鍦�
+        StringBuilder insertSqlBuilder = new StringBuilder();
+        StringBuilder valueBuilder = new StringBuilder();
+        insertSqlBuilder.append("insert into ").append(tabName).append(" (");
+        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+", "").replace("\\","");
+            insertSqlBuilder.append(key).append(", ");
+            valueBuilder.append("'").append(val.replaceAll("'","鈥�")).append("', ");
+        }
+        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(")");
+        // 鎻掑叆鏁版嵁
+        Statement statement = connection.createStatement();
+        log.info("鎻掑叆鏁版嵁 sql-> {}", insertSqlBuilder.toString());
+        statement.execute(insertSqlBuilder.toString());
+        statement.close();
+    }
+
+    /**
+     * 鑾峰彇琛ㄧ殑鍒�
+     *
+     * @param tabName
+     * @param connection
+     * @return
+     * @throws SQLException
+     */
+    private List<String> getColumns(String tabName, Connection connection) throws SQLException {
+        DatabaseMetaData metaData = connection.getMetaData();
+        ResultSet columns = metaData.getColumns(null, null, tabName, null);
+        ArrayList<String> tabColumns = new ArrayList<>();
+        while (columns.next()) {
+            String columnName = columns.getString("column_name");
+            tabColumns.add(columnName);
+        }
+        return tabColumns;
+    }
+
+    /**
+     * 鍒涘缓琛�
+     *
+     * @param resultDataIndex1 杩斿洖鏁版嵁涓殑绗竴涓弬鏁�
+     * @param tabName
+     * @param connection
+     * @throws SQLException
+     */
+    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()) {
+            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());
+        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