feat():监听上下线事件

master
Saisai Liu 2024-06-04 19:46:13 +08:00
parent 27c8cd1ae9
commit bd973ab220
3 changed files with 32 additions and 23 deletions

View File

@ -27,6 +27,7 @@ public class StayTime {
private String ip; private String ip;
private Long upTime; private Long upTime;
private Long downTime; private Long downTime;
private String stayLongTime;
private String remark; private String remark;
private String createBy; private String createBy;
private Date createTime; private Date createTime;

View File

@ -130,29 +130,34 @@ public class MessageHandler {
} }
// 方法内有判断,有则自减,无则创建 // 方法内有判断,有则自减,无则创建
redisService.decrement("onlineCar-" + ip, 1); redisService.decrement("onlineCar-" + ip, 1);
// 判断是否有该记录
StayTime vinStayTime = stayTimeService.getOne(new LambdaQueryWrapper<>() {{ StayTime vinStayTime = stayTimeService.getOne(new LambdaQueryWrapper<>() {{
eq(StayTime::getVin, vin); eq(StayTime::getVin, vin);
eq(StayTime::getDownTime, null); eq(StayTime::getDownTime, 0);
}}); }});
String format = new SimpleDateFormat("HH时mm分ss秒").format(new Date(vinStayTime.getUpTime() - timestamp));
vinStayTime.setStayLongTime(format);
vinStayTime.setDownTime(timestamp); vinStayTime.setDownTime(timestamp);
// 修改下线时间
boolean update = stayTimeService.update(vinStayTime, new LambdaUpdateWrapper<>() {{ boolean update = stayTimeService.update(vinStayTime, new LambdaUpdateWrapper<>() {{
eq(StayTime::getVin, vin); eq(StayTime::getVin, vin);
eq(StayTime::getDownTime, null); eq(StayTime::getDownTime, 0);
}}); }});
// 输出在线时长 // 输出在线时长
log.info("车辆在线时长为:{}", new SimpleDateFormat("HH时mm分ss秒").format(new Date(vinStayTime.getUpTime() - timestamp))); log.info("车辆在线时长为:{}", format);
log.info(update ? vin + "上线记录成功" : vin + "上线记录失败"); log.info(update ? vin + "下线记录成功" : vin + "下线记录失败");
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (ServletException | IOException e) { } catch (ServletException | IOException e) {
log.error("下线失败"); log.error("下线失败");
throw new RuntimeException(e);
} finally {
try { try {
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
throw new RuntimeException(e);
} }
} }

View File

@ -4,10 +4,7 @@ import com.alibaba.fastjson2.JSON;
import org.apache.poi.ss.formula.functions.T; import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.BoundSetOperations; import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.*; import java.util.*;
@ -25,6 +22,8 @@ public class RedisService {
@Autowired @Autowired
public RedisTemplate redisTemplate; public RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
/** /**
* IntegerString * IntegerString
* *
@ -261,37 +260,41 @@ public class RedisService {
} }
public void increment(String s) { public void increment(String s) {
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){ if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(s))){
redisTemplate.opsForValue().increment(s,1); stringRedisTemplate.opsForValue().increment(s,1);
}else { }else {
redisTemplate.opsForValue().set(s,1); stringRedisTemplate.opsForValue().set(s,1+"");
} }
} }
public void increment(String s,int i) { public void increment(String s,int i) {
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){ if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(s))){
redisTemplate.opsForValue().increment(s,i); String o = stringRedisTemplate.opsForValue().get(s);
System.out.println(o);
stringRedisTemplate.opsForValue().increment(s,i);
}else { }else {
redisTemplate.opsForValue().set(s,i); stringRedisTemplate.opsForValue().set(s,i+"");
} }
} }
public void decrement(String s) { public void decrement(String s) {
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){ if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(s))){
redisTemplate.opsForValue().decrement(s,1); stringRedisTemplate.opsForValue().decrement(s,1);
}else { }else {
redisTemplate.opsForValue().set(s,0); stringRedisTemplate.opsForValue().set(s,0+"");
} }
} }
public void decrement(String s,int i) { public void decrement(String s,int i) {
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){ if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(s))){
redisTemplate.opsForValue().decrement(s,i); stringRedisTemplate.opsForValue().decrement(s,i);
String o = (String) stringRedisTemplate.opsForValue().get(s);
System.out.println(o);
}else { }else {
redisTemplate.opsForValue().set(s,0); stringRedisTemplate.opsForValue().set(s,0+"");
} }
} }
public String getValue(String vin) { public String getValue(String vin) {
return (String) redisTemplate.opsForValue().get(vin); return stringRedisTemplate.opsForValue().get(vin);
} }
} }