package com.ruoyi.sim.service.impl; import java.util.Collections; import java.util.List; import cn.ele6.catalyzer.ruoyi.vue.custom.Ele6RYBaseService; import cn.ele6.catalyzer.ruoyi.vue.enhance.TableDataInfo; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.sim.domain.vo.RealExamCollectionVo; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import com.ruoyi.sim.mapper.RealExamCollectionMapper; import com.ruoyi.sim.domain.RealExamCollection; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; /** * 考试集合Service业务层处理 * * @author tom * @date 2024-12-17 */ @Service public class RealExamCollectionService extends Ele6RYBaseService { @Autowired private RealExamCollectionMapper realExamCollectionMapper; /** * todo:依赖循环的问题。 */ @Autowired @Lazy private CommSendService commSendService; @Autowired private SocketService socketService; /** * 查询考试集合 * * @param examCollectionId 考试集合主键 * @return 考试集合 */ public RealExamCollection selectRealExamCollectionByExamCollectionId(Long examCollectionId) { return realExamCollectionMapper.selectRealExamCollectionByExamCollectionId(examCollectionId); } /** * 查询考试集合列表 * * @param realExamCollection 考试集合 * @return 考试集合 */ public List selectRealExamCollectionList(RealExamCollection realExamCollection) { return realExamCollectionMapper.selectRealExamCollectionList(realExamCollection); } /** * 新增考试集合 * * @param realExamCollection 考试集合 * @return 结果 */ public int insertRealExamCollection(RealExamCollection realExamCollection) { realExamCollection.setCreateTime(DateUtils.getNowDate()); return realExamCollectionMapper.insertRealExamCollection(realExamCollection); } /** * 修改考试集合 * * @param realExamCollection 考试集合 * @return 结果 */ public int updateRealExamCollection(RealExamCollection realExamCollection) { realExamCollection.setUpdateTime(DateUtils.getNowDate()); return realExamCollectionMapper.updateRealExamCollection(realExamCollection); } /** * 批量删除考试集合 * * @param examCollectionIds 需要删除的考试集合主键 * @return 结果 */ public int deleteRealExamCollectionByExamCollectionIds(Long[] examCollectionIds) { return realExamCollectionMapper.deleteRealExamCollectionByExamCollectionIds(examCollectionIds); } /** * 删除考试集合信息 * * @param examCollectionId 考试集合主键 * @return 结果 */ public int deleteRealExamCollectionByExamCollectionId(Long examCollectionId) { return realExamCollectionMapper.deleteRealExamCollectionByExamCollectionId(examCollectionId); } // -------------------------------- tom add -------------------------------- /** * [教师][分页]查询考试集合列表 * * @param q * @return */ public TableDataInfo listExamByTeacher(RealExamCollection q) { startPage(); q.setExamCollectionType(RealExamCollection.Type.EXAM); List list = Collections.emptyList(); realExamCollectionMapper.selectRealExamCollectionList(q) .forEach((RealExamCollection s) -> { RealExamCollectionVo t = new RealExamCollectionVo(); BeanUtils.copyProperties(s, t); list.add(t); }); return getDataTable(list); } /** * [教师][分页]查询练习集合列表 * * @param p * @return */ public TableDataInfo listExerciseByTeacher(RealExamCollection p) { startPage(); p.setExamCollectionType(RealExamCollection.Type.EXERCISE); List list = Collections.emptyList(); realExamCollectionMapper.selectRealExamCollectionList(p).forEach((RealExamCollection s) -> { RealExamCollectionVo t = new RealExamCollectionVo(); BeanUtils.copyProperties(s, t); list.add(t); }); return getDataTable(list); } /** * [教师]获取考试集合详细信息 */ public AjaxResult getExamInfoByTeacher(Long id) { RealExamCollection f = realExamCollectionMapper. selectRealExamCollectionByExamCollectionId(id); return AjaxResult.success(f); } /** * [教师]获取练习集合详细信息 */ public AjaxResult getExerciseInfoByTeacher(Long id) { RealExamCollection f = realExamCollectionMapper. selectRealExamCollectionByExamCollectionId(id); return AjaxResult.success(f); } /** * [教师]新增考试集合 * * @param vo * @return */ public AjaxResult addExam(@RequestBody RealExamCollectionVo vo) { // check // 日期检查 if (StringUtils.isBlank(vo.getExamCollectionName())) { } // RealExamCollection p = new RealExamCollection(); BeanUtils.copyProperties(vo, p); p.setExamCollectionType(RealExamCollection.Type.EXAM); insertRealExamCollection(p); return AjaxResult.success(); } /** * [教师]新增练习集合 * * @param vo * @return */ public AjaxResult addExercise(@RequestBody RealExamCollectionVo vo) { // check // RealExamCollection p = new RealExamCollection(); BeanUtils.copyProperties(vo, p); p.setExamCollectionType(RealExamCollection.Type.EXERCISE); insertRealExamCollection(p); return AjaxResult.success(); } /** * [学院]新增自主练习集合 * todo:界面入口 * * @param vo * @return */ public AjaxResult addSelfExercise(@RequestBody RealExamCollectionVo vo) { // check // do. RealExamCollection p = new RealExamCollection(); BeanUtils.copyProperties(vo, p); p.setExamCollectionType(RealExamCollection.Type.SELF_EXERCISE); insertRealExamCollection(p); return AjaxResult.success(); } /** * [教师]修改考试集合 * * @param vo * @return */ public AjaxResult editExam(@RequestBody RealExamCollectionVo vo) { // check // RealExamCollection p = new RealExamCollection(); BeanUtils.copyProperties(vo, p); p.setExamCollectionType(RealExamCollection.Type.EXAM); updateRealExamCollection(p); // todo:更新关联 return AjaxResult.success(); } /** * [教师]修改练习集合 * * @param vo * @return */ public AjaxResult editExercise(@RequestBody RealExamCollectionVo vo) { // check // do. RealExamCollection p = new RealExamCollection(); BeanUtils.copyProperties(vo, p); p.setExamCollectionType(RealExamCollection.Type.EXERCISE); updateRealExamCollection(p); // todo:更新关联 return AjaxResult.success(); } /** * [教师]删除多个考试集合 * * @param ids * @return */ public AjaxResult deleteExams(Long[] ids) { // check // do. deleteRealExamCollectionByExamCollectionIds(ids); // todo:更新关联 return AjaxResult.success(); } /** * [教师]删除多个练习集合 * * @param ids * @return */ public AjaxResult deleteExercises(Long[] ids) { // check // do. deleteRealExamCollectionByExamCollectionIds(ids); // todo:更新关联 return AjaxResult.success(); } /** * [教师]打开考试/练习集合 * * @param examCollectionId * @return */ @Transactional public AjaxResult open(final Long examCollectionId, final String examCollectionType) { l.debug("open examCollectionId= {}", examCollectionId); // check 1:检查id有效。 RealExamCollection rec = selectRealExamCollectionByExamCollectionId(examCollectionId); if (rec == null) { return AjaxResult.error("考试集合不存在!"); } l.debug("rec = {}", rec); // check 2:检查考试集合类型对应。 // { // final String dbType = rec.getExamCollectionType(); // if (!StringUtils.equals(dbType, examCollectionType)) { // return AjaxResult.error("考试集合类型不对应!"); // } // } // check 3:唯一open检查。 // if (existOpened()) { // return AjaxResult.error("已经有打开的考试或者练习!"); // } // check datetime todo:时间到了 // do. // Step 1:强制停掉所有的自主练习 { // closeAllType(RealExamCollection.Type.SELF_EXERCISE); } // Step 2:强制停掉所有的练习 { // closeAllType(RealExamCollection.Type.EXERCISE); } // Step 3:强制停掉 除本考试外的 所有考试 { // closeAllExamExcludeId(rec.getExamCollectionId()); } // Step 4:尝试打开所有Socket,提前准备,允许有打开失败的 // SocketOldService实现。 // AjaxResult ar1 = socketOldService.openSocket(); // AjaxResult ar1 = socketService.tryOpenAll(); if (ar1.isError()) { return ar1; } // // 更新相关数据 // Step :修改考试集合状态。 { rec.setExamCollectionState(RealExamCollection.State.OPENED); updateRealExamCollection(rec); } return AjaxResult.success("开启成功!"); } public List listAllByType(String examCollectionType) { RealExamCollection q = new RealExamCollection(); q.setExamCollectionType(examCollectionType); return selectRealExamCollectionList(q); } /** * 是否有一个open的任何类型的集合。 * * @return */ public boolean existAtLeastOneOpened() { return (selectRealExamCollectionOpened() != null); } public RealExamCollection selectRealExamCollectionOpened() { RealExamCollection q = new RealExamCollection(); q.setExamCollectionState(RealExamCollection.State.OPENED); List list = selectRealExamCollectionList(q); if (list != null && !list.isEmpty()) { return list.get(0); } return null; } /** * [教师]关闭考试/练习集合 * * @param examCollectionId * @return */ public AjaxResult close(final Long examCollectionId) { // check // todo:是否还有正在进行的考试 RealExamCollection f = selectRealExamCollectionByExamCollectionId(examCollectionId); // 修改考试集合状态。 { f.setExamCollectionState(RealExamCollection.State.CLOSED); updateRealExamCollection(f); } // 关闭socket // 修改为socket常开,直接返回成功结果。 // return commSendService.closeSocket(); return AjaxResult.success("关闭连接成功!"); } public AjaxResult closeAll() { // SocketOldService实现 // return socketOldService.closeSocket(); return AjaxResult.success(); } private void closeAllType(final String type) { RealExamCollection q = new RealExamCollection(); q.setExamCollectionType(type); selectRealExamCollectionList(q) .forEach((RealExamCollection ec) -> { ec.setExamCollectionState(RealExamCollection.State.CLOSED); updateRealExamCollection(ec); }); } private void closeAllExamExcludeId(final Long excludeRecId) { List list = listAllByType(RealExamCollection.Type.EXAM); for (RealExamCollection ec : list) { if (ec.getExamCollectionId().equals(excludeRecId)) { // 跳过本考试。 continue; } ec.setExamCollectionState(RealExamCollection.State.CLOSED); updateRealExamCollection(ec); } } /** * @return 可以为null。 */ public RealExamCollection getCurrentExamCollection() { RealExamCollection q = new RealExamCollection(); q.setExamCollectionState(RealExamCollection.State.OPENED); List list = selectRealExamCollectionList(q); if (list == null || list.isEmpty()) { return null; } return list.get(0); } public AjaxResult resetAllSuperMan() { // Step:关闭所有Socket连接 // Step:ping路由器,返回在线情况。 // Step:ping教员端主机,返回在线情况。 // Step:ping所有学员端主机,返回在线情况列表。 // Step:ping所有RS485,返回在线情况列表。 // Step:建立所有模拟器Socket,返回情况列表。 // Step:有Socket的前提下,尝试查询所有模拟器连接情况,得到连接的模拟器 型号、序列号信息 // Step:所有连接的模拟器,清除每一个模拟器的各个故障 // Step:所有连接的模拟器,读取每一个模拟器的各个故障,是否处于考试准备ok状态 return null; } }