XjbJfServ.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. package biz.bj;
  2. import base.po.XjbJfjlPo;
  3. import com.google.gson.Gson;
  4. import jakarta.servlet.http.HttpServletRequest;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.apache.logging.log4j.LogManager;
  7. import org.apache.logging.log4j.Logger;
  8. import ss.db.Db;
  9. import ss.db.SqlCmd;
  10. import ss.obj.ObjC;
  11. import ss.serv.Serv;
  12. import ss.serv.ServU;
  13. import ss.user.UserRunB;
  14. import ss.user.UserU;
  15. import java.io.Writer;
  16. import java.sql.Timestamp;
  17. import java.time.LocalDate;
  18. import java.time.YearMonth;
  19. import java.time.ZoneId;
  20. import java.time.format.DateTimeFormatter;
  21. import java.util.Arrays;
  22. import java.util.LinkedHashMap;
  23. import java.util.LinkedHashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. /**
  28. * 星级班增分、减分事件账本。
  29. */
  30. public class XjbJfServ extends Serv {
  31. private static final Logger log4j = LogManager.getLogger(XjbJfServ.class);
  32. private static final ZoneId CN_ZONE = ZoneId.of("Asia/Shanghai");
  33. private static final DateTimeFormatter MONTH_FMT =
  34. DateTimeFormatter.ofPattern("yyyy-MM");
  35. public static final String LYLX_AWARD = "HJ";
  36. public static final String LYLX_SCHOOL_VIOLATION = "XW";
  37. public static final String LYLX_TEAM_ROLE = "TT";
  38. public static final String LYLX_TEAM_MEMBER = "TTCY";
  39. public static final String LYLX_MONTH_SAFE = "MONTH_SAFE";
  40. public static final String LYLX_FJ_SAFE = "FJ_SAFE";
  41. public static final int JFLX_ADD = 1;
  42. public static final int JFLX_SUB = 2;
  43. public static final double TEAM_BONUS_CAP = 3.0;
  44. public static final double TOTAL_BONUS_CAP = 8.0;
  45. private static final Set<String> RECALC_TYPES = new LinkedHashSet<>(
  46. Arrays.asList(LYLX_AWARD, LYLX_SCHOOL_VIOLATION,
  47. LYLX_MONTH_SAFE, LYLX_FJ_SAFE));
  48. private static final Set<String> TEAM_SNAPSHOT_TYPES = new LinkedHashSet<>(
  49. Arrays.asList(LYLX_TEAM_ROLE, LYLX_TEAM_MEMBER));
  50. /**
  51. * 学生团体先按 3 分封顶,再与其他增分合并并受总增分 8 分限制。
  52. */
  53. public static double capBonus(double otherBonus, double teamBonus) {
  54. double safeTeamBonus = Math.min(
  55. TEAM_BONUS_CAP, Math.max(0.0, teamBonus));
  56. double safeOtherBonus = Math.max(0.0, otherBonus);
  57. return Math.min(TOTAL_BONUS_CAP, safeOtherBonus + safeTeamBonus);
  58. }
  59. public static String schoolYearCode(YearMonth month) {
  60. int startYear = month.getMonthValue() >= 9
  61. ? month.getYear() : month.getYear() - 1;
  62. return startYear + "-" + (startYear + 1);
  63. }
  64. /**
  65. * 为当前班主任的指定班级重建一个月的事件账本。
  66. */
  67. @SuppressWarnings("unused")
  68. public void syncByA() throws Exception {
  69. HttpServletRequest request = super.req;
  70. super.resp.setCharacterEncoding("UTF-8");
  71. super.resp.setContentType("application/json;charset=UTF-8");
  72. Writer writer = super.resp.getWriter();
  73. Gson gson = new Gson();
  74. try (Db db = Db.makeConn()) {
  75. UserRunB user = UserU.rdUserByReq(request);
  76. if (user == null)
  77. throw new IllegalArgumentException("登录状态已失效,请重新登录");
  78. long bjid = rdLong(request.getParameter("bjid"), 0L);
  79. Map<String, Object> cls = requireTeacherClass(db, user.ryid, bjid);
  80. bjid = rdLong(cls.get("bjid"), 0L);
  81. YearMonth month = rdMonth(request.getParameter("month"));
  82. if (month.isAfter(YearMonth.now(CN_ZONE)))
  83. throw new IllegalArgumentException("不能同步未来月份");
  84. SyncCount count = syncClassMonth(db, bjid, month, user);
  85. db.commit();
  86. Map<String, Object> data = new LinkedHashMap<>();
  87. data.put("bjid", bjid);
  88. data.put("bjmc", cls.get("bjmc"));
  89. data.put("month", month.format(MONTH_FMT));
  90. data.put("awardEvents", count.awards);
  91. data.put("violationEvents", count.violations);
  92. data.put("teamRoleEvents", count.teamRoles);
  93. data.put("monthSafeEvents", count.monthSafe);
  94. data.put("fjSafeEvents", count.fjSafe);
  95. data.put("totalEvents", count.total());
  96. ServU.wrOkDataByWriter(data, writer, gson);
  97. } catch (IllegalArgumentException e) {
  98. ServU.wrErrMsgByWriter(null, e.getMessage(), writer, gson);
  99. } catch (Exception e) {
  100. log4j.error("XjbJfServ.syncByA() 异常", e);
  101. ServU.wrErrMsgByWriter(null, "星级班增扣分同步失败", writer, gson);
  102. }
  103. }
  104. /**
  105. * 定时任务入口:同步当前月和上一月。
  106. * 测试环境不加载 timer.xml,正式发布后再启用。
  107. */
  108. @SuppressWarnings("unused")
  109. public void syncDaily() throws Exception {
  110. try (Db db = Db.makeConn()) {
  111. List<Long> classIds = rdActiveClassIds(db);
  112. YearMonth current = YearMonth.now(CN_ZONE);
  113. YearMonth previous = current.minusMonths(1);
  114. for (long bjid : classIds) {
  115. syncClassMonth(db, bjid, previous, null);
  116. syncClassMonth(db, bjid, current, null);
  117. }
  118. db.commit();
  119. log4j.info("星级班增扣分定时同步完成,班级数={}", classIds.size());
  120. } catch (Exception e) {
  121. log4j.error("XjbJfServ.syncDaily() 异常", e);
  122. throw e;
  123. }
  124. }
  125. private SyncCount syncClassMonth(Db db, long bjid, YearMonth month,
  126. UserRunB user) throws Exception {
  127. String ndy = month.format(MONTH_FMT);
  128. Set<String> recalcTypes = new LinkedHashSet<>(RECALC_TYPES);
  129. if (YearMonth.now(CN_ZONE).equals(month)) {
  130. recalcTypes.add(LYLX_TEAM_ROLE);
  131. recalcTypes.add(LYLX_TEAM_MEMBER);
  132. }
  133. String placeholders = String.join(",",
  134. recalcTypes.stream().map(type -> ":lylx" + type).toList());
  135. SqlCmd delete = new SqlCmd(
  136. "delete from XJB_JFJL where BJID=:bjid and NDY=:ndy"
  137. + " and LYLX in(" + placeholders + ")");
  138. delete.wrLongParm("bjid", bjid);
  139. delete.wrStrParm("ndy", ndy);
  140. for (String type : recalcTypes)
  141. delete.wrStrParm("lylx" + type, type);
  142. db.doModByC(delete);
  143. SyncCount count = new SyncCount();
  144. count.awards = insertAwards(db, bjid, month, user);
  145. count.violations = insertSchoolViolations(db, bjid, month, user);
  146. if (YearMonth.now(CN_ZONE).equals(month))
  147. count.teamRoles = insertTeamRoles(db, bjid, month, user);
  148. if (month.isBefore(YearMonth.now(CN_ZONE))) {
  149. count.monthSafe = insertMonthSafe(db, bjid, month, user);
  150. count.fjSafe = insertFjSafe(db, bjid, month, user);
  151. }
  152. return count;
  153. }
  154. /**
  155. * 在月结核算时重建该班的团体成员快照。
  156. * 普通成员和职务成员都按执行核算时的有效班级关系固化。
  157. */
  158. public int rebuildTeamSnapshot(Db db, long bjid, YearMonth month,
  159. UserRunB user) throws Exception {
  160. String placeholders = String.join(",",
  161. TEAM_SNAPSHOT_TYPES.stream()
  162. .map(type -> ":lylx" + type).toList());
  163. SqlCmd delete = new SqlCmd(
  164. "delete from XJB_JFJL where BJID=:bjid and NDY=:ndy"
  165. + " and LYLX in(" + placeholders + ")");
  166. delete.wrLongParm("bjid", bjid);
  167. delete.wrStrParm("ndy", month.format(MONTH_FMT));
  168. for (String type : TEAM_SNAPSHOT_TYPES)
  169. delete.wrStrParm("lylx" + type, type);
  170. db.doModByC(delete);
  171. return insertTeamRoles(db, bjid, month, user);
  172. }
  173. private int insertAwards(Db db, long bjid, YearMonth month, UserRunB user)
  174. throws Exception {
  175. int count = insertPersonalAwards(db, bjid, month, user);
  176. count += insertCollectiveAwards(db, bjid, month, user);
  177. return count;
  178. }
  179. private int insertPersonalAwards(Db db, long bjid, YearMonth month,
  180. UserRunB user) throws Exception {
  181. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  182. Timestamp end = Timestamp.valueOf(month.plusMonths(1).atDay(1)
  183. .atStartOfDay());
  184. SqlCmd sqlc = new SqlCmd(
  185. "select h.HJJLID ID,h.RYID,h.HJRQ FSSJ,h.HJLBM LYLB,"
  186. + "l.MC LBMC,l.BJJF JFZ,h.MC TITLE,h.MS DESCRIPTION,"
  187. + "r.XM RYXM from HJJL h"
  188. + " inner join HJLB l on l.HJLBM=h.HJLBM"
  189. + " left join RY r on r.RYID=h.RYID"
  190. + " where h.BJID=:bjid and h.JLZTM=51"
  191. + " and nvl(h.HJXZM,1)=1"
  192. + " and h.HJRQ>=:start and h.HJRQ<:end"
  193. + " and h.HJLBM is not null and nvl(l.BJJF,0)>0"
  194. + " order by h.HJJLID");
  195. sqlc.wrLongParm("bjid", bjid);
  196. sqlc.wrTimeParm("start", start);
  197. sqlc.wrTimeParm("end", end);
  198. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  199. int count = 0;
  200. for (Map<String, Object> row : rows) {
  201. count += insertEvent(db, bjid, month, LYLX_AWARD,
  202. rdLong(row.get("id"), 0L),
  203. rdLong(row.get("lylb"), 0L),
  204. rdLongOrNull(row.get("ryid")),
  205. rdTimestamp(row.get("fssj")),
  206. JFLX_ADD, rdFloat(row.get("jfz")),
  207. rdStr(row.get("lbmc")),
  208. joinText("学校获奖",
  209. rdStr(row.get("title")),
  210. rdStr(row.get("description")),
  211. rdStr(row.get("ryxm"))),
  212. user);
  213. }
  214. return count;
  215. }
  216. private int insertCollectiveAwards(Db db, long bjid, YearMonth month,
  217. UserRunB user) throws Exception {
  218. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  219. Timestamp end = Timestamp.valueOf(month.plusMonths(1).atDay(1)
  220. .atStartOfDay());
  221. SqlCmd sqlc = new SqlCmd(
  222. "select h.HJJLID ID,h.HJRQ FSSJ,h.HJLBM LYLB,"
  223. + "l.MC LBMC,l.JTBJJF JFZ,h.MC TITLE,h.MS DESCRIPTION,"
  224. + "(select count(distinct c.RYID) from HJJLCY c"
  225. + " where c.HJJLID=h.HJJLID and c.JLZTM=51)"
  226. + "+case when h.RYID is not null then 1 else 0 end RYCOUNT"
  227. + " from HJJL h"
  228. + " inner join HJLB l on l.HJLBM=h.HJLBM"
  229. + " inner join (select h2.HJJLID,h2.BJID BJID"
  230. + " from HJJL h2 where h2.BJID is not null"
  231. + " union select c.HJJLID,nvl(c.BJID,bc.BJID) BJID"
  232. + " from HJJLCY c left join BJCY bc on bc.RYID=c.RYID"
  233. + " and bc." + ObjC.JLZT_useStr
  234. + " and nvl(bc.SFBY,0)=0"
  235. + " where c.JLZTM=51"
  236. + " and nvl(c.BJID,bc.BJID) is not null) tc"
  237. + " on tc.HJJLID=h.HJJLID"
  238. + " where h.JLZTM=51 and h.HJXZM=5"
  239. + " and h.HJRQ>=:start and h.HJRQ<:end"
  240. + " and h.HJLBM is not null and nvl(l.JTBJJF,0)>0"
  241. + " and tc.BJID=:bjid"
  242. + " order by h.HJJLID");
  243. sqlc.wrLongParm("bjid", bjid);
  244. sqlc.wrTimeParm("start", start);
  245. sqlc.wrTimeParm("end", end);
  246. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  247. int count = 0;
  248. for (Map<String, Object> row : rows) {
  249. count += insertEvent(db, bjid, month, LYLX_AWARD,
  250. rdLong(row.get("id"), 0L),
  251. rdLong(row.get("lylb"), 0L),
  252. null,
  253. rdTimestamp(row.get("fssj")),
  254. JFLX_ADD, rdFloat(row.get("jfz")),
  255. "集体获奖·" + rdStr(row.get("lbmc")),
  256. joinText("学校集体获奖",
  257. rdStr(row.get("title")),
  258. rdStr(row.get("description")),
  259. "参与学生" + rdInt(row.get("rycount"), 0) + "人"),
  260. user);
  261. }
  262. return count;
  263. }
  264. private int insertOrdinaryTeamMembers(Db db, long bjid, YearMonth month,
  265. UserRunB user, Timestamp syncTime)
  266. throws Exception {
  267. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  268. Timestamp end = Timestamp.valueOf(month.plusMonths(1).atDay(1)
  269. .atStartOfDay());
  270. SqlCmd sqlc = new SqlCmd(
  271. "select c.TTCYID ID,c.RYID,wc.TTWCYLBM LYLB,wc.MC LBMC,"
  272. + "wc.BJJF JFZ,t.MC TTMC,r.XM RYXM"
  273. + " from TTCY c"
  274. + " inner join TT t on t.TTID=c.TTID"
  275. + " inner join TTWCYLB wc on wc.TTLBM=t.TTLBM"
  276. + " and wc.TTWCYLBM=(select min(wc2.TTWCYLBM)"
  277. + " from TTWCYLB wc2 where wc2.TTLBM=t.TTLBM"
  278. + " and wc2.MC='成员')"
  279. + " inner join BJCY bc on bc.RYID=c.RYID"
  280. + " left join RY r on r.RYID=c.RYID"
  281. + " where bc.BJID=:bjid and bc." + ObjC.JLZT_useStr
  282. + " and nvl(bc.SFBY,0)=0 and c." + ObjC.JLZT_useStr
  283. + " and t." + ObjC.JLZT_useStr
  284. + " and (c.KSRQ is null or c.KSRQ<:cEnd)"
  285. + " and (c.JSRQ is null or c.JSRQ>=:cStart)"
  286. + " and (t.KSRQ is null or t.KSRQ<:tEnd)"
  287. + " and (t.JSRQ is null or t.JSRQ>=:tStart)"
  288. + " and (c.XNDM is null or c.XNDM=:cXndm)"
  289. + " and (t.XNDM is null or t.XNDM=:tXndm)"
  290. + " and nvl(wc.BJJF,0)>0"
  291. + " and not exists (select 1 from TTWCY w"
  292. + " where w.TTID=c.TTID and w.RYID=c.RYID"
  293. + " and w." + ObjC.JLZT_useStr + ")"
  294. + " order by c.TTCYID");
  295. sqlc.wrLongParm("bjid", bjid);
  296. sqlc.wrTimeParm("cStart", start);
  297. sqlc.wrTimeParm("cEnd", end);
  298. sqlc.wrStrParm("cXndm", schoolYearCode(month));
  299. sqlc.wrTimeParm("tStart", start);
  300. sqlc.wrTimeParm("tEnd", end);
  301. sqlc.wrStrParm("tXndm", schoolYearCode(month));
  302. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  303. int count = 0;
  304. for (Map<String, Object> row : rows) {
  305. long sourceId = rdLong(row.get("id"), 0L);
  306. if (eventExists(db, LYLX_TEAM_MEMBER, sourceId, bjid, month))
  307. continue;
  308. String groupName = rdStr(row.get("ttmc"));
  309. String roleName = rdStr(row.get("lbmc"));
  310. count += insertEvent(db, bjid, month, LYLX_TEAM_MEMBER,
  311. sourceId,
  312. rdLong(row.get("lylb"), 0L),
  313. rdLongOrNull(row.get("ryid")),
  314. syncTime, JFLX_ADD, rdFloat(row.get("jfz")),
  315. groupName + "·" + roleName,
  316. joinText("团体普通成员班级加分", groupName, roleName,
  317. rdStr(row.get("ryxm"))),
  318. user);
  319. }
  320. return count;
  321. }
  322. private int insertSchoolViolations(Db db, long bjid, YearMonth month,
  323. UserRunB user) throws Exception {
  324. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  325. Timestamp end = Timestamp.valueOf(month.plusMonths(1).atDay(1)
  326. .atStartOfDay());
  327. SqlCmd sqlc = new SqlCmd(
  328. "select w.WJJLID ID,w.RYID,w.WJSJ FSSJ,w.WJLBM LYLB,"
  329. + "l.MC LBMC,l.BJKF JFZ,w.MS DESCRIPTION,r.XM RYXM"
  330. + " from WJJL w inner join WJLB l on l.WJLBM=w.WJLBM"
  331. + " inner join XJB_WJLBMAP wm on wm.WJLBM=w.WJLBM"
  332. + " and wm.YTM=" + XjbWjlbC.YTM_SCHOOL
  333. + " and wm.JLZTM=51"
  334. + " left join RY r on r.RYID=w.RYID"
  335. + " where w.BJID=:bjid and w.JLZTM=51"
  336. + " and w.WJSJ>=:start and w.WJSJ<:end"
  337. + " and nvl(l.BJKF,0)>0 order by w.WJJLID");
  338. sqlc.wrLongParm("bjid", bjid);
  339. sqlc.wrTimeParm("start", start);
  340. sqlc.wrTimeParm("end", end);
  341. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  342. int count = 0;
  343. for (Map<String, Object> row : rows) {
  344. count += insertEvent(db, bjid, month, LYLX_SCHOOL_VIOLATION,
  345. rdLong(row.get("id"), 0L),
  346. rdLong(row.get("lylb"), 0L),
  347. rdLongOrNull(row.get("ryid")),
  348. rdTimestamp(row.get("fssj")),
  349. JFLX_SUB, rdFloat(row.get("jfz")),
  350. rdStr(row.get("lbmc")),
  351. joinText("学校违纪",
  352. rdStr(row.get("description")),
  353. rdStr(row.get("ryxm"))),
  354. user);
  355. }
  356. return count;
  357. }
  358. private int insertTeamRoles(Db db, long bjid, YearMonth month,
  359. UserRunB user) throws Exception {
  360. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  361. Timestamp end = Timestamp.valueOf(month.plusMonths(1).atDay(1)
  362. .atStartOfDay());
  363. SqlCmd sqlc = new SqlCmd(
  364. "select w.TTWCYID ID,w.RYID,w.TTWCYLBM LYLB,"
  365. + "wc.MC LBMC,wc.BJJF JFZ,t.MC TTMC,r.XM RYXM"
  366. + " from TTWCY w inner join TT t on t.TTID=w.TTID"
  367. + " inner join TTWCYLB wc on wc.TTWCYLBM=w.TTWCYLBM"
  368. + " inner join BJCY bc on bc.RYID=w.RYID"
  369. + " left join RY r on r.RYID=w.RYID"
  370. + " where bc.BJID=:bjid and bc." + ObjC.JLZT_useStr
  371. + " and nvl(bc.SFBY,0)=0 and w." + ObjC.JLZT_useStr
  372. + " and t." + ObjC.JLZT_useStr
  373. + " and (w.KSRQ is null or w.KSRQ<:wEnd)"
  374. + " and (w.JSRQ is null or w.JSRQ>=:wStart)"
  375. + " and (t.KSRQ is null or t.KSRQ<:tEnd)"
  376. + " and (t.JSRQ is null or t.JSRQ>=:tStart)"
  377. + " and (w.XNDM is null or w.XNDM=:wXndm)"
  378. + " and (t.XNDM is null or t.XNDM=:tXndm)"
  379. + " and nvl(wc.BJJF,0)>0"
  380. + " and wc.MC not like '%退休%'"
  381. + " order by w.TTWCYID");
  382. sqlc.wrLongParm("bjid", bjid);
  383. sqlc.wrTimeParm("wStart", start);
  384. sqlc.wrTimeParm("wEnd", end);
  385. sqlc.wrStrParm("wXndm", schoolYearCode(month));
  386. sqlc.wrTimeParm("tStart", start);
  387. sqlc.wrTimeParm("tEnd", end);
  388. sqlc.wrStrParm("tXndm", schoolYearCode(month));
  389. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  390. Timestamp syncTime = Timestamp.valueOf(
  391. LocalDate.now(CN_ZONE).atStartOfDay());
  392. int count = 0;
  393. for (Map<String, Object> row : rows) {
  394. long sourceId = rdLong(row.get("id"), 0L);
  395. if (eventExists(db, LYLX_TEAM_ROLE, sourceId, bjid, month))
  396. continue;
  397. String groupName = rdStr(row.get("ttmc"));
  398. String roleName = rdStr(row.get("lbmc"));
  399. count += insertEvent(db, bjid, month, LYLX_TEAM_ROLE,
  400. sourceId,
  401. rdLong(row.get("lylb"), 0L),
  402. rdLongOrNull(row.get("ryid")),
  403. syncTime, JFLX_ADD, rdFloat(row.get("jfz")),
  404. groupName + "·" + roleName,
  405. joinText("团体成员班级加分", groupName, roleName,
  406. rdStr(row.get("ryxm"))),
  407. user);
  408. }
  409. count += insertOrdinaryTeamMembers(db, bjid, month, user, syncTime);
  410. return count;
  411. }
  412. private boolean eventExists(Db db, String lylx, long lyid,
  413. long bjid, YearMonth month) throws Exception {
  414. SqlCmd sqlc = new SqlCmd(
  415. "select count(*) CNT from XJB_JFJL where LYLX=:lylx"
  416. + " and LYID=:lyid and BJID=:bjid and NDY=:ndy"
  417. + " and JLZTM=51");
  418. sqlc.wrStrParm("lylx", lylx);
  419. sqlc.wrLongParm("lyid", lyid);
  420. sqlc.wrLongParm("bjid", bjid);
  421. sqlc.wrStrParm("ndy", month.format(MONTH_FMT));
  422. db.doSelByC(sqlc);
  423. return db.rowNum > 0 && db.rdIntCol("cnt") > 0;
  424. }
  425. private int insertMonthSafe(Db db, long bjid, YearMonth month,
  426. UserRunB user) throws Exception {
  427. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  428. Timestamp end = Timestamp.valueOf(month.plusMonths(1).atDay(1)
  429. .atStartOfDay());
  430. SqlCmd countCmd = new SqlCmd(
  431. "select count(*) CNT from WJJL w"
  432. + " inner join XJB_WJLBMAP wm on wm.WJLBM=w.WJLBM"
  433. + " and wm.YTM in(" + XjbWjlbC.YTM_DISCIPLINE
  434. + "," + XjbWjlbC.YTM_SCHOOL + ") and wm.JLZTM=51"
  435. + " where w.BJID=:bjid and w.JLZTM=51"
  436. + " and w.WJSJ>=:start and w.WJSJ<:end");
  437. countCmd.wrLongParm("bjid", bjid);
  438. countCmd.wrTimeParm("start", start);
  439. countCmd.wrTimeParm("end", end);
  440. db.doSelByC(countCmd);
  441. if (db.rowNum > 0 && db.rdIntCol("cnt") > 0)
  442. return 0;
  443. SqlCmd legacyCmd = new SqlCmd(
  444. "select count(*) CNT from XJB_FJRY"
  445. + " where BJID=:bjid and JCRQ>=:start and JCRQ<:end"
  446. + " and JLZTM=51 and WJRS>0");
  447. legacyCmd.wrLongParm("bjid", bjid);
  448. legacyCmd.wrTimeParm("start", start);
  449. legacyCmd.wrTimeParm("end", end);
  450. db.doSelByC(legacyCmd);
  451. if (db.rowNum > 0 && db.rdIntCol("cnt") > 0)
  452. return 0;
  453. long syntheticId = month.getYear() * 100L + month.getMonthValue();
  454. Timestamp eventTime = Timestamp.valueOf(
  455. month.atEndOfMonth().atTime(23, 59, 59));
  456. return insertEvent(db, bjid, month, LYLX_MONTH_SAFE,
  457. syntheticId, 0L, null, eventTime,
  458. JFLX_ADD, 1.5F, "当月无学生处分",
  459. "当月班级无有效学校处分记录,增1.5分", user);
  460. }
  461. private int insertFjSafe(Db db, long bjid, YearMonth month,
  462. UserRunB user) throws Exception {
  463. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  464. Timestamp end = Timestamp.valueOf(month.plusMonths(1).atDay(1)
  465. .atStartOfDay());
  466. SqlCmd inspectionCmd = new SqlCmd(
  467. "select count(*) CNT,nvl(sum(nvl(mx.JFKF,0)),0) JFKF"
  468. + " from XJB_FJJCMX mx"
  469. + " inner join XJB_FJJC jc on jc.XJB_FJJCID=mx.XJB_FJJCID"
  470. + " where mx.BJID=:bjid and mx.JLZTM=51 and jc.JLZTM=51"
  471. + " and jc.JCRQ>=:start and jc.JCRQ<:end");
  472. inspectionCmd.wrLongParm("bjid", bjid);
  473. inspectionCmd.wrTimeParm("start", start);
  474. inspectionCmd.wrTimeParm("end", end);
  475. List<Map<String, Object>> inspections =
  476. db.doSelMapListByC(inspectionCmd);
  477. Map<String, Object> inspection =
  478. inspections == null || inspections.isEmpty()
  479. ? Map.of() : inspections.get(0);
  480. int inspectionCount = rdInt(inspection.get("cnt"), 0);
  481. double inspectionPenalty = rdFloat(inspection.get("jfkf"));
  482. if (inspectionCount == 0 || inspectionPenalty > 0.0)
  483. return 0;
  484. SqlCmd countCmd = new SqlCmd(
  485. "select count(*) CNT from WJJL w"
  486. + " inner join XJB_WJLBMAP wm on wm.WJLBM=w.WJLBM"
  487. + " and wm.YTM=" + XjbWjlbC.YTM_DISCIPLINE
  488. + " and wm.JLZTM=51"
  489. + " where w.BJID=:bjid and w.JLZTM=51"
  490. + " and w.WJSJ>=:start and w.WJSJ<:end");
  491. countCmd.wrLongParm("bjid", bjid);
  492. countCmd.wrTimeParm("start", start);
  493. countCmd.wrTimeParm("end", end);
  494. db.doSelByC(countCmd);
  495. if (db.rowNum > 0 && db.rdIntCol("cnt") > 0)
  496. return 0;
  497. SqlCmd legacyCmd = new SqlCmd(
  498. "select count(*) CNT from XJB_FJRY"
  499. + " where BJID=:bjid and JCRQ>=:start and JCRQ<:end"
  500. + " and JLZTM=51 and WJRS>0");
  501. legacyCmd.wrLongParm("bjid", bjid);
  502. legacyCmd.wrTimeParm("start", start);
  503. legacyCmd.wrTimeParm("end", end);
  504. db.doSelByC(legacyCmd);
  505. if (db.rowNum > 0 && db.rdIntCol("cnt") > 0)
  506. return 0;
  507. long syntheticId = month.getYear() * 100L + month.getMonthValue();
  508. Timestamp eventTime = Timestamp.valueOf(
  509. month.atEndOfMonth().atTime(23, 59, 59));
  510. return insertEvent(db, bjid, month, LYLX_FJ_SAFE,
  511. syntheticId, 0L, null, eventTime,
  512. JFLX_ADD, 0.5F, "当月已完成风纪检查且无扣分",
  513. "当月存在有效风纪检查记录,且无有效风纪扣分,增0.5分", user);
  514. }
  515. private int insertEvent(Db db, long bjid, YearMonth month, String lylx,
  516. long lyid, long lylb, Long ryid, Timestamp fssj,
  517. int jflx, float jfz, String mc, String ms,
  518. UserRunB user) throws Exception {
  519. XjbJfjlPo po = new XjbJfjlPo();
  520. po.setXjb_jfjlid(db.rdNextSeqVal("XJB_JFJLID"));
  521. po.setNdy(month.format(MONTH_FMT));
  522. po.setBjid(bjid);
  523. po.setLylx(lylx);
  524. po.setLyid(lyid);
  525. po.setLylb(lylb);
  526. po.setRyid(ryid);
  527. po.setFssj(fssj);
  528. po.setJflx(jflx);
  529. po.setJfz(jfz);
  530. po.setMc(StringUtils.abbreviate(mc, 250));
  531. po.setMs(StringUtils.abbreviate(ms, 1000));
  532. po.setJlztm(ObjC.JLZT_inUse);
  533. po.setJlsj(new Timestamp(System.currentTimeMillis()));
  534. po.setCjryid(user == null ? null : user.ryid);
  535. po.setGlryid(user == null ? null : user.ryid);
  536. po.setGlbmid(user == null ? null : user.bmid);
  537. Set<String> fields = new LinkedHashSet<>(Arrays.asList(
  538. "xjb_jfjlid", "ndy", "bjid", "lylx", "lyid", "lylb",
  539. "ryid", "fssj", "jflx", "jfz", "mc", "ms", "jlztm",
  540. "jlsj", "cjryid", "glryid", "glbmid"));
  541. db.doInsPo(po, fields);
  542. return 1;
  543. }
  544. private Map<String, Object> requireTeacherClass(Db db, long ryid,
  545. long bjid)
  546. throws Exception {
  547. SqlCmd sqlc = new SqlCmd(
  548. "select BJID,MC BJMC from BJ where BZRID=:ryid and "
  549. + ObjC.JLZT_useStr + " and BJZTM!=81"
  550. + (bjid > 0 ? " and BJID=:bjid" : "")
  551. + " order by BJID");
  552. sqlc.wrLongParm("ryid", ryid);
  553. if (bjid > 0)
  554. sqlc.wrLongParm("bjid", bjid);
  555. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  556. if (rows == null || rows.isEmpty())
  557. throw new IllegalArgumentException(
  558. bjid > 0 ? "无权同步该班级" : "当前账号未关联班主任班级");
  559. return rows.get(0);
  560. }
  561. private List<Long> rdActiveClassIds(Db db) throws Exception {
  562. SqlCmd sqlc = new SqlCmd(
  563. "select BJID from BJ where " + ObjC.JLZT_useStr
  564. + " and BJZTM!=81 order by BJID");
  565. List<Long> result = new java.util.ArrayList<>();
  566. for (Map<String, Object> row : db.doSelMapListByC(sqlc))
  567. result.add(rdLong(row.get("bjid"), 0L));
  568. return result;
  569. }
  570. private YearMonth rdMonth(String value) {
  571. String text = StringUtils.trimToNull(value);
  572. if (text == null)
  573. return YearMonth.now(CN_ZONE);
  574. try {
  575. return YearMonth.parse(text, MONTH_FMT);
  576. } catch (Exception e) {
  577. throw new IllegalArgumentException("月份格式错误");
  578. }
  579. }
  580. private static long rdLong(Object value, long defaultValue) {
  581. if (value == null)
  582. return defaultValue;
  583. String text = StringUtils.trimToNull(String.valueOf(value));
  584. return text == null ? defaultValue : Long.parseLong(text);
  585. }
  586. private static int rdInt(Object value, int defaultValue) {
  587. return (int) rdLong(value, defaultValue);
  588. }
  589. private static Long rdLongOrNull(Object value) {
  590. long result = rdLong(value, 0L);
  591. return result == 0L ? null : result;
  592. }
  593. private static float rdFloat(Object value) {
  594. if (value == null)
  595. return 0F;
  596. String text = StringUtils.trimToNull(String.valueOf(value));
  597. return text == null ? 0F : Float.parseFloat(text);
  598. }
  599. private static Timestamp rdTimestamp(Object value) {
  600. if (value instanceof Timestamp timestamp)
  601. return timestamp;
  602. if (value instanceof java.sql.Date date)
  603. return new Timestamp(date.getTime());
  604. if (value instanceof java.util.Date date)
  605. return new Timestamp(date.getTime());
  606. String text = StringUtils.trimToNull(
  607. value == null ? null : String.valueOf(value));
  608. return text == null ? null : Timestamp.valueOf(text);
  609. }
  610. private static String rdStr(Object value) {
  611. return value == null ? "" : StringUtils.defaultString(
  612. String.valueOf(value));
  613. }
  614. private static String joinText(String... parts) {
  615. StringBuilder result = new StringBuilder();
  616. for (String part : parts) {
  617. String text = StringUtils.trimToNull(part);
  618. if (text == null)
  619. continue;
  620. if (result.length() > 0)
  621. result.append(";");
  622. result.append(text);
  623. }
  624. return result.toString();
  625. }
  626. private static final class SyncCount {
  627. int awards;
  628. int violations;
  629. int teamRoles;
  630. int monthSafe;
  631. int fjSafe;
  632. int total() {
  633. return awards + violations + teamRoles + monthSafe + fjSafe;
  634. }
  635. }
  636. }