|
@@ -0,0 +1,243 @@
|
|
|
+package com.qmth.themis.mq.service.impl;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.qmth.themis.business.cache.ExamRecordCacheUtil;
|
|
|
+import com.qmth.themis.business.constant.SpringContextHolder;
|
|
|
+import com.qmth.themis.business.constant.SystemConstant;
|
|
|
+import com.qmth.themis.business.entity.TBSession;
|
|
|
+import com.qmth.themis.business.entity.TMRocketMessage;
|
|
|
+import com.qmth.themis.business.entity.TOeExamBreakHistory;
|
|
|
+import com.qmth.themis.business.entity.TOeExamRecord;
|
|
|
+import com.qmth.themis.business.enums.*;
|
|
|
+import com.qmth.themis.business.service.*;
|
|
|
+import com.qmth.themis.business.templete.TaskExportTemplete;
|
|
|
+import com.qmth.themis.business.templete.TaskImportTemplete;
|
|
|
+import com.qmth.themis.business.templete.impl.TaskExamPaperImportTemplete;
|
|
|
+import com.qmth.themis.business.templete.impl.TaskExamStudentImportTemplete;
|
|
|
+import com.qmth.themis.business.templete.impl.TaskRoomCodeExportTemplete;
|
|
|
+import com.qmth.themis.business.templete.impl.TaskRoomCodeImportTemplete;
|
|
|
+import com.qmth.themis.business.threadPool.MyThreadPool;
|
|
|
+import com.qmth.themis.business.util.JacksonUtil;
|
|
|
+import com.qmth.themis.business.util.RedisUtil;
|
|
|
+import com.qmth.themis.mq.dto.MqDto;
|
|
|
+import com.qmth.themis.mq.service.MqLogicService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: mq执行逻辑 impl
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: wangliang
|
|
|
+ * @Date: 2020/7/31
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class MqLogicServiceImpl implements MqLogicService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ TMRocketMessageService tmRocketMessageService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ TEUserLogService teUserLogService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ TEExamStudentLogService teExamStudentLogService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ TBSessionService tbSessionService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ MyThreadPool myThreadPool;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ TOeExamRecordService tOeExamRecordService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ TOeExamBreakHistoryService tOeExamBreakHistoryService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ TOeExamRecordService examRecordService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * mq最大重试次数逻辑
|
|
|
+ *
|
|
|
+ * @param mqDto
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void execMqMaxReconsumeTime(MqDto mqDto, String key) {
|
|
|
+ //超过最大重试次数,保存到数据库,后续可以发短信通知系统管理人员
|
|
|
+ Gson gson = new Gson();
|
|
|
+ mqDto.setAck(SystemConstant.POSION_ACK_TYPE);
|
|
|
+ TMRocketMessage tmRocketMessage = gson.fromJson(gson.toJson(mqDto), TMRocketMessage.class);
|
|
|
+ tmRocketMessageService.saveOrUpdate(tmRocketMessage);
|
|
|
+ redisUtil.delete(key, mqDto.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户轨迹逻辑
|
|
|
+ *
|
|
|
+ * @param mqDto
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void execMqUserLogLogic(MqDto mqDto, String key) {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ String tag = mqDto.getTag();
|
|
|
+ if (tag.contains("user")) {
|
|
|
+ teUserLogService.saveUserLogInfo(mqDto.getTimestamp(), mqDto.getObjId(), MqEnum.valueOf(String.valueOf(mqDto.getType())).getId(), SystemOperationEnum.valueOf(String.valueOf(mqDto.getBody())).getCode(), JacksonUtil.parseJson(mqDto));
|
|
|
+ } else if (tag.contains("student")) {
|
|
|
+ teExamStudentLogService.saveStudentLogInfo(mqDto.getTimestamp(), MqEnum.valueOf(String.valueOf(mqDto.getType())).getId(), SystemOperationEnum.valueOf(String.valueOf(mqDto.getBody())).getCode(), JacksonUtil.parseJson(mqDto));
|
|
|
+ }
|
|
|
+ mqDto.setAck(SystemConstant.STANDARD_ACK_TYPE);
|
|
|
+ TMRocketMessage tmRocketMessage = gson.fromJson(gson.toJson(mqDto), TMRocketMessage.class);
|
|
|
+ tmRocketMessageService.saveOrUpdate(tmRocketMessage);
|
|
|
+ redisUtil.delete(key, mqDto.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * session逻辑
|
|
|
+ *
|
|
|
+ * @param mqDto
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void execMqSessionLogic(MqDto mqDto, String key) {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ tbSessionService.saveSessionInfo(JacksonUtil.readJson(JacksonUtil.parseJson(mqDto.getBody()), TBSession.class), mqDto.getTimestamp());
|
|
|
+ mqDto.setAck(SystemConstant.STANDARD_ACK_TYPE);
|
|
|
+ TMRocketMessage tmRocketMessage = gson.fromJson(gson.toJson(mqDto), TMRocketMessage.class);
|
|
|
+ tmRocketMessage.setBody(JacksonUtil.parseJson(tmRocketMessage.getBody()));
|
|
|
+ tmRocketMessageService.saveOrUpdate(tmRocketMessage);
|
|
|
+ redisUtil.delete(key, mqDto.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * task逻辑
|
|
|
+ *
|
|
|
+ * @param mqDto
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void execMqTaskLogic(MqDto mqDto, String key) {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ Map<String, Object> map = (Map<String, Object>) mqDto.getBody();
|
|
|
+ String tag = mqDto.getTag();
|
|
|
+ myThreadPool.arbitratePoolTaskExecutor.execute(() -> {
|
|
|
+ if (tag.contains("Import")) {
|
|
|
+ TaskImportTemplete taskImportTemplete = null;
|
|
|
+ if (tag.contains("examStudentImport")) {
|
|
|
+ taskImportTemplete = SpringContextHolder.getBean(TaskExamStudentImportTemplete.class);
|
|
|
+ } else if (tag.contains("roomCodeImport")) {
|
|
|
+ taskImportTemplete = SpringContextHolder.getBean(TaskRoomCodeImportTemplete.class);
|
|
|
+ } else if (tag.contains("examPaperImport")) {
|
|
|
+ taskImportTemplete = SpringContextHolder.getBean(TaskExamPaperImportTemplete.class);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ taskImportTemplete.importTask(map);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ TaskExportTemplete taskExportTemplete = null;
|
|
|
+ if (tag.contains("roomCodeExport")) {
|
|
|
+ taskExportTemplete = SpringContextHolder.getBean(TaskRoomCodeExportTemplete.class);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ taskExportTemplete.exportTask(map);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mqDto.setAck(SystemConstant.STANDARD_ACK_TYPE);
|
|
|
+ mqDto.setBody(JacksonUtil.parseJson(mqDto.getBody()));
|
|
|
+ TMRocketMessage tmRocketMessage = gson.fromJson(gson.toJson(mqDto), TMRocketMessage.class);
|
|
|
+ tmRocketMessageService.saveOrUpdate(tmRocketMessage);
|
|
|
+ redisUtil.delete(key, mqDto.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * websocket超时退出逻辑
|
|
|
+ *
|
|
|
+ * @param mqDto
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void execMqWebsocketUnNormalLogic(MqDto mqDto, String key) {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ mqDto.setAck(SystemConstant.STANDARD_ACK_TYPE);//表示成功处理
|
|
|
+ Map<String, Object> tranMap = mqDto.getProperties();
|
|
|
+ Long recordId = Long.parseLong(String.valueOf(tranMap.get("recordId")));
|
|
|
+ Date clientLastSyncTime = ExamRecordCacheUtil.getClientLastSyncTime(recordId);
|
|
|
+ if ((System.currentTimeMillis() - clientLastSyncTime.getTime()) / 1000 / 60 >= 2) {//大于等于当前时间,说明未重连或重登录
|
|
|
+ String deviceId = String.valueOf(tranMap.get("deviceId"));
|
|
|
+ String ip = String.valueOf(tranMap.get("ip"));
|
|
|
+ Long updateTime = Long.parseLong(String.valueOf(tranMap.get("updateTime")));
|
|
|
+ Date date = new Date();
|
|
|
+ date.setTime(updateTime);
|
|
|
+ TOeExamRecord tOeExamRecord = tOeExamRecordService.getById(recordId);
|
|
|
+ Integer breakCount = tOeExamRecord.getLeftBreakResumeCount();
|
|
|
+ if (Objects.isNull(breakCount) || breakCount <= 0) {
|
|
|
+ //todo 没有断点次数,则强制交卷
|
|
|
+ tOeExamRecord.setStatus(ExamRecordStatusEnum.finished);
|
|
|
+ tOeExamRecordService.updateById(tOeExamRecord);
|
|
|
+
|
|
|
+ //加入踢下线mq
|
|
|
+ } else {
|
|
|
+ breakCount--;
|
|
|
+ //增加断点记录
|
|
|
+ TOeExamBreakHistory tOeExamBreakHistory = new TOeExamBreakHistory(recordId, new Date(), BreakReasonEnum.NET_TIME_OUT, BreakReasonEnum.NET_TIME_OUT.name());
|
|
|
+ tOeExamBreakHistoryService.save(tOeExamBreakHistory);
|
|
|
+ //更新考试记录状态
|
|
|
+ tOeExamRecord.setClientCurrentIp(ip);
|
|
|
+ tOeExamRecord.setClientWebsocketId(deviceId);
|
|
|
+ tOeExamRecord.setClientWebsocketStatus(WebsocketStatusEnum.UN_ONLINE.ordinal());
|
|
|
+ tOeExamRecord.setClientLastSyncTime(date);
|
|
|
+ tOeExamRecord.setLastBreakId(tOeExamBreakHistory.getId());
|
|
|
+ tOeExamRecord.setLastBreakTime(tOeExamBreakHistory.getBreakTime());
|
|
|
+ tOeExamRecord.setLeftBreakResumeCount(breakCount);
|
|
|
+ tOeExamRecord.setStatus(ExamRecordStatusEnum.break_off);
|
|
|
+ tOeExamRecordService.updateById(tOeExamRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ teExamStudentLogService.saveStudentLogInfo(mqDto.getTimestamp(), MqEnum.valueOf(String.valueOf(mqDto.getType())).getId(), SystemOperationEnum.valueOf(String.valueOf(mqDto.getBody())).getCode(), JacksonUtil.parseJson(mqDto));
|
|
|
+ TMRocketMessage tmRocketMessage = gson.fromJson(gson.toJson(mqDto), TMRocketMessage.class);
|
|
|
+ tmRocketMessage.setProp(JacksonUtil.parseJson(tmRocketMessage.getProperties()));
|
|
|
+ tmRocketMessageService.saveOrUpdate(tmRocketMessage);
|
|
|
+ redisUtil.delete(key, mqDto.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算客观分逻辑
|
|
|
+ *
|
|
|
+ * @param mqDto
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void execMqCalculateObjectiveScoreLogic(MqDto mqDto, String key) {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ Map<String, Object> param = (Map<String, Object>) mqDto.getBody();
|
|
|
+ examRecordService.calculateObjectiveScore(param);
|
|
|
+ mqDto.setAck(SystemConstant.STANDARD_ACK_TYPE);
|
|
|
+ TMRocketMessage tmRocketMessage = gson.fromJson(gson.toJson(mqDto), TMRocketMessage.class);
|
|
|
+ tmRocketMessage.setBody(JacksonUtil.parseJson(tmRocketMessage.getBody()));
|
|
|
+ tmRocketMessageService.saveOrUpdate(tmRocketMessage);
|
|
|
+ redisUtil.delete(key, mqDto.getId());
|
|
|
+ }
|
|
|
+}
|