diff --git a/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java b/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java index 82b731d..1257152 100644 --- a/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java +++ b/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java @@ -81,7 +81,13 @@ public interface QuestionsControllerApi { * 抽题测评 */ @ApiOperation(value = "抽题测评", notes = "抽题测评") - R evaluation (Integer userId); + R evaluation (); + + /** + * 抽题测评 + */ + @ApiOperation(value = "获取测评剩余时间", notes = "获取测评剩余时间") + R getEvaluationRemainingTime (Integer userId); /** * 通过excel批量导出 diff --git a/src/main/java/com/msdw/tms/controller/QuestionsController.java b/src/main/java/com/msdw/tms/controller/QuestionsController.java index 1763e08..7431a2d 100644 --- a/src/main/java/com/msdw/tms/controller/QuestionsController.java +++ b/src/main/java/com/msdw/tms/controller/QuestionsController.java @@ -145,11 +145,18 @@ public class QuestionsController implements QuestionsControllerApi { @Override @GetMapping("/evaluation") - public R evaluation(Integer userId) { - EvaluationVO evaluation = questionsService.evaluation(userId); + public R evaluation() { + EvaluationVO evaluation = questionsService.evaluation(); return R.ok().put("data", evaluation); } + @Override + @GetMapping("/remaining") + public R getEvaluationRemainingTime(Integer userId) { + String remainingTime = questionsService.getEvaluationRemainingTime(userId); + return R.ok().put("data", remainingTime); + } + @Override @GetMapping("/export") public void exportQuestion(HttpServletResponse response) throws Exception { diff --git a/src/main/java/com/msdw/tms/entity/vo/EvaluationVO.java b/src/main/java/com/msdw/tms/entity/vo/EvaluationVO.java index e6e0b07..68a8172 100644 --- a/src/main/java/com/msdw/tms/entity/vo/EvaluationVO.java +++ b/src/main/java/com/msdw/tms/entity/vo/EvaluationVO.java @@ -27,10 +27,6 @@ public class EvaluationVO implements Serializable { * 测评时长,单位:分钟 */ private Integer duration; - /** - * 当前用户的剩余时长,格式 HH:ss - */ - private String remainingDuration; /** * 测评总题数 */ diff --git a/src/main/java/com/msdw/tms/service/QuestionsService.java b/src/main/java/com/msdw/tms/service/QuestionsService.java index a55d45a..ee57a5e 100644 --- a/src/main/java/com/msdw/tms/service/QuestionsService.java +++ b/src/main/java/com/msdw/tms/service/QuestionsService.java @@ -44,8 +44,10 @@ public interface QuestionsService extends IService { void downloadFiles(HttpServletResponse response) throws IOException; - EvaluationVO evaluation(Integer userId); + EvaluationVO evaluation(); void exportQuestion(HttpServletResponse response) throws Exception; + + String getEvaluationRemainingTime(Integer userId); } diff --git a/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java b/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java index a87d5c1..55cdf06 100644 --- a/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java +++ b/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java @@ -397,7 +397,7 @@ public class QuestionsServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); queryWrapper.eq("is_del", Constant.IsDel.NOT_DEL.getType()) //未删除 @@ -410,30 +410,35 @@ public class QuestionsServiceImpl extends ServiceImpl ops = stringRedisTemplate.opsForValue(); - String key = REMAINING_TINE_KEY + userId; - String startTime = ops.get(key); - // 剩余时间,单位:秒 - int remainingTime = duration * 60; - if (StringUtils.isEmpty(startTime)) { - //如果没有,表示是开始测评,向redis中插入一条数据,key是前缀加用户id,value是当前时间,过期时间是测评时长加十秒 - ops.set(key, String.valueOf(System.currentTimeMillis()), duration * 60 + 10, TimeUnit.SECONDS); - } else { - // 当前时间与开始时间得时间差,单位:毫秒 - long difference = System.currentTimeMillis() - Long.parseLong(startTime); - remainingTime = (duration * 60) - (new Long(difference).intValue() / 1000); - if (remainingTime < 0) { - ExceptionCast.cast(CommonCode.EVALUATION_TIME_INVALID); - } - } - evaluation.setRemainingDuration(DateUtil.secondToTime(remainingTime)); + + +// //时长转成秒来计算 +// //先查询该用户有没有开始考试,既redis中有没有跟该用户id相关联的key +// //如果没有,表示是开始测评,向redis中插入一条数据,key是前缀加用户id,value是当前时间,过期时间是测评时长加三十秒 +// //如果有,则得到value,既开始测评的时间,用当前时间和开始测评时间做差,测评时长减去该差值 +// //TODO 提交测评时删除redis中的该条数据 +// //得到总时长,单位:分 +// Integer duration = evaluation.getDuration(); +// ValueOperations ops = stringRedisTemplate.opsForValue(); +// String key = REMAINING_TINE_KEY + userId; +// String startTime = ops.get(key); +// // 剩余时间,单位:秒 +// int remainingTime = duration * 60; +// if (StringUtils.isEmpty(startTime)) { +// //如果没有,表示是开始测评,向redis中插入一条数据,key是前缀加用户id,value是当前时间,过期时间是测评时长加十秒 +// ops.set(key, String.valueOf(System.currentTimeMillis()), duration * 60 + 10, TimeUnit.SECONDS); +// } else { +// // 当前时间与开始时间得时间差,单位:毫秒 +// long difference = System.currentTimeMillis() - Long.parseLong(startTime); +// remainingTime = (duration * 60) - (new Long(difference).intValue() / 1000); +// if (remainingTime < 0) { +// ExceptionCast.cast(CommonCode.EVALUATION_TIME_INVALID); +// } +// } +// evaluation.setRemainingDuration(DateUtil.secondToTime(remainingTime)); + + + //搜集随机抽取的试题 Set set = new HashSet<>(); //根据测评规则的类型不同 @@ -505,6 +510,36 @@ public class QuestionsServiceImpl extends ServiceImpl ops = stringRedisTemplate.opsForValue(); + String key = REMAINING_TINE_KEY + userId; + String startTime = ops.get(key); + // 剩余时间,单位:秒 + int remainingTime = duration * 60; + if (StringUtils.isEmpty(startTime)) { + //如果没有,表示是开始测评,向redis中插入一条数据,key是前缀加用户id,value是当前时间,过期时间是测评时长加十秒 + ops.set(key, String.valueOf(System.currentTimeMillis()), duration * 60 + 10, TimeUnit.SECONDS); + } else { + // 当前时间与开始时间得时间差,单位:毫秒 + long difference = System.currentTimeMillis() - Long.parseLong(startTime); + remainingTime = (duration * 60) - (new Long(difference).intValue() / 1000); + if (remainingTime < 0) { + ExceptionCast.cast(CommonCode.EVALUATION_TIME_INVALID); + } + } + return DateUtil.secondToTime(remainingTime); + } + private Set getRandomList(int len, List list) { Set set = new HashSet<>(); Random random = new Random(); diff --git a/src/test/java/com/msdw/tms/service/QuestionsServiceTest.java b/src/test/java/com/msdw/tms/service/QuestionsServiceTest.java index 5493328..062824a 100644 --- a/src/test/java/com/msdw/tms/service/QuestionsServiceTest.java +++ b/src/test/java/com/msdw/tms/service/QuestionsServiceTest.java @@ -121,7 +121,7 @@ class QuestionsServiceTest { @Test void evaluation() { - EvaluationVO evaluation = questionsService.evaluation(1); + EvaluationVO evaluation = questionsService.evaluation(); System.out.println(evaluation.toString()); } }