Xiajijian
2025-12-26 f56ac229a0a897d81ce7066262ff22f2dd7bf651
ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java
@@ -164,7 +164,7 @@
     * @return 成功插入的记录数
     */
    @Override
    public int insertArdDroneLs(String[] boxSn, Long deptId) {
    public int insertArdDroneLs(String[] boxSn,Long deptId,String serverId) {
        // 参数校验
        if (boxSn == null || boxSn.length == 0) {
            log.warn("传入的盒子编码为空");
@@ -176,10 +176,12 @@
        try {
            // 1. 检查已存在的无人机盒子
            List<ArdDroneInfo> existingDrones = getExistingDrones(boxSn);
            // 1.1 新增时不能添加重复的boxSn
            checkDuplicateBox(existingDrones, boxSn);
            // 2. 授权数量验证
            validateAuthorization(existingDrones.size(), boxSn.length);
            // 3. 获取并组装新的无人机信息
            List<ArdDroneInfo> newDrones = assembleNewDrones(boxSn, deptId);
            List<ArdDroneInfo> newDrones = assembleNewDrones(boxSn,deptId,serverId);
            // 4. 批量插入
            return batchInsertDrones(newDrones);
        } catch (Exception e) {
@@ -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));
        }
    }
    /**
@@ -236,19 +261,19 @@
    /**
     * 组装新的无人机信息
     */
    private List<ArdDroneInfo> assembleNewDrones(String[] boxSn, Long deptId) {
    private List<ArdDroneInfo> assembleNewDrones(String[] boxSn,Long deptId,String serverId) {
        List<BoxInfoReceiver> allBoxes = tyService.getBoxList();
        Set<String> targetBoxSnSet = new HashSet<>(Arrays.asList(boxSn));
        return allBoxes.stream()
                .filter(box -> targetBoxSnSet.contains(box.getBoxSn()))
                .map(box -> createDroneInfo(box, deptId))
                .map(box -> createDroneInfo(box, deptId, serverId))
                .collect(Collectors.toList());
    }
    /**
     * 创建无人机信息对象
     */
    private ArdDroneInfo createDroneInfo(BoxInfoReceiver box, Long deptId) {
    private ArdDroneInfo createDroneInfo(BoxInfoReceiver box, Long deptId ,String serverId) {
        ArdDroneInfo drone = new ArdDroneInfo();
        drone.setId(IdUtils.simpleUUID());
        drone.setBoxSn(box.getBoxSn());
@@ -259,21 +284,13 @@
        drone.setVideoPort(box.getVideoPort());
        drone.setLastTaskId(box.getLastTaskId());
        drone.setDeptId(deptId);
        // 设置创建信息和更新信息
        setCreateAndUpdateInfo(drone);
        drone.setCreateTime(new Date());
        drone.setUpdateTime(new Date());
        drone.setServerId(serverId);
        drone.setBoxVersion(box.getBoxVersion());
        return drone;
    }
    /**
     * 设置创建和更新信息
     */
    private void setCreateAndUpdateInfo(ArdDroneInfo drone) {
        Date currentTime = new Date();
        drone.setCreateTime(currentTime);
        drone.setUpdateTime(currentTime);
    }
    /**
     * 批量插入无人机信息
@@ -320,11 +337,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 +352,6 @@
                    // 发布事件
                    eventPublisher.publishEvent(new DeviceStatusEvent(boxSn, currentOnline));
                    // 更新数据库
                    ArdDroneInfo local = localMap.get(boxSn);
                    if (local != null) {
                        local.setOnLine(currentOnline ? 1 : 0);
                        updateArdDrone(local);