DormStaleBedFix20261008.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import java.nio.file.Files;
  2. import java.nio.file.Paths;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. /**
  12. * 2026-10-08 修正:释放 08-30 全校住宿快照导入残留的两条"未结束床位"。
  13. *
  14. * 目标:孙雅妮(111150158)、赖柏因(111152140)
  15. * 依据(三方一致都说他们走读):
  16. * 1) BJCY:SFZS=0 / JDFSM=1(走读)
  17. * 2) 海康实况:orgIndexCode=走读生(980372f5…)、lodge=2
  18. * 3) 门禁:孙雅妮 05-17~07-07 有宿舍门记录、之后只剩校门;赖柏因 09-21 起只有校门
  19. * 4) 两人都没有任何住宿申请(ZSSQ)记录,床位来自 "2026-08-30全校住宿快照导入"
  20. *
  21. * 动作(与 ZssqU.makeTuis 的口径一致,不碰 BJCY/海康):
  22. * CDSY.JSSJ=sysdate → 该床位若无其他未结束记录则 NBCD.CDZTM=空闲(1) → 重算房间状态
  23. *
  24. * 用法:java DormStaleBedFix20261008 [dry-run|apply|rollback]
  25. */
  26. public class DormStaleBedFix20261008 {
  27. private static final String CTX = "/usr/local/tomcat/webapps/ROOT/META-INF/context.xml";
  28. private static final long[] TARGET_RYIDS = {111150158L, 111152140L};
  29. private static final int CDZT_KX = 1; // 空闲
  30. private static final int CDZT_SYZ = 151; // 使用中
  31. private static final int CDZT_BFSYZ = 155; // 部分使用中
  32. public static void main(String[] args) throws Exception {
  33. String mode = args.length == 0 ? "dry-run" : args[0];
  34. String xml = new String(Files.readAllBytes(Paths.get(CTX)), "UTF-8");
  35. Matcher mu = Pattern.compile("url=\"(jdbc:oracle:thin:@[^\"]*ORCLPDB[^\"]*)\"").matcher(xml);
  36. if (!mu.find())
  37. throw new IllegalStateException("未找到 context.xml 中主库连接");
  38. String url = mu.group(1);
  39. Matcher muu = Pattern.compile("username=\"([^\"]+)\"\\s*password=\"([^\"]+)\"")
  40. .matcher(xml.substring(mu.end()));
  41. if (!muu.find())
  42. throw new IllegalStateException("未找到 context.xml 中主库账号");
  43. Class.forName("oracle.jdbc.OracleDriver");
  44. try (Connection conn = DriverManager.getConnection(url, muu.group(1), muu.group(2))) {
  45. conn.setAutoCommit(false);
  46. System.out.println("mode=" + mode);
  47. List<Row> rows = new ArrayList<>();
  48. for (long ryid : TARGET_RYIDS) {
  49. System.out.println("\n== RYID=" + ryid + " ==");
  50. printPerson(conn, ryid);
  51. rows.addAll(loadStaleRows(conn, ryid));
  52. }
  53. if (rows.isEmpty()) {
  54. System.out.println("\n没有需要处理的行(可能已经处理过)。");
  55. return;
  56. }
  57. System.out.println("\n== 待处理(未结束 + 来源为快照导入)==");
  58. for (Row r : rows)
  59. System.out.println(" CDSYID=" + r.cdsyid + " RYID=" + r.ryid + " 姓名=" + r.syr
  60. + " 房间=" + r.cdid + " 床位=" + r.nbcdid + " 入住=" + r.kssj
  61. + " 来源=" + r.ms);
  62. switch (mode) {
  63. case "dry-run" -> System.out.println("\n[DRY-RUN] 未修改任何数据。");
  64. case "apply" -> {
  65. for (Row r : rows) {
  66. release(conn, r);
  67. fixBed(conn, r);
  68. }
  69. conn.commit();
  70. System.out.println("\n已提交。");
  71. }
  72. case "rollback" -> {
  73. for (Row r : rows) {
  74. restore(conn, r);
  75. execute(conn, "update NBCD set CDZTM=? where NBCDID=?",
  76. CDZT_SYZ, r.nbcdid);
  77. }
  78. conn.commit();
  79. System.out.println("\n已回滚(床位恢复为使用中)。");
  80. }
  81. default -> throw new IllegalArgumentException("未知参数:" + mode);
  82. }
  83. System.out.println("\n== 处理后的状态 ==");
  84. for (Row r : rows) {
  85. printCdsy(conn, r.cdsyid);
  86. printBedRoom(conn, r.nbcdid);
  87. }
  88. }
  89. }
  90. private static class Row {
  91. long cdsyid, ryid, cdid;
  92. Long nbcdid;
  93. String syr, kssj, ms;
  94. }
  95. private static void printPerson(Connection conn, long ryid) throws Exception {
  96. try (PreparedStatement ps = conn.prepareStatement(
  97. "select r.XM,r.RYH,(select max(bc.SFZS) from BJCY bc where bc.RYID=r.RYID and bc.SFBY=0) sfzs,"
  98. + "(select count(*) from ZSSQ z where z.SQRYID=r.RYID) zssq_cnt"
  99. + " from RY r where r.RYID=?")) {
  100. ps.setLong(1, ryid);
  101. try (ResultSet rs = ps.executeQuery()) {
  102. if (rs.next())
  103. System.out.println(" 姓名=" + rs.getString(1) + " 学号=" + rs.getString(2)
  104. + " BJCY.SFZS=" + rs.getInt(3) + "(0=走读) 住宿申请记录数=" + rs.getInt(4));
  105. }
  106. }
  107. }
  108. private static List<Row> loadStaleRows(Connection conn, long ryid) throws Exception {
  109. List<Row> list = new ArrayList<>();
  110. try (PreparedStatement ps = conn.prepareStatement(
  111. "select c.CDSYID,c.SYRYID,c.SYR,c.CDID,c.NBCDID,to_char(c.KSSJ,'YYYY-MM-DD HH24:MI'),c.MS"
  112. + " from CDSY c where c.SYRYID=? and c.JSSJ is null and c.JLZTM in (51,52,55)"
  113. + " and c.MS like '%快照导入%' order by c.KSSJ")) {
  114. ps.setLong(1, ryid);
  115. try (ResultSet rs = ps.executeQuery()) {
  116. while (rs.next()) {
  117. Row r = new Row();
  118. r.cdsyid = rs.getLong(1);
  119. r.ryid = rs.getLong(2);
  120. r.syr = rs.getString(3);
  121. r.cdid = rs.getLong(4);
  122. r.nbcdid = rs.getLong(5);
  123. if (rs.wasNull())
  124. r.nbcdid = null;
  125. r.kssj = rs.getString(6);
  126. r.ms = rs.getString(7);
  127. list.add(r);
  128. }
  129. }
  130. }
  131. return list;
  132. }
  133. private static void release(Connection conn, Row r) throws Exception {
  134. int n = execute(conn, "update CDSY set JSSJ=sysdate where CDSYID=? and JSSJ is null", r.cdsyid);
  135. System.out.println(" 释放住宿记录 CDSYID=" + r.cdsyid + " → 更新行数 " + n);
  136. }
  137. private static void restore(Connection conn, Row r) throws Exception {
  138. int n = execute(conn, "update CDSY set JSSJ=null where CDSYID=?", r.cdsyid);
  139. System.out.println(" 回滚住宿记录 CDSYID=" + r.cdsyid + " → 更新行数 " + n);
  140. }
  141. /** 床位无其他未结束记录则置空闲,并重算房间状态(与 ZssqU.makeTuis / CdU.refreshCdCdztm 同口径)。 */
  142. private static void fixBed(Connection conn, Row r) throws Exception {
  143. if (r.nbcdid == null) {
  144. int inUse = count(conn, "select count(*) from CDSY where CDID=? and JSSJ is null and JLZTM in (51,52,55)", r.cdid);
  145. if (inUse == 0) {
  146. execute(conn, "update CD set CDZTM=? where CDID=?", CDZT_KX, r.cdid);
  147. System.out.println(" 房间 " + r.cdid + " → 空闲(1)");
  148. }
  149. return;
  150. }
  151. int inUse = count(conn, "select count(*) from CDSY where NBCDID=? and JSSJ is null and JLZTM in (51,52,55)", r.nbcdid);
  152. if (inUse == 0) {
  153. execute(conn, "update NBCD set CDZTM=? where NBCDID=?", CDZT_KX, r.nbcdid);
  154. System.out.println(" 床位 " + r.nbcdid + " → 空闲(1)");
  155. } else {
  156. System.out.println(" 床位 " + r.nbcdid + " 仍有 " + inUse + " 条未结束记录,保持使用中");
  157. }
  158. // 重算房间状态
  159. int nbcds = count(conn, "select NBCDS from CD where CDID=?", r.cdid);
  160. if (nbcds > 0) {
  161. int roomInUse = count(conn, "select count(*) from CDSY where CDID=? and JSSJ is null and JLZTM in (51,52,55)", r.cdid);
  162. int newCdztm = roomInUse == 0 ? CDZT_KX : (roomInUse >= nbcds ? CDZT_SYZ : CDZT_BFSYZ);
  163. int cur = count(conn, "select CDZTM from CD where CDID=?", r.cdid);
  164. if (newCdztm != cur) {
  165. execute(conn, "update CD set CDZTM=? where CDID=?", newCdztm, r.cdid);
  166. System.out.println(" 房间 " + r.cdid + " 状态 " + cur + " → " + newCdztm);
  167. } else {
  168. System.out.println(" 房间 " + r.cdid + " 状态不变(" + cur + ")");
  169. }
  170. }
  171. }
  172. private static void printCdsy(Connection conn, long cdsyid) throws Exception {
  173. try (PreparedStatement ps = conn.prepareStatement(
  174. "select CDSYID,to_char(KSSJ,'YYYY-MM-DD HH24:MI'),to_char(JSSJ,'YYYY-MM-DD HH24:MI'),MS"
  175. + " from CDSY where CDSYID=?")) {
  176. ps.setLong(1, cdsyid);
  177. try (ResultSet rs = ps.executeQuery()) {
  178. if (rs.next())
  179. System.out.println(" CDSYID=" + rs.getLong(1) + " 入住=" + rs.getString(2)
  180. + " 结束=" + rs.getString(3) + " 来源=" + rs.getString(4));
  181. }
  182. }
  183. }
  184. private static void printBedRoom(Connection conn, Long nbcdid) throws Exception {
  185. if (nbcdid == null)
  186. return;
  187. try (PreparedStatement ps = conn.prepareStatement(
  188. "select n.NBCDID,n.CDZTM,cc.CDID,cc.CDZTM,cc.NBCDS from NBCD n join CD cc on cc.CDID=n.CDID"
  189. + " where n.NBCDID=?")) {
  190. ps.setLong(1, nbcdid);
  191. try (ResultSet rs = ps.executeQuery()) {
  192. if (rs.next())
  193. System.out.println(" 床位 " + rs.getLong(1) + " CDZTM=" + rs.getInt(2)
  194. + " | 房间 " + rs.getLong(3) + " CDZTM=" + rs.getInt(4)
  195. + " 床位数=" + rs.getInt(5));
  196. }
  197. }
  198. }
  199. private static int execute(Connection conn, String sql, Object... params) throws Exception {
  200. try (PreparedStatement ps = conn.prepareStatement(sql)) {
  201. for (int i = 0; i < params.length; i++)
  202. ps.setObject(i + 1, params[i]);
  203. return ps.executeUpdate();
  204. }
  205. }
  206. private static int count(Connection conn, String sql, Object... params) throws Exception {
  207. try (PreparedStatement ps = conn.prepareStatement(sql)) {
  208. for (int i = 0; i < params.length; i++)
  209. ps.setObject(i + 1, params[i]);
  210. try (ResultSet rs = ps.executeQuery()) {
  211. rs.next();
  212. return rs.getInt(1);
  213. }
  214. }
  215. }
  216. }