性能优化

This commit is contained in:
磷叶 2025-05-15 17:38:44 +08:00
parent 6c8c9ae112
commit ce0437814b
6 changed files with 45 additions and 18 deletions

View File

@ -17,7 +17,8 @@ public enum RedisLockKey {
DEVICE_SN_UNIQUE("device_sn_unique", "设备SN唯一"),
DEVICE_MAC_UNIQUE("device_mac_unique", "设备MAC唯一"),
USER_NAME_UNIQUE("user_name_unique", "用户账号唯一"),
DEVICE_IOT_REFRESH("device_iot_refresh", "设备物联网信息刷新");
DEVICE_IOT_REFRESH("device_iot_refresh", "设备物联网信息刷新"),
DEVICE_UPDATE_IOT_LOCK("device_update_iot_lock", "设备更新物联网信息锁");
private final String key;
private final String name;

View File

@ -111,7 +111,7 @@ public class DeviceIotServiceImpl implements DeviceIotService {
transactionTemplate.execute(status -> {
// 更新数据库
int rows = deviceMapper.updateByQuery(data, query);
int rows = deviceMapper.updateByQuerySimple(data, query);
vo.setDb(rows);
if (rows > 0) {
@ -157,9 +157,9 @@ public class DeviceIotServiceImpl implements DeviceIotService {
query.setStatusList(DeviceStatus.canUserLock());
}
Integer result = transactionTemplate.execute(status -> {
transactionTemplate.execute(status -> {
// 更新设备状态
int rows = deviceMapper.updateByQuery(data, query);
int rows = deviceMapper.updateByQuerySimple(data, query);
vo.setDb(rows);
if (rows > 0) {
@ -200,7 +200,7 @@ public class DeviceIotServiceImpl implements DeviceIotService {
// 是否有正在进行的订单
boolean hasOrder = device.getOrderDeviceId() != null && OrderDeviceStatus.inUse().contains(device.getOrderDeviceStatus());
Integer result = transactionTemplate.execute(status -> {
transactionTemplate.execute(status -> {
// 更新设备状态
Device data = new Device();
data.setLockStatus(DeviceLockStatus.CLOSE.getCode());
@ -208,7 +208,7 @@ public class DeviceIotServiceImpl implements DeviceIotService {
DeviceQuery query = new DeviceQuery();
query.setId(device.getId());
query.setStatusList(DeviceStatus.canQLock());
int rows = deviceMapper.updateByQuery(data, query);
int rows = deviceMapper.updateByQuerySimple(data, query);
vo.setDb(rows);
if (rows > 0) {

View File

@ -480,7 +480,7 @@ public class DeviceServiceImpl implements DeviceService
data.setOnlineStatus(onlineStatus);
DeviceQuery query = new DeviceQuery();
query.setEqMac(mac);
return deviceMapper.updateByQuery(data, query);
return deviceMapper.updateByQuerySimple(data, query);
}
@Override

View File

@ -235,7 +235,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
select
bll.longitude,
bll.latitude
<include refid="searchTables"/>
from bst_location_log bll
<where>
<include refid="searchCondition"/>
</where>

View File

@ -29,6 +29,8 @@ import com.ruoyi.bst.locationLog.domain.LocationLog;
import com.ruoyi.bst.locationLog.service.LocationLogConverter;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.redis.RedisLock;
import com.ruoyi.common.core.redis.enums.RedisLockKey;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtils;
@ -76,6 +78,9 @@ public class IotReceiveServiceImpl implements IotReceiveService {
@Autowired
private OperLogService operLogService;
@Autowired
private RedisLock redisLock;
@Override
public void handleReceive(ReceiveMsg msg) {
if (msg == null) {
@ -103,10 +108,9 @@ public class IotReceiveServiceImpl implements IotReceiveService {
// 更新设备信息
device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus());
device.setLastOnlineTime(at);
int update = deviceIotService.updateIot(device);
if (update != 1) {
log.error("更新设备信息失败: {}", msg.getDevName());
}
// 增加更新频率控制
this.updateDeviceIot(device);
// 定位无效不处理
if (device.getLongitude() == null || device.getLatitude() == null) {
@ -143,6 +147,22 @@ public class IotReceiveServiceImpl implements IotReceiveService {
}
}
/**
* 更新设备物联网信息
* @param device
*/
private void updateDeviceIot(DeviceVO device) {
boolean lock = redisLock.lock(RedisLockKey.DEVICE_UPDATE_IOT_LOCK.getKey(), device.getMac(), 20L, TimeUnit.SECONDS, 1);
if (lock) {
int update = deviceIotService.updateIot(device);
if (update != 1) {
log.error("更新设备信息失败: {}", device.getMac());
}
} else {
log.info("设备{}更新太频繁,跳过本次更新", device.getMac());
}
}
/**
* 处理设备重启
*/

View File

@ -289,13 +289,19 @@ public class IotServiceImpl implements IotService {
// 转为在线状态并缓存结果10秒
String status = this.parseToOnlineStatus(res, deviceName, true);
redisCache.setCacheObject(this.getOnlineCacheKey(deviceName), status, 10, TimeUnit.SECONDS);
// 异步更新设备在线状态
scheduledExecutorService.schedule(() -> {
int update = deviceService.updateOnlineStatusByMac(deviceName, status);
if (update != 1) {
log.error("异步更新设备在线状态失败,MAC={},status={}", deviceName, status);
}
},0, TimeUnit.SECONDS);
boolean lock = redisLock.lock(RedisLockKey.DEVICE_UPDATE_IOT_LOCK.getKey(), deviceName, 20L, TimeUnit.SECONDS, 1);
if (lock ) {
scheduledExecutorService.execute(() -> {
int update = deviceService.updateOnlineStatusByMac(deviceName, status);
if (update != 1) {
log.error("异步更新设备在线状态失败,MAC={},status={}", deviceName, status);
}
});
} else {
log.info("设备{}更新太频繁,跳过本次更新", deviceName);
}
return res;
} catch (Exception e) {