package com.ltkj.web.controller.his;
|
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.json.JSONArray;
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.ltkj.common.core.domain.AjaxResult;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.nio.charset.StandardCharsets;
|
import java.sql.*;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
import java.util.Date;
|
|
/**
|
* @Company: 西安路泰科技有限公司
|
* @Author: zhaowenxuan
|
* @Date: 2024/5/31 14:38
|
*/
|
@Slf4j
|
@Component
|
public class HisApiGetMethodService {
|
// 数据库配置文件路径
|
private static final String CONFIG_PATH = "/Users/chacca/开发相关/代码/ltkj_peis/ltkj-admin/src/main/resources/config.properties";
|
// 数据库名
|
private static String DB_NAME = "";
|
@Autowired
|
private HisApiMethodService controller;
|
|
static {
|
try {
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
} catch (ClassNotFoundException e) {
|
}
|
}
|
|
|
/**
|
* 获取数据
|
*
|
* @param type 接口代码
|
* @param params 请求参数
|
* @return
|
*/
|
public AjaxResult getHISData(String type, Map<String, Object> params) {
|
// Getoutaccountrecord 获取门诊结算记录
|
// Getoutpatientcostinfo 获取门诊患者费用清单信息
|
// Getlaburgentinfo 获取危急值信息
|
// Getlabgermdetailinfo 获取微生物药敏信息
|
// Getlabgermrepinfo 获取微生物报告记录信息
|
// Getlabreportinfo 获取检验报告记录信息
|
// Getlabapplyinfo 获取检验申请信息
|
// Getexamurgentinfo 获取检查危急值信息
|
// Getexamreportinfo 获取检查报告信息
|
// Getexamapplyinfo 获取检查申请信息
|
AjaxResult result = null;
|
// TODO 创建表 弄成数据库动态获取 接口地址、代码、是否分页
|
// TODO 从2.4.2获取到数据 根据his_registration_id删除旧数据在插入新数据 下层业务均一样根据返回的JYBGID删除旧数据在插入新数据
|
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;
|
case "Getlabdetailinfo":
|
result = controller.Getlabdetailinfo(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")) {
|
AjaxResult ajaxResult = AjaxResult.success();
|
if ((boolean) result.get("limit")) {
|
ajaxResult = saveArray(json, type);
|
JSONArray resultData = response.getJSONArray("ResultData");
|
List<Map<String, String>> list = new ArrayList<>();
|
for (Object resultDatum : resultData) {
|
JSONObject object = (JSONObject) resultDatum;
|
Map<String, String> map = new HashMap<>();
|
for (String key : object.keySet()) {
|
String defaultVal = object.get(key).toString();
|
String val = defaultVal.trim().replaceAll("\\s+", "");
|
map.put(key, val);
|
}
|
list.add(map);
|
}
|
ajaxResult.put("data", list);
|
} else {
|
// TODO 如果不带分页参数 返回的数据都是对象 则在这里通过result.get("limit")进行判断 处理对象或集合并返回
|
// TODO 不确定 ResultData 直接是对象或是集合中一个对象的形式
|
// 直接是对象的格式获取
|
// ajaxResult = save(response.getJSONObject("ResultData"),type);
|
// JSONObject resultData = response.getJSONObject("ResultData");
|
// HashMap<String, Object> map = new HashMap<>();
|
// for (String key : resultData.keySet()) {
|
// map.put(key, resultData.get(key));
|
// }
|
// ajaxResult.put("data",map);
|
// 以集合返回
|
JSONObject data = (JSONObject) response.getJSONArray("ResultData").get(0);
|
ajaxResult = save(data, type);
|
HashMap<String, String> map1 = new HashMap<>();
|
for (String key : data.keySet()) {
|
String defaultVal = data.get(key).toString();
|
String val = defaultVal.trim().replaceAll("\\s+", "");
|
map1.put(key, val);
|
}
|
ArrayList<Map<String, String>> maps = new ArrayList<>();
|
maps.add(map1);
|
ajaxResult.put("data", maps);
|
}
|
return ajaxResult;
|
} else {
|
return AjaxResult.error().put("result", JSONUtil.parseObj(json));
|
}
|
}
|
|
/**
|
* 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()) {
|
String key = entry.getKey().trim().toLowerCase();
|
responseColums.add(key);
|
}
|
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();
|
StringBuilder selectSqlBuilder = new StringBuilder();
|
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 defaultVal = entry.getValue().toString();
|
String key = entry.getKey().trim().toLowerCase();
|
if (StrUtil.isBlank(defaultVal) || defaultVal.equals("null"))
|
continue;
|
String val = defaultVal.trim().replaceAll("\\s+", "");
|
insertSqlBuilder.append(key).append(", ");
|
valueBuilder.append("'").append(val).append("', ");
|
selectSqlBuilder.append(key).append(" = '").append(val).append("' and ");
|
}
|
selectSqlBuilder.delete(selectSqlBuilder.length() - 5, selectSqlBuilder.length());
|
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)) {
|
// log.info("数据存在不需要插入 {}", jsonObject);
|
return;
|
}
|
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 = 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()) {
|
String key = entry.getKey().trim().toLowerCase();
|
sql.append(key).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);
|
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
Properties props = new Properties();
|
props.load(reader);
|
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");
|
log.info("数据库配置信息 ->数据库名-{},user-{},password-{},url-{}", name, user, password, url);
|
Connection connection = DriverManager.getConnection(url, user, password);
|
DB_NAME = name;
|
return connection;
|
} catch (SQLException | IOException throwables) {
|
throwables.printStackTrace();
|
}
|
return null;
|
}
|
}
|