添加定时任务,每二十分钟执行一次 查询用户是否离线

main
whb 3 months ago
parent 69ca1bb721
commit f5e0805bb5

@ -33,7 +33,8 @@ public class TaskController {
@Autowired
private TchLoginLogMapper tchLoginLogMapper;
@Scheduled(fixedDelay = 10000)
//每二十分钟执行一次 查询用户是否离线
@Scheduled(fixedDelay = 1200000)
public void updateUserRank(){
Set<String> keys = redisUtil.keys("userId:*");
for (String key : keys) {
@ -70,7 +71,6 @@ public class TaskController {
}
redisUtil.del(key);
}

@ -53,10 +53,14 @@ public class TchLoginLogAndStuListController {
if (redisUtil.get("userId:"+loginLogDTO.getUserId())== null) {
return new ResultEntity(HttpStatus.OK);
}
//在线的登陆时间
Long loginTime = redisUtil.get("userId:" + loginLogDTO.getUserId());
loginLogDTO.setLoginTimeLast(Convert.toDate(loginTime));
if (loginLogDTO.getLoginTimeLast() != null) {
//在线的登陆时间
Long loginTime = redisUtil.get("userId:" + loginLogDTO.getUserId());
loginLogDTO.setLoginTimeLast(Convert.toDate(loginTime));
}
return tchLoginLogAndStuListService.loginLog(loginLogDTO, request);

@ -14,11 +14,13 @@ import com.sztzjy.trade.mapper.StuPublicNoticeReadInfoMapper;
import com.sztzjy.trade.mapper.StuUserMapper;
import com.sztzjy.trade.mapper.TchLoginLogMapper;
import com.sztzjy.trade.service.TchLoginLogAndStuListService;
import com.sztzjy.trade.util.RedisUtil;
import com.sztzjy.trade.util.ResultEntity;
import com.sztzjy.trade.util.excel.FilePortUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.geolatte.geom.V;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
@ -47,6 +49,9 @@ public class TchLoginLogAndStuListServiceImpl implements TchLoginLogAndStuListSe
@Autowired
private StuUserMapper stuUserMapper;
@Autowired
private RedisUtil redisUtil;
/**
* /退
*
@ -74,11 +79,11 @@ public class TchLoginLogAndStuListServiceImpl implements TchLoginLogAndStuListSe
//登录时间不为空
if (loginLogDTO.getLoginTimeLast() != null) {
if (loginLogDTO.getLoginTimeLast() != null ) {
//loginTimeLast 都不为 null 的情况
TchLoginLogExample example = new TchLoginLogExample();
example.setOrderByClause("exit_time_last desc");
example.setOrderByClause("login_time_last desc");
example.createCriteria().andUserIdEqualTo(loginLogDTO.getUserId()).andSchoolIdEqualTo(loginLogDTO.getSchoolId());
List<TchLoginLog> tchLoginLogList = tchLoginLogMapper.selectByExample(example);
@ -135,19 +140,30 @@ public class TchLoginLogAndStuListServiceImpl implements TchLoginLogAndStuListSe
tchLoginLogMapper.insertSelective(tchLoginLogOld);
return new ResultEntity<>(HttpStatus.OK, "success");
}
}else if (loginLogDTO.getExitTimeLast() != null) {
exitLogin(loginLogDTO,ip,ipAddress);
return new ResultEntity(HttpStatus.BAD_REQUEST, "请重新登录!");
}
return new ResultEntity(HttpStatus.BAD_REQUEST, "请重新登录!");
}
public void exitLogin(LoginLogDTO loginLogDTO,String ip,String ipAddress){
//退出时间不为空
if (loginLogDTO.getExitTimeLast() != null) {
TchLoginLogExample example = new TchLoginLogExample();
example.setOrderByClause("exit_time_last desc");
example.createCriteria().andUserIdEqualTo(loginLogDTO.getUserId()).andSchoolIdEqualTo(loginLogDTO.getSchoolId());
example.createCriteria().andUserIdEqualTo(loginLogDTO.getUserId()).andSchoolIdEqualTo(loginLogDTO.getSchoolId()).andExitTimeLastIsNull();
List<TchLoginLog> tchLoginLogList = tchLoginLogMapper.selectByExample(example);
if (tchLoginLogList.isEmpty()) { //首次登录
return new ResultEntity(HttpStatus.BAD_REQUEST, "请重新登录!");
return;
} else {
@ -176,15 +192,13 @@ public class TchLoginLogAndStuListServiceImpl implements TchLoginLogAndStuListSe
tchLoginLog.setOnline((byte) 0);
tchLoginLogMapper.updateByPrimaryKeySelective(tchLoginLog);
redisUtil.del("userId:"+loginLogDTO.getUserId());
return new ResultEntity<>(HttpStatus.OK, "success");
}
}
return new ResultEntity(HttpStatus.BAD_REQUEST, "请重新登录!");
}
//分页查询登录日志

Loading…
Cancel
Save