|
|
|
@ -1,6 +1,10 @@
|
|
|
|
|
package com.sztzjy.trade.controller.task;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
|
|
import com.sztzjy.trade.entity.TchLoginLog;
|
|
|
|
|
import com.sztzjy.trade.entity.TchLoginLogExample;
|
|
|
|
|
import com.sztzjy.trade.mapper.StuUserMapper;
|
|
|
|
|
import com.sztzjy.trade.mapper.TchLoginLogMapper;
|
|
|
|
|
import com.sztzjy.trade.util.CacheProvider;
|
|
|
|
|
import com.sztzjy.trade.util.RedisUtil;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
@ -11,8 +15,10 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author 17803
|
|
|
|
@ -24,14 +30,57 @@ public class TaskController {
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisUtil redisUtil;
|
|
|
|
|
|
|
|
|
|
// @Scheduled(fixedDelay = 10000)
|
|
|
|
|
@Autowired
|
|
|
|
|
private TchLoginLogMapper tchLoginLogMapper;
|
|
|
|
|
|
|
|
|
|
@Scheduled(fixedDelay = 10000)
|
|
|
|
|
public void updateUserRank(){
|
|
|
|
|
//先查询所有学校id
|
|
|
|
|
Map<String, Long> log = (Map<String, Long>)redisUtil.get("loginLog");
|
|
|
|
|
Set<String> keys = redisUtil.keys("userId:*");
|
|
|
|
|
for (String key : keys) {
|
|
|
|
|
|
|
|
|
|
// 假设 redisUtil.get(key) 返回的是一个 long 类型的时间戳
|
|
|
|
|
Long newTime = redisUtil.get(key);
|
|
|
|
|
|
|
|
|
|
if (newTime != null) {
|
|
|
|
|
long currentTime = System.currentTimeMillis(); // 获取当前时间戳
|
|
|
|
|
long timeDifference = currentTime - newTime; // 计算时间差
|
|
|
|
|
|
|
|
|
|
long time = 1 * 60 * 1000;
|
|
|
|
|
// 判断时间差是否大于 20 分钟(20 分钟 = 20 * 60 * 1000 毫秒)
|
|
|
|
|
if (timeDifference > time) {
|
|
|
|
|
//大于20分钟未操作 表明用户已经离线 在线时长+20分钟
|
|
|
|
|
System.out.println("大于1分钟未操作"+key);
|
|
|
|
|
String userId = key.split("userId:")[1];
|
|
|
|
|
|
|
|
|
|
//String userId = key.split("userId:").toString();
|
|
|
|
|
|
|
|
|
|
TchLoginLogExample tchLoginLogExample = new TchLoginLogExample();
|
|
|
|
|
tchLoginLogExample.setOrderByClause("login_time_last desc");
|
|
|
|
|
tchLoginLogExample.createCriteria().andUserIdEqualTo(userId);
|
|
|
|
|
List<TchLoginLog> tchLoginLogs = tchLoginLogMapper.selectByExample(tchLoginLogExample);
|
|
|
|
|
if (!tchLoginLogs.isEmpty()) {
|
|
|
|
|
tchLoginLogs.get(0).setOnline((byte) 0);
|
|
|
|
|
|
|
|
|
|
// 将毫秒转换为分钟
|
|
|
|
|
//long timeDifferenceMinutes = timeDifferenceMillis / (1000 * 60);
|
|
|
|
|
tchLoginLogs.get(0).setExitTimeLast(Convert.toDate(currentTime));
|
|
|
|
|
tchLoginLogs.get(0).setLoginDuration(timeDifference/1000);
|
|
|
|
|
tchLoginLogs.get(0).setTotalLoginDuration((timeDifference / 1000)+ tchLoginLogs.get(0).getTotalLoginDuration());
|
|
|
|
|
tchLoginLogMapper.updateByPrimaryKey(tchLoginLogs.get(0));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
redisUtil.del(key);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("Redis 中没有找到对应的值"+key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(log);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|