diff --git a/fys-admin/src/main/java/com/fuyuanshen/global/queue/OnlineStatusTask.java b/fys-admin/src/main/java/com/fuyuanshen/global/queue/OnlineStatusTask.java index d552b17c..96930c5a 100644 --- a/fys-admin/src/main/java/com/fuyuanshen/global/queue/OnlineStatusTask.java +++ b/fys-admin/src/main/java/com/fuyuanshen/global/queue/OnlineStatusTask.java @@ -25,6 +25,7 @@ public class OnlineStatusTask { @Scheduled(cron = "0 */3 * * * ?") public void cronTask() { QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("online_status", 1); List devices = deviceMapper.selectList(queryWrapper); devices.forEach(item -> { String onlineStatusKey = GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + item.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX; diff --git a/fys-admin/src/main/java/com/fuyuanshen/web/service/device/DeviceBizService.java b/fys-admin/src/main/java/com/fuyuanshen/web/service/device/DeviceBizService.java index d77a9bdb..0aba6f84 100644 --- a/fys-admin/src/main/java/com/fuyuanshen/web/service/device/DeviceBizService.java +++ b/fys-admin/src/main/java/com/fuyuanshen/web/service/device/DeviceBizService.java @@ -179,7 +179,7 @@ public class DeviceBizService { } Device device = devices.get(0); if (device.getBindingStatus() != null && device.getBindingStatus() == BindingStatusEnum.BOUND.getCode()) { - throw new RuntimeException("设备已绑定"); + throw new RuntimeException("设备已经被绑定!"); } QueryWrapper bindRecordQueryWrapper = new QueryWrapper<>(); @@ -189,7 +189,6 @@ public class DeviceBizService { if (appDeviceBindRecord != null) { UpdateWrapper deviceUpdateWrapper = new UpdateWrapper<>(); deviceUpdateWrapper.eq("device_id", device.getId()) - .set("binding_status", BindingStatusEnum.BOUND.getCode()) .set("binding_user_id", userId) .set("communication_mode", 0) .set("update_time", new Date()) @@ -283,6 +282,25 @@ public class DeviceBizService { .set("binding_time", null); deviceMapper.update(null, deviceUpdateWrapper); } + QueryWrapper brqWrapper = new QueryWrapper<>(); + brqWrapper.eq("device_id", device.getId()); + brqWrapper.eq("binding_user_id", userId); + List appDeviceBindRecordList = appDeviceBindRecordMapper.selectList(brqWrapper); + if (CollectionUtil.isNotEmpty(appDeviceBindRecordList)) { + appDeviceBindRecordList.forEach(appDeviceBindRecord -> + appDeviceBindRecordMapper.deleteById(appDeviceBindRecord.getId())); + } + AppUserVo appUserVo = appUserMapper.selectVoById(userId); + if(appUserVo != null){ + QueryWrapper appDeviceShareQueryWrapper = new QueryWrapper<>(); + appDeviceShareQueryWrapper.eq("device_id", device.getId()); + appDeviceShareQueryWrapper.eq("phonenumber", appUserVo.getPhonenumber()); + List appDeviceShareList = appDeviceShareMapper.selectList(appDeviceShareQueryWrapper); + if (CollectionUtil.isNotEmpty(appDeviceShareList)) { + appDeviceShareList.forEach(appDeviceShare -> + appDeviceShareMapper.deleteById(appDeviceShare.getId())); + } + } }else{ QueryWrapper bindRecordQueryWrapper = new QueryWrapper<>(); bindRecordQueryWrapper.eq("device_id", device.getId()); @@ -296,25 +314,17 @@ public class DeviceBizService { .set("binding_status", BindingStatusEnum.UNBOUND.getCode()) .set("binding_time", null); deviceMapper.update(null, deviceUpdateWrapper); - } - QueryWrapper brqWrapper = new QueryWrapper<>(); - brqWrapper.eq("device_id", device.getId()); - if(userId != null){ - brqWrapper.eq("binding_user_id", userId); - } + QueryWrapper brqWrapper = new QueryWrapper<>(); + brqWrapper.eq("device_id", device.getId()); + List appDeviceBindRecordList = appDeviceBindRecordMapper.selectList(brqWrapper); + if (CollectionUtil.isNotEmpty(appDeviceBindRecordList)) { + appDeviceBindRecordList.forEach(appDeviceBindRecord -> + appDeviceBindRecordMapper.deleteById(appDeviceBindRecord.getId())); + } - List appDeviceBindRecordList = appDeviceBindRecordMapper.selectList(brqWrapper); - if (CollectionUtil.isNotEmpty(appDeviceBindRecordList)) { - appDeviceBindRecordList.forEach(appDeviceBindRecord -> - appDeviceBindRecordMapper.deleteById(appDeviceBindRecord.getId())); - } - - AppUserVo appUserVo = appUserMapper.selectVoById(userId); - if(appUserVo != null){ QueryWrapper appDeviceShareQueryWrapper = new QueryWrapper<>(); appDeviceShareQueryWrapper.eq("device_id", device.getId()); - appDeviceShareQueryWrapper.eq("phonenumber", appUserVo.getPhonenumber()); List appDeviceShareList = appDeviceShareMapper.selectList(appDeviceShareQueryWrapper); if (CollectionUtil.isNotEmpty(appDeviceShareList)) { appDeviceShareList.forEach(appDeviceShare -> @@ -384,7 +394,7 @@ public class DeviceBizService { String deviceImei = device.getDeviceImei(); String a = GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX+ deviceImei + DEVICE_LOCATION_KEY_PREFIX + ":history"; - Collection list = RedisUtils.zRangeByScore(a, startTime, endTime); + Collection list = RedisUtils.zRangeByScoreDesc(a, startTime, endTime); if (CollectionUtil.isEmpty(list)) { return null; } diff --git a/fys-common/fys-common-redis/src/main/java/com/fuyuanshen/common/redis/utils/RedisUtils.java b/fys-common/fys-common-redis/src/main/java/com/fuyuanshen/common/redis/utils/RedisUtils.java index 6357d167..04c15ae2 100644 --- a/fys-common/fys-common-redis/src/main/java/com/fuyuanshen/common/redis/utils/RedisUtils.java +++ b/fys-common/fys-common-redis/src/main/java/com/fuyuanshen/common/redis/utils/RedisUtils.java @@ -394,6 +394,26 @@ public class RedisUtils { return null; } } + /** + * 根据时间范围查询Sorted Set中的元素并降序排列 + * + * @param key 键 + * @param startTime 开始时间戳 + * @param endTime 结束时间戳 + * @return 指定时间范围内的元素集合(降序) + */ + public static Collection zRangeByScoreDesc(String key, Long startTime, Long endTime) { + try { + RScoredSortedSet sortedSet = CLIENT.getScoredSortedSet(key); + // 使用 valueRangeReversed 方法获取降序排列的结果 + return sortedSet.valueRangeReversed(startTime, true, endTime, true); + } catch (Exception e) { + // 记录错误日志(如果项目中有日志工具的话) + // log.error("根据时间范围查询Sorted Set中的元素失败: key={}, startTime={}, endTime={}, error={}", + // key, startTime, endTime, e.getMessage(), e); + return null; + } + } /** diff --git a/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceMapper.xml b/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceMapper.xml index d4edd5ac..d97ad266 100644 --- a/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceMapper.xml +++ b/fys-modules/fys-equipment/src/main/resources/mapper/equipment/DeviceMapper.xml @@ -311,7 +311,7 @@ AND a.device_name like concat('%',#{bo.deviceName},'%') - AND a.device_mac = #{bo.deviceMac} + AND a.device_mac like concat('%',#{bo.deviceMac},'%') and a.device_imei like concat('%',#{bo.deviceImei}, '%')