diff --git a/pom.xml b/pom.xml index 802b602..2d3b361 100644 --- a/pom.xml +++ b/pom.xml @@ -128,6 +128,12 @@ org.springframework.boot spring-boot-starter-data-redis + + + cn.hutool + hutool-all + 5.3.8 + diff --git a/src/main/java/com/msdw/tms/service/impl/EvaluationRulesServiceImpl.java b/src/main/java/com/msdw/tms/service/impl/EvaluationRulesServiceImpl.java index 3977fd4..dd10e50 100644 --- a/src/main/java/com/msdw/tms/service/impl/EvaluationRulesServiceImpl.java +++ b/src/main/java/com/msdw/tms/service/impl/EvaluationRulesServiceImpl.java @@ -55,11 +55,11 @@ public class EvaluationRulesServiceImpl extends ServiceImpl ops = stringRedisTemplate.opsForValue(); - String key = REMAINING_TINE_KEY + userId; - String startTime = ops.get(key); - int remainingTime = duration; + // 剩余时间,单位:秒 + int remainingTime = duration * 60; if (StringUtils.isEmpty(startTime)) { //如果没有,表示是开始测评,向redis中插入一条数据,key是前缀加用户id,value是当前时间,过期时间是测评时长加十秒 - ops.set(key, getSecondTimestamp(new Date()) + "", duration * 60 + 10, TimeUnit.SECONDS); - + ops.set(key, String.valueOf(System.currentTimeMillis()), duration * 60 + 10, TimeUnit.SECONDS); } else { - int difference = getSecondTimestamp(new Date()) - Integer.valueOf(startTime); - remainingTime = duration - difference; + // 当前时间与开始时间得时间差,单位:毫秒 + 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(formatDateTime(remainingTime)); - + evaluation.setRemainingDuration(DateUtil.secondToTime(remainingTime)); //搜集随机抽取的试题 Set set = new HashSet<>(); - //根据测评规则的类型不同 Integer evaluationType = evaluationRules.getEvaluationType(); if (evaluationType.equals(Constant.RulesType.RANDOM.getType())) {//随机 @@ -455,68 +451,36 @@ public class QuestionsServiceImpl extends ServiceImpl singleChoice = this.list(queryWrapper.eq("question_type_no", + List singleChoice = this.list(queryWrapper.eq("question_type", Constant.QuestionType.SINGLE_CHOICE.getType())); set.addAll(getRandomList(singleNum, singleChoice)); } if (evaluationRules.getIsMultipleEnable().equals(Constant.IsEnable.ENABLE.getType())) { multipleNum = evaluationRules.getMultipleNum(); - List multipleChoice = this.list(queryWrapper.eq("question_type_no", + List multipleChoice = this.list(queryWrapper.eq("question_type", Constant.QuestionType.MULTIPLE_CHOICE.getType())); set.addAll(getRandomList(multipleNum, multipleChoice)); } if (evaluationRules.getIsJudgmentEnable().equals(Constant.IsEnable.ENABLE.getType())) { judgmentNum = evaluationRules.getJudgmentNum(); - List judgments = this.list(queryWrapper.eq("question_type_no", + List judgments = this.list(queryWrapper.eq("question_type", Constant.QuestionType.TRUE_OR_FALSE.getType())); set.addAll(getRandomList(judgmentNum, judgments)); } // 总题数等于各类题目数量之和 evaluation.setQuestionNum(singleNum + multipleNum + judgmentNum); } -// Set collect = set.stream().map(item -> { -// QuestionsVO questionsVO = new QuestionsVO(); -// BeanUtils.copyProperties(item, questionsVO); -// return questionsVO; -// }).collect(Collectors.toSet()); evaluation.setQuestions(set); return evaluation; } - private int getSecondTimestamp(Date date) { - if (null == date) { - return 0; - } - String timestamp = String.valueOf(date.getTime() / 1000 / 60); - return Integer.valueOf(timestamp); - } - - private String formatDateTime(long mss) { - String DateTimes; - long hours = (mss % (60 * 24)) / (60); - long minutes = (mss % (60)); - //long seconds = mss % 60; - if (hours > 0) { - if (hours < 10) { - DateTimes = "0" + hours + ":" + minutes; - } else { - DateTimes = hours + ":" + minutes; - } - } else { - DateTimes = "00:" + minutes; - } - - return DateTimes; - } - @Override public void exportQuestion(HttpServletResponse response) throws Exception { // 请求包装类 diff --git a/src/test/java/com/msdw/tms/TmsApplicationTests.java b/src/test/java/com/msdw/tms/TmsApplicationTests.java index e392191..acdf743 100644 --- a/src/test/java/com/msdw/tms/TmsApplicationTests.java +++ b/src/test/java/com/msdw/tms/TmsApplicationTests.java @@ -1,5 +1,6 @@ package com.msdw.tms; +import cn.hutool.core.date.DateUtil; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -173,4 +174,57 @@ class TmsApplicationTests { // System.currentTimeMillis() / (long)1000 / (long)60; } + @Test + void t4() throws ParseException { + Date date = getDate("2020-08-26 09:13:51"); + + int secondTimestampTwo = getSecondTimestampTwo(date); + + int secondTimestampTwo1 = getSecondTimestampTwo(new Date()); + + int i = secondTimestampTwo1 - secondTimestampTwo; + + System.out.println(i); + // System.currentTimeMillis() / (long)1000 / (long)60; + } + + //处理时间格式 + private String handleTime(Date date) { + Instant instant = date.toInstant(); + ZoneId zoneId = ZoneId.systemDefault(); + LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); + return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); // 当前日期和时间 + } + + private Date getDate(String dateStr) throws ParseException { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM + Date date = simpleDateFormat.parse(dateStr); + System.out.println(date); //Mon Sep 02 00:00:00 CST 2019 + System.out.println(simpleDateFormat.format(date)); //2019-09-02 + return date; + } + + + @Autowired + StringRedisTemplate stringRedisTemplate; + + + @Test + void t5() throws ParseException { + + ValueOperations ops = stringRedisTemplate.opsForValue(); + + ops.set("aaa", "sss", 30, TimeUnit.SECONDS); + } + + @Test + void t6() throws ParseException { + + int se = 130*60; + + String s = DateUtil.secondToTime(se); + + System.out.println(s); + } + }