XjbMonthCloseServ.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. package biz.bj;
  2. import base.po.XjbJdPo;
  3. import base.po.XjbJgPo;
  4. import base.po.XjbPmPo;
  5. import com.google.gson.Gson;
  6. import jakarta.servlet.http.HttpServletRequest;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.logging.log4j.LogManager;
  9. import org.apache.logging.log4j.Logger;
  10. import ss.db.Db;
  11. import ss.db.SqlCmd;
  12. import ss.obj.ObjC;
  13. import ss.serv.Serv;
  14. import ss.serv.ServU;
  15. import ss.user.UserRunB;
  16. import ss.user.UserU;
  17. import java.io.Writer;
  18. import java.sql.Timestamp;
  19. import java.time.LocalDate;
  20. import java.time.YearMonth;
  21. import java.time.ZoneId;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Collections;
  26. import java.util.Comparator;
  27. import java.util.HashMap;
  28. import java.util.LinkedHashMap;
  29. import java.util.LinkedHashSet;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Set;
  33. /**
  34. * 星级班月结、快照和 PC 查询。
  35. */
  36. public class XjbMonthCloseServ extends Serv {
  37. private static final Logger log4j =
  38. LogManager.getLogger(XjbMonthCloseServ.class);
  39. private static final ZoneId CN_ZONE = ZoneId.of("Asia/Shanghai");
  40. private static final DateTimeFormatter MONTH_FMT =
  41. DateTimeFormatter.ofPattern("yyyy-MM");
  42. private static final String SOURCE_REAL = "real";
  43. private static final String SOURCE_MISSING = "missing";
  44. private static final int JD_DRAFT = 1;
  45. private static final int JD_PUBLISHED = 5;
  46. /**
  47. * 对当前用户部门完成一个已结束月份的月结。
  48. */
  49. @SuppressWarnings("unused")
  50. public void closeByA() throws Exception {
  51. HttpServletRequest request = super.req;
  52. super.resp.setCharacterEncoding("UTF-8");
  53. super.resp.setContentType("application/json;charset=UTF-8");
  54. Writer writer = super.resp.getWriter();
  55. Gson gson = new Gson();
  56. try (Db db = Db.makeConn()) {
  57. UserRunB user = UserU.rdUserByReq(request);
  58. if (user == null)
  59. throw new IllegalArgumentException("登录状态已失效,请重新登录");
  60. if (user.bmid == null)
  61. throw new IllegalArgumentException("当前账号未关联部门");
  62. YearMonth month = rdMonth(request.getParameter("month"));
  63. if (!month.isBefore(YearMonth.now(CN_ZONE)))
  64. throw new IllegalArgumentException("只能月结已经结束的月份");
  65. long bmid = user.bmid;
  66. List<ClassRow> classes = rdDepartmentClasses(db, bmid);
  67. if (classes.isEmpty())
  68. throw new IllegalArgumentException("当前部门没有有效班级");
  69. if (hasPublishedBatch(db, month, bmid))
  70. throw new IllegalArgumentException("该部门该月份已发布,不能覆盖");
  71. XjbJfServ teamSnapshot = new XjbJfServ();
  72. for (ClassRow cls : classes)
  73. teamSnapshot.rebuildTeamSnapshot(db, cls.bjid, month, user);
  74. List<ClassResult> results = calculate(db, classes, month);
  75. List<ClassResult> missing = new ArrayList<>();
  76. for (ClassResult result : results) {
  77. if (!result.missing.isEmpty())
  78. missing.add(result);
  79. }
  80. if (!missing.isEmpty()) {
  81. StringBuilder message = new StringBuilder("月结数据不完整,");
  82. message.append(missing.size()).append(" 个班级未发布:");
  83. for (int i = 0; i < Math.min(5, missing.size()); i++) {
  84. if (i > 0)
  85. message.append(";");
  86. ClassResult item = missing.get(i);
  87. message.append(item.cls.bjmc).append("[")
  88. .append(String.join("、", item.missing))
  89. .append("]");
  90. }
  91. if (missing.size() > 5)
  92. message.append(" 等");
  93. throw new IllegalArgumentException(message.toString());
  94. }
  95. assignRanks(results);
  96. deleteDraftBatch(db, month, bmid);
  97. long jdid = insertBatch(db, month, bmid, user);
  98. for (ClassResult result : results)
  99. insertResult(db, jdid, result, user);
  100. db.commit();
  101. Map<String, Object> data = new LinkedHashMap<>();
  102. data.put("jdid", jdid);
  103. data.put("month", month.format(MONTH_FMT));
  104. data.put("bmid", bmid);
  105. data.put("classCount", results.size());
  106. data.put("status", JD_PUBLISHED);
  107. ServU.wrOkDataByWriter(data, writer, gson);
  108. } catch (IllegalArgumentException e) {
  109. ServU.wrErrMsgByWriter(null, e.getMessage(), writer, gson);
  110. } catch (Exception e) {
  111. log4j.error("XjbMonthCloseServ.closeByA() 异常", e);
  112. ServU.wrErrMsgByWriter(null, "星级班月结失败", writer, gson);
  113. }
  114. }
  115. /**
  116. * PC 星级班总表只读取已发布月结。
  117. */
  118. @SuppressWarnings("unused")
  119. public void pcSummaryByA() throws Exception {
  120. HttpServletRequest request = super.req;
  121. super.resp.setCharacterEncoding("UTF-8");
  122. super.resp.setContentType("application/json;charset=UTF-8");
  123. Writer writer = super.resp.getWriter();
  124. Gson gson = new Gson();
  125. try (Db db = Db.makeConn()) {
  126. UserRunB user = UserU.rdUserByReq(request);
  127. if (user == null)
  128. throw new IllegalArgumentException("登录状态已失效,请重新登录");
  129. String monthText = StringUtils.trimToNull(request.getParameter("month"));
  130. YearMonth month;
  131. if (monthText == null) {
  132. month = rdLatestPublishedMonth(db);
  133. if (month == null)
  134. throw new IllegalArgumentException("暂无已发布星级班月结");
  135. } else {
  136. month = rdMonth(monthText);
  137. }
  138. SqlCmd batchCmd = new SqlCmd(
  139. "select XJB_JDID,NDY,FWLX,FWID,BB,ZT,FBSJ"
  140. + " from XJB_JD where NDY=:ndy and ZT=:status"
  141. + " order by BB desc,XJB_JDID desc");
  142. batchCmd.wrStrParm("ndy", month.format(MONTH_FMT));
  143. batchCmd.wrIntParm("status", JD_PUBLISHED);
  144. List<Map<String, Object>> batches =
  145. db.doSelMapListByC(batchCmd);
  146. if (batches == null || batches.isEmpty())
  147. throw new IllegalArgumentException("该月份暂无已发布月结");
  148. Map<String, Object> batch = batches.get(0);
  149. long jdid = rdLong(batch.get("xjb_jdid"), 0L);
  150. SqlCmd sqlc = new SqlCmd(
  151. "select j.BJID,b.MC BJMC,nvl(d.MC,'') DWMC,"
  152. + "nvl(z.MC,'') ZYMC,j.SSDF,j.KTDF,j.SJDF,j.CSDF,"
  153. + "j.FJDF,j.KQDF,j.ZJF,j.KJF,j.ZF,j.XJ,"
  154. + "j.ZJFCS,j.KJFCS,j.QXSM,p.DWPM,p.DWCS,p.XXPM,p.XXCS"
  155. + " from XJB_JG j"
  156. + " inner join BJ b on b.BJID=j.BJID"
  157. + " left join DW d on d.DWID=j.DWID"
  158. + " left join ZY z on z.ZYID=j.ZYID"
  159. + " left join XJB_PM p on p.XJB_JDID=j.XJB_JDID"
  160. + " and p.BJID=j.BJID"
  161. + " where j.XJB_JDID=:jdid and j.ZT=:status"
  162. + " order by p.XXPM,j.BJID");
  163. sqlc.wrLongParm("jdid", jdid);
  164. sqlc.wrIntParm("status", JD_PUBLISHED);
  165. List<Map<String, Object>> rows = new ArrayList<>();
  166. Set<String> departments = new LinkedHashSet<>();
  167. Set<String> majors = new LinkedHashSet<>();
  168. Set<String> classes = new LinkedHashSet<>();
  169. Set<String> stars = new LinkedHashSet<>();
  170. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  171. Map<String, Object> item = new LinkedHashMap<>();
  172. item.put("department", rdStr(row.get("dwmc")));
  173. item.put("major", rdStr(row.get("zymc")));
  174. item.put("className", rdStr(row.get("bjmc")));
  175. item.put("dormScore", formatScore(row.get("ssdf")));
  176. item.put("classroomScore", formatScore(row.get("ktdf")));
  177. item.put("flagCeremonyScore", formatScore(row.get("sjdf")));
  178. item.put("cleaningScore", formatScore(row.get("csdf")));
  179. item.put("disciplineScore", formatScore(row.get("fjdf")));
  180. item.put("attendanceScore", formatScore(row.get("kqdf")));
  181. item.put("bonus", formatScore(row.get("zjf")));
  182. item.put("deduction", formatScore(row.get("kjf")));
  183. item.put("monthTotal", formatScore(row.get("zf")));
  184. item.put("starLevel", rdStr(row.get("xj")));
  185. item.put("departmentRank", rdStr(row.get("dwpm")));
  186. item.put("departmentClassCount", rdStr(row.get("dwcs")));
  187. item.put("schoolRank", rdStr(row.get("xxpm")));
  188. item.put("schoolClassCount", rdStr(row.get("xxcs")));
  189. item.put("bonusCount", rdStr(row.get("zjfcs")));
  190. item.put("deductionCount", rdStr(row.get("kjfcs")));
  191. item.put("missing", rdStr(row.get("qxsm")));
  192. rows.add(item);
  193. addNonBlank(departments, item.get("department"));
  194. addNonBlank(majors, item.get("major"));
  195. addNonBlank(classes, item.get("className"));
  196. addNonBlank(stars, item.get("starLevel"));
  197. }
  198. Map<String, Object> data = new LinkedHashMap<>();
  199. data.put("month", month.format(MONTH_FMT));
  200. data.put("batchId", jdid);
  201. data.put("status", "已发布");
  202. data.put("publishedAt", formatTimestamp(batch.get("fbsj")));
  203. data.put("rows", rows);
  204. data.put("departments", new ArrayList<>(departments));
  205. data.put("majors", new ArrayList<>(majors));
  206. data.put("classes", new ArrayList<>(classes));
  207. data.put("stars", new ArrayList<>(stars));
  208. ServU.wrOkDataByWriter(data, writer, gson);
  209. } catch (IllegalArgumentException e) {
  210. ServU.wrErrMsgByWriter(null, e.getMessage(), writer, gson);
  211. } catch (Exception e) {
  212. log4j.error("XjbMonthCloseServ.pcSummaryByA() 异常", e);
  213. ServU.wrErrMsgByWriter(null, "星级班月结报表加载失败", writer, gson);
  214. }
  215. }
  216. private List<ClassRow> rdDepartmentClasses(Db db, long bmid)
  217. throws Exception {
  218. SqlCmd sqlc = new SqlCmd(
  219. "select b.BJID,b.MC BJMC,b.DWID,b.ZYID from BJ b"
  220. + " inner join ZY z on z.ZYID=b.ZYID"
  221. + " where z.FZBMID=:bmid and b." + ObjC.JLZT_useStr
  222. + " and b.BJZTM!=81 order by b.MC,b.BJID");
  223. sqlc.wrLongParm("bmid", bmid);
  224. List<ClassRow> rows = new ArrayList<>();
  225. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  226. rows.add(new ClassRow(
  227. rdLong(row.get("bjid"), 0L),
  228. rdStr(row.get("bjmc")),
  229. rdLongOrNull(row.get("dwid")),
  230. rdLongOrNull(row.get("zyid"))));
  231. }
  232. return rows;
  233. }
  234. private List<ClassResult> calculate(Db db, List<ClassRow> classes,
  235. YearMonth month) throws Exception {
  236. Timestamp start = Timestamp.valueOf(month.atDay(1).atStartOfDay());
  237. Timestamp end = Timestamp.valueOf(
  238. month.plusMonths(1).atDay(1).atStartOfDay());
  239. Map<Long, Double> classroom = rdAvg(db,
  240. "select j.BJID,avg(j.FS) SCORE from KTJL j"
  241. + " inner join KCB k on k.KCBID=j.KCBID"
  242. + " where k.KSSJ>=:start and k.KSSJ<:end"
  243. + " and j." + ObjC.JLZT_useStr
  244. + " and k." + ObjC.JLZT_useStr
  245. + " group by j.BJID", start, end);
  246. Map<Long, Double> flag = rdAvg(db,
  247. "select BJID,avg(ZFS) SCORE from SQLJC"
  248. + " where JCSJ>=:start and JCSJ<:end"
  249. + " and ZFS between 0 and 100 and "
  250. + ObjC.JLZT_useStr + " group by BJID", start, end);
  251. Map<Long, Double> cleaning = rdAvg(db,
  252. "select BJID,avg(FS) SCORE from CSWSJC"
  253. + " where JCSJ>=:start and JCSJ<:end and "
  254. + ObjC.JLZT_useStr + " group by BJID", start, end);
  255. Map<Long, DisciplineScore> discipline =
  256. rdDisciplineScores(db, start, end);
  257. Map<Long, Double> attendance = rdAttendanceScores(db, classes,
  258. start, end);
  259. Map<Long, DormScore> dorm = rdDormScores(db, classes, start, end);
  260. Map<Long, AdjustmentTotals> adjustments =
  261. rdAdjustmentTotals(db, month);
  262. List<ClassResult> result = new ArrayList<>();
  263. for (ClassRow cls : classes) {
  264. ClassResult item = new ClassResult(cls);
  265. item.ndy = month.format(MONTH_FMT);
  266. item.dorm = dormComponent(dorm.get(cls.bjid));
  267. item.classroom = component(classroom.get(cls.bjid),
  268. "课堂日志");
  269. item.flag = component(flag.get(cls.bjid), "升旗礼");
  270. item.cleaning = component(cleaning.get(cls.bjid), "场室卫生");
  271. item.discipline = disciplineComponent(discipline.get(cls.bjid));
  272. item.attendance = component(attendance.get(cls.bjid), "考勤");
  273. item.adjust = adjustments.getOrDefault(cls.bjid,
  274. new AdjustmentTotals());
  275. addMissing(item, "宿舍评分", item.dorm);
  276. addMissing(item, "课堂考核", item.classroom);
  277. addMissing(item, "升旗礼", item.flag);
  278. addMissing(item, "清洁卫生", item.cleaning);
  279. addMissing(item, "风纪仪容", item.discipline);
  280. addMissing(item, "考勤", item.attendance);
  281. if (item.missing.isEmpty()) {
  282. double weighted = item.dorm.value * 0.20
  283. + item.classroom.value * 0.05
  284. + item.flag.value * 0.14
  285. + item.cleaning.value * 0.18
  286. + item.discipline.value * 0.20
  287. + item.attendance.value * 0.15;
  288. item.total = round2(weighted
  289. + Math.min(8.0, item.adjust.bonus)
  290. - item.adjust.deduction);
  291. }
  292. result.add(item);
  293. }
  294. return result;
  295. }
  296. private Map<Long, Double> rdAvg(Db db, String sql,
  297. Timestamp start, Timestamp end)
  298. throws Exception {
  299. SqlCmd sqlc = new SqlCmd(sql);
  300. sqlc.wrTimeParm("start", start);
  301. sqlc.wrTimeParm("end", end);
  302. Map<Long, Double> result = new HashMap<>();
  303. for (Map<String, Object> row : db.doSelMapListByC(sqlc))
  304. result.put(rdLong(row.get("bjid"), 0L),
  305. round2(rdDouble(row.get("score"))));
  306. return result;
  307. }
  308. private Map<Long, DisciplineScore> rdDisciplineScores(
  309. Db db, Timestamp start, Timestamp end) throws Exception {
  310. SqlCmd sqlc = new SqlCmd(
  311. "select BJID,avg(case when WJRS>=100 then 0"
  312. + " else 100-WJRS end) SCORE,sum(WJRS) WJRS,"
  313. + "count(*) DAYS from XJB_FJRY"
  314. + " where JCRQ>=:start and JCRQ<:end"
  315. + " and JLZTM=51 group by BJID");
  316. sqlc.wrTimeParm("start", start);
  317. sqlc.wrTimeParm("end", end);
  318. Map<Long, DisciplineScore> result = new HashMap<>();
  319. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  320. DisciplineScore score = new DisciplineScore();
  321. score.value = rdDouble(row.get("score"));
  322. score.violations = rdInt(row.get("wjrs"));
  323. score.days = rdInt(row.get("days"));
  324. result.put(rdLong(row.get("bjid"), 0L), score);
  325. }
  326. return result;
  327. }
  328. private Map<Long, Double> rdAttendanceScores(
  329. Db db, List<ClassRow> classes, Timestamp start, Timestamp end)
  330. throws Exception {
  331. SqlCmd daysCmd = new SqlCmd(
  332. "select distinct trunc(KSSJ) D from KQJL"
  333. + " where KSSJ>=:start and KSSJ<:end"
  334. + " and KQJLZTM=51 and " + ObjC.JLZT_useStr);
  335. daysCmd.wrTimeParm("start", start);
  336. daysCmd.wrTimeParm("end", end);
  337. List<LocalDate> days = new ArrayList<>();
  338. for (Map<String, Object> row : db.doSelMapListByC(daysCmd))
  339. days.add(rdLocalDate(row.get("d")));
  340. // 学校确认:同一学生同一天多次异常只计一次,取最高扣分。
  341. SqlCmd penaltyCmd = new SqlCmd(
  342. "select BJID,D,sum(PENALTY) PENALTY from ("
  343. + "select BJID,NVL(RYID,-KQJLID) STUDENT_KEY,"
  344. + "trunc(KSSJ) D,max(case KQLBM"
  345. + " when 1 then 3 when 21 then 1 when 51 then 1"
  346. + " when 52 then 1 when 61 then 2 else 0 end) PENALTY"
  347. + " from KQJL where KSSJ>=:start and KSSJ<:end"
  348. + " and KQJLZTM=51 and " + ObjC.JLZT_useStr
  349. + " group by BJID,NVL(RYID,-KQJLID),trunc(KSSJ)"
  350. + ") group by BJID,D");
  351. penaltyCmd.wrTimeParm("start", start);
  352. penaltyCmd.wrTimeParm("end", end);
  353. Map<Long, Map<LocalDate, Double>> penalties = new HashMap<>();
  354. for (Map<String, Object> row : db.doSelMapListByC(penaltyCmd)) {
  355. long bjid = rdLong(row.get("bjid"), 0L);
  356. LocalDate day = rdLocalDate(row.get("d"));
  357. penalties.computeIfAbsent(bjid, key -> new HashMap<>())
  358. .put(day, rdDouble(row.get("penalty")));
  359. }
  360. Map<Long, Double> result = new HashMap<>();
  361. if (days.isEmpty())
  362. return result;
  363. for (ClassRow cls : classes) {
  364. double total = 0.0;
  365. Map<LocalDate, Double> classPenalty =
  366. penalties.getOrDefault(cls.bjid, Collections.emptyMap());
  367. for (LocalDate day : days)
  368. total += Math.max(0.0, 100.0
  369. - classPenalty.getOrDefault(day, 0.0));
  370. result.put(cls.bjid, round2(total / days.size()));
  371. }
  372. return result;
  373. }
  374. private Map<Long, DormScore> rdDormScores(
  375. Db db, List<ClassRow> classes, Timestamp start, Timestamp end)
  376. throws Exception {
  377. // 学校确认:宿舍内务不再区分上午/下午,所有有效检查记录参与平均。
  378. SqlCmd itemCmd = new SqlCmd(
  379. "select BJID,avg(FS) SCORE from SSNWJC"
  380. + " where JCSJ>=:start and JCSJ<:end"
  381. + " and " + ObjC.JLZT_useStr + " group by BJID");
  382. itemCmd.wrTimeParm("start", start);
  383. itemCmd.wrTimeParm("end", end);
  384. Map<Long, Double> internal = new HashMap<>();
  385. for (Map<String, Object> row : db.doSelMapListByC(itemCmd))
  386. internal.put(rdLong(row.get("bjid"), 0L),
  387. rdDouble(row.get("score")));
  388. // 集体宿舍违纪会按在住学生逐人生成 WJJL;班级月结按同一事件只扣一次。
  389. SqlCmd violationCmd = new SqlCmd(
  390. "select BJID,sum(PENALTY) PENALTY from ("
  391. + "select w.BJID,nvl(lb.BJKF,0) PENALTY,"
  392. + "row_number() over(partition by w.BJID,w.WJLXM,"
  393. + "case when w.WJLXM=5 then w.WJLBM end,"
  394. + "case when w.WJLXM=5 then w.WJSJ end,"
  395. + "case when w.WJLXM=5 then w.JZWID end,"
  396. + "case when w.WJLXM=5 then w.KFID end,"
  397. + "case when w.WJLXM=5 then nvl(w.MS,' ') end,"
  398. + "case when w.WJLXM=5 then nvl(w.FJID,0) end,"
  399. + "case when w.WJLXM=5 then 0 else w.WJJLID end"
  400. + " order by w.WJJLID) RN"
  401. + " from WJJL w inner join WJLB lb on lb.WJLBM=w.WJLBM"
  402. + " where w.WJSJ>=:start and w.WJSJ<:end"
  403. + " and w.WJLBM>=20 and w.JLZTM=51)"
  404. + " where RN=1 group by BJID");
  405. violationCmd.wrTimeParm("start", start);
  406. violationCmd.wrTimeParm("end", end);
  407. Map<Long, Double> violations = new HashMap<>();
  408. for (Map<String, Object> row : db.doSelMapListByC(violationCmd)) {
  409. violations.put(rdLong(row.get("bjid"), 0L),
  410. rdDouble(row.get("penalty")));
  411. }
  412. Map<Long, Double> attendance = rdAttendanceScores(db, classes,
  413. start, end);
  414. Map<Long, DormScore> result = new HashMap<>();
  415. for (ClassRow cls : classes) {
  416. Double internalScore = internal.get(cls.bjid);
  417. Double attend = attendance.get(cls.bjid);
  418. if (internalScore == null || attend == null)
  419. continue;
  420. // 学校确认:宿舍评分允许出现负数,因此这里不再截断到 0。
  421. double violationScore =
  422. 100.0 - violations.getOrDefault(cls.bjid, 0.0);
  423. double base = internalScore * 0.35 + attend * 0.15
  424. + violationScore * 0.35;
  425. DormScore score = new DormScore();
  426. score.value = round2(base + base / 85.0 * 10.0);
  427. score.internal = round2(internalScore);
  428. score.attendance = attend;
  429. score.violation = round2(violationScore);
  430. result.put(cls.bjid, score);
  431. }
  432. return result;
  433. }
  434. private Map<Long, AdjustmentTotals> rdAdjustmentTotals(
  435. Db db, YearMonth month) throws Exception {
  436. SqlCmd sqlc = new SqlCmd(
  437. "select BJID,sum(case when JFLX=1 then JFZ else 0 end) BONUS,"
  438. + "sum(case when JFLX=2 then JFZ else 0 end) DEDUCTION,"
  439. + "sum(case when JFLX=1 then 1 else 0 end) BONUS_CNT,"
  440. + "sum(case when JFLX=2 then 1 else 0 end) DEDUCTION_CNT"
  441. + " from XJB_JFJL where NDY=:ndy and JLZTM=51 group by BJID");
  442. sqlc.wrStrParm("ndy", month.format(MONTH_FMT));
  443. Map<Long, AdjustmentTotals> result = new HashMap<>();
  444. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  445. AdjustmentTotals total = new AdjustmentTotals();
  446. total.bonus = Math.min(8.0, rdDouble(row.get("bonus")));
  447. total.deduction = rdDouble(row.get("deduction"));
  448. total.bonusCount = rdInt(row.get("bonus_cnt"));
  449. total.deductionCount = rdInt(row.get("deduction_cnt"));
  450. result.put(rdLong(row.get("bjid"), 0L), total);
  451. }
  452. return result;
  453. }
  454. private Component component(Double value, String detail) {
  455. Component item = new Component();
  456. item.value = value;
  457. item.source = value == null ? SOURCE_MISSING : SOURCE_REAL;
  458. item.detail = detail;
  459. return item;
  460. }
  461. private Component disciplineComponent(DisciplineScore score) {
  462. Component item = new Component();
  463. if (score == null)
  464. return item;
  465. item.value = round2(score.value);
  466. item.source = SOURCE_REAL;
  467. item.detail = "违纪" + score.violations + "人次,登记"
  468. + score.days + "天";
  469. return item;
  470. }
  471. private Component dormComponent(DormScore score) {
  472. Component item = new Component();
  473. if (score == null)
  474. return item;
  475. item.value = score.value;
  476. item.source = SOURCE_REAL;
  477. item.detail = "内务" + score.internal
  478. + ",住宿考勤" + score.attendance
  479. + ",违纪" + score.violation;
  480. return item;
  481. }
  482. private void addMissing(ClassResult item, String label,
  483. Component component) {
  484. if (component == null || component.value == null)
  485. item.missing.add(label);
  486. }
  487. private void assignRanks(List<ClassResult> results) {
  488. List<ClassResult> school = new ArrayList<>(results);
  489. school.sort(Comparator
  490. .comparingDouble((ClassResult item) -> -item.total)
  491. .thenComparingLong(item -> item.cls.bjid));
  492. assignSchoolRanks(school);
  493. Map<Long, List<ClassResult>> departments = new LinkedHashMap<>();
  494. for (ClassResult item : results) {
  495. departments.computeIfAbsent(item.cls.dwid, key ->
  496. new ArrayList<>()).add(item);
  497. }
  498. for (List<ClassResult> group : departments.values()) {
  499. group.sort(Comparator
  500. .comparingDouble((ClassResult item) -> -item.total)
  501. .thenComparingLong(item -> item.cls.bjid));
  502. double previous = Double.NaN;
  503. int rank = 0;
  504. for (int i = 0; i < group.size(); i++) {
  505. ClassResult item = group.get(i);
  506. if (i == 0 || Double.compare(previous, item.total) != 0)
  507. rank = i + 1;
  508. item.departmentRank = rank;
  509. item.departmentClassCount = group.size();
  510. previous = item.total;
  511. }
  512. }
  513. for (ClassResult item : results)
  514. item.star = starLevel(item.total, item.schoolRank);
  515. }
  516. private void assignSchoolRanks(List<ClassResult> sorted) {
  517. double previous = Double.NaN;
  518. int rank = 0;
  519. for (int i = 0; i < sorted.size(); i++) {
  520. ClassResult item = sorted.get(i);
  521. if (i == 0 || Double.compare(previous, item.total) != 0)
  522. rank = i + 1;
  523. item.schoolRank = rank;
  524. item.schoolClassCount = sorted.size();
  525. previous = item.total;
  526. }
  527. }
  528. private String starLevel(double score, int schoolRank) {
  529. if (schoolRank <= 10 && score >= 90.0)
  530. return "五星级";
  531. if (score >= 80.0)
  532. return "四星级";
  533. if (score >= 75.0)
  534. return "三星级";
  535. if (score >= 65.0)
  536. return "二星级";
  537. if (score >= 55.0)
  538. return "一星级";
  539. return "无星级";
  540. }
  541. private boolean hasPublishedBatch(Db db, YearMonth month, long bmid)
  542. throws Exception {
  543. SqlCmd sqlc = new SqlCmd(
  544. "select count(*) CNT from XJB_JD where NDY=:ndy"
  545. + " and FWLX='BM' and FWID=:fwid and ZT=:status");
  546. sqlc.wrStrParm("ndy", month.format(MONTH_FMT));
  547. sqlc.wrLongParm("fwid", bmid);
  548. sqlc.wrIntParm("status", JD_PUBLISHED);
  549. db.doSelByC(sqlc);
  550. return db.rowNum > 0 && db.rdIntCol("cnt") > 0;
  551. }
  552. private void deleteDraftBatch(Db db, YearMonth month, long bmid)
  553. throws Exception {
  554. SqlCmd find = new SqlCmd(
  555. "select XJB_JDID from XJB_JD where NDY=:ndy"
  556. + " and FWLX='BM' and FWID=:fwid and ZT=:status");
  557. find.wrStrParm("ndy", month.format(MONTH_FMT));
  558. find.wrLongParm("fwid", bmid);
  559. find.wrIntParm("status", JD_DRAFT);
  560. List<Map<String, Object>> rows = db.doSelMapListByC(find);
  561. if (rows == null)
  562. return;
  563. for (Map<String, Object> row : rows) {
  564. long jdid = rdLong(row.get("xjb_jdid"), 0L);
  565. deleteByJdId(db, "XJB_PM", jdid);
  566. deleteByJdId(db, "XJB_JG", jdid);
  567. SqlCmd delete = new SqlCmd(
  568. "delete from XJB_JD where XJB_JDID=:id and ZT=:status");
  569. delete.wrLongParm("id", jdid);
  570. delete.wrIntParm("status", JD_DRAFT);
  571. db.doModByC(delete);
  572. }
  573. }
  574. private void deleteByJdId(Db db, String table, long jdid)
  575. throws Exception {
  576. SqlCmd sqlc = new SqlCmd(
  577. "delete from " + table + " where XJB_JDID=:id");
  578. sqlc.wrLongParm("id", jdid);
  579. db.doModByC(sqlc);
  580. }
  581. private long insertBatch(Db db, YearMonth month, long bmid,
  582. UserRunB user) throws Exception {
  583. long id = db.rdNextSeqVal("XJB_JDID");
  584. Timestamp now = new Timestamp(System.currentTimeMillis());
  585. XjbJdPo po = new XjbJdPo();
  586. po.setXjb_jdid(id);
  587. po.setNdy(month.format(MONTH_FMT));
  588. po.setFwlx("BM");
  589. po.setFwid(bmid);
  590. po.setBb(1);
  591. po.setZt(JD_PUBLISHED);
  592. po.setMs("星级班月结");
  593. po.setCjsj(now);
  594. po.setFbsj(now);
  595. po.setJsryid(user.ryid);
  596. po.setJsbmid(user.bmid);
  597. Set<String> fields = new LinkedHashSet<>(Arrays.asList(
  598. "xjb_jdid", "ndy", "fwlx", "fwid", "bb", "zt", "ms",
  599. "cjsj", "fbsj", "jsryid", "jsbmid"));
  600. db.doInsPo(po, fields);
  601. return id;
  602. }
  603. private void insertResult(Db db, long jdid, ClassResult result,
  604. UserRunB user) throws Exception {
  605. XjbJgPo jg = new XjbJgPo();
  606. jg.setXjb_jgid(db.rdNextSeqVal("XJB_JGID"));
  607. jg.setXjb_jdid(jdid);
  608. jg.setNdy(result.ndy);
  609. jg.setBjid(result.cls.bjid);
  610. jg.setDwid(result.cls.dwid);
  611. jg.setZyid(result.cls.zyid);
  612. jg.setSsdf(result.dorm.value.floatValue());
  613. jg.setKtdf(result.classroom.value.floatValue());
  614. jg.setSjdf(result.flag.value.floatValue());
  615. jg.setCsdf(result.cleaning.value.floatValue());
  616. jg.setFjdf(result.discipline.value.floatValue());
  617. jg.setKqdf(result.attendance.value.floatValue());
  618. jg.setZjf((float) Math.min(8.0, result.adjust.bonus));
  619. jg.setKjf((float) result.adjust.deduction);
  620. jg.setZf((float) result.total);
  621. jg.setXj(result.star);
  622. jg.setSsly(result.dorm.source);
  623. jg.setKtly(result.classroom.source);
  624. jg.setSjly(result.flag.source);
  625. jg.setCsly(result.cleaning.source);
  626. jg.setFjly(result.discipline.source);
  627. jg.setKqly(result.attendance.source);
  628. jg.setZjfcs(result.adjust.bonusCount);
  629. jg.setKjfcs(result.adjust.deductionCount);
  630. jg.setQxsm("");
  631. jg.setZt(JD_PUBLISHED);
  632. jg.setJlsj(new Timestamp(System.currentTimeMillis()));
  633. jg.setCjryid(user.ryid);
  634. jg.setGlbmid(user.bmid);
  635. Set<String> jgFields = new LinkedHashSet<>(Arrays.asList(
  636. "xjb_jgid", "xjb_jdid", "ndy", "bjid", "dwid", "zyid",
  637. "ssdf", "ktdf", "sjdf", "csdf", "fjdf", "kqdf",
  638. "zjf", "kjf", "zf", "xj", "ssly", "ktly", "sjly",
  639. "csly", "fjly", "kqly", "zjfcs", "kjfcs", "qxsm",
  640. "zt", "jlsj", "cjryid", "glbmid"));
  641. db.doInsPo(jg, jgFields);
  642. XjbPmPo pm = new XjbPmPo();
  643. pm.setXjb_pmid(db.rdNextSeqVal("XJB_PMID"));
  644. pm.setXjb_jdid(jdid);
  645. pm.setBjid(result.cls.bjid);
  646. pm.setDwid(result.cls.dwid);
  647. pm.setDwpm(result.departmentRank);
  648. pm.setDwcs(result.departmentClassCount);
  649. pm.setXxpm(result.schoolRank);
  650. pm.setXxcs(result.schoolClassCount);
  651. Set<String> pmFields = new LinkedHashSet<>(Arrays.asList(
  652. "xjb_pmid", "xjb_jdid", "bjid", "dwid",
  653. "dwpm", "dwcs", "xxpm", "xxcs"));
  654. db.doInsPo(pm, pmFields);
  655. }
  656. private YearMonth rdLatestPublishedMonth(Db db) throws Exception {
  657. SqlCmd sqlc = new SqlCmd(
  658. "select max(NDY) NDY from XJB_JD where ZT=:status");
  659. sqlc.wrIntParm("status", JD_PUBLISHED);
  660. db.doSelByC(sqlc);
  661. if (db.rowNum == 0)
  662. return null;
  663. String value = db.rdStrCol("ndy");
  664. return StringUtils.isBlank(value) ? null
  665. : YearMonth.parse(value, MONTH_FMT);
  666. }
  667. private static YearMonth rdMonth(String value) {
  668. String text = StringUtils.trimToNull(value);
  669. if (text == null)
  670. throw new IllegalArgumentException("请选择月份");
  671. try {
  672. return YearMonth.parse(text, MONTH_FMT);
  673. } catch (Exception e) {
  674. throw new IllegalArgumentException("月份格式错误");
  675. }
  676. }
  677. private static void addNonBlank(Set<String> target, Object value) {
  678. String text = String.valueOf(value == null ? "" : value).trim();
  679. if (!text.isEmpty())
  680. target.add(text);
  681. }
  682. private static String formatScore(Object value) {
  683. if (value == null)
  684. return "-";
  685. return String.format("%.2f", rdDouble(value));
  686. }
  687. private static String formatTimestamp(Object value) {
  688. if (value instanceof Timestamp timestamp)
  689. return timestamp.toLocalDateTime()
  690. .format(DateTimeFormatter.ofPattern(
  691. "yyyy-MM-dd HH:mm:ss"));
  692. return value == null ? "" : String.valueOf(value);
  693. }
  694. private static LocalDate rdLocalDate(Object value) {
  695. if (value instanceof java.sql.Date date)
  696. return date.toLocalDate();
  697. if (value instanceof Timestamp timestamp)
  698. return timestamp.toLocalDateTime().toLocalDate();
  699. if (value == null)
  700. return null;
  701. String text = String.valueOf(value);
  702. return text.length() < 10 ? null
  703. : LocalDate.parse(text.substring(0, 10));
  704. }
  705. private static long rdLong(Object value, long defaultValue) {
  706. if (value == null)
  707. return defaultValue;
  708. String text = StringUtils.trimToNull(String.valueOf(value));
  709. return text == null ? defaultValue : Long.parseLong(text);
  710. }
  711. private static Long rdLongOrNull(Object value) {
  712. long parsed = rdLong(value, 0L);
  713. return parsed == 0L ? null : parsed;
  714. }
  715. private static int rdInt(Object value) {
  716. return value == null ? 0 : Integer.parseInt(String.valueOf(value));
  717. }
  718. private static double rdDouble(Object value) {
  719. return value == null ? 0.0 : Double.parseDouble(String.valueOf(value));
  720. }
  721. private static String rdStr(Object value) {
  722. return value == null ? "" : String.valueOf(value);
  723. }
  724. private static double round2(double value) {
  725. return Math.round(value * 100.0) / 100.0;
  726. }
  727. private static final class ClassRow {
  728. final long bjid;
  729. final String bjmc;
  730. final Long dwid;
  731. final Long zyid;
  732. ClassRow(long bjid, String bjmc, Long dwid, Long zyid) {
  733. this.bjid = bjid;
  734. this.bjmc = bjmc;
  735. this.dwid = dwid;
  736. this.zyid = zyid;
  737. }
  738. }
  739. private static final class Component {
  740. Double value;
  741. String source = SOURCE_MISSING;
  742. String detail = "";
  743. }
  744. private static final class DisciplineScore {
  745. double value;
  746. int violations;
  747. int days;
  748. }
  749. private static final class DormScore {
  750. double value;
  751. double internal;
  752. double attendance;
  753. double violation;
  754. }
  755. private static final class AdjustmentTotals {
  756. double bonus;
  757. double deduction;
  758. int bonusCount;
  759. int deductionCount;
  760. }
  761. private static final class ClassResult {
  762. final ClassRow cls;
  763. Component dorm;
  764. Component classroom;
  765. Component flag;
  766. Component cleaning;
  767. Component discipline;
  768. Component attendance;
  769. AdjustmentTotals adjust = new AdjustmentTotals();
  770. final List<String> missing = new ArrayList<>();
  771. double total;
  772. String star = "待评分";
  773. int departmentRank;
  774. int departmentClassCount;
  775. int schoolRank;
  776. int schoolClassCount;
  777. String ndy;
  778. ClassResult(ClassRow cls) {
  779. this.cls = cls;
  780. }
  781. }
  782. }