测评剩余时间

hehai
shijie 4 years ago
parent f043c9f4b9
commit 403d8e1602
  1. 6
      pom.xml
  2. 49
      src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java
  3. 63
      src/test/java/com/msdw/tms/TmsApplicationTests.java

@ -128,6 +128,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.8</version>
</dependency>
</dependencies>
<build>

@ -1,5 +1,6 @@
package com.msdw.tms.service.impl;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -409,37 +410,32 @@ public class QuestionsServiceImpl extends ServiceImpl<QuestionsDao, QuestionsEnt
BeanUtils.copyProperties(evaluationRules, evaluation);
//TODO 处理时长,获取当前登录用户id,将用户剩余的时长实时计算,存在Redis中
//时长转成秒来计算
//先查询该用户有没有开始考试,既redis中有没有跟该用户id相关联的key
//如果没有,表示是开始测评,向redis中插入一条数据,key是前缀加用户id,value是当前时间,过期时间是测评时长加三十秒
//如果有,则得到value,既开始测评的时间,用当前时间和开始测评时间做差,测评时长减去该差值
//TODO 提交测评时删除redis中的该条数据
//得到总时长
//得到总时长,单位:分
Integer duration = evaluation.getDuration();
ValueOperations<String, String> 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<QuestionsVO> set = new HashSet<>();
//根据测评规则的类型不同
Integer evaluationType = evaluationRules.getEvaluationType();
if (evaluationType.equals(Constant.RulesType.RANDOM.getType())) {//随机
@ -455,7 +451,6 @@ public class QuestionsServiceImpl extends ServiceImpl<QuestionsDao, QuestionsEnt
int multipleNum = 0;
// 判断题数量
int judgmentNum = 0;
//判断各条目的题型是否启用
if (evaluationRules.getIsSingleEnable().equals(Constant.IsEnable.ENABLE.getType())) {
singleNum = evaluationRules.getSingleNum();
@ -486,32 +481,6 @@ public class QuestionsServiceImpl extends ServiceImpl<QuestionsDao, QuestionsEnt
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 {
// 请求包装类

@ -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;
@ -98,6 +99,57 @@ class TmsApplicationTests {
return k;
}
@Test
void t1() {
long mss = 150 * 60;
String ss = formatDateTime3(mss);
System.out.println(ss);
}
private String formatDateTime3(long mss) {
String DateTimes = "";
long hours = (mss % (60 * 60 * 24)) / (60 * 60);
long minutes = (mss % (60 * 60)) / 60;
long seconds = mss % 60;
if (hours > 0) {
if (hours < 10) {
DateTimes += "0" + hours;
} else {
DateTimes += hours + "";
}
}
if (minutes > 0) {
if (minutes < 10) {
DateTimes += "0" + minutes;
} else {
DateTimes += minutes;
}
}
if (seconds > 0) {
if (seconds < 10) {
DateTimes += "0" + seconds;
} else {
DateTimes += seconds;
}
}
// if (hours < 10) {
// DateTimes += "0" + hours + ":";
// } else {
// DateTimes += hours + "" + ":";
// }
// if (minutes < 10) {
// DateTimes += "0" + minutes + ":";
// } else {
// DateTimes += minutes + ":";
// }
// if (seconds < 10) {
// DateTimes += "0" + seconds;
// } else {
// DateTimes += seconds;
// }
return DateTimes;
}
private String formatDateTime(long mss) {
String DateTimes = null;
long days = mss / (60 * 60 * 24);
@ -215,4 +267,15 @@ class TmsApplicationTests {
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);
}
}

Loading…
Cancel
Save