BjStarClassMpServ.java 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093
  1. package biz.bj;
  2. import com.google.gson.Gson;
  3. import jakarta.servlet.http.HttpServletRequest;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.apache.logging.log4j.LogManager;
  6. import org.apache.logging.log4j.Logger;
  7. import ss.db.Db;
  8. import ss.db.SqlCmd;
  9. import ss.obj.ObjC;
  10. import ss.serv.Serv;
  11. import ss.serv.ServU;
  12. import ss.user.UserRunB;
  13. import ss.user.UserU;
  14. import java.io.Writer;
  15. import java.sql.Date;
  16. import java.sql.SQLException;
  17. import java.sql.Timestamp;
  18. import java.time.DayOfWeek;
  19. import java.time.LocalDate;
  20. import java.time.YearMonth;
  21. import java.time.ZoneId;
  22. import java.time.format.DateTimeFormatter;
  23. import java.time.format.DateTimeParseException;
  24. import java.time.temporal.TemporalAdjusters;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.Comparator;
  28. import java.util.HashMap;
  29. import java.util.LinkedHashMap;
  30. import java.util.LinkedHashSet;
  31. import java.util.List;
  32. import java.util.Locale;
  33. import java.util.Map;
  34. import java.util.Set;
  35. /**
  36. * 班主任星级班移动报表。
  37. */
  38. public class BjStarClassMpServ extends Serv {
  39. private static final Logger log4j = LogManager.getLogger(BjStarClassMpServ.class);
  40. private static final ZoneId CN_ZONE = ZoneId.of("Asia/Shanghai");
  41. private static final DateTimeFormatter MONTH_FMT = DateTimeFormatter.ofPattern("yyyy-MM");
  42. private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  43. private static final DateTimeFormatter DATETIME_FMT =
  44. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
  45. private static final String SOURCE_REAL = "real";
  46. private static final String SOURCE_MISSING = "missing";
  47. private static final String ZHIXIN_STAR_MARKER_PREFIX = "ZHIXIN_STAR_";
  48. private static final int DEFAULT_PAGE_SIZE = 20;
  49. private static final int DETAIL_PAGE_SIZE = 30;
  50. private static final List<CategoryDef> CATEGORY_DEFS = List.of(
  51. new CategoryDef("dorm", "宿舍评分", 0.20),
  52. new CategoryDef("classroom", "课堂考核", 0.05),
  53. new CategoryDef("flag", "升旗礼", 0.14),
  54. new CategoryDef("cleaning", "清洁卫生", 0.18),
  55. new CategoryDef("discipline", "风纪仪容", 0.20),
  56. new CategoryDef("attendance", "考勤", 0.15)
  57. );
  58. /**
  59. * 班主任、本人班级和默认月份。
  60. */
  61. @SuppressWarnings("unused")
  62. public void initByA() throws Exception {
  63. HttpServletRequest request = super.req;
  64. prepareJson();
  65. Writer writer = super.resp.getWriter();
  66. Gson gson = new Gson();
  67. try (Db db = Db.makeConn()) {
  68. UserRunB user = requireUser(request);
  69. List<ClassInfo> classes = rdTeacherClasses(db, user.ryid);
  70. if (classes.isEmpty())
  71. throw new IllegalArgumentException("当前账号未关联班主任班级");
  72. List<Map<String, Object>> classList = new ArrayList<>();
  73. for (ClassInfo item : classes) {
  74. Map<String, Object> classRow = new LinkedHashMap<>();
  75. classRow.put("bjid", item.bjid);
  76. classRow.put("bjmc", item.bjmc);
  77. classRow.put("dwid", item.dwid);
  78. classRow.put("dwmc", item.dwmc);
  79. classList.add(classRow);
  80. }
  81. Map<String, Object> data = new LinkedHashMap<>();
  82. data.put("teacherName", user.xm);
  83. data.put("defaultBjid", classes.get(0).bjid);
  84. data.put("defaultMonth", YearMonth.now(CN_ZONE).format(MONTH_FMT));
  85. data.put("classes", classList);
  86. ServU.wrOkDataByWriter(data, writer, gson);
  87. } catch (IllegalArgumentException e) {
  88. ServU.wrErrMsgByWriter(null, e.getMessage(), writer, gson);
  89. } catch (Exception e) {
  90. log4j.error("BjStarClassMpServ.initByA() 异常", e);
  91. ServU.wrErrMsgByWriter(null, "星级班信息加载失败", writer, gson);
  92. }
  93. }
  94. /**
  95. * 月度汇总和每日列表。
  96. */
  97. @SuppressWarnings("unused")
  98. public void summaryByA() throws Exception {
  99. HttpServletRequest request = super.req;
  100. prepareJson();
  101. Writer writer = super.resp.getWriter();
  102. Gson gson = new Gson();
  103. try (Db db = Db.makeConn()) {
  104. UserRunB user = requireUser(request);
  105. ClassInfo selected = requireTeacherClass(db, user.ryid,
  106. rdLongParam(request, "bjid", false));
  107. YearMonth month = rdMonth(request);
  108. MonthPeriod period = MonthPeriod.of(month);
  109. List<ClassInfo> classes = rdActiveClasses(db);
  110. ScoreBook scores = rdScoreBook(db, classes, period);
  111. RankBook ranks = rdRanks(classes, scores);
  112. ClassScore classScore = scores.classScores.get(selected.bjid);
  113. if (classScore == null)
  114. throw new IllegalStateException("班级评分未生成");
  115. PublishedSnapshot snapshot = rdPublishedSnapshot(
  116. db, selected.bjid, month);
  117. int pageNo = Math.max(1, rdIntParam(request, "pageNo", 1));
  118. int pageSize = clampPageSize(rdIntParam(request, "pageSize",
  119. DEFAULT_PAGE_SIZE));
  120. List<DailyAccumulator> allDays = rdDailyList(db, selected, period,
  121. scores.categorySources(selected.bjid));
  122. int from = Math.min(allDays.size(), (pageNo - 1) * pageSize);
  123. int to = Math.min(allDays.size(), from + pageSize);
  124. List<Map<String, Object>> dayRows = new ArrayList<>();
  125. for (int i = from; i < to; i++) {
  126. dayRows.add(allDays.get(i).toMap());
  127. }
  128. Map<String, Object> data = new LinkedHashMap<>();
  129. data.put("bjid", selected.bjid);
  130. data.put("bjmc", selected.bjmc);
  131. data.put("dwid", selected.dwid);
  132. data.put("dwmc", selected.dwmc);
  133. data.put("month", month.format(MONTH_FMT));
  134. if (snapshot != null) {
  135. data.put("score", snapshot.total);
  136. data.put("starLevel", snapshot.starLevel);
  137. data.put("departmentRank", snapshot.departmentRank);
  138. data.put("departmentClassCount", snapshot.departmentClassCount);
  139. data.put("schoolRank", snapshot.schoolRank);
  140. data.put("schoolClassCount", snapshot.schoolClassCount);
  141. data.put("bonusTotal", snapshot.bonus);
  142. data.put("deductionTotal", snapshot.deduction);
  143. data.put("bonusCount", snapshot.bonusCount);
  144. data.put("deductionCount", snapshot.deductionCount);
  145. data.put("rankingPreview", false);
  146. data.put("snapshotPublished", true);
  147. data.put("categorySources", snapshot.sources);
  148. } else {
  149. data.put("score", classScore.hasMissing
  150. ? null : round2(classScore.total));
  151. data.put("starLevel", classScore.hasMissing
  152. ? "待评分" : starLevel(classScore.total,
  153. ranks.schoolRank.getOrDefault(selected.bjid, 0)));
  154. data.put("departmentRank", classScore.hasMissing
  155. ? null : ranks.departmentRank.get(selected.bjid));
  156. data.put("departmentClassCount",
  157. ranks.departmentClassCount.getOrDefault(
  158. selected.dwid, 0));
  159. data.put("schoolRank", classScore.hasMissing
  160. ? null : ranks.schoolRank.get(selected.bjid));
  161. data.put("schoolClassCount", classes.size());
  162. data.put("bonusTotal", round2(classScore.adjust.bonus));
  163. data.put("deductionTotal",
  164. round2(classScore.adjust.deduction));
  165. data.put("bonusCount", classScore.adjust.bonusEvents);
  166. data.put("deductionCount", classScore.adjust.deductionEvents);
  167. data.put("rankingPreview", classScore.hasMissing);
  168. data.put("snapshotPublished", false);
  169. data.put("categorySources",
  170. scores.categorySources(selected.bjid));
  171. data.put("missingItems", classScore.missing);
  172. }
  173. data.put("pageNo", pageNo);
  174. data.put("pageSize", pageSize);
  175. data.put("total", allDays.size());
  176. data.put("hasMore", to < allDays.size());
  177. data.put("days", dayRows);
  178. ServU.wrOkDataByWriter(data, writer, gson);
  179. } catch (IllegalArgumentException e) {
  180. ServU.wrErrMsgByWriter(null, e.getMessage(), writer, gson);
  181. } catch (Exception e) {
  182. log4j.error("BjStarClassMpServ.summaryByA() 异常", e);
  183. ServU.wrErrMsgByWriter(null, "星级班月度数据加载失败", writer, gson);
  184. }
  185. }
  186. /**
  187. * 指定日期的六项得分。
  188. */
  189. @SuppressWarnings("unused")
  190. public void dayByA() throws Exception {
  191. HttpServletRequest request = super.req;
  192. prepareJson();
  193. Writer writer = super.resp.getWriter();
  194. Gson gson = new Gson();
  195. try (Db db = Db.makeConn()) {
  196. UserRunB user = requireUser(request);
  197. ClassInfo selected = requireTeacherClass(db, user.ryid,
  198. rdLongParam(request, "bjid", false));
  199. LocalDate day = rdDate(request, "date");
  200. if (day.isAfter(LocalDate.now(CN_ZONE)))
  201. throw new IllegalArgumentException("不能查询未来日期");
  202. YearMonth month = YearMonth.from(day);
  203. MonthPeriod period = MonthPeriod.of(month);
  204. CategoryDaily daily = rdCategoryDaily(db, selected, day, period);
  205. List<Map<String, Object>> categories = new ArrayList<>();
  206. for (CategoryDef def : CATEGORY_DEFS)
  207. categories.add(daily.toMap(def, day));
  208. Map<String, Object> data = new LinkedHashMap<>();
  209. data.put("bjid", selected.bjid);
  210. data.put("bjmc", selected.bjmc);
  211. data.put("date", day.format(DATE_FMT));
  212. data.put("weekday", weekdayCn(day));
  213. data.put("categories", categories);
  214. data.put("regularDeduction", round2(daily.regularDeduction(day)));
  215. ServU.wrOkDataByWriter(data, writer, gson);
  216. } catch (DateTimeParseException e) {
  217. ServU.wrErrMsgByWriter(null, "日期格式错误", writer, gson);
  218. } catch (IllegalArgumentException e) {
  219. ServU.wrErrMsgByWriter(null, e.getMessage(), writer, gson);
  220. } catch (Exception e) {
  221. log4j.error("BjStarClassMpServ.dayByA() 异常", e);
  222. ServU.wrErrMsgByWriter(null, "单日星级班数据加载失败", writer, gson);
  223. }
  224. }
  225. /**
  226. * 项目扣分明细,或月度增分/减分明细。
  227. */
  228. @SuppressWarnings("unused")
  229. public void detailByA() throws Exception {
  230. writeDetailResponse(false);
  231. }
  232. /**
  233. * PC 已发布月结报表的明细下钻入口。
  234. */
  235. @SuppressWarnings("unused")
  236. public void pcDetailByA() throws Exception {
  237. writeDetailResponse(true);
  238. }
  239. private void writeDetailResponse(boolean pcReport) throws Exception {
  240. HttpServletRequest request = super.req;
  241. prepareJson();
  242. Writer writer = super.resp.getWriter();
  243. Gson gson = new Gson();
  244. try (Db db = Db.makeConn()) {
  245. UserRunB user = requireUser(request);
  246. ClassInfo selected;
  247. YearMonth reportMonth = null;
  248. if (pcReport) {
  249. long bjid = rdLongParam(request, "bjid", true);
  250. long jdid = rdLongParam(request, "jdid", true);
  251. selected = requireReportClass(db, bjid, jdid);
  252. reportMonth = rdReportBatchMonth(db, jdid);
  253. } else {
  254. selected = requireTeacherClass(db, user.ryid,
  255. rdLongParam(request, "bjid", false));
  256. }
  257. Map<String, Object> data = buildDetailData(db, selected,
  258. request, reportMonth);
  259. ServU.wrOkDataByWriter(data, writer, gson);
  260. } catch (DateTimeParseException e) {
  261. ServU.wrErrMsgByWriter(null, "日期格式错误", writer, gson);
  262. } catch (IllegalArgumentException e) {
  263. ServU.wrErrMsgByWriter(null, e.getMessage(), writer, gson);
  264. } catch (Exception e) {
  265. log4j.error("BjStarClassMpServ.writeDetailResponse() 异常,"
  266. + "pcReport=" + pcReport, e);
  267. ServU.wrErrMsgByWriter(null, "星级班明细加载失败", writer, gson);
  268. }
  269. }
  270. private static Map<String, Object> buildDetailData(
  271. Db db, ClassInfo selected, HttpServletRequest request,
  272. YearMonth reportMonth)
  273. throws Exception {
  274. String mode = StringUtils.defaultIfBlank(
  275. request.getParameter("mode"), "dayItem");
  276. if ("trace".equals(mode)) {
  277. String kind = StringUtils.defaultIfBlank(
  278. request.getParameter("kind"), "regular");
  279. long recordId = rdLongParam(request, "id", true);
  280. if ("adjustment".equals(kind))
  281. return rdAdjustmentTrace(db, selected, recordId,
  282. reportMonth);
  283. if ("dormManagement".equals(kind))
  284. return rdDormManagementTrace(db, selected,
  285. rdDate(request, "date"), reportMonth);
  286. LocalDate day = rdDate(request, "date");
  287. if (day.isAfter(LocalDate.now(CN_ZONE)))
  288. throw new IllegalArgumentException("不能查询未来日期");
  289. ensureReportMonth(reportMonth, YearMonth.from(day));
  290. CategoryDef category = requireCategory(StringUtils.trimToNull(
  291. request.getParameter("category")));
  292. String source = StringUtils.defaultIfBlank(
  293. request.getParameter("source"), SOURCE_REAL);
  294. return rdTraceDetail(db, selected, day, category, recordId,
  295. source);
  296. }
  297. int pageNo = Math.max(1, rdIntParam(request, "pageNo", 1));
  298. int pageSize = clampPageSize(rdIntParam(request, "pageSize",
  299. DETAIL_PAGE_SIZE));
  300. List<Map<String, Object>> rows;
  301. String title;
  302. String monthText = null;
  303. String dateText = null;
  304. String categoryCode = null;
  305. String displayType = null;
  306. List<String> columns = null;
  307. if ("dormDaySummary".equals(mode)) {
  308. LocalDate day = rdDate(request, "date");
  309. if (day.isAfter(LocalDate.now(CN_ZONE)))
  310. throw new IllegalArgumentException("不能查询未来日期");
  311. ensureReportMonth(reportMonth, YearMonth.from(day));
  312. rows = rdDormDaySummary(db, selected, day);
  313. title = "宿舍评分 · " + day.format(DATE_FMT);
  314. dateText = day.format(DATE_FMT);
  315. categoryCode = "dorm";
  316. } else if ("dormAttendanceDetail".equals(mode)) {
  317. LocalDate day = rdDate(request, "date");
  318. if (day.isAfter(LocalDate.now(CN_ZONE)))
  319. throw new IllegalArgumentException("不能查询未来日期");
  320. ensureReportMonth(reportMonth, YearMonth.from(day));
  321. CategoryDef category = requireCategory("dorm");
  322. rows = rdAttendanceDetail(db, selected.bjid, day,
  323. XjbDormScoreU.AttendanceScope.BOARDING_STUDENT);
  324. for (Map<String, Object> row : rows) {
  325. row.put("detailKind", "regular");
  326. row.put("sourceType", "dormAttendance");
  327. }
  328. title = "住宿生考勤 · " + day.format(DATE_FMT);
  329. dateText = day.format(DATE_FMT);
  330. categoryCode = category.code;
  331. } else if ("flagDaySummary".equals(mode)) {
  332. LocalDate day = rdDate(request, "date");
  333. if (day.isAfter(LocalDate.now(CN_ZONE)))
  334. throw new IllegalArgumentException("不能查询未来日期");
  335. ensureReportMonth(reportMonth, YearMonth.from(day));
  336. rows = rdFlagDaySummary(db, selected.bjid, day);
  337. title = "升旗礼 · " + day.format(DATE_FMT);
  338. dateText = day.format(DATE_FMT);
  339. categoryCode = "flag";
  340. displayType = "deduction";
  341. columns = List.of("项目", "扣分");
  342. } else if ("flagAttendanceDetail".equals(mode)) {
  343. LocalDate day = rdDate(request, "date");
  344. if (day.isAfter(LocalDate.now(CN_ZONE)))
  345. throw new IllegalArgumentException("不能查询未来日期");
  346. ensureReportMonth(reportMonth, YearMonth.from(day));
  347. rows = rdFlagAttendanceDetail(db, selected.bjid, day);
  348. title = "考勤 · " + day.format(DATE_FMT);
  349. dateText = day.format(DATE_FMT);
  350. categoryCode = "flag";
  351. displayType = "value";
  352. columns = List.of("项目", "数值");
  353. } else if ("flagDemeanorDetail".equals(mode)) {
  354. LocalDate day = rdDate(request, "date");
  355. if (day.isAfter(LocalDate.now(CN_ZONE)))
  356. throw new IllegalArgumentException("不能查询未来日期");
  357. ensureReportMonth(reportMonth, YearMonth.from(day));
  358. rows = rdFlagDemeanorDetail(db, selected.bjid, day);
  359. title = "风貌 · " + day.format(DATE_FMT);
  360. dateText = day.format(DATE_FMT);
  361. categoryCode = "flag";
  362. displayType = "score";
  363. columns = List.of("项目", "得分");
  364. } else if ("dormInternalDetail".equals(mode)) {
  365. LocalDate day = rdDate(request, "date");
  366. if (day.isAfter(LocalDate.now(CN_ZONE)))
  367. throw new IllegalArgumentException("不能查询未来日期");
  368. ensureReportMonth(reportMonth, YearMonth.from(day));
  369. rows = rdDormInternalDetail(db, selected.bjid, day);
  370. title = "宿舍内务 · " + day.format(DATE_FMT);
  371. dateText = day.format(DATE_FMT);
  372. categoryCode = "dorm";
  373. } else if ("dormViolationDetail".equals(mode)) {
  374. LocalDate day = rdDate(request, "date");
  375. if (day.isAfter(LocalDate.now(CN_ZONE)))
  376. throw new IllegalArgumentException("不能查询未来日期");
  377. ensureReportMonth(reportMonth, YearMonth.from(day));
  378. rows = rdDormViolationDetail(db, selected.bjid, day);
  379. title = "宿舍违纪扣分 · " + day.format(DATE_FMT);
  380. dateText = day.format(DATE_FMT);
  381. categoryCode = "dorm";
  382. } else if ("bonus".equals(mode) || "deduction".equals(mode)) {
  383. YearMonth month = rdMonth(request);
  384. ensureReportMonth(reportMonth, month);
  385. List<Adjustment> adjustments = rdAdjustments(
  386. db, selected.bjid, month);
  387. List<Adjustment> selectedAdjustments = new ArrayList<>();
  388. for (Adjustment item : adjustments) {
  389. if ((("bonus".equals(mode) && item.score > 0)
  390. || ("deduction".equals(mode) && item.score < 0)))
  391. selectedAdjustments.add(item);
  392. }
  393. selectedAdjustments.sort(Comparator
  394. .comparing((Adjustment item) -> item.date).reversed()
  395. .thenComparingLong(item -> item.id).reversed());
  396. rows = rdAdjustmentRows(selectedAdjustments);
  397. title = "bonus".equals(mode) ? "当月增分明细" : "当月减分明细";
  398. monthText = month.format(MONTH_FMT);
  399. } else if ("monthCategory".equals(mode)) {
  400. YearMonth month = rdMonth(request);
  401. ensureReportMonth(reportMonth, month);
  402. categoryCode = StringUtils.trimToNull(
  403. request.getParameter("category"));
  404. CategoryDef category = requireCategory(categoryCode);
  405. MonthPeriod period = MonthPeriod.of(month);
  406. rows = rdMonthCategoryRows(db, selected, period, category);
  407. title = category.label + " · " + month.format(MONTH_FMT);
  408. monthText = month.format(MONTH_FMT);
  409. } else if ("dayItem".equals(mode)) {
  410. LocalDate day = rdDate(request, "date");
  411. if (day.isAfter(LocalDate.now(CN_ZONE)))
  412. throw new IllegalArgumentException("不能查询未来日期");
  413. ensureReportMonth(reportMonth, YearMonth.from(day));
  414. categoryCode = StringUtils.trimToNull(
  415. request.getParameter("category"));
  416. CategoryDef category = requireCategory(categoryCode);
  417. YearMonth month = YearMonth.from(day);
  418. MonthPeriod period = MonthPeriod.of(month);
  419. rows = rdDayDetail(db, selected, day, period, category);
  420. for (Map<String, Object> row : rows) {
  421. row.put("detailKind", "regular");
  422. if (!row.containsKey("sourceType"))
  423. row.put("sourceType", category.code);
  424. }
  425. title = category.label + " · " + day.format(DATE_FMT);
  426. dateText = day.format(DATE_FMT);
  427. } else {
  428. throw new IllegalArgumentException("明细模式参数错误");
  429. }
  430. int from = Math.min(rows.size(), (pageNo - 1) * pageSize);
  431. int to = Math.min(rows.size(), from + pageSize);
  432. List<Map<String, Object>> pageRows = rows.subList(from, to);
  433. Map<String, Object> data = new LinkedHashMap<>();
  434. data.put("bjid", selected.bjid);
  435. data.put("bjmc", selected.bjmc);
  436. data.put("mode", mode);
  437. data.put("title", title);
  438. data.put("month", monthText);
  439. data.put("date", dateText);
  440. data.put("category", categoryCode);
  441. data.put("displayType", displayType);
  442. data.put("columns", columns);
  443. data.put("pageNo", pageNo);
  444. data.put("pageSize", pageSize);
  445. data.put("total", rows.size());
  446. data.put("hasMore", to < rows.size());
  447. data.put("list", pageRows);
  448. return data;
  449. }
  450. private void prepareJson() throws Exception {
  451. super.resp.setCharacterEncoding("UTF-8");
  452. super.resp.setContentType("application/json;charset=UTF-8");
  453. }
  454. private static UserRunB requireUser(HttpServletRequest request) {
  455. UserRunB user = UserU.rdUserByReq(request);
  456. if (user == null)
  457. throw new IllegalArgumentException("登录状态已失效,请重新进入");
  458. return user;
  459. }
  460. private static List<ClassInfo> rdTeacherClasses(Db db, long ryid)
  461. throws Exception {
  462. SqlCmd sqlc = new SqlCmd(
  463. "select b.BJID,b.MC,z.FZBMID DWID,nvl(bm.MC,'') DWMC"
  464. + " from BJ b inner join ZY z on z.ZYID=b.ZYID"
  465. + " left join BM bm on bm.BMID=z.FZBMID"
  466. + " where b.BZRID=:ryid and b." + ObjC.JLZT_useStr
  467. + " and b.BJZTM!=81 order by b.MC,b.BJID");
  468. sqlc.wrLongParm("ryid", ryid);
  469. return rdClasses(db.doSelMapListByC(sqlc));
  470. }
  471. private static List<ClassInfo> rdActiveClasses(Db db) throws Exception {
  472. SqlCmd sqlc = new SqlCmd(
  473. "select b.BJID,b.MC,z.FZBMID DWID,nvl(bm.MC,'') DWMC"
  474. + " from BJ b inner join ZY z on z.ZYID=b.ZYID"
  475. + " left join BM bm on bm.BMID=z.FZBMID"
  476. + " where b." + ObjC.JLZT_useStr
  477. + " and b.BJZTM!=81 order by b.BJID");
  478. return rdClasses(db.doSelMapListByC(sqlc));
  479. }
  480. private static List<ClassInfo> rdClasses(List<Map<String, Object>> rows) {
  481. List<ClassInfo> list = new ArrayList<>();
  482. for (Map<String, Object> row : rows)
  483. list.add(new ClassInfo(rdLong(row.get("bjid")),
  484. rdStr(row.get("mc")), rdLongObj(row.get("dwid")),
  485. rdStr(row.get("dwmc"))));
  486. return list;
  487. }
  488. private static ClassInfo requireTeacherClass(Db db, long ryid, long bjid)
  489. throws Exception {
  490. List<ClassInfo> classes = rdTeacherClasses(db, ryid);
  491. if (classes.isEmpty())
  492. throw new IllegalArgumentException("当前账号未关联班主任班级");
  493. if (bjid <= 0)
  494. return classes.get(0);
  495. for (ClassInfo item : classes) {
  496. if (item.bjid == bjid)
  497. return item;
  498. }
  499. throw new IllegalArgumentException("无权查看该班级");
  500. }
  501. private static ClassInfo requireReportClass(Db db, long bjid, long jdid)
  502. throws Exception {
  503. SqlCmd sqlc = new SqlCmd(
  504. "select b.BJID,b.MC,z.FZBMID DWID,nvl(bm.MC,'') DWMC"
  505. + " from BJ b inner join ZY z on z.ZYID=b.ZYID"
  506. + " left join BM bm on bm.BMID=z.FZBMID"
  507. + " where b.BJID=:bjid and b." + ObjC.JLZT_useStr
  508. + " and b.BJZTM!=81");
  509. sqlc.wrLongParm("bjid", bjid);
  510. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  511. if (rows == null || rows.isEmpty())
  512. throw new IllegalArgumentException("班级不存在或已停用");
  513. if (jdid > 0) {
  514. SqlCmd check = new SqlCmd(
  515. "select count(*) CNT from XJB_JG g"
  516. + " inner join XJB_JD d on d.XJB_JDID=g.XJB_JDID"
  517. + " where g.XJB_JDID=:jdid and g.BJID=:bjid"
  518. + " and g.ZT=5 and d.ZT=5");
  519. check.wrLongParm("jdid", jdid);
  520. check.wrLongParm("bjid", bjid);
  521. db.doSelByC(check);
  522. if (db.rowNum == 0 || db.rdIntCol("cnt") == 0)
  523. throw new IllegalArgumentException("该班级不属于当前已发布月结");
  524. }
  525. return rdClasses(rows).get(0);
  526. }
  527. private static YearMonth rdReportBatchMonth(Db db, long jdid)
  528. throws Exception {
  529. SqlCmd sqlc = new SqlCmd(
  530. "select NDY from XJB_JD where XJB_JDID=:jdid and ZT=5");
  531. sqlc.wrLongParm("jdid", jdid);
  532. db.doSelByC(sqlc);
  533. if (db.rowNum == 0)
  534. throw new IllegalArgumentException("月结批次不存在或未发布");
  535. return YearMonth.parse(db.rdStrCol("ndy"), MONTH_FMT);
  536. }
  537. private static void ensureReportMonth(YearMonth reportMonth,
  538. YearMonth requestedMonth) {
  539. if (reportMonth != null && !reportMonth.equals(requestedMonth))
  540. throw new IllegalArgumentException("明细月份不属于当前月结批次");
  541. }
  542. private static ScoreBook rdScoreBook(Db db, List<ClassInfo> classes,
  543. MonthPeriod period) throws Exception {
  544. ScoreBook book = new ScoreBook();
  545. book.cleaning = rdAvgScoreByClass(db, "CSWSJC", period);
  546. book.flag = rdFlagScoreByClass(db, period);
  547. book.classroom = rdAvgKtjlScoreByClass(db, period);
  548. Set<LocalDate> schoolDays = rdSchoolDays(db, period);
  549. Map<Long, Map<LocalDate, Double>> attendancePenalty =
  550. rdAttendancePenalty(db, period,
  551. XjbDormScoreU.AttendanceScope.DAY_STUDENT);
  552. book.attendance = computeAttendanceScores(
  553. classes, schoolDays, attendancePenalty);
  554. Map<Long, Map<LocalDate, Double>> dormAttendancePenalty =
  555. rdAttendancePenalty(db, period,
  556. XjbDormScoreU.AttendanceScope.BOARDING_STUDENT);
  557. book.dormAttendance = computeAttendanceScores(
  558. classes, schoolDays, dormAttendancePenalty);
  559. book.dorm = rdDormScores(db, classes, period, book.dormAttendance);
  560. book.discipline = rdDisciplineScores(db, classes, schoolDays, period);
  561. book.adjustments = rdAdjustmentTotals(db, period.month);
  562. for (ClassInfo cls : classes) {
  563. ClassScore score = new ClassScore();
  564. putScore(score, "dorm", "宿舍评分", book.dorm.get(cls.bjid));
  565. putScore(score, "classroom", "课堂考核",
  566. book.classroom.get(cls.bjid));
  567. putScore(score, "flag", "升旗礼", book.flag.get(cls.bjid));
  568. putScore(score, "cleaning", "清洁卫生",
  569. book.cleaning.get(cls.bjid));
  570. putScore(score, "discipline", "风纪仪容",
  571. book.discipline.get(cls.bjid));
  572. putScore(score, "attendance", "考勤",
  573. book.attendance.get(cls.bjid));
  574. score.adjust = book.adjustments.getOrDefault(
  575. cls.bjid, new AdjustmentTotals());
  576. if (score.missing.isEmpty()) {
  577. double weighted = 0.0;
  578. for (CategoryDef def : CATEGORY_DEFS)
  579. weighted += score.values.get(def.code) * def.weight;
  580. score.total = round2(weighted
  581. + Math.min(8.0, score.adjust.bonus)
  582. - score.adjust.deduction);
  583. }
  584. score.hasMissing = !score.missing.isEmpty();
  585. book.classScores.put(cls.bjid, score);
  586. }
  587. return book;
  588. }
  589. private static void putScore(ClassScore score, String code, String label,
  590. Double value) {
  591. if (value == null) {
  592. score.values.put(code, 0.0);
  593. score.sources.put(code, SOURCE_MISSING);
  594. score.missing.add(label);
  595. return;
  596. }
  597. score.values.put(code, round2(value));
  598. score.sources.put(code, SOURCE_REAL);
  599. }
  600. private static Map<Long, Double> rdAvgScoreByClass(Db db, String table,
  601. MonthPeriod period)
  602. throws Exception {
  603. if (!"SSNWJC".equals(table) && !"CSWSJC".equals(table))
  604. throw new IllegalArgumentException("评分表参数错误");
  605. SqlCmd sqlc = new SqlCmd(
  606. "select BJID,avg(FS) SCORE from " + table
  607. + " where JCSJ>=:start and JCSJ<:end and "
  608. + ObjC.JLZT_useStr + " group by BJID");
  609. sqlc.wrTimeParm("start", period.start);
  610. sqlc.wrTimeParm("end", period.end);
  611. return rdScoreMap(db.doSelMapListByC(sqlc));
  612. }
  613. private static Map<Long, Double> rdDormScores(
  614. Db db, List<ClassInfo> classes, MonthPeriod period,
  615. Map<Long, Double> attendanceScores) throws Exception {
  616. // 学校确认:宿舍内务不再要求上午、下午各有一条,所有有效检查取平均。
  617. SqlCmd internalCmd = new SqlCmd(
  618. "select BJID,avg(FS) SCORE from SSNWJC"
  619. + " where JCSJ>=:start and JCSJ<:end"
  620. + " and " + ObjC.JLZT_useStr + " group by BJID");
  621. internalCmd.wrTimeParm("start", period.start);
  622. internalCmd.wrTimeParm("end", period.end);
  623. Map<Long, Double> internal = new HashMap<>();
  624. for (Map<String, Object> row : db.doSelMapListByC(internalCmd))
  625. internal.put(rdLong(row.get("bjid")), rdDouble(row.get("score")));
  626. SqlCmd violationCmd = new SqlCmd(
  627. "select BJID,sum(PENALTY) PENALTY from ("
  628. + XjbWjjlU.dedupEventSql()
  629. + " where WJSJ>=:start and WJSJ<:end)"
  630. + " where RN=1 group by BJID");
  631. violationCmd.wrTimeParm("start", period.start);
  632. violationCmd.wrTimeParm("end", period.end);
  633. Map<Long, Double> violations = new HashMap<>();
  634. for (Map<String, Object> row : db.doSelMapListByC(violationCmd))
  635. violations.put(rdLong(row.get("bjid")),
  636. rdDouble(row.get("penalty")));
  637. Map<Long, Double> result = new HashMap<>();
  638. for (ClassInfo cls : classes) {
  639. Double internalScore = internal.get(cls.bjid);
  640. Double attendance = attendanceScores.get(cls.bjid);
  641. if (internalScore == null || attendance == null)
  642. continue;
  643. // 学校确认:宿舍评分允许出现负数。
  644. double violationScore =
  645. 100.0 - violations.getOrDefault(cls.bjid, 0.0);
  646. double base = internalScore * 0.35 + attendance * 0.15
  647. + violationScore * 0.35;
  648. XjbDormScoreU.ManagementSummary management =
  649. XjbDormScoreU.rdManagementSummary(
  650. db, cls.bjid, period.start, period.end);
  651. result.put(cls.bjid, round2(base + base / 85.0 * 10.0
  652. + management.bonus));
  653. }
  654. return result;
  655. }
  656. private static Map<Long, Double> rdDisciplineScores(
  657. Db db, List<ClassInfo> classes, Set<LocalDate> schoolDays,
  658. MonthPeriod period) throws Exception {
  659. Map<Long, Double> result = new HashMap<>();
  660. if (schoolDays.isEmpty())
  661. return result;
  662. DisciplinePenalties penalties = rdDisciplinePenalties(
  663. db, null, period.start, period.end);
  664. for (ClassInfo cls : classes) {
  665. Map<LocalDate, Double> mapped = penalties.mapped.getOrDefault(
  666. cls.bjid, Collections.emptyMap());
  667. Map<LocalDate, Double> legacy = penalties.legacy.getOrDefault(
  668. cls.bjid, Collections.emptyMap());
  669. double total = 0.0;
  670. for (LocalDate day : schoolDays)
  671. total += Math.max(0.0, 100.0
  672. - disciplinePenalty(mapped, legacy, day));
  673. result.put(cls.bjid, round2(total / schoolDays.size()));
  674. }
  675. return result;
  676. }
  677. private static DisciplinePenalties rdDisciplinePenalties(
  678. Db db, Long bjid, Timestamp start, Timestamp end) throws Exception {
  679. DisciplinePenalties result = new DisciplinePenalties();
  680. String bjFilter = bjid == null ? "" : " and w.BJID=:bjid";
  681. SqlCmd current = new SqlCmd(
  682. "select w.BJID,trunc(w.WJSJ) D,sum(nvl(lb.BJKF,0)) PENALTY"
  683. + " from WJJL w"
  684. + " inner join WJLB lb on lb.WJLBM=w.WJLBM"
  685. + " inner join XJB_WJLBMAP wm on wm.WJLBM=w.WJLBM"
  686. + " and wm.YTM=" + XjbWjlbC.YTM_DISCIPLINE
  687. + " and wm.JLZTM=51"
  688. + " where w.WJSJ>=:start and w.WJSJ<:end"
  689. + " and w.JLZTM=51" + bjFilter
  690. + " group by w.BJID,trunc(w.WJSJ)");
  691. current.wrTimeParm("start", start);
  692. current.wrTimeParm("end", end);
  693. if (bjid != null)
  694. current.wrLongParm("bjid", bjid);
  695. for (Map<String, Object> row : db.doSelMapListByC(current)) {
  696. long classId = rdLong(row.get("bjid"));
  697. LocalDate day = rdLocalDate(row.get("d"));
  698. if (day != null)
  699. result.mapped.computeIfAbsent(classId, key -> new HashMap<>())
  700. .put(day, rdDouble(row.get("penalty")));
  701. }
  702. SqlCmd legacy = new SqlCmd(
  703. "select BJID,JCRQ D,sum(case when MS like :importMarker"
  704. + " then nvl(WJRS,0)/10 else nvl(WJRS,0) end) PENALTY"
  705. + " from XJB_FJRY"
  706. + " where JCRQ>=:start and JCRQ<:end and JLZTM=51"
  707. + (bjid == null ? "" : " and BJID=:bjid")
  708. + " group by BJID,JCRQ");
  709. legacy.wrStrParm("importMarker", ZHIXIN_STAR_MARKER_PREFIX + "%");
  710. legacy.wrTimeParm("start", start);
  711. legacy.wrTimeParm("end", end);
  712. if (bjid != null)
  713. legacy.wrLongParm("bjid", bjid);
  714. for (Map<String, Object> row : db.doSelMapListByC(legacy)) {
  715. long classId = rdLong(row.get("bjid"));
  716. LocalDate day = rdLocalDate(row.get("d"));
  717. if (day != null)
  718. result.legacy.computeIfAbsent(classId, key -> new HashMap<>())
  719. .put(day, rdDouble(row.get("penalty")));
  720. }
  721. return result;
  722. }
  723. private static double disciplinePenalty(
  724. Map<LocalDate, Double> mapped, Map<LocalDate, Double> legacy,
  725. LocalDate day) {
  726. Double penalty = mapped.get(day);
  727. if (penalty == null)
  728. penalty = legacy.get(day);
  729. return penalty == null ? 0.0 : penalty;
  730. }
  731. private static Map<Long, Double> rdFlagScoreByClass(Db db,
  732. MonthPeriod period)
  733. throws Exception {
  734. SqlCmd sqlc = new SqlCmd(
  735. "select s.BJID,avg(s.ZFS) SCORE from SQLJC s"
  736. + " where s.JCSJ>=:start and s.JCSJ<:end"
  737. + " and s.ZFS between 0 and 100 and s."
  738. + ObjC.JLZT_useStr + " group by s.BJID");
  739. sqlc.wrTimeParm("start", period.start);
  740. sqlc.wrTimeParm("end", period.end);
  741. return rdScoreMap(db.doSelMapListByC(sqlc));
  742. }
  743. private static Map<Long, Double> rdAvgKtjlScoreByClass(
  744. Db db, MonthPeriod period) throws Exception {
  745. try {
  746. SqlCmd sqlc = new SqlCmd(
  747. "select j.BJID,avg(j.FS) SCORE from KTJL j"
  748. + " inner join KCB k on k.KCBID=j.KCBID"
  749. + " where k.KSSJ>=:start and k.KSSJ<:end"
  750. + " and j." + ObjC.JLZT_useStr
  751. + " and k." + ObjC.JLZT_useStr
  752. + " group by j.BJID");
  753. sqlc.wrTimeParm("start", period.start);
  754. sqlc.wrTimeParm("end", period.end);
  755. return rdScoreMap(db.doSelMapListByC(sqlc));
  756. } catch (SQLException e) {
  757. if (isMissingTable(e, "KTJL"))
  758. return Collections.emptyMap();
  759. throw e;
  760. }
  761. }
  762. private static Set<LocalDate> rdSchoolDays(Db db, MonthPeriod period)
  763. throws Exception {
  764. SqlCmd sqlc = new SqlCmd(
  765. "select distinct trunc(KSSJ) D from KQJL"
  766. + " where KSSJ>=:start and KSSJ<:end"
  767. + " and KQJLZTM=51 and " + ObjC.JLZT_useStr);
  768. sqlc.wrTimeParm("start", period.start);
  769. sqlc.wrTimeParm("end", period.end);
  770. Set<LocalDate> days = new LinkedHashSet<>();
  771. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  772. LocalDate day = rdLocalDate(row.get("d"));
  773. if (day != null && !day.isAfter(period.maxDate))
  774. days.add(day);
  775. }
  776. return days;
  777. }
  778. private static Map<Long, Map<LocalDate, Double>> rdAttendancePenalty(
  779. Db db, MonthPeriod period,
  780. XjbDormScoreU.AttendanceScope scope) throws Exception {
  781. Map<Long, Map<LocalDate, Double>> result =
  782. XjbDormScoreU.rdAttendancePenalty(
  783. db, period.start, period.end, scope);
  784. for (Map<LocalDate, Double> values : result.values())
  785. values.keySet().removeIf(day -> day.isAfter(period.maxDate));
  786. return result;
  787. }
  788. private static Map<Long, Double> computeAttendanceScores(
  789. List<ClassInfo> classes, Set<LocalDate> schoolDays,
  790. Map<Long, Map<LocalDate, Double>> penalties) {
  791. Map<Long, Double> result = new HashMap<>();
  792. if (schoolDays.isEmpty())
  793. return result;
  794. List<LocalDate> days = new ArrayList<>(schoolDays);
  795. for (ClassInfo cls : classes) {
  796. Map<LocalDate, Double> classPenalties = penalties.getOrDefault(
  797. cls.bjid, Collections.emptyMap());
  798. double total = 0.0;
  799. for (LocalDate day : days) {
  800. double penalty = classPenalties.getOrDefault(day, 0.0);
  801. total += Math.max(0.0, 100.0 - penalty);
  802. }
  803. result.put(cls.bjid, round2(total / days.size()));
  804. }
  805. return result;
  806. }
  807. private static RankBook rdRanks(List<ClassInfo> classes, ScoreBook book) {
  808. RankBook ranks = new RankBook();
  809. List<ClassInfo> school = new ArrayList<>();
  810. for (ClassInfo cls : classes) {
  811. ClassScore score = book.classScores.get(cls.bjid);
  812. if (score != null && !score.hasMissing)
  813. school.add(cls);
  814. }
  815. school.sort(Comparator
  816. .comparingDouble((ClassInfo cls) ->
  817. -book.classScores.get(cls.bjid).total)
  818. .thenComparingLong(cls -> cls.bjid));
  819. assignRanks(school, book, ranks.schoolRank, "school");
  820. Map<Long, List<ClassInfo>> departments = new LinkedHashMap<>();
  821. for (ClassInfo cls : school)
  822. departments.computeIfAbsent(cls.dwid, key -> new ArrayList<>()).add(cls);
  823. for (Map.Entry<Long, List<ClassInfo>> entry : departments.entrySet()) {
  824. entry.getValue().sort(Comparator
  825. .comparingDouble((ClassInfo cls) ->
  826. -book.classScores.get(cls.bjid).total)
  827. .thenComparingLong(cls -> cls.bjid));
  828. assignRanks(entry.getValue(), book, ranks.departmentRank, "department");
  829. ranks.departmentClassCount.put(entry.getKey(), entry.getValue().size());
  830. }
  831. return ranks;
  832. }
  833. private static void assignRanks(List<ClassInfo> sorted, ScoreBook book,
  834. Map<Long, Integer> rankMap, String type) {
  835. double prev = Double.NaN;
  836. int rank = 0;
  837. for (int i = 0; i < sorted.size(); i++) {
  838. ClassInfo cls = sorted.get(i);
  839. double score = book.classScores.get(cls.bjid).total;
  840. if (i == 0 || Double.compare(prev, score) != 0)
  841. rank = i + 1;
  842. rankMap.put(cls.bjid, rank);
  843. prev = score;
  844. }
  845. }
  846. private static List<DailyAccumulator> rdDailyList(
  847. Db db, ClassInfo cls, MonthPeriod period,
  848. Map<String, String> sources) throws Exception {
  849. CategoryDaily daily = rdCategoryDaily(db, cls, null, period);
  850. Map<LocalDate, DailyAccumulator> days = new HashMap<>();
  851. for (CategoryDef def : CATEGORY_DEFS) {
  852. String source = sources.getOrDefault(def.code, SOURCE_MISSING);
  853. Map<LocalDate, DailyPoint> actual = daily.actual.get(def.code);
  854. for (LocalDate date = period.month.atDay(1);
  855. !date.isAfter(period.maxDate);
  856. date = date.plusDays(1)) {
  857. DailyPoint point = null;
  858. if (SOURCE_REAL.equals(source)) {
  859. point = actual == null ? null : actual.get(date);
  860. }
  861. if (point == null)
  862. continue;
  863. DailyAccumulator acc = days.computeIfAbsent(date,
  864. DailyAccumulator::new);
  865. acc.regularDeduction += Math.max(0.0, 100.0 - point.score);
  866. acc.hasRegular = true;
  867. }
  868. }
  869. List<DailyAccumulator> result = new ArrayList<>(days.values());
  870. result.sort(Comparator.comparing(
  871. (DailyAccumulator item) -> item.date).reversed());
  872. return result;
  873. }
  874. private static CategoryDaily rdCategoryDaily(Db db, ClassInfo cls,
  875. LocalDate onlyDay,
  876. MonthPeriod period)
  877. throws Exception {
  878. CategoryDaily result = new CategoryDaily(cls);
  879. Timestamp start = onlyDay == null ? period.start
  880. : Timestamp.valueOf(onlyDay.atStartOfDay());
  881. Timestamp end = onlyDay == null ? period.end
  882. : Timestamp.valueOf(onlyDay.plusDays(1).atStartOfDay());
  883. result.actual.put("dorm", rdDailyDormScore(db, cls,
  884. onlyDay, period));
  885. result.actual.put("cleaning", rdDailyAvgScore(db, "CSWSJC",
  886. cls.bjid, start, end));
  887. result.actual.put("flag", rdDailyFlagScore(db, cls.bjid, start, end));
  888. result.actual.put("classroom", rdDailyKtjlScore(
  889. db, cls.bjid, start, end));
  890. result.actual.put("attendance", rdDailyAttendanceScore(db,
  891. cls.bjid, start, end,
  892. XjbDormScoreU.AttendanceScope.DAY_STUDENT));
  893. result.actual.put("discipline", rdDailyDisciplineScore(
  894. db, cls.bjid, start, end));
  895. MonthPeriod fullMonth = MonthPeriod.of(period.month);
  896. Map<Long, Double> monthClean = rdAvgScoreByClass(db, "CSWSJC", fullMonth);
  897. Map<Long, Double> monthFlag = rdFlagScoreByClass(db, fullMonth);
  898. Set<LocalDate> monthSchoolDays = rdSchoolDays(db, fullMonth);
  899. Map<Long, Double> monthAttendance = computeAttendanceScores(
  900. List.of(cls), monthSchoolDays,
  901. rdAttendancePenalty(db, fullMonth,
  902. XjbDormScoreU.AttendanceScope.DAY_STUDENT));
  903. Map<Long, Double> monthDormAttendance = computeAttendanceScores(
  904. List.of(cls), monthSchoolDays,
  905. rdAttendancePenalty(db, fullMonth,
  906. XjbDormScoreU.AttendanceScope.BOARDING_STUDENT));
  907. Map<Long, Double> monthDorm = rdDormScores(db, List.of(cls),
  908. fullMonth, monthDormAttendance);
  909. Map<Long, Double> monthClassroom =
  910. rdAvgKtjlScoreByClass(db, fullMonth);
  911. Map<Long, Double> monthDiscipline =
  912. rdDisciplineScores(db, List.of(cls), monthSchoolDays,
  913. fullMonth);
  914. result.sources.put("dorm", monthDorm.containsKey(cls.bjid)
  915. ? SOURCE_REAL : SOURCE_MISSING);
  916. result.sources.put("classroom",
  917. monthClassroom.containsKey(cls.bjid)
  918. ? SOURCE_REAL : SOURCE_MISSING);
  919. result.sources.put("flag", monthFlag.containsKey(cls.bjid)
  920. ? SOURCE_REAL : SOURCE_MISSING);
  921. result.sources.put("cleaning", monthClean.containsKey(cls.bjid)
  922. ? SOURCE_REAL : SOURCE_MISSING);
  923. result.sources.put("discipline",
  924. monthDiscipline.containsKey(cls.bjid)
  925. ? SOURCE_REAL : SOURCE_MISSING);
  926. result.sources.put("attendance", monthAttendance.containsKey(cls.bjid)
  927. ? SOURCE_REAL : SOURCE_MISSING);
  928. return result;
  929. }
  930. private static Map<LocalDate, DailyPoint> rdDailyAvgScore(
  931. Db db, String table, long bjid, Timestamp start, Timestamp end)
  932. throws Exception {
  933. SqlCmd sqlc = new SqlCmd(
  934. "select trunc(JCSJ) D,avg(FS) SCORE,count(*) CNT from "
  935. + table + " where BJID=:bjid and JCSJ>=:start"
  936. + " and JCSJ<:end and " + ObjC.JLZT_useStr
  937. + " group by trunc(JCSJ)");
  938. sqlc.wrLongParm("bjid", bjid);
  939. sqlc.wrTimeParm("start", start);
  940. sqlc.wrTimeParm("end", end);
  941. return rdDailyPoints(db.doSelMapListByC(sqlc));
  942. }
  943. private static Map<LocalDate, DailyPoint> rdDailyDormScore(
  944. Db db, ClassInfo cls, LocalDate onlyDay, MonthPeriod period)
  945. throws Exception {
  946. Timestamp start = onlyDay == null ? period.start
  947. : Timestamp.valueOf(onlyDay.atStartOfDay());
  948. Timestamp end = onlyDay == null ? period.end
  949. : Timestamp.valueOf(onlyDay.plusDays(1).atStartOfDay());
  950. // 学校确认:单日宿舍内务按当天所有有效记录取平均,不区分上午/下午。
  951. SqlCmd itemCmd = new SqlCmd(
  952. "select trunc(JCSJ) D,avg(FS) SCORE,count(*) CNT"
  953. + " from SSNWJC where BJID=:bjid and JCSJ>=:start"
  954. + " and JCSJ<:end and " + ObjC.JLZT_useStr
  955. + " group by trunc(JCSJ)");
  956. itemCmd.wrLongParm("bjid", cls.bjid);
  957. itemCmd.wrTimeParm("start", start);
  958. itemCmd.wrTimeParm("end", end);
  959. Map<LocalDate, DailyPoint> internal = rdDailyPoints(
  960. db.doSelMapListByC(itemCmd));
  961. Map<LocalDate, Double> violations =
  962. rdDormViolationPenalties(db, cls.bjid, start, end);
  963. Map<Long, Map<LocalDate, Double>> attendancePenalties =
  964. XjbDormScoreU.rdAttendancePenalty(
  965. db, start, end,
  966. XjbDormScoreU.AttendanceScope.BOARDING_STUDENT);
  967. Map<LocalDate, Double> classAttendancePenalty =
  968. attendancePenalties.getOrDefault(
  969. cls.bjid, Collections.emptyMap());
  970. Map<LocalDate, DailyPoint> result = new HashMap<>();
  971. for (Map.Entry<LocalDate, DailyPoint> entry : internal.entrySet()) {
  972. DailyPoint internalPoint = entry.getValue();
  973. double attendanceScore = Math.max(0.0, 100.0
  974. - classAttendancePenalty.getOrDefault(
  975. entry.getKey(), 0.0));
  976. double violationScore =
  977. 100.0 - violations.getOrDefault(entry.getKey(), 0.0);
  978. double base = internalPoint.score * 0.35
  979. + attendanceScore * 0.15
  980. + violationScore * 0.35;
  981. result.put(entry.getKey(), new DailyPoint(entry.getKey(),
  982. round2(base + base / 85.0 * 10.0),
  983. internalPoint.count));
  984. }
  985. return result;
  986. }
  987. private static List<Map<String, Object>> rdDormDaySummary(
  988. Db db, ClassInfo cls, LocalDate day) throws Exception {
  989. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  990. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  991. MonthPeriod month = MonthPeriod.of(YearMonth.from(day));
  992. Map<LocalDate, DailyPoint> internal =
  993. rdDailyAvgScore(db, "SSNWJC", cls.bjid, start, end);
  994. DailyPoint internalPoint = internal.get(day);
  995. Map<Long, Map<LocalDate, Double>> attendancePenalties =
  996. XjbDormScoreU.rdAttendancePenalty(
  997. db, start, end,
  998. XjbDormScoreU.AttendanceScope.BOARDING_STUDENT);
  999. double attendancePenalty = attendancePenalties
  1000. .getOrDefault(cls.bjid, Collections.emptyMap())
  1001. .getOrDefault(day, 0.0);
  1002. double attendanceScore = Math.max(0.0, 100.0 - attendancePenalty);
  1003. double violationPenalty = rdDormViolationPenalties(
  1004. db, cls.bjid, start, end).getOrDefault(day, 0.0);
  1005. XjbDormScoreU.ManagementSummary management =
  1006. XjbDormScoreU.rdManagementSummary(
  1007. db, cls.bjid, month.start, month.end);
  1008. List<Map<String, Object>> rows = new ArrayList<>();
  1009. rows.add(dormSummaryRow(day, "attendance", "考勤",
  1010. round2(attendanceScore), "实", "住宿生当日考勤",
  1011. true, "attendance"));
  1012. if (internalPoint == null) {
  1013. rows.add(dormSummaryRow(day, "internal", "内务",
  1014. null, "缺", "当日暂无有效内务检查", false, "internal"));
  1015. } else {
  1016. rows.add(dormSummaryRow(day, "internal", "内务",
  1017. round2(internalPoint.score), "实",
  1018. "共" + internalPoint.count + "条内务检查",
  1019. true, "internal"));
  1020. }
  1021. rows.add(dormSummaryRow(day, "violation", "违纪扣分",
  1022. violationPenalty > 0.0 ? -round2(violationPenalty) : 0.0,
  1023. "实",
  1024. violationPenalty > 0
  1025. ? "宿舍违纪已在基础分中扣减"
  1026. : "当日无宿舍违纪扣分",
  1027. violationPenalty > 0, "violation"));
  1028. double managementScore = management.available
  1029. ? management.bonus : 0.0;
  1030. rows.add(dormSummaryRow(day, "management", "增分",
  1031. managementScore, "实", "",
  1032. management.available, "management"));
  1033. rows.add(dormSummaryRow(day, "deduction", "减分",
  1034. 0.0, "停", "宿舍减分规则暂未启用",
  1035. false, "deduction"));
  1036. return rows;
  1037. }
  1038. private static Map<String, Object> dormSummaryRow(
  1039. LocalDate day, String key, String itemName, Double score,
  1040. String state, String meta, boolean traceable, String detailType) {
  1041. Map<String, Object> row = baseDetail(day, SOURCE_REAL);
  1042. row.put("id", key);
  1043. row.put("sourceId", key);
  1044. row.put("sourceType", "dormDaySummary");
  1045. row.put("detailKind", "dormSummary");
  1046. row.put("category", "dorm");
  1047. row.put("key", key);
  1048. row.put("itemName", itemName);
  1049. row.put("title", itemName);
  1050. row.put("score", score);
  1051. row.put("scoreText", score == null ? "-"
  1052. : String.format(Locale.ROOT, "%.2f", score));
  1053. row.put("state", state);
  1054. row.put("remark", meta);
  1055. row.put("traceable", traceable);
  1056. row.put("detailType", detailType);
  1057. row.put("sortTime", key);
  1058. return row;
  1059. }
  1060. private static Map<LocalDate, Double> rdDormViolationPenalties(
  1061. Db db, long bjid, Timestamp start, Timestamp end)
  1062. throws Exception {
  1063. SqlCmd violationCmd = new SqlCmd(
  1064. "select trunc(WJSJ) D,sum(PENALTY) PENALTY from ("
  1065. + XjbWjjlU.dedupEventSql()
  1066. + " where TARGET_BJID=:bjid"
  1067. + " and WJSJ>=:start and WJSJ<:end)"
  1068. + " where RN=1 group by trunc(WJSJ)");
  1069. violationCmd.wrLongParm("bjid", bjid);
  1070. violationCmd.wrTimeParm("start", start);
  1071. violationCmd.wrTimeParm("end", end);
  1072. Map<LocalDate, Double> result = new HashMap<>();
  1073. for (Map<String, Object> row : db.doSelMapListByC(violationCmd)) {
  1074. LocalDate day = rdLocalDate(row.get("d"));
  1075. if (day != null)
  1076. result.put(day, rdDouble(row.get("penalty")));
  1077. }
  1078. return result;
  1079. }
  1080. private static Map<String, Object> rdDormManagementTrace(
  1081. Db db, ClassInfo cls, LocalDate day, YearMonth reportMonth)
  1082. throws Exception {
  1083. YearMonth month = reportMonth == null
  1084. ? YearMonth.from(day) : reportMonth;
  1085. MonthPeriod period = MonthPeriod.of(month);
  1086. XjbDormScoreU.ManagementSummary summary =
  1087. XjbDormScoreU.rdManagementSummary(
  1088. db, cls.bjid, period.start, period.end);
  1089. Map<String, Object> record = new LinkedHashMap<>();
  1090. record.put("time", day.format(DATE_FMT));
  1091. record.put("className", cls.bjmc);
  1092. record.put("teacherName", summary.teacherName);
  1093. record.put("visitCount", summary.visitCount);
  1094. record.put("bonus", summary.bonus);
  1095. record.put("description", summary.message);
  1096. record.put("source", summary.source);
  1097. List<Map<String, Object>> items = new ArrayList<>();
  1098. for (XjbDormScoreU.Visit visit : summary.visits) {
  1099. Map<String, Object> item = new LinkedHashMap<>();
  1100. item.put("itemName", "有效参与");
  1101. item.put("remark", XjbDormScoreU.formatMinute(visit.enterTime)
  1102. + " 至 " + XjbDormScoreU.formatMinute(visit.leaveTime)
  1103. + ",共" + visit.minutes + "分钟"
  1104. + (StringUtils.isBlank(visit.mjdName)
  1105. ? "" : "," + visit.mjdName));
  1106. item.put("deducted", 0.0);
  1107. items.add(item);
  1108. }
  1109. CategoryDef category = requireCategory("dorm");
  1110. return traceResult(cls, day, category, SOURCE_REAL,
  1111. "班主任宿舍管理参与详情", record, items,
  1112. summary.available ? "" : summary.message);
  1113. }
  1114. private static List<Map<String, Object>> rdDormInternalDetail(
  1115. Db db, long bjid, LocalDate day) throws Exception {
  1116. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  1117. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  1118. SqlCmd sqlc = new SqlCmd(
  1119. "select s.SSNWJCID,s.FS,s.MS,s.JCSJ,cd.MC KFMC,"
  1120. + "jc.XM JCRYXM from SSNWJC s"
  1121. + " left join CD cd on cd.CDID=s.KFID"
  1122. + " left join RY jc on jc.RYID=s.JCRYID"
  1123. + " where s.BJID=:bjid and s.JCSJ>=:start"
  1124. + " and s.JCSJ<:end and s." + ObjC.JLZT_useStr
  1125. + " order by s.JCSJ desc,s.SSNWJCID desc");
  1126. sqlc.wrLongParm("bjid", bjid);
  1127. sqlc.wrTimeParm("start", start);
  1128. sqlc.wrTimeParm("end", end);
  1129. List<Map<String, Object>> result = new ArrayList<>();
  1130. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  1131. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1132. long id = rdLong(row.get("ssnwjcid"));
  1133. double score = rdDouble(row.get("fs"));
  1134. item.put("id", id);
  1135. item.put("sourceId", id);
  1136. item.put("category", "dorm");
  1137. item.put("traceable", true);
  1138. item.put("itemName", "内务检查");
  1139. item.put("roomName", rdStr(row.get("kfmc")));
  1140. item.put("title", rdStr(row.get("kfmc")));
  1141. item.put("remark", rdStr(row.get("ms")));
  1142. item.put("scoreText", score + "/100");
  1143. item.put("delta", round2(score - 100.0));
  1144. item.put("sortTime", formatTimestamp(row.get("jcsj")));
  1145. result.add(item);
  1146. }
  1147. return result;
  1148. }
  1149. private static List<Map<String, Object>> rdImportedCleaningSummary(
  1150. Db db, long bjid, LocalDate day) throws Exception {
  1151. SqlCmd sqlc = new SqlCmd(
  1152. "select CSWSJCID,FS,JCSJ from CSWSJC"
  1153. + " where BJID=:bjid"
  1154. + " and trunc(JCSJ)=to_date(:day,'YYYY-MM-DD')"
  1155. + " and MS like :marker and JLZTM=51");
  1156. sqlc.wrLongParm("bjid", bjid);
  1157. sqlc.wrStrParm("day", day.format(DATE_FMT));
  1158. sqlc.wrStrParm("marker", ZHIXIN_STAR_MARKER_PREFIX + "%");
  1159. List<Map<String, Object>> result = new ArrayList<>();
  1160. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  1161. double score = rdDouble(row.get("fs"));
  1162. double deduction = round2(Math.max(0.0, 100.0 - score));
  1163. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1164. item.put("id", rdLong(row.get("cswsjcid")));
  1165. item.put("sourceId", rdLong(row.get("cswsjcid")));
  1166. item.put("category", "cleaning");
  1167. item.put("traceable", false);
  1168. item.put("itemName", "清洁卫生日总分");
  1169. item.put("title", "清洁卫生日总分");
  1170. item.put("remark", "");
  1171. item.put("delta", -deduction);
  1172. item.put("scoreText", plainNumber(score) + "/100");
  1173. item.put("sortTime", formatTimestamp(row.get("jcsj")));
  1174. result.add(item);
  1175. }
  1176. return result;
  1177. }
  1178. private static List<Map<String, Object>> rdDormViolationDetail(
  1179. Db db, long bjid, LocalDate day) throws Exception {
  1180. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  1181. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  1182. SqlCmd sqlc = new SqlCmd(
  1183. "select WJJLID,D,RYXM,WJLBMC,BJKF,WJSJ,MS from ("
  1184. + "select w.WJJLID,trunc(w.WJSJ) D,ry.XM RYXM,"
  1185. + "lb.MC WJLBMC,nvl(lb.BJKF,0) BJKF,w.WJSJ,w.MS,"
  1186. + "row_number() over(partition by w.BJID,w.WJLXM,"
  1187. + "case when w.WJLXM=5 then w.WJLBM end,"
  1188. + "case when w.WJLXM=5 then w.WJSJ end,"
  1189. + "case when w.WJLXM=5 then w.JZWID end,"
  1190. + "case when w.WJLXM=5 then w.KFID end,"
  1191. + "case when w.WJLXM=5 then nvl(w.MS,' ') end,"
  1192. + "case when w.WJLXM=5 then nvl(w.FJID,0) end,"
  1193. + "case when w.WJLXM=5 then 0 else w.WJJLID end"
  1194. + " order by w.WJJLID) RN"
  1195. + " from WJJL w inner join WJLB lb on lb.WJLBM=w.WJLBM"
  1196. + " inner join XJB_WJLBMAP wm on wm.WJLBM=w.WJLBM"
  1197. + " and wm.YTM=" + XjbWjlbC.YTM_DORM
  1198. + " and wm.JLZTM=51"
  1199. + " left join RY ry on ry.RYID=w.RYID"
  1200. + " where w.BJID=:bjid and w.WJSJ>=:start"
  1201. + " and w.WJSJ<:end and w.JLZTM=51)"
  1202. + " where RN=1 order by WJSJ desc,WJJLID desc");
  1203. sqlc.wrLongParm("bjid", bjid);
  1204. sqlc.wrTimeParm("start", start);
  1205. sqlc.wrTimeParm("end", end);
  1206. List<Map<String, Object>> result = new ArrayList<>();
  1207. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  1208. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1209. item.put("id", rdLong(row.get("wjjlid")));
  1210. item.put("sourceId", rdLong(row.get("wjjlid")));
  1211. item.put("category", "dorm");
  1212. item.put("traceable", false);
  1213. item.put("itemName", rdStr(row.get("wjlbmc")));
  1214. item.put("studentName", rdStr(row.get("ryxm")));
  1215. item.put("title", rdStr(row.get("wjlbmc")));
  1216. item.put("remark", rdStr(row.get("ms")));
  1217. item.put("delta", -rdDouble(row.get("bjkf")));
  1218. item.put("scoreText", "");
  1219. item.put("sortTime", formatTimestamp(row.get("wjsj")));
  1220. result.add(item);
  1221. }
  1222. return result;
  1223. }
  1224. private static Map<LocalDate, DailyPoint> rdDailyDisciplineScore(
  1225. Db db, long bjid, Timestamp start, Timestamp end)
  1226. throws Exception {
  1227. DisciplinePenalties penalties = rdDisciplinePenalties(
  1228. db, bjid, start, end);
  1229. Map<LocalDate, Double> mapped = penalties.mapped.getOrDefault(
  1230. bjid, Collections.emptyMap());
  1231. Map<LocalDate, Double> legacy = penalties.legacy.getOrDefault(
  1232. bjid, Collections.emptyMap());
  1233. Set<LocalDate> dates = new LinkedHashSet<>(mapped.keySet());
  1234. dates.addAll(legacy.keySet());
  1235. Map<LocalDate, DailyPoint> result = new HashMap<>();
  1236. for (LocalDate day : dates) {
  1237. double penalty = disciplinePenalty(mapped, legacy, day);
  1238. result.put(day, new DailyPoint(day,
  1239. Math.max(0.0, 100.0 - penalty), 1));
  1240. }
  1241. return result;
  1242. }
  1243. private static Map<LocalDate, DailyPoint> rdDailyFlagScore(
  1244. Db db, long bjid, Timestamp start, Timestamp end) throws Exception {
  1245. SqlCmd sqlc = new SqlCmd(
  1246. "select trunc(s.JCSJ) D,avg(s.ZFS) SCORE,count(*) CNT"
  1247. + " from SQLJC s where s.BJID=:bjid and s.JCSJ>=:start"
  1248. + " and s.JCSJ<:end and s.ZFS between 0 and 100"
  1249. + " and s." + ObjC.JLZT_useStr
  1250. + " group by trunc(s.JCSJ)");
  1251. sqlc.wrLongParm("bjid", bjid);
  1252. sqlc.wrTimeParm("start", start);
  1253. sqlc.wrTimeParm("end", end);
  1254. return rdDailyPoints(db.doSelMapListByC(sqlc));
  1255. }
  1256. private static Map<LocalDate, DailyPoint> rdDailyKtjlScore(
  1257. Db db, long bjid, Timestamp start, Timestamp end) throws Exception {
  1258. try {
  1259. SqlCmd sqlc = new SqlCmd(
  1260. "select trunc(k.KSSJ) D,avg(j.FS) SCORE,count(*) CNT"
  1261. + " from KTJL j inner join KCB k on k.KCBID=j.KCBID"
  1262. + " where j.BJID=:bjid and k.KSSJ>=:start"
  1263. + " and k.KSSJ<:end and j." + ObjC.JLZT_useStr
  1264. + " and k." + ObjC.JLZT_useStr
  1265. + " group by trunc(k.KSSJ)");
  1266. sqlc.wrLongParm("bjid", bjid);
  1267. sqlc.wrTimeParm("start", start);
  1268. sqlc.wrTimeParm("end", end);
  1269. return rdDailyPoints(db.doSelMapListByC(sqlc));
  1270. } catch (SQLException e) {
  1271. if (isMissingTable(e, "KTJL"))
  1272. return Collections.emptyMap();
  1273. throw e;
  1274. }
  1275. }
  1276. private static Map<LocalDate, DailyPoint> rdDailyAttendanceScore(
  1277. Db db, long bjid, Timestamp start, Timestamp end,
  1278. XjbDormScoreU.AttendanceScope scope) throws Exception {
  1279. Map<Long, Map<LocalDate, Double>> penalties =
  1280. XjbDormScoreU.rdAttendancePenalty(db, start, end, scope);
  1281. Map<LocalDate, Double> classPenalties =
  1282. penalties.getOrDefault(bjid, Collections.emptyMap());
  1283. Map<LocalDate, DailyPoint> points = new HashMap<>();
  1284. for (Map.Entry<LocalDate, Double> entry : classPenalties.entrySet()) {
  1285. points.put(entry.getKey(), new DailyPoint(entry.getKey(),
  1286. Math.max(0.0, 100.0 - entry.getValue()), 1));
  1287. }
  1288. return points;
  1289. }
  1290. private static Map<LocalDate, DailyPoint> rdDailyPoints(
  1291. List<Map<String, Object>> rows) {
  1292. Map<LocalDate, DailyPoint> points = new HashMap<>();
  1293. for (Map<String, Object> row : rows) {
  1294. LocalDate day = rdLocalDate(row.get("d"));
  1295. if (day != null)
  1296. points.put(day, new DailyPoint(day,
  1297. rdDouble(row.get("score")), rdInt(row.get("cnt"))));
  1298. }
  1299. return points;
  1300. }
  1301. private static List<Map<String, Object>> rdMonthCategoryRows(
  1302. Db db, ClassInfo cls, MonthPeriod period, CategoryDef category)
  1303. throws Exception {
  1304. CategoryDaily monthly = rdCategoryDaily(db, cls, null, period);
  1305. String source = monthly.sources.getOrDefault(
  1306. category.code, SOURCE_MISSING);
  1307. Map<LocalDate, DailyPoint> points = monthly.actual.get(category.code);
  1308. List<Map<String, Object>> rows = new ArrayList<>();
  1309. if (SOURCE_REAL.equals(source) && points != null) {
  1310. for (DailyPoint point : points.values()) {
  1311. double deduction = round2(
  1312. Math.max(0.0, 100.0 - point.score));
  1313. Map<String, Object> row = baseDetail(point.date, SOURCE_REAL);
  1314. row.put("id", point.date.format(DATE_FMT));
  1315. row.put("sourceId", point.date.format(DATE_FMT));
  1316. row.put("sourceType", category.code);
  1317. row.put("detailKind", "regular");
  1318. row.put("category", category.code);
  1319. row.put("itemName", category.label);
  1320. row.put("weekday", weekdayCn(point.date));
  1321. row.put("score", round2(point.score));
  1322. row.put("deduction", deduction);
  1323. row.put("delta", -deduction);
  1324. row.put("count", point.count);
  1325. row.put("traceable", true);
  1326. row.put("sortTime", point.date.format(DATE_FMT));
  1327. rows.add(row);
  1328. }
  1329. }
  1330. rows.sort(Comparator.comparing(
  1331. (Map<String, Object> row) -> String.valueOf(
  1332. row.getOrDefault("sortTime", ""))).reversed());
  1333. return rows;
  1334. }
  1335. private static List<Map<String, Object>> rdDayDetail(
  1336. Db db, ClassInfo cls, LocalDate day, MonthPeriod period,
  1337. CategoryDef category) throws Exception {
  1338. CategoryDaily monthly = rdCategoryDaily(db, cls, day, period);
  1339. String source = monthly.sources.getOrDefault(
  1340. category.code, SOURCE_MISSING);
  1341. List<Map<String, Object>> rows;
  1342. if (SOURCE_REAL.equals(source)) {
  1343. if ("attendance".equals(category.code))
  1344. rows = rdAttendanceDetail(db, cls.bjid, day,
  1345. XjbDormScoreU.AttendanceScope.DAY_STUDENT);
  1346. else if ("dorm".equals(category.code))
  1347. rows = rdItemDetail(db, cls.bjid, day, "dorm");
  1348. else if ("flag".equals(category.code))
  1349. rows = rdItemDetail(db, cls.bjid, day, "flag");
  1350. else if ("cleaning".equals(category.code))
  1351. rows = rdItemDetail(db, cls.bjid, day, "cleaning");
  1352. else if ("classroom".equals(category.code))
  1353. rows = rdKtjlDetail(db, cls.bjid, day);
  1354. else if ("discipline".equals(category.code))
  1355. rows = rdDisciplineDetail(db, cls.bjid, day);
  1356. else
  1357. rows = new ArrayList<>();
  1358. } else {
  1359. rows = new ArrayList<>();
  1360. }
  1361. rows.sort(Comparator.comparing(
  1362. (Map<String, Object> row) -> String.valueOf(
  1363. row.getOrDefault("sortTime", ""))).reversed());
  1364. return rows;
  1365. }
  1366. private static List<Map<String, Object>> rdAttendanceDetail(
  1367. Db db, long bjid, LocalDate day,
  1368. XjbDormScoreU.AttendanceScope scope) throws Exception {
  1369. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  1370. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  1371. SqlCmd sqlc = new SqlCmd(
  1372. "select * from (select k.KQJLID,k.KQLBM,lb.MC KQLBMC,"
  1373. + "k.RYID,ry.XM,k.MC,k.MS,k.KSSJ,bjcy.SFZS,"
  1374. + "case k.KQLBM when 1 then 3 when 21 then 1"
  1375. + " when 51 then 1 when 52 then 1 when 61 then 2"
  1376. + " else 0 end PENALTY,"
  1377. + "row_number() over(partition by NVL(k.RYID,-k.KQJLID)"
  1378. + " order by case k.KQLBM when 1 then 3 when 21 then 1"
  1379. + " when 51 then 1 when 52 then 1 when 61 then 2"
  1380. + " else 0 end desc,k.KSSJ desc,k.KQJLID desc) RN"
  1381. + " from KQJL k"
  1382. + " inner join BJCY bjcy on bjcy.RYID=k.RYID"
  1383. + " and bjcy.BJID=k.BJID and bjcy.SFBY=0"
  1384. + " and bjcy." + ObjC.JLZT_useStr
  1385. + " left join KQLB lb on lb.KQLBM=k.KQLBM"
  1386. + " left join RY ry on ry.RYID=k.RYID"
  1387. + " where k.BJID=:bjid and k.KSSJ>=:start"
  1388. + " and k.KSSJ<:end and k.KQJLZTM=51 and k."
  1389. + ObjC.JLZT_useStr
  1390. + " and bjcy.SFZS=:sfzs"
  1391. + " and k.KQLBM in(" + scope.codes + ")) where RN=1"
  1392. + " order by KSSJ desc,KQJLID desc");
  1393. sqlc.wrLongParm("bjid", bjid);
  1394. sqlc.wrTimeParm("start", start);
  1395. sqlc.wrTimeParm("end", end);
  1396. sqlc.wrIntParm("sfzs", scope.sfzs);
  1397. List<Map<String, Object>> result = new ArrayList<>();
  1398. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  1399. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1400. item.put("id", rdLong(row.get("kqjlid")));
  1401. item.put("sourceId", rdLong(row.get("kqjlid")));
  1402. item.put("category", "attendance");
  1403. item.put("traceable", true);
  1404. item.put("itemName", rdStr(row.get("kqlbmc")));
  1405. item.put("studentType",
  1406. scope == XjbDormScoreU.AttendanceScope.DAY_STUDENT
  1407. ? "走读生" : "住校生");
  1408. item.put("studentName", rdStr(row.get("xm")));
  1409. item.put("title", rdStr(row.get("mc")));
  1410. item.put("remark", rdStr(row.get("ms")));
  1411. item.put("delta", -rdDouble(row.get("penalty")));
  1412. item.put("scoreText", "");
  1413. item.put("sortTime", formatTimestamp(row.get("kssj")));
  1414. result.add(item);
  1415. }
  1416. return result;
  1417. }
  1418. private static List<Map<String, Object>> rdDisciplineDetail(
  1419. Db db, long bjid, LocalDate day) throws Exception {
  1420. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  1421. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  1422. SqlCmd sqlc = new SqlCmd(
  1423. "select w.WJJLID,w.RYID,ry.XM,lb.WJLBM,lb.MC WJLBMC,"
  1424. + "lb.BJKF,w.WJSJ,w.MS,w.WJLXM"
  1425. + " from WJJL w inner join WJLB lb on lb.WJLBM=w.WJLBM"
  1426. + " inner join XJB_WJLBMAP wm on wm.WJLBM=w.WJLBM"
  1427. + " and wm.YTM=" + XjbWjlbC.YTM_DISCIPLINE
  1428. + " and wm.JLZTM=51"
  1429. + " left join RY ry on ry.RYID=w.RYID"
  1430. + " where w.BJID=:bjid and w.WJSJ>=:start"
  1431. + " and w.WJSJ<:end and w.JLZTM=51"
  1432. + " order by w.WJSJ,w.WJJLID");
  1433. sqlc.wrLongParm("bjid", bjid);
  1434. sqlc.wrTimeParm("start", start);
  1435. sqlc.wrTimeParm("end", end);
  1436. List<Map<String, Object>> result = new ArrayList<>();
  1437. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  1438. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1439. item.put("id", rdLong(row.get("wjjlid")));
  1440. item.put("sourceId", rdLong(row.get("wjjlid")));
  1441. item.put("category", "discipline");
  1442. item.put("traceable", true);
  1443. item.put("itemName", rdStr(row.get("wjlbmc")));
  1444. item.put("studentName", rdStr(row.get("xm")));
  1445. item.put("title", rdStr(row.get("wjlbmc")));
  1446. item.put("remark", rdStr(row.get("ms")));
  1447. item.put("delta", -rdDouble(row.get("bjkf")));
  1448. item.put("scoreText", "");
  1449. item.put("sortTime", formatTimestamp(row.get("wjsj")));
  1450. result.add(item);
  1451. }
  1452. if (result.isEmpty())
  1453. result.addAll(rdLegacyDisciplineDetail(db, bjid, day));
  1454. return result;
  1455. }
  1456. private static List<Map<String, Object>> rdLegacyDisciplineDetail(
  1457. Db db, long bjid, LocalDate day) throws Exception {
  1458. SqlCmd sqlc = new SqlCmd(
  1459. "select XJB_FJRYID,WJRS,MS from XJB_FJRY"
  1460. + " where BJID=:bjid and JCRQ=:jcrq and JLZTM=51");
  1461. sqlc.wrLongParm("bjid", bjid);
  1462. sqlc.wrTimeParm("jcrq", Timestamp.valueOf(day.atStartOfDay()));
  1463. List<Map<String, Object>> result = new ArrayList<>();
  1464. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  1465. boolean imported = isZhixinStarImport(rdStr(row.get("ms")));
  1466. double count = imported
  1467. ? rdDouble(row.get("wjrs")) / 10.0
  1468. : rdInt(row.get("wjrs"));
  1469. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1470. item.put("id", rdLong(row.get("xjb_fjryid")));
  1471. item.put("sourceId", rdLong(row.get("xjb_fjryid")));
  1472. item.put("category", "discipline");
  1473. item.put("traceable", true);
  1474. item.put("itemName", imported ? "风纪仪容扣分" : "风纪仪容违纪");
  1475. item.put("studentName", "");
  1476. item.put("title", imported
  1477. ? "风纪仪容扣分 " + plainNumber(count)
  1478. : "风纪仪容违纪 " + (int) count + "人次");
  1479. item.put("remark", imported ? "" : rdStr(row.get("ms")));
  1480. item.put("delta", -count);
  1481. item.put("scoreText",
  1482. plainNumber(Math.max(0, 100 - count)) + "/100");
  1483. item.put("sortTime", day.format(DATE_FMT));
  1484. result.add(item);
  1485. }
  1486. return result;
  1487. }
  1488. private static List<Map<String, Object>> rdItemDetail(
  1489. Db db, long bjid, LocalDate day, String category) throws Exception {
  1490. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  1491. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  1492. String sql;
  1493. if ("dorm".equals(category)) {
  1494. sql = "select s.SSNWJCID ID,s.KFID,cd.MC KFMC,"
  1495. + "m.SSNWJCXMM XMM,m.FS,x.MFZ,x.MC XMMC,s.JCSJ"
  1496. + " from SSNWJC s inner join SSNWJCMX m"
  1497. + " on m.SSNWJCID=s.SSNWJCID"
  1498. + " inner join SSNWJCXM x on x.SSNWJCXMM=m.SSNWJCXMM"
  1499. + " left join CD cd on cd.CDID=s.KFID"
  1500. + " where s.BJID=:bjid and s.JCSJ>=:start"
  1501. + " and s.JCSJ<:end and s." + ObjC.JLZT_useStr
  1502. + " and m." + ObjC.JLZT_useStr
  1503. + " and m.FS<x.MFZ"
  1504. + " order by s.JCSJ desc,m.SSNWJCMXID";
  1505. } else if ("flag".equals(category)) {
  1506. sql = "select s.SQLJCID ID,m.SQLJCXMM XMM,m.FS,x.MFZ,"
  1507. + "x.MC XMMC,s.JCSJ from SQLJC s"
  1508. + " inner join SQLJCMX m on m.SQLJCID=s.SQLJCID"
  1509. + " inner join SQLJCXM x on x.SQLJCXMM=m.SQLJCXMM"
  1510. + " where s.BJID=:bjid and s.JCSJ>=:start"
  1511. + " and s.JCSJ<:end and s." + ObjC.JLZT_useStr
  1512. + " and m." + ObjC.JLZT_useStr
  1513. + " and x.SQLJCXMM in(11,21,22,23,31)"
  1514. + " and x.MFZ is not null and m.FS is not null"
  1515. + " and m.FS<x.MFZ order by s.JCSJ desc,m.SQLJCMXID";
  1516. } else if ("cleaning".equals(category)) {
  1517. sql = "select s.CSWSJCID ID,m.CSWSJCXMM XMM,m.FS,x.MFZ,"
  1518. + "x.MC XMMC,s.JCSJ from CSWSJC s"
  1519. + " inner join CSWSJCMX m on m.CSWSJCID=s.CSWSJCID"
  1520. + " inner join CSWSJCXM x on x.CSWSJCXMM=m.CSWSJCXMM"
  1521. + " where s.BJID=:bjid and s.JCSJ>=:start"
  1522. + " and s.JCSJ<:end and s." + ObjC.JLZT_useStr
  1523. + " and m." + ObjC.JLZT_useStr
  1524. + " and m.FS<x.MFZ order by s.JCSJ desc,m.CSWSJCMXID";
  1525. } else {
  1526. throw new IllegalArgumentException("明细类型参数错误");
  1527. }
  1528. SqlCmd sqlc = new SqlCmd(sql);
  1529. sqlc.wrLongParm("bjid", bjid);
  1530. sqlc.wrTimeParm("start", start);
  1531. sqlc.wrTimeParm("end", end);
  1532. List<Map<String, Object>> result = new ArrayList<>();
  1533. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  1534. int score = rdInt(row.get("fs"));
  1535. int maxScore = rdInt(row.get("mfz"));
  1536. int rawDeducted = Math.max(0, maxScore - score);
  1537. int code = rdInt(row.get("xmm"));
  1538. double deducted = "flag".equals(category)
  1539. ? round2(rawDeducted * flagWeightPct(code) / 100.0)
  1540. : rawDeducted;
  1541. if (deducted <= 0.0)
  1542. continue;
  1543. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1544. item.put("id", rdLong(row.get("id")));
  1545. item.put("sourceId", rdLong(row.get("id")));
  1546. item.put("category", category);
  1547. item.put("traceable", true);
  1548. item.put("itemName", rdStr(row.get("xmmc")));
  1549. item.put("studentName", "");
  1550. item.put("roomName", rdStr(row.get("kfmc")));
  1551. item.put("title", rdStr(row.get("xmmc")));
  1552. item.put("remark", "得分 " + score + " / " + maxScore
  1553. + ("flag".equals(category)
  1554. ? ",折算扣 " + deducted : ""));
  1555. item.put("deducted", deducted);
  1556. item.put("rawDeducted", rawDeducted);
  1557. item.put("delta", -deducted);
  1558. item.put("scoreText", score + "/" + maxScore);
  1559. item.put("sortTime", formatTimestamp(row.get("jcsj")));
  1560. result.add(item);
  1561. }
  1562. if (result.isEmpty() && "cleaning".equals(category))
  1563. result.addAll(rdImportedCleaningSummary(db, bjid, day));
  1564. return result;
  1565. }
  1566. private static List<Map<String, Object>> rdFlagDaySummary(
  1567. Db db, long bjid, LocalDate day) throws Exception {
  1568. Map<String, Object> record = rdFlagRecord(db, bjid, day);
  1569. if (record == null)
  1570. return new ArrayList<>();
  1571. if (isZhixinStarImport(rdStr(record.get("ms")))) {
  1572. double score = flagRate(record.get("zfs"));
  1573. List<Map<String, Object>> rows = new ArrayList<>();
  1574. rows.add(flagSummaryRow(day, record, "aggregate", "升旗礼",
  1575. round2(100.0 - score), score, false, ""));
  1576. return rows;
  1577. }
  1578. double attendanceRate = flagRate(record.get("cql"));
  1579. double queueScore = flagRate(record.get("dlfs"));
  1580. double demeanorScore = flagRate(record.get("fmfs"));
  1581. double disciplineScore = flagRate(record.get("jlfs"));
  1582. List<Map<String, Object>> rows = new ArrayList<>();
  1583. rows.add(flagSummaryRow(day, record, "attendance", "考勤",
  1584. round2((100.0 - attendanceRate) * 0.25),
  1585. attendanceRate, true, "flagAttendanceDetail"));
  1586. rows.add(flagSummaryRow(day, record, "queue", "队列",
  1587. round2((100.0 - queueScore) * 0.20),
  1588. queueScore, false, ""));
  1589. rows.add(flagSummaryRow(day, record, "demeanor", "风貌",
  1590. round2((100.0 - demeanorScore) * 0.40),
  1591. demeanorScore, true, "flagDemeanorDetail"));
  1592. rows.add(flagSummaryRow(day, record, "discipline", "纪律",
  1593. round2((100.0 - disciplineScore) * 0.15),
  1594. disciplineScore, false, ""));
  1595. return rows;
  1596. }
  1597. private static List<Map<String, Object>> rdFlagAttendanceDetail(
  1598. Db db, long bjid, LocalDate day) throws Exception {
  1599. Map<String, Object> record = rdFlagRecord(db, bjid, day);
  1600. if (record == null)
  1601. return new ArrayList<>();
  1602. Map<Integer, Integer> values = rdFlagItemValues(
  1603. db, rdLong(record.get("sqljcid")));
  1604. String[] labels = {
  1605. "总人数",
  1606. "队列人数",
  1607. "本部学生会值日人数",
  1608. "学校学生会值日人数",
  1609. "国旗队人数",
  1610. "公假人数",
  1611. "参与宿管人数",
  1612. "升旗礼前请假人数"
  1613. };
  1614. List<Map<String, Object>> rows = new ArrayList<>();
  1615. for (int i = 0; i < labels.length; i++) {
  1616. int code = i + 1;
  1617. int value = values.getOrDefault(code, 0);
  1618. rows.add(flagValueRow(day, labels[i], value, String.valueOf(value)));
  1619. }
  1620. double attendanceRate = flagRate(record.get("cql"));
  1621. double attendanceScore = flagRate(record.get("kqfs"));
  1622. rows.add(flagValueRow(day, "出勤率", attendanceRate,
  1623. plainNumber(attendanceRate) + "%"));
  1624. rows.add(flagValueRow(day, "考勤分", attendanceScore,
  1625. plainNumber(attendanceScore)));
  1626. return rows;
  1627. }
  1628. private static List<Map<String, Object>> rdFlagDemeanorDetail(
  1629. Db db, long bjid, LocalDate day) throws Exception {
  1630. Map<String, Object> record = rdFlagRecord(db, bjid, day);
  1631. if (record == null)
  1632. return new ArrayList<>();
  1633. Map<Integer, Integer> values = rdFlagItemValues(
  1634. db, rdLong(record.get("sqljcid")));
  1635. List<Map<String, Object>> rows = new ArrayList<>();
  1636. rows.add(flagValueRow(day, "国歌",
  1637. values.getOrDefault(21, 0),
  1638. String.valueOf(values.getOrDefault(21, 0))));
  1639. rows.add(flagValueRow(day, "校歌",
  1640. values.getOrDefault(22, 0),
  1641. String.valueOf(values.getOrDefault(22, 0))));
  1642. rows.add(flagValueRow(day, "服装",
  1643. values.getOrDefault(23, 0),
  1644. String.valueOf(values.getOrDefault(23, 0))));
  1645. return rows;
  1646. }
  1647. private static Map<String, Object> rdFlagRecord(
  1648. Db db, long bjid, LocalDate day) throws Exception {
  1649. SqlCmd sqlc = new SqlCmd(
  1650. "select SQLJCID,CQL,KQFS,DLFS,FMFS,JLFS,ZFS,JCSJ,MS"
  1651. + " from SQLJC where BJID=:bjid"
  1652. + " and trunc(JCSJ)=to_date(:day,'YYYY-MM-DD')"
  1653. + " and JLZTM in(22,25,51)"
  1654. + " order by SQLJCID desc");
  1655. sqlc.wrLongParm("bjid", bjid);
  1656. sqlc.wrStrParm("day", day.format(DATE_FMT));
  1657. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  1658. return rows == null || rows.isEmpty() ? null : rows.get(0);
  1659. }
  1660. private static Map<Integer, Integer> rdFlagItemValues(
  1661. Db db, long sqljcid) throws Exception {
  1662. SqlCmd sqlc = new SqlCmd(
  1663. "select SQLJCXMM,FS from SQLJCMX where SQLJCID=:sqljcid"
  1664. + " and JLZTM=51 and SQLJCXMM in(1,2,3,4,5,6,7,8,"
  1665. + "21,22,23)");
  1666. sqlc.wrLongParm("sqljcid", sqljcid);
  1667. Map<Integer, Integer> result = new HashMap<>();
  1668. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  1669. if (rows == null)
  1670. return result;
  1671. for (Map<String, Object> row : rows)
  1672. result.put(rdInt(row.get("sqljcxmm")), rdInt(row.get("fs")));
  1673. return result;
  1674. }
  1675. private static Map<String, Object> flagSummaryRow(
  1676. LocalDate day, Map<String, Object> record, String code,
  1677. String name, double deduction, double rawScore,
  1678. boolean traceable, String detailMode) {
  1679. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1680. item.put("id", rdLong(record.get("sqljcid")));
  1681. item.put("sourceId", rdLong(record.get("sqljcid")));
  1682. item.put("category", "flag");
  1683. item.put("code", code);
  1684. item.put("itemName", name);
  1685. item.put("title", name);
  1686. item.put("deduction", deduction);
  1687. item.put("delta", -deduction);
  1688. item.put("rawScore", round2(rawScore));
  1689. item.put("traceable", traceable);
  1690. item.put("detailMode", detailMode);
  1691. item.put("sortTime", formatTimestamp(record.get("jcsj")));
  1692. return item;
  1693. }
  1694. private static Map<String, Object> flagValueRow(
  1695. LocalDate day, String name, double value, String displayValue) {
  1696. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1697. item.put("itemName", name);
  1698. item.put("title", name);
  1699. item.put("value", round2(value));
  1700. item.put("displayValue", displayValue);
  1701. item.put("traceable", false);
  1702. item.put("sortTime", day.format(DATE_FMT));
  1703. return item;
  1704. }
  1705. private static double flagRate(Object value) {
  1706. return Math.max(0.0, Math.min(100.0, rdDouble(value)));
  1707. }
  1708. private static String plainNumber(double value) {
  1709. String text = String.format(Locale.CHINA, "%.2f", round2(value));
  1710. return text.replaceAll("0+$", "").replaceAll("\\.$", "");
  1711. }
  1712. private static List<Map<String, Object>> rdKtjlDetail(
  1713. Db db, long bjid, LocalDate day) throws Exception {
  1714. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  1715. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  1716. SqlCmd sqlc = new SqlCmd(
  1717. "select j.KTJLID,j.FS,j.MS,j.MSWJ,j.JLSJ,k.KSSJ,"
  1718. + "k.KCBID,nvl(kc.MC,k.MC) KCM,ry.XM JLRYXM"
  1719. + " from KTJL j"
  1720. + " inner join KCB k on k.KCBID=j.KCBID"
  1721. + " left join KC kc on kc.KCID=k.KCID"
  1722. + " left join RY ry on ry.RYID=j.JLRYID"
  1723. + " where j.BJID=:bjid and k.KSSJ>=:start"
  1724. + " and k.KSSJ<:end and j." + ObjC.JLZT_useStr
  1725. + " and k." + ObjC.JLZT_useStr
  1726. + " order by k.KSSJ desc,j.KTJLID desc");
  1727. sqlc.wrLongParm("bjid", bjid);
  1728. sqlc.wrTimeParm("start", start);
  1729. sqlc.wrTimeParm("end", end);
  1730. List<Map<String, Object>> result = new ArrayList<>();
  1731. List<Map<String, Object>> rows;
  1732. try {
  1733. rows = db.doSelMapListByC(sqlc);
  1734. } catch (SQLException e) {
  1735. if (isMissingTable(e, "KTJL"))
  1736. return result;
  1737. throw e;
  1738. }
  1739. if (rows == null)
  1740. return result;
  1741. for (Map<String, Object> row : rows) {
  1742. int score = rdInt(row.get("fs"));
  1743. if (score >= 100)
  1744. continue;
  1745. Map<String, Object> item = baseDetail(day, SOURCE_REAL);
  1746. long id = rdLong(row.get("ktjlid"));
  1747. item.put("id", id);
  1748. item.put("sourceId", id);
  1749. item.put("category", "classroom");
  1750. item.put("traceable", true);
  1751. item.put("itemName", "课堂考核");
  1752. item.put("studentName", "");
  1753. item.put("title", rdStr(row.get("kcm")));
  1754. item.put("remark", defaultText(row.get("ms"),
  1755. defaultText(row.get("mswj"), "课堂考核扣分")));
  1756. item.put("delta", -(100 - score));
  1757. item.put("scoreText", score + "/100");
  1758. item.put("sortTime", formatTimestamp(row.get("kssj")));
  1759. result.add(item);
  1760. }
  1761. return result;
  1762. }
  1763. private static Map<String, Object> rdTraceDetail(Db db, ClassInfo cls,
  1764. LocalDate day,
  1765. CategoryDef category,
  1766. long sourceId,
  1767. String source)
  1768. throws Exception {
  1769. if (!SOURCE_REAL.equals(source))
  1770. throw new IllegalArgumentException("扣分来源参数错误");
  1771. if ("attendance".equals(category.code))
  1772. return rdAttendanceTrace(db, cls, day, category, sourceId);
  1773. if ("dorm".equals(category.code))
  1774. return rdCheckTrace(db, cls, day, category, sourceId, "dorm");
  1775. if ("flag".equals(category.code))
  1776. return rdCheckTrace(db, cls, day, category, sourceId, "flag");
  1777. if ("cleaning".equals(category.code))
  1778. return rdCheckTrace(db, cls, day, category, sourceId, "cleaning");
  1779. if ("classroom".equals(category.code))
  1780. return rdKtjlTrace(db, cls, day, category, sourceId);
  1781. if ("discipline".equals(category.code))
  1782. return rdDisciplineTrace(db, cls, day, category, sourceId);
  1783. throw new IllegalArgumentException("评分项目暂无可追踪明细");
  1784. }
  1785. private static Map<String, Object> rdDisciplineTrace(
  1786. Db db, ClassInfo cls, LocalDate day,
  1787. CategoryDef category, long sourceId) throws Exception {
  1788. SqlCmd current = new SqlCmd(
  1789. "select w.WJJLID,w.WJSJ,w.MS,w.WJLBM,lb.MC WJLBMC,"
  1790. + "lb.BJKF,ry.XM RYXM,cj.XM CJRYXM"
  1791. + " from WJJL w inner join WJLB lb on lb.WJLBM=w.WJLBM"
  1792. + " inner join XJB_WJLBMAP wm on wm.WJLBM=w.WJLBM"
  1793. + " and wm.YTM=" + XjbWjlbC.YTM_DISCIPLINE
  1794. + " and wm.JLZTM=51"
  1795. + " left join RY ry on ry.RYID=w.RYID"
  1796. + " left join RY cj on cj.RYID=w.CJRYID"
  1797. + " where w.WJJLID=:id and w.BJID=:bjid"
  1798. + " and w.JLZTM=51");
  1799. current.wrLongParm("id", sourceId);
  1800. current.wrLongParm("bjid", cls.bjid);
  1801. List<Map<String, Object>> currentRows =
  1802. db.doSelMapListByC(current);
  1803. if (currentRows != null && !currentRows.isEmpty()) {
  1804. Map<String, Object> row = currentRows.get(0);
  1805. double deducted = rdDouble(row.get("bjkf"));
  1806. Map<String, Object> record = new LinkedHashMap<>();
  1807. record.put("time", formatTimestamp(row.get("wjsj")));
  1808. record.put("className", cls.bjmc);
  1809. record.put("inspectorName", rdStr(row.get("cjryxm")));
  1810. record.put("studentName", rdStr(row.get("ryxm")));
  1811. record.put("description", rdStr(row.get("ms")));
  1812. List<Map<String, Object>> items = new ArrayList<>();
  1813. Map<String, Object> item = new LinkedHashMap<>();
  1814. item.put("itemName", rdStr(row.get("wjlbmc")));
  1815. item.put("deducted", deducted);
  1816. item.put("remark", rdStr(row.get("ms")));
  1817. items.add(item);
  1818. return traceResult(cls, day, category, SOURCE_REAL,
  1819. "风纪仪容扣分详情", record, items, "");
  1820. }
  1821. return rdLegacyDisciplineTrace(db, cls, day, category, sourceId);
  1822. }
  1823. private static Map<String, Object> rdLegacyDisciplineTrace(
  1824. Db db, ClassInfo cls, LocalDate day,
  1825. CategoryDef category, long sourceId) throws Exception {
  1826. SqlCmd sqlc = new SqlCmd(
  1827. "select XJB_FJRYID,JCRQ,WJRS,MS from XJB_FJRY"
  1828. + " where XJB_FJRYID=:id and BJID=:bjid and JLZTM=51");
  1829. sqlc.wrLongParm("id", sourceId);
  1830. sqlc.wrLongParm("bjid", cls.bjid);
  1831. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  1832. if (rows == null || rows.isEmpty())
  1833. throw new IllegalArgumentException("风纪仪容记录不存在或已不可查看");
  1834. Map<String, Object> row = rows.get(0);
  1835. int violations = rdInt(row.get("wjrs"));
  1836. Map<String, Object> record = new LinkedHashMap<>();
  1837. record.put("time", day.format(DATE_FMT));
  1838. record.put("className", cls.bjmc);
  1839. record.put("inspectorName", "风纪检查");
  1840. record.put("violationCount", violations);
  1841. record.put("description", rdStr(row.get("ms")));
  1842. List<Map<String, Object>> items = new ArrayList<>();
  1843. if (violations > 0) {
  1844. Map<String, Object> item = new LinkedHashMap<>();
  1845. item.put("itemName", "风纪仪容违纪");
  1846. item.put("deducted", violations);
  1847. item.put("remark", "违纪" + violations + "人次,每人次扣1分");
  1848. items.add(item);
  1849. }
  1850. return traceResult(cls, day, category, SOURCE_REAL,
  1851. "风纪仪容扣分详情", record, items,
  1852. violations == 0 ? "当日无风纪仪容扣分" : "");
  1853. }
  1854. private static Map<String, Object> rdCheckTrace(Db db, ClassInfo cls,
  1855. LocalDate day,
  1856. CategoryDef category,
  1857. long sourceId,
  1858. String type)
  1859. throws Exception {
  1860. String mainSql;
  1861. String itemSql;
  1862. if ("dorm".equals(type)) {
  1863. mainSql = "select s.SSNWJCID ID,s.JCSJ,s.FS,s.MS,s.KFID,"
  1864. + "cd.MC KFMC,b.MC BJMC,jc.XM JCRYXM from SSNWJC s"
  1865. + " left join CD cd on cd.CDID=s.KFID"
  1866. + " left join BJ b on b.BJID=s.BJID"
  1867. + " left join RY jc on jc.RYID=s.JCRYID"
  1868. + " where s.SSNWJCID=:id and s.BJID=:bjid and s."
  1869. + ObjC.JLZT_useStr;
  1870. itemSql = "select x.SSNWJCXMM CODE,x.MC ITEM_NAME,"
  1871. + "m.FS CURRENT_SCORE,x.MFZ MAX_SCORE"
  1872. + " from SSNWJCMX m inner join SSNWJCXM x"
  1873. + " on x.SSNWJCXMM=m.SSNWJCXMM"
  1874. + " where m.SSNWJCID=:id and m." + ObjC.JLZT_useStr
  1875. + " order by x.SSNWJCXMM";
  1876. } else if ("flag".equals(type)) {
  1877. mainSql = "select s.SQLJCID ID,s.JCSJ,s.ZFS FS,s.MS,s.BMID,"
  1878. + "bm.MC BMMC,b.MC BJMC,jc.XM JCRYXM from SQLJC s"
  1879. + " left join BM bm on bm.BMID=s.BMID"
  1880. + " left join BJ b on b.BJID=s.BJID"
  1881. + " left join RY jc on jc.RYID=s.JCRYID"
  1882. + " where s.SQLJCID=:id and s.BJID=:bjid and s."
  1883. + ObjC.JLZT_useStr;
  1884. itemSql = "select x.SQLJCXMM CODE,x.MC ITEM_NAME,"
  1885. + "m.FS CURRENT_SCORE,x.MFZ MAX_SCORE"
  1886. + " from SQLJCMX m inner join SQLJCXM x"
  1887. + " on x.SQLJCXMM=m.SQLJCXMM"
  1888. + " where m.SQLJCID=:id and m." + ObjC.JLZT_useStr
  1889. + " and x.SQLJCXMM in(11,21,22,23,31)"
  1890. + " order by x.SQLJCXMM";
  1891. } else {
  1892. mainSql = "select s.CSWSJCID ID,s.JCSJ,s.FS,s.MS,"
  1893. + "b.MC BJMC,jc.XM JCRYXM from CSWSJC s"
  1894. + " left join BJ b on b.BJID=s.BJID"
  1895. + " left join RY jc on jc.RYID=s.JCRYID"
  1896. + " where s.CSWSJCID=:id and s.BJID=:bjid and s."
  1897. + ObjC.JLZT_useStr;
  1898. itemSql = "select x.MC ITEM_NAME,m.FS CURRENT_SCORE,x.MFZ MAX_SCORE"
  1899. + " from CSWSJCMX m inner join CSWSJCXM x"
  1900. + " on x.CSWSJCXMM=m.CSWSJCXMM"
  1901. + " where m.CSWSJCID=:id and m." + ObjC.JLZT_useStr
  1902. + " order by x.CSWSJCXMM";
  1903. }
  1904. SqlCmd mainCmd = new SqlCmd(mainSql);
  1905. mainCmd.wrLongParm("id", sourceId);
  1906. mainCmd.wrLongParm("bjid", cls.bjid);
  1907. List<Map<String, Object>> mainRows = db.doSelMapListByC(mainCmd);
  1908. if (mainRows == null || mainRows.isEmpty())
  1909. throw new IllegalArgumentException("扣分记录不存在或已不可查看");
  1910. Map<String, Object> main = mainRows.get(0);
  1911. SqlCmd itemCmd = new SqlCmd(itemSql);
  1912. itemCmd.wrLongParm("id", sourceId);
  1913. List<Map<String, Object>> itemRows = db.doSelMapListByC(itemCmd);
  1914. if (itemRows == null)
  1915. itemRows = new ArrayList<>();
  1916. List<Map<String, Object>> items = new ArrayList<>();
  1917. for (Map<String, Object> row : itemRows) {
  1918. int current = rdInt(row.get("current_score"));
  1919. int max = rdInt(row.get("max_score"));
  1920. int rawDeducted = Math.max(0, max - current);
  1921. double deducted = "flag".equals(type)
  1922. ? round2(rawDeducted
  1923. * flagWeightPct(rdInt(row.get("code"))) / 100.0)
  1924. : rawDeducted;
  1925. if (deducted <= 0.0)
  1926. continue;
  1927. Map<String, Object> item = new LinkedHashMap<>();
  1928. item.put("itemName", rdStr(row.get("item_name")));
  1929. item.put("currentScore", current);
  1930. item.put("maxScore", max);
  1931. item.put("deducted", deducted);
  1932. item.put("rawDeducted", rawDeducted);
  1933. item.put("remark", "得分 " + current + " / " + max
  1934. + ("flag".equals(type)
  1935. ? ",折算扣 " + deducted : ""));
  1936. items.add(item);
  1937. }
  1938. Map<String, Object> record = new LinkedHashMap<>();
  1939. record.put("time", formatTimestamp(main.get("jcsj")));
  1940. record.put("className", defaultText(main.get("bjmc"), cls.bjmc));
  1941. record.put("roomName", defaultText(main.get("kfmc"), ""));
  1942. record.put("departmentName", defaultText(main.get("bmmc"), ""));
  1943. record.put("inspectorName", defaultText(main.get("jcryxm"), "未记录"));
  1944. record.put("totalScore", rdDouble(main.get("fs")));
  1945. record.put("description", defaultText(main.get("ms"), ""));
  1946. return traceResult(cls, day, category, SOURCE_REAL,
  1947. category.label + "扣分详情", record, items, "");
  1948. }
  1949. private static Map<String, Object> rdAttendanceTrace(Db db, ClassInfo cls,
  1950. LocalDate day,
  1951. CategoryDef category,
  1952. long sourceId)
  1953. throws Exception {
  1954. SqlCmd cmd = new SqlCmd(
  1955. "select k.KQJLID,k.KQLBM,lb.MC KQLBMC,k.RYID,ry.XM,"
  1956. + "k.MC,k.MS,k.KSSJ,k.JSSJ,k.BJID,b.MC BJMC"
  1957. + " from KQJL k left join KQLB lb on lb.KQLBM=k.KQLBM"
  1958. + " left join RY ry on ry.RYID=k.RYID"
  1959. + " left join BJ b on b.BJID=k.BJID"
  1960. + " where k.KQJLID=:id and k.BJID=:bjid"
  1961. + " and k.KQJLZTM=51 and k." + ObjC.JLZT_useStr);
  1962. cmd.wrLongParm("id", sourceId);
  1963. cmd.wrLongParm("bjid", cls.bjid);
  1964. List<Map<String, Object>> rows = db.doSelMapListByC(cmd);
  1965. if (rows == null || rows.isEmpty())
  1966. throw new IllegalArgumentException("考勤扣分记录不存在或已不可查看");
  1967. Map<String, Object> main = rows.get(0);
  1968. int kqlbm = rdInt(main.get("kqlbm"));
  1969. double deducted = attendancePenalty(kqlbm);
  1970. Map<String, Object> record = new LinkedHashMap<>();
  1971. record.put("time", formatTimestamp(main.get("kssj")));
  1972. record.put("className", defaultText(main.get("bjmc"), cls.bjmc));
  1973. record.put("studentName", defaultText(main.get("xm"), ""));
  1974. record.put("inspectorName", "考勤系统");
  1975. record.put("description", defaultText(main.get("ms"), ""));
  1976. record.put("period", formatTimestamp(main.get("kssj")) + " - "
  1977. + formatTimestamp(main.get("jssj")));
  1978. List<Map<String, Object>> items = new ArrayList<>();
  1979. Map<String, Object> item = new LinkedHashMap<>();
  1980. item.put("itemName", defaultText(main.get("kqlbmc"), "考勤异常"));
  1981. item.put("deducted", deducted);
  1982. item.put("remark", defaultText(main.get("mc"), ""));
  1983. items.add(item);
  1984. return traceResult(cls, day, category, SOURCE_REAL,
  1985. category.label + "扣分详情", record, items, "");
  1986. }
  1987. private static Map<String, Object> rdKtjlTrace(
  1988. Db db, ClassInfo cls, LocalDate day,
  1989. CategoryDef category, long sourceId) throws Exception {
  1990. Timestamp start = Timestamp.valueOf(day.atStartOfDay());
  1991. Timestamp end = Timestamp.valueOf(day.plusDays(1).atStartOfDay());
  1992. SqlCmd mainCmd = new SqlCmd(
  1993. "select j.KTJLID,j.FS,j.MS,j.MSWJ,j.FJID,j.JLSJ,"
  1994. + "k.KSSJ,nvl(kc.MC,k.MC) KCM,b.MC BJMC,ry.XM JLRYXM"
  1995. + " from KTJL j"
  1996. + " inner join KCB k on k.KCBID=j.KCBID"
  1997. + " left join KC kc on kc.KCID=k.KCID"
  1998. + " left join BJ b on b.BJID=j.BJID"
  1999. + " left join RY ry on ry.RYID=j.JLRYID"
  2000. + " where j.KTJLID=:id and j.BJID=:bjid"
  2001. + " and k.KSSJ>=:start and k.KSSJ<:end"
  2002. + " and j." + ObjC.JLZT_useStr
  2003. + " and k." + ObjC.JLZT_useStr);
  2004. mainCmd.wrLongParm("id", sourceId);
  2005. mainCmd.wrLongParm("bjid", cls.bjid);
  2006. mainCmd.wrTimeParm("start", start);
  2007. mainCmd.wrTimeParm("end", end);
  2008. List<Map<String, Object>> mainRows = db.doSelMapListByC(mainCmd);
  2009. if (mainRows == null || mainRows.isEmpty())
  2010. throw new IllegalArgumentException("课堂考核记录不存在或已不可查看");
  2011. Map<String, Object> main = mainRows.get(0);
  2012. SqlCmd itemCmd = new SqlCmd(
  2013. "select x.KTJLXMM,x.MC ITEM_NAME,x.MFZ MAX_SCORE,m.FS"
  2014. + " from KTJLMX m inner join KTJLXM x"
  2015. + " on x.KTJLXMM=m.KTJLXMM"
  2016. + " where m.KTJLID=:id and m." + ObjC.JLZT_useStr
  2017. + " order by x.KTJLXMM");
  2018. itemCmd.wrLongParm("id", sourceId);
  2019. List<Map<String, Object>> itemRows = db.doSelMapListByC(itemCmd);
  2020. if (itemRows == null)
  2021. itemRows = new ArrayList<>();
  2022. List<Map<String, Object>> items = new ArrayList<>();
  2023. for (Map<String, Object> row : itemRows) {
  2024. int current = rdInt(row.get("fs"));
  2025. int max = rdInt(row.get("max_score"));
  2026. int deducted = Math.max(0, max - current);
  2027. if (deducted == 0)
  2028. continue;
  2029. Map<String, Object> item = new LinkedHashMap<>();
  2030. item.put("itemName", rdStr(row.get("item_name")));
  2031. item.put("currentScore", current);
  2032. item.put("maxScore", max);
  2033. item.put("deducted", deducted);
  2034. item.put("remark", "得分 " + current + " / " + max);
  2035. items.add(item);
  2036. }
  2037. Map<String, Object> record = new LinkedHashMap<>();
  2038. record.put("time", formatTimestamp(main.get("kssj")));
  2039. record.put("className", defaultText(main.get("bjmc"), cls.bjmc));
  2040. record.put("courseName", defaultText(main.get("kcm"), ""));
  2041. record.put("inspectorName", defaultText(main.get("jlryxm"), "未记录"));
  2042. record.put("totalScore", rdDouble(main.get("fs")));
  2043. record.put("description", defaultText(main.get("mswj"),
  2044. defaultText(main.get("ms"), "")));
  2045. record.put("attachmentId", main.get("fjid"));
  2046. return traceResult(cls, day, category, SOURCE_REAL,
  2047. category.label + "扣分详情", record, items, "");
  2048. }
  2049. private static Map<String, Object> rdAdjustmentTrace(
  2050. Db db, ClassInfo cls, long ledgerId, YearMonth reportMonth)
  2051. throws Exception {
  2052. SqlCmd ledgerCmd = new SqlCmd(
  2053. "select j.XJB_JFJLID,j.NDY,j.FSSJ,j.JFLX,j.JFZ,j.MC,j.MS,"
  2054. + "j.LYLX,j.LYID,j.LYLB,j.RYID,r.XM RYXM"
  2055. + " from XJB_JFJL j left join RY r on r.RYID=j.RYID"
  2056. + " where j.XJB_JFJLID=:id and j.BJID=:bjid"
  2057. + " and j.JLZTM=51");
  2058. ledgerCmd.wrLongParm("id", ledgerId);
  2059. ledgerCmd.wrLongParm("bjid", cls.bjid);
  2060. List<Map<String, Object>> ledgerRows =
  2061. db.doSelMapListByC(ledgerCmd);
  2062. if (ledgerRows == null || ledgerRows.isEmpty())
  2063. throw new IllegalArgumentException("增扣分记录不存在或已不可查看");
  2064. Map<String, Object> ledger = ledgerRows.get(0);
  2065. if (reportMonth != null) {
  2066. YearMonth ledgerMonth = YearMonth.parse(
  2067. rdStr(ledger.get("ndy")), MONTH_FMT);
  2068. ensureReportMonth(reportMonth, ledgerMonth);
  2069. }
  2070. String sourceType = defaultText(ledger.get("lylx"), "");
  2071. long sourceId = rdLong(ledger.get("lyid"));
  2072. if (XjbJfServ.LYLX_AWARD.equals(sourceType))
  2073. return rdAwardAdjustmentTrace(db, cls, ledger, sourceId);
  2074. if (XjbJfServ.LYLX_SCHOOL_VIOLATION.equals(sourceType))
  2075. return rdViolationAdjustmentTrace(db, cls, ledger, sourceId);
  2076. if (XjbJfServ.LYLX_TEAM_ROLE.equals(sourceType)
  2077. || XjbJfServ.LYLX_TEAM_MEMBER.equals(sourceType))
  2078. return rdTeamAdjustmentTrace(db, cls, ledger, sourceType,
  2079. sourceId);
  2080. if (XjbJfServ.LYLX_MONTH_SAFE.equals(sourceType)
  2081. || XjbJfServ.LYLX_FJ_SAFE.equals(sourceType))
  2082. return rdRuleAdjustmentTrace(cls, ledger, sourceType);
  2083. return rdLedgerOnlyTrace(cls, ledger, sourceType);
  2084. }
  2085. private static Map<String, Object> rdAwardAdjustmentTrace(
  2086. Db db, ClassInfo cls, Map<String, Object> ledger, long sourceId)
  2087. throws Exception {
  2088. SqlCmd sqlc = new SqlCmd(
  2089. "select h.HJJLID,h.HJXZ,h.HJRQ,h.MS,h.MC,h.HJLBM,"
  2090. + "l.MC LBMC,l.BJJF,l.JTBJJF,r.XM RYXM"
  2091. + " from HJJL h inner join HJLB l on l.HJLBM=h.HJLBM"
  2092. + " left join RY r on r.RYID=h.RYID"
  2093. + " where h.HJJLID=:id and h.JLZTM=51");
  2094. sqlc.wrLongParm("id", sourceId);
  2095. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  2096. if (rows == null || rows.isEmpty())
  2097. return rdLedgerOnlyTrace(cls, ledger,
  2098. XjbJfServ.LYLX_AWARD);
  2099. Map<String, Object> row = rows.get(0);
  2100. int awardType = rdInt(row.get("hjxz"));
  2101. String awardTypeText = awardType == 2 ? "集体获奖" : "个人获奖";
  2102. Map<String, Object> record = adjustmentRecord(cls, ledger,
  2103. "学校获奖", row.get("ryxm"));
  2104. record.put("awardNature", awardTypeText);
  2105. record.put("awardCategory", rdStr(row.get("lbmc")));
  2106. record.put("description", defaultText(row.get("ms"),
  2107. defaultText(row.get("mc"), "")));
  2108. List<Map<String, Object>> items = new ArrayList<>();
  2109. if (awardType == 2) {
  2110. SqlCmd memberCmd = new SqlCmd(
  2111. "select c.RYID,r.XM,b.MC BJMC from HJJLCY c"
  2112. + " left join RY r on r.RYID=c.RYID"
  2113. + " left join BJ b on b.BJID=c.BJID"
  2114. + " where c.HJJLID=:id and c.JLZTM=51"
  2115. + " order by c.BJID,c.RYID");
  2116. memberCmd.wrLongParm("id", sourceId);
  2117. List<Map<String, Object>> members =
  2118. db.doSelMapListByC(memberCmd);
  2119. for (Map<String, Object> member : members) {
  2120. Map<String, Object> item = new LinkedHashMap<>();
  2121. item.put("itemName", "参与学生");
  2122. item.put("studentName", defaultText(member.get("xm"), ""));
  2123. item.put("className", defaultText(member.get("bjmc"), ""));
  2124. item.put("remark", "集体获奖参与学生");
  2125. items.add(item);
  2126. }
  2127. }
  2128. Map<String, Object> main = adjustmentItem(ledger,
  2129. defaultText(row.get("lbmc"), awardTypeText),
  2130. defaultText(row.get("ms"), ""));
  2131. items.add(0, main);
  2132. return adjustmentTraceResult(cls, ledger, sourceId,
  2133. XjbJfServ.LYLX_AWARD, awardTypeText + "增分详情",
  2134. record, items, "");
  2135. }
  2136. private static Map<String, Object> rdViolationAdjustmentTrace(
  2137. Db db, ClassInfo cls, Map<String, Object> ledger, long sourceId)
  2138. throws Exception {
  2139. SqlCmd sqlc = new SqlCmd(
  2140. "select w.WJJLID,w.WJSJ,w.MS,w.WJLBM,lb.MC WJLBMC,"
  2141. + "lb.BJKF,ry.XM RYXM,cj.XM CJRYXM"
  2142. + " from WJJL w left join WJLB lb on lb.WJLBM=w.WJLBM"
  2143. + " left join RY ry on ry.RYID=w.RYID"
  2144. + " left join RY cj on cj.RYID=w.CJRYID"
  2145. + " where w.WJJLID=:id and w.JLZTM=51");
  2146. sqlc.wrLongParm("id", sourceId);
  2147. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  2148. if (rows == null || rows.isEmpty())
  2149. return rdLedgerOnlyTrace(cls, ledger,
  2150. XjbJfServ.LYLX_SCHOOL_VIOLATION);
  2151. Map<String, Object> row = rows.get(0);
  2152. Map<String, Object> record = adjustmentRecord(cls, ledger,
  2153. "学校违纪", row.get("ryxm"));
  2154. record.put("eventTime", formatTimestamp(row.get("wjsj")));
  2155. record.put("inspectorName",
  2156. defaultText(row.get("cjryxm"), "未记录"));
  2157. record.put("businessCategory", rdStr(row.get("wjlbmc")));
  2158. record.put("description", defaultText(row.get("ms"), ""));
  2159. List<Map<String, Object>> items = List.of(adjustmentItem(ledger,
  2160. defaultText(row.get("wjlbmc"), "学校违纪"),
  2161. defaultText(row.get("ms"), "")));
  2162. return adjustmentTraceResult(cls, ledger, sourceId,
  2163. XjbJfServ.LYLX_SCHOOL_VIOLATION,
  2164. "学校违纪减分详情", record, items, "");
  2165. }
  2166. private static Map<String, Object> rdTeamAdjustmentTrace(
  2167. Db db, ClassInfo cls, Map<String, Object> ledger,
  2168. String sourceType, long sourceId) throws Exception {
  2169. String sql;
  2170. if (XjbJfServ.LYLX_TEAM_ROLE.equals(sourceType)) {
  2171. sql = "select w.TTWCYID ID,w.RYID,w.TTWCYLBM,"
  2172. + "t.MC TTMC,wc.MC ROLEMC,wc.BJJF,ry.XM RYXM"
  2173. + " from TTWCY w inner join TT t on t.TTID=w.TTID"
  2174. + " inner join TTWCYLB wc on wc.TTWCYLBM=w.TTWCYLBM"
  2175. + " left join RY ry on ry.RYID=w.RYID"
  2176. + " where w.TTWCYID=:id and w.JLZTM=51";
  2177. } else {
  2178. sql = "select c.TTCYID ID,c.RYID,wc.TTWCYLBM,"
  2179. + "t.MC TTMC,wc.MC ROLEMC,wc.BJJF,ry.XM RYXM"
  2180. + " from TTCY c inner join TT t on t.TTID=c.TTID"
  2181. + " inner join TTWCYLB wc on wc.TTLBM=t.TTLBM"
  2182. + " and wc.TTWCYLBM=(select min(wc2.TTWCYLBM)"
  2183. + " from TTWCYLB wc2 where wc2.TTLBM=t.TTLBM"
  2184. + " and wc2.MC='成员')"
  2185. + " left join RY ry on ry.RYID=c.RYID"
  2186. + " where c.TTCYID=:id and c.JLZTM=51";
  2187. }
  2188. SqlCmd sqlc = new SqlCmd(sql);
  2189. sqlc.wrLongParm("id", sourceId);
  2190. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  2191. if (rows == null || rows.isEmpty())
  2192. return rdLedgerOnlyTrace(cls, ledger, sourceType);
  2193. Map<String, Object> row = rows.get(0);
  2194. Map<String, Object> record = adjustmentRecord(cls, ledger,
  2195. "团体成员增分", row.get("ryxm"));
  2196. record.put("teamName", rdStr(row.get("ttmc")));
  2197. record.put("roleName", rdStr(row.get("rolemc")));
  2198. record.put("description", defaultText(ledger.get("ms"), ""));
  2199. List<Map<String, Object>> items = List.of(adjustmentItem(ledger,
  2200. defaultText(row.get("ttmc"), "")
  2201. + "·" + defaultText(row.get("rolemc"), ""),
  2202. defaultText(ledger.get("ms"), "")));
  2203. return adjustmentTraceResult(cls, ledger, sourceId, sourceType,
  2204. "团体成员增分详情", record, items, "");
  2205. }
  2206. private static Map<String, Object> rdRuleAdjustmentTrace(
  2207. ClassInfo cls, Map<String, Object> ledger, String sourceType) {
  2208. Map<String, Object> record = adjustmentRecord(cls, ledger,
  2209. "星级班规则增分", null);
  2210. record.put("ruleName",
  2211. XjbJfServ.LYLX_MONTH_SAFE.equals(sourceType)
  2212. ? "当月无学生处分" : "当月无风纪扣分");
  2213. record.put("description", defaultText(ledger.get("ms"), ""));
  2214. List<Map<String, Object>> items = List.of(adjustmentItem(ledger,
  2215. defaultText(ledger.get("mc"),
  2216. String.valueOf(record.get("ruleName"))),
  2217. defaultText(ledger.get("ms"), "")));
  2218. return adjustmentTraceResult(cls, ledger, rdLong(ledger.get("lyid")),
  2219. sourceType, "星级班规则增分详情", record, items,
  2220. "该记录由月度规则自动生成,无其他原始业务单据");
  2221. }
  2222. private static Map<String, Object> rdLedgerOnlyTrace(
  2223. ClassInfo cls, Map<String, Object> ledger, String sourceType) {
  2224. Map<String, Object> record = adjustmentRecord(cls, ledger,
  2225. adjustmentSourceLabel(sourceType), null);
  2226. record.put("description", defaultText(ledger.get("ms"), ""));
  2227. List<Map<String, Object>> items = List.of(adjustmentItem(ledger,
  2228. defaultText(ledger.get("mc"), "星级班增扣分"),
  2229. defaultText(ledger.get("ms"), "")));
  2230. return adjustmentTraceResult(cls, ledger, rdLong(ledger.get("lyid")),
  2231. sourceType, "星级班增扣分详情", record, items,
  2232. "原业务记录已不可查看,以下内容来自星级班增扣分账本");
  2233. }
  2234. private static Map<String, Object> adjustmentRecord(
  2235. ClassInfo cls, Map<String, Object> ledger, String businessType,
  2236. Object studentName) {
  2237. Map<String, Object> record = new LinkedHashMap<>();
  2238. record.put("time", formatTimestamp(ledger.get("fssj")));
  2239. record.put("className", cls.bjmc);
  2240. record.put("studentName",
  2241. defaultText(studentName, defaultText(ledger.get("ryxm"), "")));
  2242. record.put("businessType", businessType);
  2243. record.put("ledgerId", rdLong(ledger.get("xjb_jfjlid")));
  2244. record.put("score", signedAdjustmentScore(ledger));
  2245. return record;
  2246. }
  2247. private static Map<String, Object> adjustmentItem(
  2248. Map<String, Object> ledger, String itemName, String remark) {
  2249. Map<String, Object> item = new LinkedHashMap<>();
  2250. item.put("itemName", itemName);
  2251. item.put("delta", signedAdjustmentScore(ledger));
  2252. item.put("remark", remark);
  2253. return item;
  2254. }
  2255. private static Map<String, Object> adjustmentTraceResult(
  2256. ClassInfo cls, Map<String, Object> ledger, long sourceId,
  2257. String sourceType, String title, Map<String, Object> record,
  2258. List<Map<String, Object>> items, String notice) {
  2259. Map<String, Object> data = new LinkedHashMap<>();
  2260. data.put("bjid", cls.bjid);
  2261. data.put("bjmc", cls.bjmc);
  2262. data.put("kind", "adjustment");
  2263. data.put("ledgerId", rdLong(ledger.get("xjb_jfjlid")));
  2264. data.put("sourceType", sourceType);
  2265. data.put("sourceId", sourceId);
  2266. data.put("title", title);
  2267. data.put("record", record);
  2268. data.put("items", items);
  2269. data.put("notice", notice);
  2270. return data;
  2271. }
  2272. private static double signedAdjustmentScore(Map<String, Object> ledger) {
  2273. double points = Math.abs(rdDouble(ledger.get("jfz")));
  2274. return rdInt(ledger.get("jflx"))
  2275. == XjbJfServ.JFLX_ADD ? points : -points;
  2276. }
  2277. private static String adjustmentSourceLabel(String sourceType) {
  2278. if (XjbJfServ.LYLX_AWARD.equals(sourceType))
  2279. return "学校获奖";
  2280. if (XjbJfServ.LYLX_SCHOOL_VIOLATION.equals(sourceType))
  2281. return "学校违纪";
  2282. if (XjbJfServ.LYLX_TEAM_ROLE.equals(sourceType))
  2283. return "团体职务";
  2284. if (XjbJfServ.LYLX_TEAM_MEMBER.equals(sourceType))
  2285. return "团体成员";
  2286. if (XjbJfServ.LYLX_MONTH_SAFE.equals(sourceType))
  2287. return "当月无学生处分";
  2288. if (XjbJfServ.LYLX_FJ_SAFE.equals(sourceType))
  2289. return "当月无风纪扣分";
  2290. return StringUtils.defaultIfBlank(sourceType, "星级班增扣分");
  2291. }
  2292. private static Map<String, Object> traceResult(
  2293. ClassInfo cls, LocalDate day, CategoryDef category, String source,
  2294. String title, Map<String, Object> record,
  2295. List<Map<String, Object>> items, String notice) {
  2296. Map<String, Object> data = new LinkedHashMap<>();
  2297. data.put("bjid", cls.bjid);
  2298. data.put("bjmc", cls.bjmc);
  2299. data.put("date", day.format(DATE_FMT));
  2300. data.put("category", category.code);
  2301. data.put("categoryLabel", category.label);
  2302. data.put("source", source);
  2303. data.put("title", title);
  2304. data.put("record", record);
  2305. data.put("items", items);
  2306. data.put("notice", notice);
  2307. return data;
  2308. }
  2309. private static String defaultText(Object value, String defaultValue) {
  2310. String text = rdStr(value);
  2311. return StringUtils.isBlank(text) ? defaultValue : text;
  2312. }
  2313. private static int flagWeightPct(int code) {
  2314. if (code == 11)
  2315. return 20;
  2316. if (code == 21 || code == 22 || code == 23)
  2317. return 40;
  2318. if (code == 31)
  2319. return 15;
  2320. return 0;
  2321. }
  2322. private static List<Map<String, Object>> rdAdjustmentRows(
  2323. List<Adjustment> adjustments) {
  2324. List<Map<String, Object>> rows = new ArrayList<>();
  2325. for (Adjustment item : adjustments) {
  2326. Map<String, Object> row = baseDetail(item.date, SOURCE_REAL);
  2327. row.put("id", item.id);
  2328. row.put("ledgerId", item.id);
  2329. row.put("sourceId", item.sourceRecordId);
  2330. row.put("sourceType", item.sourceType);
  2331. row.put("detailKind", "adjustment");
  2332. row.put("traceable", true);
  2333. row.put("itemName", item.itemName);
  2334. row.put("studentName", item.studentName);
  2335. row.put("title", item.itemName);
  2336. row.put("remark", item.remark);
  2337. row.put("delta", item.score);
  2338. row.put("scoreText", "");
  2339. row.put("sortTime", item.date.format(DATE_FMT));
  2340. rows.add(row);
  2341. }
  2342. return rows;
  2343. }
  2344. private static Map<String, Object> baseDetail(LocalDate day, String source) {
  2345. Map<String, Object> item = new LinkedHashMap<>();
  2346. item.put("date", day.format(DATE_FMT));
  2347. item.put("source", source);
  2348. return item;
  2349. }
  2350. private static CategoryDef requireCategory(String code) {
  2351. for (CategoryDef def : CATEGORY_DEFS) {
  2352. if (def.code.equals(code))
  2353. return def;
  2354. }
  2355. throw new IllegalArgumentException("评分项目参数错误");
  2356. }
  2357. private static Map<Long, AdjustmentTotals> rdAdjustmentTotals(
  2358. Db db, YearMonth month) throws Exception {
  2359. SqlCmd sqlc = new SqlCmd(
  2360. "select BJID,"
  2361. + "sum(case when JFLX=1 and LYLX in('"
  2362. + XjbJfServ.LYLX_TEAM_ROLE + "','"
  2363. + XjbJfServ.LYLX_TEAM_MEMBER
  2364. + "') then JFZ else 0 end) TEAM_BONUS,"
  2365. + "sum(case when JFLX=1 and nvl(LYLX,'#') not in('"
  2366. + XjbJfServ.LYLX_TEAM_ROLE + "','"
  2367. + XjbJfServ.LYLX_TEAM_MEMBER
  2368. + "') then JFZ else 0 end) OTHER_BONUS,"
  2369. + "sum(case when JFLX=2 then JFZ else 0 end) DEDUCTION,"
  2370. + "sum(case when JFLX=1 then 1 else 0 end) BONUS_CNT,"
  2371. + "sum(case when JFLX=2 then 1 else 0 end) DEDUCTION_CNT"
  2372. + " from XJB_JFJL where NDY=:ndy and JLZTM=51"
  2373. + " group by BJID");
  2374. sqlc.wrStrParm("ndy", month.format(MONTH_FMT));
  2375. try {
  2376. Map<Long, AdjustmentTotals> result = new HashMap<>();
  2377. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  2378. AdjustmentTotals total = new AdjustmentTotals();
  2379. total.bonus = XjbJfServ.capBonus(
  2380. rdDouble(row.get("other_bonus")),
  2381. rdDouble(row.get("team_bonus")));
  2382. total.deduction = rdDouble(row.get("deduction"));
  2383. total.bonusEvents = rdInt(row.get("bonus_cnt"));
  2384. total.deductionEvents = rdInt(row.get("deduction_cnt"));
  2385. result.put(rdLong(row.get("bjid")), total);
  2386. }
  2387. return result;
  2388. } catch (SQLException e) {
  2389. if (isMissingTable(e, "XJB_JFJL"))
  2390. return Collections.emptyMap();
  2391. throw e;
  2392. }
  2393. }
  2394. private static PublishedSnapshot rdPublishedSnapshot(
  2395. Db db, long bjid, YearMonth month) throws Exception {
  2396. SqlCmd sqlc = new SqlCmd(
  2397. "select jg.ZF,jg.XJ,jg.ZJF,jg.KJF,jg.ZJFCS,jg.KJFCS,"
  2398. + "jg.SSLY,jg.KTLY,jg.SJLY,jg.CSLY,jg.FJLY,jg.KQLY,"
  2399. + "pm.DWPM,pm.DWCS,pm.XXPM,pm.XXCS from XJB_JG jg"
  2400. + " inner join XJB_JD jd on jd.XJB_JDID=jg.XJB_JDID"
  2401. + " inner join XJB_PM pm on pm.XJB_JDID=jg.XJB_JDID"
  2402. + " and pm.BJID=jg.BJID"
  2403. + " where jg.BJID=:bjid and jg.NDY=:ndy"
  2404. + " and jg.ZT=5 and jd.ZT=5"
  2405. + " and jd.FWLX in('XX','BM')"
  2406. + " order by case when jd.FWLX='XX' then 0 else 1 end,"
  2407. + " jd.BB desc,jd.XJB_JDID desc");
  2408. sqlc.wrLongParm("bjid", bjid);
  2409. sqlc.wrStrParm("ndy", month.format(MONTH_FMT));
  2410. try {
  2411. List<Map<String, Object>> rows = db.doSelMapListByC(sqlc);
  2412. if (rows == null || rows.isEmpty())
  2413. return null;
  2414. Map<String, Object> row = rows.get(0);
  2415. PublishedSnapshot snapshot = new PublishedSnapshot();
  2416. snapshot.total = rdDouble(row.get("zf"));
  2417. snapshot.starLevel = rdStr(row.get("xj"));
  2418. snapshot.bonus = rdDouble(row.get("zjf"));
  2419. snapshot.deduction = rdDouble(row.get("kjf"));
  2420. snapshot.bonusCount = rdInt(row.get("zjfcs"));
  2421. snapshot.deductionCount = rdInt(row.get("kjfcs"));
  2422. snapshot.departmentRank = rdInt(row.get("dwpm"));
  2423. snapshot.departmentClassCount = rdInt(row.get("dwcs"));
  2424. snapshot.schoolRank = rdInt(row.get("xxpm"));
  2425. snapshot.schoolClassCount = rdInt(row.get("xxcs"));
  2426. snapshot.sources.put("dorm", rdStr(row.get("ssly")));
  2427. snapshot.sources.put("classroom", rdStr(row.get("ktly")));
  2428. snapshot.sources.put("flag", rdStr(row.get("sjly")));
  2429. snapshot.sources.put("cleaning", rdStr(row.get("csly")));
  2430. snapshot.sources.put("discipline", rdStr(row.get("fjly")));
  2431. snapshot.sources.put("attendance", rdStr(row.get("kqly")));
  2432. return snapshot;
  2433. } catch (SQLException e) {
  2434. if (isMissingTable(e, "XJB_JG"))
  2435. return null;
  2436. throw e;
  2437. }
  2438. }
  2439. private static List<Adjustment> rdAdjustments(
  2440. Db db, long bjid, YearMonth month) throws Exception {
  2441. SqlCmd sqlc = new SqlCmd(
  2442. "select j.XJB_JFJLID ID,j.FSSJ,j.JFLX,j.JFZ,j.MC,j.MS,"
  2443. + "j.LYLX,j.LYID,j.LYLB,r.XM RYXM from XJB_JFJL j"
  2444. + " left join RY r on r.RYID=j.RYID"
  2445. + " where j.BJID=:bjid and j.NDY=:ndy and j.JLZTM=51"
  2446. + " order by j.FSSJ desc,j.XJB_JFJLID desc");
  2447. sqlc.wrLongParm("bjid", bjid);
  2448. sqlc.wrStrParm("ndy", month.format(MONTH_FMT));
  2449. try {
  2450. List<Adjustment> result = new ArrayList<>();
  2451. for (Map<String, Object> row : db.doSelMapListByC(sqlc)) {
  2452. int type = rdInt(row.get("jflx"));
  2453. double points = rdDouble(row.get("jfz"));
  2454. result.add(new Adjustment(
  2455. rdLong(row.get("id")),
  2456. rdLocalDate(row.get("fssj")),
  2457. type == 1 ? points : -points,
  2458. defaultText(row.get("mc"), "星级班增扣分"),
  2459. defaultText(row.get("ms"), ""),
  2460. defaultText(row.get("lylx"), ""),
  2461. rdLong(row.get("lyid")),
  2462. defaultText(row.get("ryxm"), "")));
  2463. }
  2464. return result;
  2465. } catch (SQLException e) {
  2466. if (isMissingTable(e, "XJB_JFJL"))
  2467. return Collections.emptyList();
  2468. throw e;
  2469. }
  2470. }
  2471. private static AdjustmentTotals adjustmentTotals(
  2472. List<Adjustment> adjustments) {
  2473. AdjustmentTotals total = new AdjustmentTotals();
  2474. double otherBonus = 0.0;
  2475. double teamBonus = 0.0;
  2476. for (Adjustment item : adjustments) {
  2477. if (item.score > 0) {
  2478. if (XjbJfServ.LYLX_TEAM_ROLE.equals(item.sourceType)
  2479. || XjbJfServ.LYLX_TEAM_MEMBER.equals(
  2480. item.sourceType)) {
  2481. teamBonus += item.score;
  2482. } else {
  2483. otherBonus += item.score;
  2484. }
  2485. total.bonusEvents++;
  2486. } else {
  2487. total.deduction += Math.abs(item.score);
  2488. total.deductionEvents++;
  2489. }
  2490. }
  2491. total.bonus = round2(XjbJfServ.capBonus(otherBonus, teamBonus));
  2492. total.deduction = round2(total.deduction);
  2493. return total;
  2494. }
  2495. private static String starLevel(double score, int schoolRank) {
  2496. if (schoolRank <= 10 && score > 90.0)
  2497. return "五星级";
  2498. if (score > 80.0)
  2499. return "四星级";
  2500. if (score > 75.0)
  2501. return "三星级";
  2502. if (score > 65.0)
  2503. return "二星级";
  2504. if (score > 55.0)
  2505. return "一星级";
  2506. return "无星级";
  2507. }
  2508. private static int attendancePenalty(int kqlbm) {
  2509. return switch (kqlbm) {
  2510. case 1 -> 3;
  2511. case 21 -> 1;
  2512. case 51 -> 1;
  2513. case 52 -> 1;
  2514. case 61 -> 2;
  2515. default -> 0;
  2516. };
  2517. }
  2518. private static boolean isZhixinStarImport(String marker) {
  2519. return StringUtils.startsWith(marker, ZHIXIN_STAR_MARKER_PREFIX);
  2520. }
  2521. private static YearMonth rdMonth(HttpServletRequest request) {
  2522. String value = StringUtils.trimToNull(request.getParameter("month"));
  2523. YearMonth now = YearMonth.now(CN_ZONE);
  2524. YearMonth month = value == null ? now : YearMonth.parse(value, MONTH_FMT);
  2525. if (month.isAfter(now))
  2526. throw new IllegalArgumentException("不能查询未来月份");
  2527. return month;
  2528. }
  2529. private static LocalDate rdDate(HttpServletRequest request, String name) {
  2530. String value = StringUtils.trimToNull(request.getParameter(name));
  2531. if (value == null)
  2532. throw new IllegalArgumentException("缺少参数:" + name);
  2533. return LocalDate.parse(value, DATE_FMT);
  2534. }
  2535. private static long rdLongParam(HttpServletRequest request, String name,
  2536. boolean required) {
  2537. String value = StringUtils.trimToNull(request.getParameter(name));
  2538. if (value == null) {
  2539. if (required)
  2540. throw new IllegalArgumentException("缺少参数:" + name);
  2541. return 0L;
  2542. }
  2543. try {
  2544. return Long.parseLong(value);
  2545. } catch (NumberFormatException e) {
  2546. throw new IllegalArgumentException("参数格式错误:" + name);
  2547. }
  2548. }
  2549. private static int rdIntParam(HttpServletRequest request, String name,
  2550. int defaultValue) {
  2551. String value = StringUtils.trimToNull(request.getParameter(name));
  2552. if (value == null)
  2553. return defaultValue;
  2554. try {
  2555. return Integer.parseInt(value);
  2556. } catch (NumberFormatException e) {
  2557. throw new IllegalArgumentException("参数格式错误:" + name);
  2558. }
  2559. }
  2560. private static int clampPageSize(int value) {
  2561. return Math.max(1, Math.min(100, value));
  2562. }
  2563. private static Map<Long, Double> rdScoreMap(
  2564. List<Map<String, Object>> rows) {
  2565. Map<Long, Double> result = new HashMap<>();
  2566. for (Map<String, Object> row : rows)
  2567. result.put(rdLong(row.get("bjid")), round2(
  2568. rdDouble(row.get("score"))));
  2569. return result;
  2570. }
  2571. private static LocalDate rdLocalDate(Object value) {
  2572. if (value instanceof Date)
  2573. return ((Date) value).toLocalDate();
  2574. if (value instanceof Timestamp)
  2575. return ((Timestamp) value).toLocalDateTime().toLocalDate();
  2576. if (value == null)
  2577. return null;
  2578. String text = String.valueOf(value);
  2579. if (text.length() >= 10)
  2580. return LocalDate.parse(text.substring(0, 10), DATE_FMT);
  2581. return null;
  2582. }
  2583. private static String formatTimestamp(Object value) {
  2584. if (value instanceof Timestamp)
  2585. return ((Timestamp) value).toLocalDateTime().format(DATETIME_FMT);
  2586. return value == null ? "" : String.valueOf(value);
  2587. }
  2588. private static String weekdayCn(LocalDate date) {
  2589. DayOfWeek day = date.getDayOfWeek();
  2590. return switch (day) {
  2591. case MONDAY -> "星期一";
  2592. case TUESDAY -> "星期二";
  2593. case WEDNESDAY -> "星期三";
  2594. case THURSDAY -> "星期四";
  2595. case FRIDAY -> "星期五";
  2596. case SATURDAY -> "星期六";
  2597. case SUNDAY -> "星期日";
  2598. };
  2599. }
  2600. private static long rdLong(Object value) {
  2601. return value == null ? 0L : Long.parseLong(String.valueOf(value));
  2602. }
  2603. private static Long rdLongObj(Object value) {
  2604. return value == null ? null : Long.valueOf(String.valueOf(value));
  2605. }
  2606. private static int rdInt(Object value) {
  2607. return value == null ? 0 : Integer.parseInt(String.valueOf(value));
  2608. }
  2609. private static double rdDouble(Object value) {
  2610. return value == null ? 0.0 : Double.parseDouble(String.valueOf(value));
  2611. }
  2612. private static String rdStr(Object value) {
  2613. return value == null ? null : String.valueOf(value);
  2614. }
  2615. private static double round2(double value) {
  2616. return Math.round(value * 100.0) / 100.0;
  2617. }
  2618. private static boolean isMissingTable(SQLException e, String tableName) {
  2619. String message = StringUtils.defaultString(e.getMessage()).toUpperCase();
  2620. return e.getErrorCode() == 942
  2621. && message.contains(tableName.toUpperCase());
  2622. }
  2623. private static final class MonthPeriod {
  2624. final YearMonth month;
  2625. final Timestamp start;
  2626. final Timestamp end;
  2627. final LocalDate maxDate;
  2628. final int maxDay;
  2629. private MonthPeriod(YearMonth month) {
  2630. this.month = month;
  2631. LocalDate first = month.atDay(1);
  2632. LocalDate today = LocalDate.now(CN_ZONE);
  2633. LocalDate monthEnd = month.atEndOfMonth();
  2634. this.maxDate = month.equals(YearMonth.from(today))
  2635. ? today : monthEnd;
  2636. this.maxDay = maxDate.getDayOfMonth();
  2637. this.start = Timestamp.valueOf(first.atStartOfDay());
  2638. this.end = Timestamp.valueOf(maxDate.plusDays(1).atStartOfDay());
  2639. }
  2640. static MonthPeriod of(YearMonth month) {
  2641. return new MonthPeriod(month);
  2642. }
  2643. }
  2644. private static final class ClassInfo {
  2645. final long bjid;
  2646. final String bjmc;
  2647. final Long dwid;
  2648. final String dwmc;
  2649. ClassInfo(long bjid, String bjmc, Long dwid, String dwmc) {
  2650. this.bjid = bjid;
  2651. this.bjmc = bjmc;
  2652. this.dwid = dwid;
  2653. this.dwmc = dwmc;
  2654. }
  2655. }
  2656. private record CategoryDef(String code, String label, double weight) {
  2657. }
  2658. private static final class DailyPoint {
  2659. final LocalDate date;
  2660. final double score;
  2661. final int count;
  2662. DailyPoint(LocalDate date, double score, int count) {
  2663. this.date = date;
  2664. this.score = score;
  2665. this.count = count;
  2666. }
  2667. }
  2668. private static final class DailyAccumulator {
  2669. final LocalDate date;
  2670. double regularDeduction;
  2671. boolean hasRegular;
  2672. DailyAccumulator(LocalDate date) {
  2673. this.date = date;
  2674. }
  2675. Map<String, Object> toMap() {
  2676. Map<String, Object> row = new LinkedHashMap<>();
  2677. row.put("date", date.format(DATE_FMT));
  2678. row.put("weekday", weekdayCn(date));
  2679. row.put("regularDeduction", round2(regularDeduction));
  2680. row.put("totalDeduction", round2(regularDeduction));
  2681. row.put("hasRegular", hasRegular);
  2682. return row;
  2683. }
  2684. }
  2685. private static final class CategoryDaily {
  2686. final ClassInfo cls;
  2687. final Map<String, Map<LocalDate, DailyPoint>> actual = new HashMap<>();
  2688. final Map<String, String> sources = new HashMap<>();
  2689. CategoryDaily(ClassInfo cls) {
  2690. this.cls = cls;
  2691. }
  2692. Map<String, Object> toMap(CategoryDef def, LocalDate date) {
  2693. String source = sources.getOrDefault(def.code, SOURCE_MISSING);
  2694. DailyPoint point = null;
  2695. if (SOURCE_REAL.equals(source)) {
  2696. Map<LocalDate, DailyPoint> values = actual.get(def.code);
  2697. point = values == null ? null : values.get(date);
  2698. }
  2699. boolean available;
  2700. double score;
  2701. double deduction;
  2702. if (point != null) {
  2703. available = true;
  2704. score = point.score;
  2705. deduction = Math.max(0.0, 100.0 - score);
  2706. } else {
  2707. available = false;
  2708. score = 0.0;
  2709. deduction = 0.0;
  2710. }
  2711. Map<String, Object> row = new LinkedHashMap<>();
  2712. row.put("code", def.code);
  2713. row.put("label", def.label);
  2714. row.put("score", round2(score));
  2715. row.put("deduction", round2(deduction));
  2716. row.put("source", source);
  2717. row.put("available", available);
  2718. row.put("count", point == null ? 0 : point.count);
  2719. return row;
  2720. }
  2721. double regularDeduction(LocalDate date) {
  2722. double total = 0.0;
  2723. for (CategoryDef def : CATEGORY_DEFS) {
  2724. Map<String, Object> row = toMap(def, date);
  2725. total += rdDouble(row.get("deduction"));
  2726. }
  2727. return total;
  2728. }
  2729. }
  2730. private static final class ScoreBook {
  2731. Map<Long, Double> dorm = new HashMap<>();
  2732. Map<Long, Double> dormAttendance = new HashMap<>();
  2733. Map<Long, Double> classroom = new HashMap<>();
  2734. Map<Long, Double> flag = new HashMap<>();
  2735. Map<Long, Double> cleaning = new HashMap<>();
  2736. Map<Long, Double> attendance = new HashMap<>();
  2737. Map<Long, Double> discipline = new HashMap<>();
  2738. Map<Long, AdjustmentTotals> adjustments = new HashMap<>();
  2739. Map<Long, ClassScore> classScores = new HashMap<>();
  2740. Map<String, String> categorySources(long bjid) {
  2741. ClassScore score = classScores.get(bjid);
  2742. return score == null ? Collections.emptyMap() : score.sources;
  2743. }
  2744. }
  2745. private static final class ClassScore {
  2746. final Map<String, Double> values = new LinkedHashMap<>();
  2747. final Map<String, String> sources = new LinkedHashMap<>();
  2748. final List<String> missing = new ArrayList<>();
  2749. AdjustmentTotals adjust = new AdjustmentTotals();
  2750. double total;
  2751. boolean hasMissing;
  2752. }
  2753. private static final class RankBook {
  2754. final Map<Long, Integer> schoolRank = new HashMap<>();
  2755. final Map<Long, Integer> departmentRank = new HashMap<>();
  2756. final Map<Long, Integer> departmentClassCount = new HashMap<>();
  2757. }
  2758. private static final class DisciplinePenalties {
  2759. final Map<Long, Map<LocalDate, Double>> mapped = new HashMap<>();
  2760. final Map<Long, Map<LocalDate, Double>> legacy = new HashMap<>();
  2761. }
  2762. private static final class Adjustment {
  2763. final long id;
  2764. final LocalDate date;
  2765. final double score;
  2766. final String itemName;
  2767. final String remark;
  2768. final String sourceType;
  2769. final long sourceRecordId;
  2770. final String studentName;
  2771. Adjustment(long id, LocalDate date, double score,
  2772. String itemName, String remark, String sourceType,
  2773. long sourceRecordId, String studentName) {
  2774. this.id = id;
  2775. this.date = date;
  2776. this.score = score;
  2777. this.itemName = itemName;
  2778. this.remark = remark;
  2779. this.sourceType = sourceType;
  2780. this.sourceRecordId = sourceRecordId;
  2781. this.studentName = studentName;
  2782. }
  2783. }
  2784. private static final class AdjustmentTotals {
  2785. double bonus;
  2786. double deduction;
  2787. int bonusEvents;
  2788. int deductionEvents;
  2789. }
  2790. private static final class PublishedSnapshot {
  2791. double total;
  2792. String starLevel;
  2793. double bonus;
  2794. double deduction;
  2795. int bonusCount;
  2796. int deductionCount;
  2797. int departmentRank;
  2798. int departmentClassCount;
  2799. int schoolRank;
  2800. int schoolClassCount;
  2801. final Map<String, String> sources = new LinkedHashMap<>();
  2802. }
  2803. }