Xiajijian
2025-12-25 579f3590f60df0e26c3400394222b26d3d7cc87c
ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java
@@ -176,6 +176,8 @@
        try {
            // 1. 检查已存在的无人机盒子
            List<ArdDroneInfo> existingDrones = getExistingDrones(boxSn);
            // 1.1 新增时不能添加重复的
            checkDuplicateBox(existingDrones, boxSn);
            // 2. 授权数量验证
            validateAuthorization(existingDrones.size(), boxSn.length);
            // 3. 获取并组装新的无人机信息
@@ -204,6 +206,29 @@
                .map(ArdDroneInfoMapper::selectArdDroneByBoxSn)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
    /**
     * 检查是否有重复的盒子编码
     */
    private void checkDuplicateBox(List<ArdDroneInfo> existingDrone,String[] boxSn) {
        Set<String> existingBoxSnSet = new HashSet<>();
        for (ArdDroneInfo droneInfo : existingDrone) {
            existingBoxSnSet.add(droneInfo.getBoxSn());
        }
        // 检查是否有重复的盒子编码
        List<String> duplicateBoxSns = new ArrayList<>();
        for (String sn : boxSn) {
            if (existingBoxSnSet.contains(sn)) {
                duplicateBoxSns.add(sn);
            }
        }
        if (!duplicateBoxSns.isEmpty()) {
            log.warn("存在重复的无人机盒子编码: {}", duplicateBoxSns);
            throw new ServiceException("以下无人机盒子编码已存在: " + String.join(", ", duplicateBoxSns));
        }
    }
    /**
@@ -320,11 +345,12 @@
                String redisKey = CacheConstants.UAV_ONLINE_KEY + boxSn;
                boolean currentOnline = remote.getOnLine() == 1;
                // 获取上次状态
                boolean lastOnline = redisCache.getCacheObject(redisKey) != null;
                ArdDroneInfo local = localMap.get(boxSn);
                boolean dbOnline = local != null && local.getOnLine() == 1;
                boolean needUpdate = (dbOnline != currentOnline);
                // 如果状态变化才更新
                if (lastOnline != currentOnline) {
                if (needUpdate){
                    // 更新 Redis
                    if (currentOnline) {
                        redisCache.setCacheObject(redisKey, remote);
@@ -334,7 +360,6 @@
                    // 发布事件
                    eventPublisher.publishEvent(new DeviceStatusEvent(boxSn, currentOnline));
                    // 更新数据库
                    ArdDroneInfo local = localMap.get(boxSn);
                    if (local != null) {
                        local.setOnLine(currentOnline ? 1 : 0);
                        updateArdDrone(local);