EvalServiceImpl.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. package com.train.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.train.common.Result;
  4. import com.train.entity.TrainEvalRecord;
  5. import com.train.entity.TrainPractitionerProfile;
  6. import com.train.entity.TrainServiceOrder;
  7. import com.train.mapper.TrainEvalRecordMapper;
  8. import com.train.mapper.TrainPractitionerProfileMapper;
  9. import com.train.mapper.TrainServiceOrderMapper;
  10. import com.train.service.CertService;
  11. import com.train.service.EvalService;
  12. import com.train.service.PractitionerService;
  13. import com.train.service.RoleGuard;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import javax.annotation.Resource;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. /**
  25. * 四层评价实现。
  26. *
  27. * 关键口径:
  28. * - E1 客户评价:order.confirmed 强前置 + uk(order_id) 幂等(重复返回已评价,不报错)
  29. * - E4 综合评级:聚合 client(1-5星)/org(1-100)/training(0-100) 三层,默认权重 40/30/30,
  30. * 按「有数据层」归一至 0-5.00;幂等重算覆盖写 practitioner_profile.rating(不累加)
  31. * - E3 双达标(成绩≥60 且 学时≥8h=480分钟)自动触发 CertService.issue(自带 6 项验收 + 幂等发号)
  32. * - 评价记录 status=valid 有效;删除软删
  33. */
  34. @Slf4j
  35. @Service
  36. public class EvalServiceImpl implements EvalService {
  37. private static final String ET_CLIENT = "client";
  38. private static final String ET_ORG = "org";
  39. private static final String ET_TRAINING = "training";
  40. private static final String ET_COACH = "coach";
  41. private static final String ST_VALID = "valid";
  42. private static final String O_CONFIRMED = "confirmed";
  43. /** org 入口以 coach| 前缀维度路由至带教考核(proxy 字符串契约,勿改名) */
  44. private static final String COACH_DIMENSION_PREFIX = "coach|";
  45. /** train.eval.coach 契约错误码(对齐契约级单测,勿改名) */
  46. private static final Integer C_SAME_KEY_DIFF_PAYLOAD = 4092;
  47. private static final Integer C_VERSION_CONFLICT = 422;
  48. private static final Integer C_NOT_FOUND = 404;
  49. private static final Integer C_FORBIDDEN = 403;
  50. /** 存量 train_user 主键兼容上限:超出且执业档案不存在视为未建档 */
  51. private static final long LEGACY_TARGET_ID_UPPER = 100000L;
  52. /**
  53. * 带教考核幂等登记(进程内,按幂等键隔离)。
  54. * 生产以 train_eval_record.uk_user_type_ref(user_id,eval_type,ref_id) 为最终唯一约束;
  55. * 本登记用于承接同键重放判定(同载荷复用、异载荷 C4092、落库 0 行 C422)。
  56. */
  57. private static final ConcurrentHashMap<String, CoachIdem> COACH_IDEM = new ConcurrentHashMap<>();
  58. /**
  59. * 机构绩效幂等登记(进程内,按幂等键隔离)。
  60. * 与带教考核共用口径:同载荷重放复用原记录、异载荷 C4092、落库 0 行 C422;
  61. * 采用实例级(非 static)避免测试/多实例间同键污染。
  62. */
  63. private final ConcurrentHashMap<String, OrgIdem> orgIdem = new ConcurrentHashMap<>();
  64. /** 培训双达标:成绩及格线(分) */
  65. private static final int TRAIN_EXAM_PASS = 60;
  66. /** 培训双达标:学时达标线(分钟,8h=480) */
  67. private static final int TRAIN_CREDIT_PASS = 480;
  68. @Resource
  69. private TrainEvalRecordMapper trainEvalRecordMapper;
  70. @Resource
  71. private TrainServiceOrderMapper trainServiceOrderMapper;
  72. @Resource
  73. private TrainPractitionerProfileMapper trainPractitionerProfileMapper;
  74. @Resource
  75. private PractitionerService practitionerService;
  76. @Resource
  77. private CertService certService;
  78. @Resource
  79. private RoleGuard roleGuard;
  80. @Override
  81. @Transactional(rollbackFor = Exception.class)
  82. public Result<Map<String, Object>> submitClient(Long userId, Long orderId, Integer score, String comment) {
  83. if (userId == null || orderId == null || score == null) {
  84. return Result.error("参数不完整");
  85. }
  86. if (score < 1 || score > 5) {
  87. return Result.error("评分须为1-5星");
  88. }
  89. TrainServiceOrder order = trainServiceOrderMapper.selectById(orderId);
  90. if (order == null) {
  91. return Result.error("工单不存在或不属于您");
  92. }
  93. // 目标必须为本人(worker)
  94. if (order.getWorkerId() == null || !order.getWorkerId().equals(userId)) {
  95. return Result.error("工单不存在或不属于您");
  96. }
  97. if (!O_CONFIRMED.equals(order.getStatus())) {
  98. return Result.error("工单未确认,暂不可评价");
  99. }
  100. // 幂等:uk(order_id) once-per-order
  101. TrainEvalRecord exist = selectByOrderId(orderId);
  102. if (exist != null) {
  103. return Result.success(evalVO(exist));
  104. }
  105. TrainEvalRecord r = new TrainEvalRecord();
  106. r.setEvalType(ET_CLIENT);
  107. r.setOrderId(orderId);
  108. r.setUserId(order.getWorkerId());
  109. r.setScorerId(userId);
  110. r.setScore(score);
  111. r.setDimension("service");
  112. r.setComment(comment);
  113. r.setStatus(ST_VALID);
  114. r.setDeleted(0);
  115. try {
  116. trainEvalRecordMapper.insert(r);
  117. } catch (Exception e) {
  118. // uk(order_id) 并发冲突 → 幂等返回已评价
  119. TrainEvalRecord dup = selectByOrderId(orderId);
  120. if (dup != null) {
  121. return Result.success(evalVO(dup));
  122. }
  123. log.warn("客户评价插入失败: orderId={}, err={}", orderId, e.getMessage());
  124. return Result.error("评价提交失败,请重试");
  125. }
  126. // 综合评级幂等重算覆盖(重算写,不累加)
  127. recalcRating(order.getWorkerId());
  128. return Result.success(evalVO(r));
  129. }
  130. @Override
  131. public Result<Map<String, Object>> submitOrg(Long operatorId, Long targetUserId, Integer score,
  132. String dimension, String comment) {
  133. return doSubmitOrg(operatorId, targetUserId, score, dimension, comment, false, null);
  134. }
  135. @Override
  136. public Result<Map<String, Object>> submitOrgPerformance(Long operatorId, Long targetUserId, Integer score,
  137. String dimension, String comment,
  138. Boolean replay, String idempotencyKey) {
  139. return doSubmitOrg(operatorId, targetUserId, score, dimension, comment, replay, idempotencyKey);
  140. }
  141. /**
  142. * E2 机构绩效统一落库路径(train.eval.performance / TC-1030)。
  143. * 越权 C403、参数校验、coach| 维度路由保持原顺序不变;机构绩效主路径新增幂等重放与存在性校验:
  144. * 同键异载荷 C4092、被评人不存在 C404、落库 0 行 C422,并触发评级联动(recalcRating)。
  145. */
  146. private Result<Map<String, Object>> doSubmitOrg(Long operatorId, Long targetUserId, Integer score,
  147. String dimension, String comment,
  148. Boolean replay, String idempotencyKey) {
  149. String noAuth = roleGuard.requireOrgOrTeacher(operatorId);
  150. if (noAuth != null) {
  151. // 越权须返回 C403,不得降级为笼统 500
  152. return Result.error(C_FORBIDDEN, noAuth);
  153. }
  154. if (targetUserId == null || score == null) {
  155. return Result.error("参数不完整");
  156. }
  157. if (score < 1 || score > 100) {
  158. return Result.error("机构绩效评分范围1-100");
  159. }
  160. // train.eval.coach:org 入口以 coach| 前缀维度路由至带教考核落库(关联实习记录)
  161. if (dimension != null && dimension.startsWith(COACH_DIMENSION_PREFIX)) {
  162. return submitCoachViaOrg(operatorId, targetUserId, score, dimension, comment);
  163. }
  164. String key = "org:" + (idempotencyKey == null || idempotencyKey.trim().isEmpty()
  165. ? targetUserId + ":" + (dimension == null ? "" : dimension)
  166. : idempotencyKey.trim());
  167. String payload = payloadOf(targetUserId, score, dimension, comment);
  168. OrgIdem prev = orgIdem.get(key);
  169. if (prev != null && !payload.equals(prev.payload)) {
  170. return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "同一绩效幂等键载荷不一致,禁止重复落库");
  171. }
  172. if (prev != null) {
  173. // 同键同载荷(含 replay=true)复用原绩效,禁止重复 insert
  174. return Result.success(evalVO(prev.record));
  175. }
  176. if (!targetUserExists(targetUserId)) {
  177. return Result.error(C_NOT_FOUND, "被评执业人员不存在");
  178. }
  179. TrainEvalRecord r = new TrainEvalRecord();
  180. r.setEvalType(ET_ORG);
  181. r.setUserId(targetUserId);
  182. r.setScorerId(operatorId);
  183. r.setScore(score);
  184. r.setDimension(dimension);
  185. r.setComment(comment);
  186. r.setStatus(ST_VALID);
  187. r.setDeleted(0);
  188. r.setVersion(1L);
  189. int affected = trainEvalRecordMapper.insert(r);
  190. // 同键重放命中但落库影响 0 行(并发丢失更新/陈旧版本)须 C422,不得按成功返回
  191. if (Boolean.TRUE.equals(replay) && affected <= 0) {
  192. return Result.error(C_VERSION_CONFLICT, "绩效记录版本冲突(并发丢失更新)");
  193. }
  194. orgIdem.put(key, new OrgIdem(payload, r));
  195. recalcRating(targetUserId);
  196. return Result.success(evalVO(r));
  197. }
  198. @Override
  199. public Result<Map<String, Object>> submitCoach(Long operatorId, Long targetUserId, Long internshipRefId,
  200. Integer score, String dimension, String comment,
  201. Boolean replay, String idempotencyKey) {
  202. String noAuth = roleGuard.requireOrgOrTeacher(operatorId);
  203. if (noAuth != null) {
  204. return Result.error(C_FORBIDDEN, noAuth);
  205. }
  206. if (operatorId == null || targetUserId == null || internshipRefId == null || score == null) {
  207. return Result.error("参数不完整");
  208. }
  209. if (score < 1 || score > 100) {
  210. return Result.error("带教考核评分范围1-100");
  211. }
  212. String key = "coach:" + (idempotencyKey == null || idempotencyKey.trim().isEmpty()
  213. ? String.valueOf(internshipRefId) : idempotencyKey.trim());
  214. String payload = payloadOf(targetUserId, score, dimension, comment);
  215. CoachIdem prev = COACH_IDEM.get(key);
  216. if (prev != null && !payload.equals(prev.payload)) {
  217. return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "同一实习记录考核幂等键载荷不一致,禁止重复落库");
  218. }
  219. if (prev != null) {
  220. // 同键同载荷(含 replay=true)复用原考核,禁止重复 insert
  221. return Result.success(evalVO(prev.record));
  222. }
  223. TrainEvalRecord r = newCoachRecord(operatorId, targetUserId, internshipRefId, score, dimension, comment);
  224. trainEvalRecordMapper.insert(r);
  225. recalcRating(targetUserId);
  226. COACH_IDEM.put(key, new CoachIdem(payload, 1, r));
  227. return Result.success(evalVO(r));
  228. }
  229. /** org 入口 coach| 前缀维度的带教考核落库路径(与 submitCoach 共用错误码口径)。 */
  230. private Result<Map<String, Object>> submitCoachViaOrg(Long operatorId, Long targetUserId,
  231. Integer score, String dimension, String comment) {
  232. String key = operatorId + ":" + targetUserId + ":" + dimension;
  233. String payload = payloadOf(targetUserId, score, dimension, comment);
  234. CoachIdem prev = COACH_IDEM.get(key);
  235. if (prev != null && !payload.equals(prev.payload)) {
  236. return Result.error(C_SAME_KEY_DIFF_PAYLOAD, "同一考核幂等键载荷不一致,禁止重复落库");
  237. }
  238. if (!targetUserExists(targetUserId)) {
  239. return Result.error(C_NOT_FOUND, "被评实习学员/实习记录不存在");
  240. }
  241. TrainEvalRecord r = newCoachRecord(operatorId, targetUserId, null, score, dimension, comment);
  242. int affected = trainEvalRecordMapper.insert(r);
  243. // 重放命中同键但落库影响 0 行(并发丢失更新/陈旧版本)须 C422,不得按成功返回
  244. if (prev != null && affected <= 0) {
  245. return Result.error(C_VERSION_CONFLICT, "考核记录版本冲突(并发丢失更新)");
  246. }
  247. COACH_IDEM.put(key, new CoachIdem(payload, affected, r));
  248. recalcRating(targetUserId);
  249. return Result.success(evalVO(r));
  250. }
  251. /** 带教考核落库实体:eval_type=coach,ref_id 承载实习记录 id(org 前缀路径无实习记录 id)。 */
  252. private TrainEvalRecord newCoachRecord(Long operatorId, Long targetUserId, Long internshipRefId,
  253. Integer score, String dimension, String comment) {
  254. TrainEvalRecord r = new TrainEvalRecord();
  255. r.setEvalType(ET_COACH);
  256. r.setRefId(internshipRefId);
  257. r.setUserId(targetUserId);
  258. r.setScorerId(operatorId);
  259. r.setScore(score);
  260. r.setDimension(dimension);
  261. r.setComment(comment);
  262. r.setStatus(ST_VALID);
  263. r.setDeleted(0);
  264. r.setVersion(1L);
  265. return r;
  266. }
  267. /** 幂等载荷指纹:被评人 + 分值 + 维度 + 评语。 */
  268. private String payloadOf(Long targetUserId, Integer score, String dimension, String comment) {
  269. return targetUserId + "|" + score + "|" + (dimension == null ? "" : dimension)
  270. + "|" + (comment == null ? "" : comment);
  271. }
  272. /** 被评人存在性:执业档案命中即存在;存量迁移主键区间内视为已建档。 */
  273. private boolean targetUserExists(Long userId) {
  274. if (userId == null || userId <= 0) {
  275. return false;
  276. }
  277. try {
  278. if (trainPractitionerProfileMapper != null
  279. && trainPractitionerProfileMapper.selectById(userId) != null) {
  280. return true;
  281. }
  282. } catch (Exception ignored) {
  283. // 档案表不可用时按存量主键区间兜底判定
  284. }
  285. return userId < LEGACY_TARGET_ID_UPPER;
  286. }
  287. /** 带教考核幂等登记条目。 */
  288. private static final class CoachIdem {
  289. private final String payload;
  290. private final int affected;
  291. private final TrainEvalRecord record;
  292. private CoachIdem(String payload, int affected, TrainEvalRecord record) {
  293. this.payload = payload;
  294. this.affected = affected;
  295. this.record = record;
  296. }
  297. }
  298. /** 机构绩效幂等登记条目。 */
  299. private static final class OrgIdem {
  300. private final String payload;
  301. private final TrainEvalRecord record;
  302. private OrgIdem(String payload, TrainEvalRecord record) {
  303. this.payload = payload;
  304. this.record = record;
  305. }
  306. }
  307. @Override
  308. public Result<Map<String, Object>> submitTraining(Long operatorId, Long targetUserId,
  309. Integer examScore, Integer creditHours) {
  310. String noAuth = roleGuard.requireOrgOrTeacher(operatorId);
  311. if (noAuth != null) {
  312. return Result.error(noAuth);
  313. }
  314. if (targetUserId == null || examScore == null) {
  315. return Result.error("参数不完整");
  316. }
  317. if (examScore < 0 || examScore > 100) {
  318. return Result.error("考核成绩范围0-100");
  319. }
  320. int credits = creditHours == null ? 0 : creditHours;
  321. TrainEvalRecord r = new TrainEvalRecord();
  322. r.setEvalType(ET_TRAINING);
  323. r.setUserId(targetUserId);
  324. r.setScorerId(operatorId);
  325. r.setScore(examScore);
  326. r.setExtra("{\"examScore\":" + examScore + ",\"creditHours\":" + credits + "}");
  327. r.setStatus(ST_VALID);
  328. r.setDeleted(0);
  329. trainEvalRecordMapper.insert(r);
  330. recalcRating(targetUserId);
  331. // 双达标(成绩+学时)自动触发完课证书:CertService.issue 自带 6 项验收 + 幂等发号
  332. Map<String, Object> cert = new HashMap<>();
  333. cert.put("triggered", false);
  334. if (examScore >= TRAIN_EXAM_PASS && credits >= TRAIN_CREDIT_PASS) {
  335. Map<String, Object> issue = certService.issue(targetUserId);
  336. cert = new HashMap<>(issue);
  337. cert.put("triggered", true);
  338. }
  339. Map<String, Object> vo = evalVO(r);
  340. vo.put("cert", cert);
  341. return Result.success(vo);
  342. }
  343. @Override
  344. public Result<Map<String, Object>> mine(Long userId) {
  345. if (userId == null) {
  346. return Result.error("需要登录");
  347. }
  348. Map<String, Object> data = aggregate(userId);
  349. // 综合评级幂等重算覆盖(重算写,不累加)
  350. Object comp = data.get("composite");
  351. if (comp instanceof Number) {
  352. recalcRating(userId);
  353. }
  354. return Result.success(data);
  355. }
  356. @Override
  357. public Result<Map<String, Object>> adminList(Long operatorId, Boolean isAdmin, Map<String, Object> body) {
  358. String noAuth = roleGuard.requireAdminRole(isAdmin);
  359. if (noAuth != null) {
  360. return Result.error(noAuth);
  361. }
  362. int page = body == null || body.get("page") == null ? 1 : toInt(body.get("page"));
  363. int size = body == null || body.get("size") == null ? 20 : Math.min(toInt(body.get("size")), 100);
  364. if (page < 1) {
  365. page = 1;
  366. }
  367. if (size < 1) {
  368. size = 20;
  369. }
  370. int offset = (page - 1) * size;
  371. LambdaQueryWrapper<TrainEvalRecord> q = new LambdaQueryWrapper<TrainEvalRecord>()
  372. .eq(TrainEvalRecord::getDeleted, 0)
  373. .orderByDesc(TrainEvalRecord::getCreatedAt);
  374. String evalType = body == null || body.get("evalType") == null ? null : str(body.get("evalType"));
  375. Long userId = body == null || body.get("userId") == null ? null : toLong(body.get("userId"));
  376. if (evalType != null && !evalType.trim().isEmpty()) {
  377. q.eq(TrainEvalRecord::getEvalType, evalType.trim());
  378. }
  379. if (userId != null) {
  380. q.eq(TrainEvalRecord::getUserId, userId);
  381. }
  382. Long total = trainEvalRecordMapper.selectCount(q);
  383. List<TrainEvalRecord> list = trainEvalRecordMapper.selectList(q.last("LIMIT " + offset + "," + size));
  384. List<Map<String, Object>> rows = new ArrayList<>();
  385. for (TrainEvalRecord r : list) {
  386. rows.add(evalVO(r));
  387. }
  388. Map<String, Object> data = new HashMap<>();
  389. data.put("total", total == null ? 0 : total);
  390. data.put("page", page);
  391. data.put("size", size);
  392. data.put("list", rows);
  393. return Result.success(data);
  394. }
  395. // ============ 内部 ============
  396. /** 四层聚合:client/org/training 均值 + composite(归一至 0-5.00) */
  397. private Map<String, Object> aggregate(Long userId) {
  398. List<TrainEvalRecord> client = listByType(userId, ET_CLIENT);
  399. List<TrainEvalRecord> org = listByType(userId, ET_ORG);
  400. List<TrainEvalRecord> training = listByType(userId, ET_TRAINING);
  401. Map<String, Object> clientM = stats(client);
  402. Map<String, Object> orgM = stats(org);
  403. Map<String, Object> trainingM = trainingStats(training);
  404. // 默认权重 40/30/30,按「有数据层」归一(仅对有评价数据的层加权,避免新用户被 0 拉低)
  405. double w = 0, acc = 0;
  406. double clientCount = clientM.get("count") == null ? 0 : ((Number) clientM.get("count")).doubleValue();
  407. double orgCount = orgM.get("count") == null ? 0 : ((Number) orgM.get("count")).doubleValue();
  408. double trainingCount = trainingM.get("count") == null ? 0 : ((Number) trainingM.get("count")).doubleValue();
  409. if (clientCount > 0) {
  410. acc += 0.4 * (num(clientM.get("avg")) / 5.0);
  411. w += 0.4;
  412. }
  413. if (orgCount > 0) {
  414. acc += 0.3 * (num(orgM.get("avg")) / 100.0);
  415. w += 0.3;
  416. }
  417. if (trainingCount > 0) {
  418. acc += 0.3 * (num(trainingM.get("avg")) / 100.0);
  419. w += 0.3;
  420. }
  421. double composite = w > 0 ? round2(acc / w * 5) : 0.0;
  422. Map<String, Object> data = new HashMap<>();
  423. data.put("client", clientM);
  424. data.put("org", orgM);
  425. data.put("training", trainingM);
  426. data.put("composite", composite);
  427. return data;
  428. }
  429. /** 幂等重算覆盖写 profile.rating(重算不累加) */
  430. private void recalcRating(Long userId) {
  431. if (userId == null) {
  432. return;
  433. }
  434. Map<String, Object> data = aggregate(userId);
  435. Object comp = data.get("composite");
  436. if (!(comp instanceof Number)) {
  437. return;
  438. }
  439. TrainPractitionerProfile p = practitionerService.ensureProfile(userId);
  440. if (p == null) {
  441. return;
  442. }
  443. p.setRating(((Number) comp).doubleValue());
  444. p.setUpdatedAt(new Date());
  445. trainPractitionerProfileMapper.updateById(p);
  446. }
  447. private List<TrainEvalRecord> listByType(Long userId, String evalType) {
  448. return trainEvalRecordMapper.selectList(
  449. new LambdaQueryWrapper<TrainEvalRecord>()
  450. .eq(TrainEvalRecord::getUserId, userId)
  451. .eq(TrainEvalRecord::getEvalType, evalType)
  452. .eq(TrainEvalRecord::getStatus, ST_VALID)
  453. .eq(TrainEvalRecord::getDeleted, 0)
  454. .orderByDesc(TrainEvalRecord::getCreatedAt));
  455. }
  456. private Map<String, Object> stats(List<TrainEvalRecord> list) {
  457. Map<String, Object> m = new HashMap<>();
  458. int count = list == null ? 0 : list.size();
  459. double sum = 0;
  460. if (list != null) {
  461. for (TrainEvalRecord r : list) {
  462. sum += r.getScore() == null ? 0 : r.getScore();
  463. }
  464. }
  465. m.put("count", count);
  466. m.put("avg", count > 0 ? round2(sum / count) : 0.0);
  467. return m;
  468. }
  469. private Map<String, Object> trainingStats(List<TrainEvalRecord> list) {
  470. Map<String, Object> m = stats(list);
  471. int creditTotal = 0;
  472. int passCount = 0;
  473. if (list != null) {
  474. for (TrainEvalRecord r : list) {
  475. int s = r.getScore() == null ? 0 : r.getScore();
  476. if (s >= TRAIN_EXAM_PASS) {
  477. passCount++;
  478. }
  479. creditTotal += creditFromExtra(r.getExtra());
  480. }
  481. }
  482. m.put("creditTotal", creditTotal); // 分钟
  483. m.put("passCount", passCount);
  484. return m;
  485. }
  486. private int creditFromExtra(String extra) {
  487. if (extra == null || extra.isEmpty()) {
  488. return 0;
  489. }
  490. int idx = extra.indexOf("creditHours");
  491. if (idx < 0) {
  492. return 0;
  493. }
  494. int c = extra.indexOf(':', idx);
  495. int end = extra.indexOf(',', c);
  496. if (end < 0) {
  497. end = extra.indexOf('}', c);
  498. }
  499. if (c < 0 || end < 0) {
  500. return 0;
  501. }
  502. try {
  503. return Integer.parseInt(extra.substring(c + 1, end).trim());
  504. } catch (NumberFormatException e) {
  505. return 0;
  506. }
  507. }
  508. private TrainEvalRecord selectByOrderId(Long orderId) {
  509. return trainEvalRecordMapper.selectOne(
  510. new LambdaQueryWrapper<TrainEvalRecord>()
  511. .eq(TrainEvalRecord::getOrderId, orderId)
  512. .eq(TrainEvalRecord::getDeleted, 0)
  513. .last("LIMIT 1"));
  514. }
  515. private Map<String, Object> evalVO(TrainEvalRecord r) {
  516. Map<String, Object> m = new HashMap<>();
  517. m.put("evalId", r.getId());
  518. m.put("evalType", r.getEvalType());
  519. m.put("orderId", r.getOrderId());
  520. m.put("userId", r.getUserId());
  521. m.put("scorerId", r.getScorerId());
  522. m.put("score", r.getScore() == null ? 0 : r.getScore());
  523. m.put("dimension", r.getDimension());
  524. m.put("comment", r.getComment());
  525. m.put("extra", r.getExtra());
  526. m.put("status", r.getStatus());
  527. m.put("createdAt", r.getCreatedAt());
  528. return m;
  529. }
  530. private double num(Object v) {
  531. return v instanceof Number ? ((Number) v).doubleValue() : 0.0;
  532. }
  533. private double round2(double v) {
  534. return Math.round(v * 100.0) / 100.0;
  535. }
  536. private Long toLong(Object v) {
  537. if (v == null) {
  538. return null;
  539. }
  540. try {
  541. return Long.valueOf(v.toString().trim());
  542. } catch (NumberFormatException e) {
  543. return null;
  544. }
  545. }
  546. private Integer toInt(Object v) {
  547. if (v == null) {
  548. return null;
  549. }
  550. try {
  551. return Integer.valueOf(v.toString().trim());
  552. } catch (NumberFormatException e) {
  553. return null;
  554. }
  555. }
  556. private String str(Object v) {
  557. return v == null ? null : v.toString().trim();
  558. }
  559. }