From d6adfb1b4442e5c4f53fd10b8b89caff012a5ea3 Mon Sep 17 00:00:00 2001
From: linjj <850658129@qq.com>
Date: Fri, 10 Oct 2025 16:13:45 +0800
Subject: [PATCH] =?UTF-8?q?=E6=BD=AE=E5=B7=9E=E4=BA=BA=E5=8C=BB=E6=82=A3?=
=?UTF-8?q?=E8=80=85=E5=9F=BA=E7=A1=80=E4=BF=A1=E6=81=AF=E5=90=8C=E6=AD=A5?=
=?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=88=9D=E5=A7=8B=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 38 +++++
dependency-reduced-pom.xml | 62 ++++++++
pom.xml | 109 +++++++++++++
src/main/java/com/chaozhou/ArchiveSync.java | 167 ++++++++++++++++++++
src/main/java/com/chaozhou/Main.java | 7 +
src/main/resources/application.properties | 11 ++
src/main/resources/logback.xml | 13 ++
7 files changed, 407 insertions(+)
create mode 100644 .gitignore
create mode 100644 dependency-reduced-pom.xml
create mode 100644 pom.xml
create mode 100644 src/main/java/com/chaozhou/ArchiveSync.java
create mode 100644 src/main/java/com/chaozhou/Main.java
create mode 100644 src/main/resources/application.properties
create mode 100644 src/main/resources/logback.xml
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5ff6309
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml
new file mode 100644
index 0000000..4e0024e
--- /dev/null
+++ b/dependency-reduced-pom.xml
@@ -0,0 +1,62 @@
+
+
+ 4.0.0
+ com.chaozhou
+ emr_sync_czry
+ 1.0-SNAPSHOT
+
+
+
+ maven-compiler-plugin
+ 3.8.1
+
+ 8
+ 8
+
+
+
+ maven-shade-plugin
+ 3.4.1
+
+
+ package
+
+ shade
+
+
+
+
+
+ com.chaozhou.ArchiveSync
+
+
+
+
+ *:*
+
+ META-INF/*.SF
+ META-INF/*.DSA
+ META-INF/*.RSA
+
+
+
+
+
+
+
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.24
+ provided
+
+
+
+ UTF-8
+ 8
+ 8
+
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..4f6f0db
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,109 @@
+
+
+ 4.0.0
+
+ com.chaozhou
+ emr_sync_czry
+ 1.0-SNAPSHOT
+ jar
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+
+
+
+
+ com.oracle.database.jdbc
+ ojdbc8
+ 19.3.0.0
+
+
+
+
+ com.oracle.database.nls
+ orai18n
+ 21.3.0.0
+
+
+
+ com.microsoft.sqlserver
+ mssql-jdbc
+ 11.2.1.jre8
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.24
+ provided
+
+
+
+
+ ch.qos.logback
+ logback-classic
+ 1.2.11
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+ 8
+ 8
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-shade-plugin
+ 3.4.1
+
+
+ package
+
+ shade
+
+
+
+
+
+
+
+ com.chaozhou.ArchiveSync
+
+
+
+
+
+ *:*
+
+ META-INF/*.SF
+ META-INF/*.DSA
+ META-INF/*.RSA
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/chaozhou/ArchiveSync.java b/src/main/java/com/chaozhou/ArchiveSync.java
new file mode 100644
index 0000000..8353a5a
--- /dev/null
+++ b/src/main/java/com/chaozhou/ArchiveSync.java
@@ -0,0 +1,167 @@
+package com.chaozhou;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.*;
+import java.time.LocalDateTime;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * @ClassName ArchiveSync
+ * @Description
+ * @Author linjj
+ * @Date 2025/9/26 16:17
+ * @Version 1.0
+ */
+@Slf4j
+public class ArchiveSync {
+
+
+ /* 读取当天全部病例 */
+ private static final String SQL_ORA_TODAY =
+ "SELECT ID号,住院号,住院次数,姓名,性别,入院时间,入院科室编码," +
+ "主管医生工号,年龄,身份证号,出院日期,当前科室编码 " +
+ "FROM PORTAL_HIS.V_WZHGD_HZJCXX h " +
+ "WHERE 出院日期 >= TRUNC(SYSDATE) " + // 今天 00:00:00
+ "AND 出院日期 < TRUNC(SYSDATE) + 1"; // 明天 00:00:00(不含)
+
+
+// /* 读取 9 月 25 日到今天的全部病例 */
+// private static final String SQL_ORA_TODAY =
+// "SELECT ID号,住院号,住院次数,姓名,性别,入院时间,入院科室编码," +
+// " 主管医生工号,年龄,身份证号,出院日期,当前科室编码 " +
+// "FROM PORTAL_HIS.V_WZHGD_HZJCXX h " +
+// "WHERE 出院日期 >= TO_DATE('2025-09-25', 'YYYY-MM-DD') " +
+// "AND 出院日期 <= TRUNC(SYSDATE)";
+
+
+
+
+
+ /* SQL Server:当天已归档主键 */
+ private static final String SQL_MSS_TODAY_KEYS =
+ "SELECT inp_no + '|' + CAST(visit_id AS VARCHAR) AS key1 " +
+ "FROM archive_master " +
+ "WHERE CONVERT(date, discharge_date_time) = CONVERT(date, GETDATE())";
+
+
+// /* SQL Server:9 月 25 日到今天已归档主键 */
+// private static final String SQL_MSS_TODAY_KEYS =
+// "SELECT inp_no + '|' + CAST(visit_id AS VARCHAR) AS key1 " +
+// "FROM archive_master " +
+// "WHERE CONVERT(date, discharge_date_time) >= '2025-09-25' " +
+// "AND CONVERT(date, discharge_date_time) <= CONVERT(date, GETDATE())";
+
+
+
+
+ /* 写入 SQL Server */
+ private static final String SQL_MSS_INSERT =
+ "INSERT INTO archive_master " +
+ "(id, patient_id, inp_no, visit_id, name, sex, dept_admission_to, " +
+ " admission_date_time, discharge_date_time, DOCTOR_IN_CHARGE, SubAssort, ID_NO, " +
+ " dept_name,id_card,ArchiveState,C1) " +
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, '64','ljj')";
+
+ /* 当前最大 id */
+ private static final String SQL_MAX_ID = "SELECT ISNULL(MAX(id), 0) FROM archive_master";
+
+
+ public static void main(String[] args) throws Exception {
+
+
+
+
+ Properties cfg = new Properties();
+ try (InputStream in = ArchiveSync.class.getResourceAsStream("/application.properties")) {
+ cfg.load(in);
+ }
+
+ while (true) {
+ try (Connection ora = getConn(cfg, "oracle");
+ Connection mss = getConn(cfg, "sqlserver");
+ PreparedStatement psOra = ora.prepareStatement(SQL_ORA_TODAY);
+ PreparedStatement psKeys = mss.prepareStatement(SQL_MSS_TODAY_KEYS);
+ PreparedStatement psMaxId = mss.prepareStatement(SQL_MAX_ID);
+ PreparedStatement psIns = mss.prepareStatement(SQL_MSS_INSERT)) {
+
+
+ /*取 SQL Server 当天已归档主键 */
+ Set archivedToday = new HashSet<>();
+ try (ResultSet rs = psKeys.executeQuery()) {
+ while (rs.next()) archivedToday.add(rs.getString(1));
+ }
+ log.info("SQL Server 当天已存在 {} 条", archivedToday.size());
+
+ /* 获取当前最大 id */
+ ResultSet rsMax = psMaxId.executeQuery();
+ rsMax.next();
+ long baseId = rsMax.getLong(1);
+ rsMax.close();
+
+
+ /*查询 Oracle 缺失数据 */
+ ResultSet rs = psOra.executeQuery();
+ int total = 0, insert = 0;
+ while (rs.next()) {
+ total++;
+ String key = rs.getString("住院号") + "|" + rs.getString("住院次数");
+ if (archivedToday.contains(key)) continue; // 已归档,跳过
+ psIns.setLong(1, baseId + (++insert)); // id
+ psIns.setString(2, rs.getString("ID号")); // patient_id
+ psIns.setString(3, rs.getString("住院号")); // inp_no
+ psIns.setString(4, rs.getString("住院次数")); // visit_id
+ psIns.setNString(5, rs.getString("姓名")); // name
+ // 取出 Oracle 值
+ String sexOra = rs.getString("性别");
+ // 字典转换
+ String sexMss;
+ if (sexOra == null || sexOra.trim().isEmpty()) {
+ sexMss = "不详";
+ } else if ("1".equals(sexOra)) {
+ sexMss = "男";
+ } else if ("2".equals(sexOra)) {
+ sexMss = "女";
+ } else {
+ sexMss = sexOra;
+ }
+ psIns.setNString(6, sexMss); // 对应 archive_master.sex
+ psIns.setNString(7, rs.getString("入院科室编码")); // dept_admission_to
+ psIns.setTimestamp(8, rs.getTimestamp("入院时间")); // admission_date_time
+ psIns.setTimestamp(9, rs.getTimestamp("出院日期")); // discharge_date_time
+ psIns.setString(10, rs.getString("主管医生工号")); // DOCTOR_IN_CHARGE
+ psIns.setNString(11, rs.getString("年龄")); // SubAssort
+ psIns.setString(12, rs.getString("身份证号")); // id_card
+ psIns.setString(13, rs.getString("当前科室编码")); // dept_name
+ psIns.setString(14, rs.getString("身份证号")); // ID_NO
+ psIns.addBatch();
+ if (insert % 1000 == 0) psIns.executeBatch();
+ }
+ if (insert % 1000 != 0) psIns.executeBatch();
+ mss.commit();
+
+ log.info("Oracle 当天共 {} 条,缺失 {} 条,已插入 {} 条", total, insert, insert);
+
+ } catch (SQLException e) {
+ log.error("同步异常", e);
+ Thread.sleep(5000);
+ }
+
+ Thread.sleep(Integer.parseInt(System.getProperty("poll.interval", "30")) * 1000L);
+ }
+ }
+
+
+ /* 工具:根据前缀拿连接 */
+ private static Connection getConn(Properties cfg, String db) throws SQLException {
+ String url = cfg.getProperty(db + ".url");
+ String user = cfg.getProperty(db + ".user");
+ String pwd = cfg.getProperty(db + ".password");
+ return DriverManager.getConnection(url, user, pwd);
+ }
+
+}
diff --git a/src/main/java/com/chaozhou/Main.java b/src/main/java/com/chaozhou/Main.java
new file mode 100644
index 0000000..afe4351
--- /dev/null
+++ b/src/main/java/com/chaozhou/Main.java
@@ -0,0 +1,7 @@
+package com.chaozhou;
+
+public class Main {
+ public static void main(String[] args) {
+ System.out.println("Hello world!");
+ }
+}
\ No newline at end of file
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
new file mode 100644
index 0000000..b03830b
--- /dev/null
+++ b/src/main/resources/application.properties
@@ -0,0 +1,11 @@
+oracle.url=jdbc:oracle:thin:@//172.16.11.110:1521/bsrun
+oracle.user=wzhgd_js
+oracle.password=cz3261
+
+
+sqlserver.url=jdbc:sqlserver://localhost:1433;databaseName=emr_record;encrypt=false
+sqlserver.user=sa
+sqlserver.password=docus@702
+
+#???????
+poll.interval=300
\ No newline at end of file
diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml
new file mode 100644
index 0000000..ae3ca03
--- /dev/null
+++ b/src/main/resources/logback.xml
@@ -0,0 +1,13 @@
+
+
+
+
+ %d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{36} - %msg%n
+
+
+
+
+
+
+
+
\ No newline at end of file