赵文轩
2024-06-05 d2149a2de5ce0152cc674f1b40bb40173c0c6c0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package com.ltkj.web.controller.his;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ltkj.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zhaowenxuan
 * @Date: 2024/5/31 14:38
 */
@Component
public class HisApiGetMethodService {
    // 数据库配置文件路径
    private static final String CONFIG_PATH = "D:\\ltkjprojectconf\\config.properties";
    // 数据库名
    private static String DB_NAME = "";
    @Autowired
    private HisApiMethodService controller;
 
 
    /**
     * 获取
     *
     * @return
     */
    public AjaxResult getHISData(String type, Map<String, Object> params) {
        // Getoutaccountrecord 获取门诊结算记录
        // Getoutpatientcostinfo 获取门诊患者费用清单信息
        // Getlaburgentinfo 获取危急值信息
        // Getlabgermdetailinfo 获取微生物药敏信息
        // Getlabgermrepinfo 获取微生物报告记录信息
        // Getlabreportinfo 获取检验报告记录信息
        // Getlabapplyinfo 获取检验申请信息
        // Getexamurgentinfo 获取检查危急值信息
        // Getexamreportinfo 获取检查报告信息
        // Getexamapplyinfo 获取检查申请信息
        AjaxResult result = null;
        switch (type) {
            case "Getoutaccountrecord":
                result = controller.Getoutaccountrecord(params);
                result.put("limit",true);
                break;
            case "Getoutpatientcostinfo":
                result = controller.Getoutpatientcostinfo(params);
                result.put("limit",true);
                break;
            case "Getlaburgentinfo":
                result = controller.Getlaburgentinfo(params);
                result.put("limit",false);
                break;
            case "Getlabgermdetailinfo":
                result = controller.Getlabgermdetailinfo(params);
                result.put("limit",false);
                break;
            case "Getlabgermrepinfo":
                result = controller.Getlabgermrepinfo(params);
                result.put("limit",false);
                break;
            case "Getlabreportinfo":
                result = controller.Getlabreportinfo(params);
                result.put("limit",true);
                break;
            case "Getlabapplyinfo":
                result = controller.Getlabapplyinfo(params);
                result.put("limit",false);
                break;
            case "Getexamurgentinfo":
                result = controller.Getexamurgentinfo(params);
                result.put("limit",true);
                break;
            case "Getexamreportinfo":
                result = controller.Getexamreportinfo(params);
                result.put("limit",true);
                break;
            case "Getexamapplyinfo":
                result = controller.Getexamapplyinfo(params);
                result.put("limit",false);
                break;
        }
        String json = result.get("data").toString();
        JSONObject response = JSONUtil.parseObj(json).getJSONObject("Response");
        if (response.getStr("ResultCode").toString().equals("0")) {
            // TODO 如果不带分页参数 返回的数据都是对象 则在这里通过result.get("limit")进行判断 处理对象或集合并返回
            JSONArray resultData = response.getJSONArray("ResultData");
            List<Map<String, Object>> list = new ArrayList<>();
            for (Object resultDatum : resultData) {
                JSONObject object = (JSONObject) resultDatum;
                Map<String, Object> map = new HashMap<>();
                for (String key : object.keySet()) {
                    map.put(key, object.get(key));
                }
                list.add(map);
            }
            AjaxResult ajaxResult = null;
            if ((boolean)result.get("limit")){
                ajaxResult = saveArray(json, type);
            }else {
                ajaxResult = save(response.getJSONObject("ResultData"),type);
            }
            ajaxResult.put("data", list);
            return ajaxResult;
        } else {
            return AjaxResult.error();
        }
    }
 
    /**
     * 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");
        Connection connection = getConnection();
//            for (Object resultDatum : resultData) {
//                JSONObject object = (JSONObject) resultDatum;
        Boolean isExists = tabIsExists(connection, tabName);
        if (null == isExists)
            return AjaxResult.error();
        if (!isExists) {
            try {
                creatTable(object, tabName, connection);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                return AjaxResult.error();
            }
        }
        //插入数据
        try {
            operationTable(object, tabName, connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return AjaxResult.error();
        }
//            }
        try {
            if (connection != null)
                connection.close();
        } catch (SQLException throwables) {
        }
//        }
        return AjaxResult.success();
    }
 
    /**
     * ResultData为集合
     * 根据his接口返回值 插入数据
     * 如果没有表则创建表 接口增加了字段则为表增加字段
     *
     * @param code 接口代码
     * @return 执行是否成功
     */
    public AjaxResult saveArray(String json, 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")) {
            JSONArray resultData = response.getJSONArray("ResultData");
            Connection connection = getConnection();
            for (Object resultDatum : resultData) {
                JSONObject object = (JSONObject) resultDatum;
                Boolean isExists = tabIsExists(connection, tabName);
                if (null == isExists)
                    return AjaxResult.error();
                if (!isExists) {
                    try {
                        creatTable(object, tabName, connection);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                        return AjaxResult.error();
                    }
                }
                //插入数据
                try {
                    operationTable(object, tabName, connection);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                    return AjaxResult.error();
                }
            }
            try {
                if (connection != null)
                    connection.close();
            } catch (SQLException throwables) {
            }
        }
        return AjaxResult.success();
    }
 
    /**
     * 操作表
     *
     * @param tabName
     * @param connection
     * @throws SQLException
     */
    private void operationTable(JSONObject jsonObject, String tabName, Connection connection) throws SQLException {
        List<String> columns = getColumns(tabName, connection);
        ArrayList<String> responseColums = new ArrayList<>();
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            responseColums.add(entry.getKey());
        }
        responseColums.removeAll(columns);
        if (!responseColums.isEmpty()) {
            for (String colum : responseColums) {
                String sql = "alter table " + tabName + " add column " + colum + " VARCHAR(200) null";
                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()) {
            insertSqlBuilder.append(entry.getKey()).append(", ");
            valueBuilder.append("'").append(entry.getValue()).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();
        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 data
     * @param tabName
     * @param connection
     * @throws SQLException
     */
    private void creatTable(JSONObject data, String tabName, Connection connection) throws SQLException {
        StringBuilder sql = new StringBuilder("CREATE TABLE " + tabName + " (");
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            sql.append(entry.getKey()).append(" VARCHAR(200) null,");
        }
        sql.append("insert_time").append(" VARCHAR(200) null,");
        sql = new StringBuilder(sql.substring(0, sql.length() - 1));
        sql.append(");");
        Statement statement = connection.createStatement();
        statement.execute(sql.toString());
    }
 
    /**
     * 判断表是否存在
     *
     * @param connection
     * @param tableName
     * @return
     */
    private Boolean tabIsExists(Connection connection, String tableName) {
        String tabSql = "SELECT table_name FROM information_schema.tables WHERE table_schema = ? AND table_name = ?";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(tabSql);
            statement.setString(1, DB_NAME);
            statement.setString(2, tableName);
            ResultSet resultSet = statement.executeQuery();
            boolean next = resultSet.next();
            statement.close();
            return next;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
 
    /**
     * 获取数据库连接
     *
     * @return
     */
    private Connection getConnection() {
        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");
            Connection connection = DriverManager.getConnection(url, user, password);
            DB_NAME = name;
            return connection;
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}