import java.nio.file.Files; import java.nio.file.Paths; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 2026-10-08 修正:释放 08-30 全校住宿快照导入残留的两条"未结束床位"。 * * 目标:孙雅妮(111150158)、赖柏因(111152140) * 依据(三方一致都说他们走读): * 1) BJCY:SFZS=0 / JDFSM=1(走读) * 2) 海康实况:orgIndexCode=走读生(980372f5…)、lodge=2 * 3) 门禁:孙雅妮 05-17~07-07 有宿舍门记录、之后只剩校门;赖柏因 09-21 起只有校门 * 4) 两人都没有任何住宿申请(ZSSQ)记录,床位来自 "2026-08-30全校住宿快照导入" * * 动作(与 ZssqU.makeTuis 的口径一致,不碰 BJCY/海康): * CDSY.JSSJ=sysdate → 该床位若无其他未结束记录则 NBCD.CDZTM=空闲(1) → 重算房间状态 * * 用法:java DormStaleBedFix20261008 [dry-run|apply|rollback] */ public class DormStaleBedFix20261008 { private static final String CTX = "/usr/local/tomcat/webapps/ROOT/META-INF/context.xml"; private static final long[] TARGET_RYIDS = {111150158L, 111152140L}; private static final int CDZT_KX = 1; // 空闲 private static final int CDZT_SYZ = 151; // 使用中 private static final int CDZT_BFSYZ = 155; // 部分使用中 public static void main(String[] args) throws Exception { String mode = args.length == 0 ? "dry-run" : args[0]; String xml = new String(Files.readAllBytes(Paths.get(CTX)), "UTF-8"); Matcher mu = Pattern.compile("url=\"(jdbc:oracle:thin:@[^\"]*ORCLPDB[^\"]*)\"").matcher(xml); if (!mu.find()) throw new IllegalStateException("未找到 context.xml 中主库连接"); String url = mu.group(1); Matcher muu = Pattern.compile("username=\"([^\"]+)\"\\s*password=\"([^\"]+)\"") .matcher(xml.substring(mu.end())); if (!muu.find()) throw new IllegalStateException("未找到 context.xml 中主库账号"); Class.forName("oracle.jdbc.OracleDriver"); try (Connection conn = DriverManager.getConnection(url, muu.group(1), muu.group(2))) { conn.setAutoCommit(false); System.out.println("mode=" + mode); List rows = new ArrayList<>(); for (long ryid : TARGET_RYIDS) { System.out.println("\n== RYID=" + ryid + " =="); printPerson(conn, ryid); rows.addAll(loadStaleRows(conn, ryid)); } if (rows.isEmpty()) { System.out.println("\n没有需要处理的行(可能已经处理过)。"); return; } System.out.println("\n== 待处理(未结束 + 来源为快照导入)=="); for (Row r : rows) System.out.println(" CDSYID=" + r.cdsyid + " RYID=" + r.ryid + " 姓名=" + r.syr + " 房间=" + r.cdid + " 床位=" + r.nbcdid + " 入住=" + r.kssj + " 来源=" + r.ms); switch (mode) { case "dry-run" -> System.out.println("\n[DRY-RUN] 未修改任何数据。"); case "apply" -> { for (Row r : rows) { release(conn, r); fixBed(conn, r); } conn.commit(); System.out.println("\n已提交。"); } case "rollback" -> { for (Row r : rows) { restore(conn, r); execute(conn, "update NBCD set CDZTM=? where NBCDID=?", CDZT_SYZ, r.nbcdid); } conn.commit(); System.out.println("\n已回滚(床位恢复为使用中)。"); } default -> throw new IllegalArgumentException("未知参数:" + mode); } System.out.println("\n== 处理后的状态 =="); for (Row r : rows) { printCdsy(conn, r.cdsyid); printBedRoom(conn, r.nbcdid); } } } private static class Row { long cdsyid, ryid, cdid; Long nbcdid; String syr, kssj, ms; } private static void printPerson(Connection conn, long ryid) throws Exception { try (PreparedStatement ps = conn.prepareStatement( "select r.XM,r.RYH,(select max(bc.SFZS) from BJCY bc where bc.RYID=r.RYID and bc.SFBY=0) sfzs," + "(select count(*) from ZSSQ z where z.SQRYID=r.RYID) zssq_cnt" + " from RY r where r.RYID=?")) { ps.setLong(1, ryid); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) System.out.println(" 姓名=" + rs.getString(1) + " 学号=" + rs.getString(2) + " BJCY.SFZS=" + rs.getInt(3) + "(0=走读) 住宿申请记录数=" + rs.getInt(4)); } } } private static List loadStaleRows(Connection conn, long ryid) throws Exception { List list = new ArrayList<>(); try (PreparedStatement ps = conn.prepareStatement( "select c.CDSYID,c.SYRYID,c.SYR,c.CDID,c.NBCDID,to_char(c.KSSJ,'YYYY-MM-DD HH24:MI'),c.MS" + " from CDSY c where c.SYRYID=? and c.JSSJ is null and c.JLZTM in (51,52,55)" + " and c.MS like '%快照导入%' order by c.KSSJ")) { ps.setLong(1, ryid); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { Row r = new Row(); r.cdsyid = rs.getLong(1); r.ryid = rs.getLong(2); r.syr = rs.getString(3); r.cdid = rs.getLong(4); r.nbcdid = rs.getLong(5); if (rs.wasNull()) r.nbcdid = null; r.kssj = rs.getString(6); r.ms = rs.getString(7); list.add(r); } } } return list; } private static void release(Connection conn, Row r) throws Exception { int n = execute(conn, "update CDSY set JSSJ=sysdate where CDSYID=? and JSSJ is null", r.cdsyid); System.out.println(" 释放住宿记录 CDSYID=" + r.cdsyid + " → 更新行数 " + n); } private static void restore(Connection conn, Row r) throws Exception { int n = execute(conn, "update CDSY set JSSJ=null where CDSYID=?", r.cdsyid); System.out.println(" 回滚住宿记录 CDSYID=" + r.cdsyid + " → 更新行数 " + n); } /** 床位无其他未结束记录则置空闲,并重算房间状态(与 ZssqU.makeTuis / CdU.refreshCdCdztm 同口径)。 */ private static void fixBed(Connection conn, Row r) throws Exception { if (r.nbcdid == null) { int inUse = count(conn, "select count(*) from CDSY where CDID=? and JSSJ is null and JLZTM in (51,52,55)", r.cdid); if (inUse == 0) { execute(conn, "update CD set CDZTM=? where CDID=?", CDZT_KX, r.cdid); System.out.println(" 房间 " + r.cdid + " → 空闲(1)"); } return; } int inUse = count(conn, "select count(*) from CDSY where NBCDID=? and JSSJ is null and JLZTM in (51,52,55)", r.nbcdid); if (inUse == 0) { execute(conn, "update NBCD set CDZTM=? where NBCDID=?", CDZT_KX, r.nbcdid); System.out.println(" 床位 " + r.nbcdid + " → 空闲(1)"); } else { System.out.println(" 床位 " + r.nbcdid + " 仍有 " + inUse + " 条未结束记录,保持使用中"); } // 重算房间状态 int nbcds = count(conn, "select NBCDS from CD where CDID=?", r.cdid); if (nbcds > 0) { int roomInUse = count(conn, "select count(*) from CDSY where CDID=? and JSSJ is null and JLZTM in (51,52,55)", r.cdid); int newCdztm = roomInUse == 0 ? CDZT_KX : (roomInUse >= nbcds ? CDZT_SYZ : CDZT_BFSYZ); int cur = count(conn, "select CDZTM from CD where CDID=?", r.cdid); if (newCdztm != cur) { execute(conn, "update CD set CDZTM=? where CDID=?", newCdztm, r.cdid); System.out.println(" 房间 " + r.cdid + " 状态 " + cur + " → " + newCdztm); } else { System.out.println(" 房间 " + r.cdid + " 状态不变(" + cur + ")"); } } } private static void printCdsy(Connection conn, long cdsyid) throws Exception { try (PreparedStatement ps = conn.prepareStatement( "select CDSYID,to_char(KSSJ,'YYYY-MM-DD HH24:MI'),to_char(JSSJ,'YYYY-MM-DD HH24:MI'),MS" + " from CDSY where CDSYID=?")) { ps.setLong(1, cdsyid); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) System.out.println(" CDSYID=" + rs.getLong(1) + " 入住=" + rs.getString(2) + " 结束=" + rs.getString(3) + " 来源=" + rs.getString(4)); } } } private static void printBedRoom(Connection conn, Long nbcdid) throws Exception { if (nbcdid == null) return; try (PreparedStatement ps = conn.prepareStatement( "select n.NBCDID,n.CDZTM,cc.CDID,cc.CDZTM,cc.NBCDS from NBCD n join CD cc on cc.CDID=n.CDID" + " where n.NBCDID=?")) { ps.setLong(1, nbcdid); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) System.out.println(" 床位 " + rs.getLong(1) + " CDZTM=" + rs.getInt(2) + " | 房间 " + rs.getLong(3) + " CDZTM=" + rs.getInt(4) + " 床位数=" + rs.getInt(5)); } } } private static int execute(Connection conn, String sql, Object... params) throws Exception { try (PreparedStatement ps = conn.prepareStatement(sql)) { for (int i = 0; i < params.length; i++) ps.setObject(i + 1, params[i]); return ps.executeUpdate(); } } private static int count(Connection conn, String sql, Object... params) throws Exception { try (PreparedStatement ps = conn.prepareStatement(sql)) { for (int i = 0; i < params.length; i++) ps.setObject(i + 1, params[i]); try (ResultSet rs = ps.executeQuery()) { rs.next(); return rs.getInt(1); } } } }