RealExamService.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.domain.*;
  8. import com.ruoyi.sim.domain.vo.RealExamVo;
  9. import com.ruoyi.sim.domain.vo.StudentRealExamIngVo;
  10. import com.ruoyi.sim.domain.vo.StudentRealExamPostVo;
  11. import com.ruoyi.sim.domain.vo.StudentRealExamPreVo;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import com.ruoyi.sim.mapper.RealExamMapper;
  17. import org.springframework.transaction.annotation.Transactional;
  18. /**
  19. * 考试Service业务层处理
  20. *
  21. * @author tom
  22. * @date 2024-12-15
  23. */
  24. @Service
  25. public class RealExamService {
  26. @Autowired
  27. private RealExamMapper realExamMapper;
  28. /**
  29. * 查询考试
  30. *
  31. * @param examId 考试主键
  32. * @return 考试
  33. */
  34. public RealExam selectRealExamByExamId(Long examId) {
  35. return realExamMapper.selectRealExamByExamId(examId);
  36. }
  37. /**
  38. * 查询考试列表
  39. *
  40. * @param realExam 考试
  41. * @return 考试
  42. */
  43. public List<RealExam> selectRealExamList(RealExam realExam) {
  44. return realExamMapper.selectRealExamList(realExam);
  45. }
  46. /**
  47. * 新增考试
  48. *
  49. * @param realExam 考试
  50. * @return 结果
  51. */
  52. public int insertRealExam(RealExam realExam) {
  53. realExam.setCreateTime(DateUtils.getNowDate());
  54. return realExamMapper.insertRealExam(realExam);
  55. }
  56. /**
  57. * 修改考试
  58. *
  59. * @param realExam 考试
  60. * @return 结果
  61. */
  62. public int updateRealExam(RealExam realExam) {
  63. realExam.setUpdateTime(DateUtils.getNowDate());
  64. return realExamMapper.updateRealExam(realExam);
  65. }
  66. /**
  67. * 批量删除考试
  68. *
  69. * @param examIds 需要删除的考试主键
  70. * @return 结果
  71. */
  72. public int deleteRealExamByExamIds(Long[] examIds) {
  73. return realExamMapper.deleteRealExamByExamIds(examIds);
  74. }
  75. /**
  76. * 删除考试信息
  77. *
  78. * @param examId 考试主键
  79. * @return 结果
  80. */
  81. public int deleteRealExamByExamId(Long examId) {
  82. return realExamMapper.deleteRealExamByExamId(examId);
  83. }
  84. // -------------------------------- tom add --------------------------------
  85. private static final Logger l = LoggerFactory.getLogger(CommSendService.class);
  86. @Autowired
  87. private CommReceiveService commReceiveService;
  88. @Autowired
  89. private StudentService studentService;
  90. @Autowired
  91. private SimService simService;
  92. @Autowired
  93. private SeatService seatService;
  94. @Autowired
  95. private RealExamCollectionService realExamCollectionService;
  96. @Autowired
  97. private RealExamFaultService realExamFaultService;
  98. @Autowired
  99. private CommSendService commSendService;
  100. public List<RealExamVo> list(RealExam q) {
  101. List<RealExamVo> list = new ArrayList<>();
  102. realExamMapper.selectRealExamList(q).forEach(re -> {
  103. RealExamVo v = new RealExamVo();
  104. RealExamCollection rec = realExamCollectionService.selectRealExamCollectionByExamCollectionId(re.getExamCollectionId());
  105. v.setRealExam(re);
  106. v.setRealExamCollection(rec);
  107. list.add(v);
  108. });
  109. return list;
  110. }
  111. public List<RealExam> listAllByStatus(String state) {
  112. RealExam q = new RealExam();
  113. q.setExamStatus(state);
  114. return selectRealExamList(q);
  115. }
  116. /**
  117. * 交卷自动修改关联状态
  118. *
  119. * @param examId
  120. * @param state
  121. * @return
  122. */
  123. @Transactional
  124. public int updateOneState(long examId, final String state) {
  125. RealExam q = selectRealExamByExamId(examId);
  126. // todo:屏蔽
  127. if (false && RealExam.State.SUBMITTED.equals(state)) {
  128. // 关联故障list同步锁死。
  129. realExamFaultService.listAllType2LoopReadStateByExamId(q.getExamId())
  130. .forEach(ref -> {
  131. ref.setRefState(RealExamFault.State.FINISH);
  132. realExamFaultService.updateRealExamFault(ref);
  133. });
  134. }
  135. q.setExamStatus(state);
  136. return updateRealExam(q);
  137. }
  138. /**
  139. * [学生]进入考试。
  140. *
  141. * @return
  142. */
  143. public AjaxResult studentEnterRealExam(Long examId) {
  144. RealExam re = selectRealExamByExamId(examId);
  145. if (re == null) {
  146. AjaxResult.error("realExamId error!");
  147. }
  148. // todo:应该在登录位置实现
  149. // todo: temp
  150. updateOneState(examId, RealExam.State.LOGGED_IN);
  151. // todo: temp
  152. realExamFaultService.resetAllType2(examId);
  153. return AjaxResult.success(re);
  154. }
  155. /**
  156. * [轮询][学生]准备考试界面。
  157. *
  158. * @param realExamId
  159. * @return
  160. */
  161. public AjaxResult studentLoopPrepareRealExam(Long realExamId) {
  162. l.info("studentLoopPrepareRealExam");
  163. // check
  164. if (realExamId == null || realExamId == 0) {
  165. // todo:
  166. }
  167. //
  168. RealExam re = selectRealExamByExamId(realExamId);
  169. if (re == null) {
  170. // todo:
  171. }
  172. // todo: 日期,不可进入考试。
  173. //
  174. // todo: 验证学生登录身份
  175. Objects.requireNonNull(re);
  176. RealExamCollection collection =
  177. realExamCollectionService.selectRealExamCollectionByExamCollectionId(re.getExamCollectionId());
  178. // check collection
  179. Sim sim = simService.selectSimBySimId(re.getSimId());
  180. // check sim
  181. Student student = studentService.selectStudentByUserId(re.getUserId());
  182. // check student
  183. Seat seat = seatService.selectSeatBySeatId(re.getSeatId());
  184. // check seat
  185. StudentRealExamPreVo vo = new StudentRealExamPreVo();
  186. vo.setRealExam(re);
  187. vo.setRealExamCollection(collection);
  188. vo.setSim(sim);
  189. vo.setStudent(student);
  190. vo.setSeat(seat);
  191. // todo:多人请求同时进入的问题
  192. boolean next = studentPrepareRealExamCheck(vo);
  193. vo.setNext(next);
  194. if (!next) {
  195. // 执行模拟器通信,让模拟器准备好。
  196. // 异步执行
  197. commSendService.clearListFaultByRealExamAsync(re);
  198. }
  199. l.info("vo = {}", vo);
  200. return AjaxResult.success(vo);
  201. }
  202. public boolean studentPrepareRealExamCheck(StudentRealExamPreVo v) {
  203. if (v == null ||
  204. v.getRealExam() == null ||
  205. v.getRealExamCollection() == null ||
  206. v.getSim() == null ||
  207. v.getStudent() == null ||
  208. v.getSeat() == null) {
  209. return false;
  210. }
  211. // todo:在考试日期内。
  212. // check 一个模拟器的所有选中故障点位都下发成功,准备是否可以
  213. //
  214. // todo:??
  215. // 学生答题中可以再次进入。
  216. String examStatus = v.getRealExam().getExamStatus();
  217. String simStatus = v.getSim().getSimState();
  218. if ((RealExam.State.SIM_PREPARE_OK.equals(examStatus) ||
  219. RealExam.State.ANSWERING.equals(examStatus)) &&
  220. Sim.State.ONLINE.equals(simStatus)
  221. ) {
  222. return true;
  223. }
  224. return false;
  225. }
  226. /**
  227. * [学生]开始考试
  228. *
  229. * @param examId
  230. * @return
  231. */
  232. @Transactional
  233. public AjaxResult studentStartRealExam(Long examId) {
  234. l.info("studentStartRealExam");
  235. RealExam re = selectRealExamByExamId(examId);
  236. l.info("re = {}", re);
  237. re.setExamStatus(RealExam.State.ANSWERING);
  238. re.setStartTime(DateUtils.getNowDate());
  239. updateRealExam(re);
  240. return AjaxResult.success(re);
  241. }
  242. /**
  243. * [轮询][学生]正在考试界面。
  244. *
  245. * @param realExamId
  246. * @return
  247. */
  248. public AjaxResult studentLoopAnsweringRealExam(Long realExamId) {
  249. RealExam re = selectRealExamByExamId(realExamId);
  250. RealExamCollection rec = realExamCollectionService.selectRealExamCollectionByExamCollectionId(re.getExamCollectionId());
  251. StudentRealExamIngVo vo = new StudentRealExamIngVo();
  252. vo.setRealExam(re);
  253. long remaining = (re.getStartTime().getTime() + rec.getLimitDuration() * 60 * 1000) - DateUtils.getNowDate().getTime();
  254. vo.setRemainingMilliseconds(remaining);
  255. vo.setCompulsiveSubmit(remaining >= RealExam.EXAM_TIMEOUT_LIMIT);
  256. l.info("studentLoopAnsweringRealExam vo = {}", vo);
  257. return AjaxResult.success(vo);
  258. }
  259. /**
  260. * [学生]交卷
  261. *
  262. * @param examId
  263. * @return
  264. */
  265. public AjaxResult studentSubmitRealExam(Long examId) {
  266. // 最后检查一下模拟器状态。
  267. // 最后读取一下模拟器电阻值。
  268. // todo:
  269. RealExam re1 = selectRealExamByExamId(examId);
  270. commSendService.readOneExamAtLastAsync(re1);
  271. submit(examId);
  272. return AjaxResult.success(re1);
  273. }
  274. /**
  275. * @param examId
  276. */
  277. @Transactional
  278. public void submit(long examId) {
  279. l.info("submit");
  280. RealExam re2 = selectRealExamByExamId(examId);
  281. re2.setExamStatus(RealExam.State.SUBMITTED);
  282. re2.setEndTime(DateUtils.getNowDate());
  283. updateRealExam(re2);
  284. }
  285. /**
  286. * [轮询][学生]结束考试界面。
  287. *
  288. * @param examId
  289. * @return
  290. */
  291. public AjaxResult studentLoopPostRealExam(Long examId) {
  292. RealExam re = selectRealExamByExamId(examId);
  293. StudentRealExamPostVo vo = new StudentRealExamPostVo();
  294. {
  295. }
  296. vo.setRealExam(re);
  297. vo.setListPart1(realExamFaultService.getReportListPart1(examId));
  298. return AjaxResult.success(vo);
  299. }
  300. }