RealExamService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. package com.ruoyi.sim.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Objects;
  5. import com.ruoyi.common.core.domain.AjaxResult;
  6. import com.ruoyi.common.utils.DateUtils;
  7. import com.ruoyi.sim.config.SimConfig;
  8. import com.ruoyi.sim.config.SimDebugConfig;
  9. import com.ruoyi.sim.domain.*;
  10. import com.ruoyi.sim.domain.vo.RealExamVo;
  11. import com.ruoyi.sim.domain.vo.StudentRealExamIngVo;
  12. import com.ruoyi.sim.domain.vo.StudentRealExamPostVo;
  13. import com.ruoyi.sim.domain.vo.StudentRealExamPreVo;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.context.annotation.Lazy;
  19. import org.springframework.stereotype.Service;
  20. import com.ruoyi.sim.mapper.RealExamMapper;
  21. import org.springframework.transaction.annotation.Transactional;
  22. /**
  23. * 考试Service业务层处理
  24. *
  25. * @author tom
  26. * @date 2024-12-15
  27. */
  28. @Service
  29. public class RealExamService {
  30. @Autowired
  31. private RealExamMapper realExamMapper;
  32. /**
  33. * 查询考试
  34. *
  35. * @param examId 考试主键
  36. * @return 考试
  37. */
  38. public RealExam selectRealExamByExamId(Long examId) {
  39. return realExamMapper.selectRealExamByExamId(examId);
  40. }
  41. /**
  42. * 查询考试列表
  43. *
  44. * @param realExam 考试
  45. * @return 考试
  46. */
  47. public List<RealExam> selectRealExamList(RealExam realExam) {
  48. return realExamMapper.selectRealExamList(realExam);
  49. }
  50. /**
  51. * 新增考试
  52. *
  53. * @param realExam 考试
  54. * @return 结果
  55. */
  56. public int insertRealExam(RealExam realExam) {
  57. realExam.setCreateTime(DateUtils.getNowDate());
  58. return realExamMapper.insertRealExam(realExam);
  59. }
  60. /**
  61. * 修改考试
  62. *
  63. * @param realExam 考试
  64. * @return 结果
  65. */
  66. public int updateRealExam(RealExam realExam) {
  67. realExam.setUpdateTime(DateUtils.getNowDate());
  68. return realExamMapper.updateRealExam(realExam);
  69. }
  70. /**
  71. * 批量删除考试
  72. *
  73. * @param examIds 需要删除的考试主键
  74. * @return 结果
  75. */
  76. public int deleteRealExamByExamIds(Long[] examIds) {
  77. return realExamMapper.deleteRealExamByExamIds(examIds);
  78. }
  79. /**
  80. * 删除考试信息
  81. *
  82. * @param examId 考试主键
  83. * @return 结果
  84. */
  85. public int deleteRealExamByExamId(Long examId) {
  86. return realExamMapper.deleteRealExamByExamId(examId);
  87. }
  88. // -------------------------------- tom add --------------------------------
  89. private static final Logger l = LoggerFactory.getLogger(CommSendService.class);
  90. @Autowired
  91. private CommReceiveService commReceiveService;
  92. @Autowired
  93. private StudentService studentService;
  94. @Autowired
  95. private SimService simService;
  96. @Autowired
  97. private SeatService seatService;
  98. @Autowired
  99. private RealExamCollectionService realExamCollectionService;
  100. @Autowired
  101. private RealExamFaultService realExamFaultService;
  102. @Autowired
  103. @Lazy
  104. private CommSendService commSendService;
  105. @Autowired
  106. @Lazy
  107. private CommCheckService commCheckService;
  108. @Autowired
  109. private SimConfig simConfig;
  110. @Autowired
  111. private SocketService socketService;
  112. /**
  113. * examId 是否有效。
  114. *
  115. * @param examId
  116. * @return
  117. */
  118. public boolean exist(Long examId) {
  119. if (examId == null) {
  120. return false;
  121. }
  122. if (examId == 0) {
  123. return false;
  124. }
  125. RealExam re = selectRealExamByExamId(examId);
  126. if (re == null) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. public List<RealExamVo> list(RealExam q) {
  132. List<RealExamVo> list = new ArrayList<>();
  133. realExamMapper.selectRealExamList(q).forEach(re -> {
  134. RealExamVo v = new RealExamVo();
  135. RealExamCollection rec = realExamCollectionService.selectRealExamCollectionByExamCollectionId(re.getExamCollectionId());
  136. v.setRealExam(re);
  137. v.setRealExamCollection(rec);
  138. list.add(v);
  139. });
  140. return list;
  141. }
  142. public List<RealExam> listAllByStatus(String state) {
  143. RealExam q = new RealExam();
  144. q.setExamStatus(state);
  145. return selectRealExamList(q);
  146. }
  147. /**
  148. * 交卷自动修改关联状态
  149. *
  150. * @param examId
  151. * @param state
  152. * @return
  153. */
  154. @Transactional
  155. public int updateOneState(long examId, final String state) {
  156. if (false) {
  157. // php项目维护exam_status字段。java这边不操作。
  158. // 屏蔽
  159. return 0;
  160. }
  161. RealExam q = selectRealExamByExamId(examId);
  162. // todo:屏蔽
  163. if (false && RealExam.State.SUBMITTED.equals(state)) {
  164. // 关联故障list同步锁死。
  165. realExamFaultService.listAllType2State2and3ByExamId(q.getExamId())
  166. .forEach(ref -> {
  167. ref.setRefState(RealExamFault.State.FINISH);
  168. realExamFaultService.updateRealExamFault(ref);
  169. });
  170. }
  171. q.setExamStatus(state);
  172. return updateRealExam(q);
  173. }
  174. /**
  175. * [学生]进入考试。
  176. *
  177. * @return RealExam
  178. */
  179. public AjaxResult studentEnterRealExam(Long examId) {
  180. RealExam re = selectRealExamByExamId(examId);
  181. if (re == null) {
  182. AjaxResult.error("realExamId error!");
  183. }
  184. // todo:应该在登录位置实现
  185. // todo: temp
  186. updateOneState(examId, RealExam.State.LOGGED_IN);
  187. // todo: temp
  188. realExamFaultService.resetAllType2(examId);
  189. return AjaxResult.success(re);
  190. }
  191. /**
  192. * [轮询][学生]准备考试界面。
  193. *
  194. * @param realExamId
  195. * @return StudentRealExamPreVo
  196. */
  197. public AjaxResult studentLoopPrepareRealExam(Long realExamId) {
  198. l.info("studentLoopPrepareRealExam");
  199. // check
  200. if (realExamId == null || realExamId == 0) {
  201. // todo:
  202. }
  203. //
  204. RealExam re = selectRealExamByExamId(realExamId);
  205. if (re == null) {
  206. // todo:
  207. }
  208. // todo: 日期,不可进入考试。
  209. //
  210. // todo: 验证学生登录身份`
  211. Objects.requireNonNull(re);
  212. RealExamCollection collection =
  213. realExamCollectionService.selectRealExamCollectionByExamCollectionId(re.getExamCollectionId());
  214. // check collection
  215. Sim sim = simService.selectSimBySimId(re.getSimId());
  216. // check sim
  217. Student student = studentService.selectStudentByUserId(re.getUserId());
  218. // check student
  219. Seat seat = seatService.selectSeatBySeatId(re.getSeatId());
  220. // check seat
  221. StudentRealExamPreVo vo = new StudentRealExamPreVo();
  222. vo.setRealExam(re);
  223. vo.setRealExamCollection(collection);
  224. vo.setSim(sim);
  225. vo.setStudent(student);
  226. vo.setSeat(seat);
  227. // todo:多人请求同时进入的问题
  228. boolean next = studentPrepareRealExamCheck(vo);
  229. vo.setNext(next);
  230. if (!next) {
  231. // 执行模拟器通信,让模拟器准备好。
  232. // 异步执行
  233. commSendService.clearListFaultByRealExamAsync(re);
  234. }
  235. l.info("vo = {}", vo);
  236. return AjaxResult.success(vo);
  237. }
  238. public boolean studentPrepareRealExamCheck(StudentRealExamPreVo v) {
  239. if (v == null ||
  240. v.getRealExam() == null ||
  241. v.getRealExamCollection() == null ||
  242. v.getSim() == null ||
  243. v.getStudent() == null ||
  244. v.getSeat() == null) {
  245. return false;
  246. }
  247. // todo:在考试日期内。
  248. // check 一个模拟器的所有选中故障点位都下发成功,准备是否可以
  249. //
  250. // todo:??
  251. // 学生答题中可以再次进入。
  252. String examStatus = v.getRealExam().getExamStatus();
  253. String simStatus = v.getSim().getSimState();
  254. // 兜底
  255. {
  256. if (realExamFaultService.isAllType2StateXiaFa(v.getRealExam().getExamId())) {
  257. // updateOneState(v.getRealExam().getExamId(), RealExam.State.SIM_PREPARE_OK);
  258. }
  259. }
  260. // 模拟器不通信。
  261. if (!simConfig.isCommGlobal()) {
  262. return true;
  263. }
  264. if ((RealExam.State.SIM_PREPARE_OK.equals(examStatus) ||
  265. RealExam.State.ANSWERING.equals(examStatus)) &&
  266. Sim.State.ONLINE.equals(simStatus)
  267. ) {
  268. return true;
  269. }
  270. return false;
  271. }
  272. /**
  273. * [学生]开始考试
  274. *
  275. * @param examId
  276. * @param studentBindIp
  277. * @param type
  278. * @return
  279. */
  280. @Transactional
  281. public AjaxResult studentStartRealExam(final Long examId, final String studentBindIp, final String type) {
  282. l.info("studentStartRealExam = {}", examId);
  283. // todo: 暂时没有解决方案 检查 考试的sim和seat,是否正确对应。
  284. {
  285. // todo:delete
  286. // re.setSimId(getFakeSimId(re));
  287. // l.info("fake re = {}", re);
  288. }
  289. // check id data.
  290. // Step :检查参数有效性。
  291. {
  292. AjaxResult ar01 = checkExamId(examId);
  293. if (ar01.isError()) {
  294. return ar01;
  295. }
  296. }
  297. if (StringUtils.isBlank(studentBindIp)) {
  298. return AjaxResult.error("ip地址无效。");
  299. }
  300. RealExam re = selectRealExamByExamId(examId);
  301. Seat seat = seatService.uniqueByBindIp(studentBindIp);
  302. {
  303. if (seat == null) {
  304. throw new IllegalArgumentException("XXX");
  305. }
  306. }
  307. // Step :ping通 路由器。
  308. {
  309. AjaxResult ar = commCheckService.checkRouterState(simConfig.getRouterIp());
  310. if (ar.isError()) {
  311. return ar;
  312. }
  313. }
  314. // Step :ping通 学员端电脑。
  315. {
  316. AjaxResult ar = commCheckService.checkPingStudentPcState(studentBindIp);
  317. if (ar.isError()) {
  318. return ar;
  319. }
  320. }
  321. // Step :ping通 RS485。
  322. {
  323. AjaxResult ar = commCheckService.checkPingRs485State(seat.getSeatRs485Ip());
  324. if (ar.isError()) {
  325. // todo:重复
  326. // 更新SimId
  327. seatService.updateSimIdBySeatNum(seat.getSeatNum(), Seat.ID_0);
  328. // 更新SocketState
  329. seatService.updateSocketStateBySeatNum(seat.getSeatNum(), Seat.SocketState.OFFLINE);
  330. return ar;
  331. } else {
  332. // Ping通不代表在线,Socket连接建立表示在线。
  333. }
  334. }
  335. // Step :如果有缓存Socket并且可用,使用缓存Socket,检查并建立Socket连接;否则返回对应错误。
  336. {
  337. AjaxResult ar = socketService.openOne(seat.toSimSocketVo());
  338. if (ar.isError()) {
  339. return ar;
  340. }
  341. }
  342. // Step :发送通用询问指令,询问是连接的哪种型号的哪一台模拟器;否则返回对应错误。
  343. {
  344. AjaxResult ar = commCheckService.checkOneSeatState(seat, true);
  345. if (ar.isError()) {
  346. return ar;
  347. }
  348. }
  349. // 重新查询。已经确定simId了。
  350. {
  351. // 修改exam表对应examId的一条数据,填充并锁定seat_id和sim_id值。
  352. // 设置上seatId和simId
  353. re = selectRealExamByExamId(examId);
  354. seat = seatService.uniqueByBindIp(studentBindIp);
  355. re.setSeatId(seat.getSeatId());
  356. re.setSimId(seat.getCurrentSimId());
  357. l.debug("re = {}", re);
  358. updateRealExam(re);
  359. }
  360. // 查询模拟器在线状态,纯DB查询。
  361. {
  362. AjaxResult ar = commCheckService.checkOneSimOnlineState(seat.getCurrentSimId());
  363. if (ar.isError()) {
  364. return ar;
  365. }
  366. }
  367. Sim sim = simService.selectSimBySimId(re.getSimId());
  368. // 检查模拟器类型
  369. {
  370. String targetSimType = re.getSimType();
  371. AjaxResult ar = commCheckService.checkOneSimType(seat, true, targetSimType);
  372. if (ar.isError()) {
  373. return ar;
  374. }
  375. }
  376. // Step 5:
  377. {
  378. // AjaxResult arE3 = commSendService.checkOneSimStateActive(seat);
  379. // if (arE3.isError()) {
  380. // return arE3;
  381. // }
  382. }
  383. // Step 7:可换件检查,读取对应一台模拟器 所有故障部位值。检查模拟器所有的 真实的 故障部位 是否异常 或者 空值。特殊的故障部位要单独判断。
  384. if (SimDebugConfig.CHECK_REPLACE_EMPTY) {
  385. AjaxResult arE2 = commSendService.readOneSimAllFaultCheck(seat, sim);
  386. if (arE2.isError()) {
  387. return arE2;
  388. }
  389. }
  390. // Step 8:清除对应一台模拟器 所有 真实的 故障部位故障。
  391. {
  392. commSendService.clearOneSimAllFaultByExam(re);
  393. }
  394. // Step 9:下发对应一台模拟器 出题选中的 故障位置故障。
  395. {
  396. commSendService.writeOneSimAllSelectFaultByExam(re);
  397. }
  398. // Step 10:读取对应一台模拟器 所有的 真实的 故障部位 电阻值代表值 作为出题值。
  399. // 修改关联状态
  400. {
  401. commSendService.readOneSimAllFaultFirstTimeByExam(re);
  402. }
  403. // Step 11:修改当前exam_id状态。
  404. if (realExamFaultService.isType2ExamPrepareStartOk(re.getExamId())) {
  405. updateOneState(re.getExamId(), RealExam.State.SIM_PREPARE_OK);
  406. updateOneState(re.getExamId(), RealExam.State.ANSWERING);
  407. // 修改真实考试开始时间。
  408. re.setStartTime(DateUtils.getNowDate());
  409. updateRealExam(re);
  410. return AjaxResult.success("开始考试成功!");
  411. } else {
  412. return AjaxResult.error("连接超时,请检查模拟器连接!");
  413. }
  414. }
  415. public AjaxResult checkExamId(final Long examId) {
  416. // check
  417. // 检查 examId 是否正确存在
  418. {
  419. if (!exist(examId)) {
  420. return AjaxResult.error("对应考试Id不存在!");
  421. }
  422. }
  423. RealExam re = selectRealExamByExamId(examId);
  424. // 检查 seat_id 是否正确存在
  425. // Java后端处理填写,不检查。
  426. if (false) {
  427. if (!seatService.exist(re.getSeatId())) {
  428. return AjaxResult.error("对应座Id不存在!");
  429. }
  430. }
  431. // Java后端处理填写,不检查。
  432. // 检查 sim_id 是否正确存在
  433. if (false) {
  434. if (!simService.existBySimId(re.getSimId())) {
  435. return AjaxResult.error("对应模拟器Id不存在!");
  436. }
  437. }
  438. return AjaxResult.success();
  439. }
  440. /**
  441. * 根据考试集合获得sim_id
  442. *
  443. * @param exam
  444. * @return
  445. */
  446. @Deprecated
  447. public long getFakeSimId(RealExam exam) {
  448. {
  449. RealExamCollection coll = realExamCollectionService.selectRealExamCollectionByExamCollectionId(exam.getExamCollectionId());
  450. String simType = coll.getSimType();
  451. switch (simType) {
  452. case Sim.TYPE_0001 -> {
  453. return 11L;
  454. }
  455. case Sim.TYPE_0002 -> {
  456. return 12L;
  457. }
  458. case Sim.TYPE_0003 -> {
  459. return 31L;
  460. }
  461. }
  462. }
  463. return 0L;
  464. }
  465. /**
  466. * [轮询][学生]正在考试界面。
  467. *
  468. * @param realExamId
  469. * @return StudentRealExamIngVo
  470. */
  471. public AjaxResult studentLoopAnsweringRealExam(Long realExamId) {
  472. RealExam re = selectRealExamByExamId(realExamId);
  473. RealExamCollection rec = realExamCollectionService.selectRealExamCollectionByExamCollectionId(re.getExamCollectionId());
  474. StudentRealExamIngVo vo = new StudentRealExamIngVo();
  475. vo.setRealExam(re);
  476. long remaining = (re.getStartTime().getTime() + rec.getLimitDuration() * 60 * 1000) - DateUtils.getNowDate().getTime();
  477. vo.setRemainingMilliseconds(remaining);
  478. vo.setCompulsiveSubmit(remaining >= RealExam.EXAM_TIMEOUT_LIMIT);
  479. l.info("studentLoopAnsweringRealExam vo = {}", vo);
  480. return AjaxResult.success(vo);
  481. }
  482. /**
  483. * [学生]交卷考试
  484. *
  485. * @param examId
  486. * @return RealExam
  487. */
  488. @Transactional
  489. public AjaxResult studentSubmitRealExam(Long examId) {
  490. RealExam re = selectRealExamByExamId(examId);
  491. {
  492. // todo:delete
  493. // re.setSimId(getFakeSimId(re));
  494. }
  495. // check part.
  496. {
  497. AjaxResult arE1 = checkExamId(examId);
  498. if (arE1.isError()) {
  499. return arE1;
  500. }
  501. }
  502. // 检查一下模拟器状态。
  503. Seat seat = seatService.selectSeatBySeatId(re.getSeatId());
  504. // 如果模拟器离线
  505. {
  506. // AjaxResult arE3 = commSendService.checkOneSimStateActive(seat);
  507. AjaxResult arE3 = commCheckService.checkOneSeatState(seat, true);
  508. if (arE3.isError()) {
  509. return arE3;
  510. }
  511. }
  512. // 最后读取一下模拟器电阻值。
  513. commSendService.readOneExamAtLast(re);
  514. if (realExamFaultService.isType2ExamPrepareSubmitOk(re.getExamId())) {
  515. re.setExamStatus(RealExam.State.SUBMITTED);
  516. // 修改真实考试结束时间。
  517. re.setEndTime(DateUtils.getNowDate());
  518. updateRealExam(re);
  519. return AjaxResult.success("成功交卷!");
  520. } else {
  521. return AjaxResult.error("失败交卷!");
  522. }
  523. }
  524. /**
  525. * [轮询][学生]结束考试界面。
  526. *
  527. * @param examId
  528. * @return StudentRealExamPostVo
  529. */
  530. public AjaxResult studentLoopPostRealExam(Long examId) {
  531. RealExam re = selectRealExamByExamId(examId);
  532. StudentRealExamPostVo vo = new StudentRealExamPostVo();
  533. {
  534. }
  535. vo.setRealExam(re);
  536. vo.setListPart1(realExamFaultService.getReportListPart1(examId));
  537. vo.setListPart2(realExamFaultService.getReportListPart2(examId));
  538. vo.setPart3(realExamFaultService.getReportPart3(examId));
  539. return AjaxResult.success(vo);
  540. }
  541. }