From a2e7e8ff9cfaa69b001d483710bddbda50d55c91 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Mon, 01 Jun 2026 16:14:19 +0800
Subject: [PATCH] 优化

---
 ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/service/impl/DeviceChannelServiceImpl.java |   31 +++++++++++++++++++++++++++----
 1 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/service/impl/DeviceChannelServiceImpl.java b/ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/service/impl/DeviceChannelServiceImpl.java
index 1344e69..1c84c42 100644
--- a/ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/service/impl/DeviceChannelServiceImpl.java
+++ b/ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/service/impl/DeviceChannelServiceImpl.java
@@ -11,6 +11,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -34,7 +35,7 @@
      * @param deviceId
      */
     @Override
-    public void cleanChannelsForDevice(int deviceId) {
+    public void cleanChannelsForDevice(String deviceId) {
         redisCatchStorage.cleanChannelsForDevice(deviceId);
     }
 
@@ -62,8 +63,9 @@
      * 自动为设备下的每个通道创建 GbChannel 记录
      */
     private void autoCreateGbChannelForChannels(Device device, List<DeviceChannel> channels) {
+        String parentDeviceId = device.getDeviceId();
         try {
-            String parentDeviceId = device.getDeviceId();
+            boolean hasRealSubChannel = false;
             for (DeviceChannel channel : channels) {
                 String channelId = channel.getDeviceId();
                 if (channelId == null) {
@@ -84,23 +86,44 @@
                     }
                 }
 
+                // 判断是否有真实子通道(不是设备自身的默认通道)
+                if (!channelId.equals(parentDeviceId)) {
+                    hasRealSubChannel = true;
+                }
+
                 // 检查是否已存在
                 GbChannel existing = gbChannelService.selectByGbDeviceIdAndGbChannelId(parentDeviceId, channelId);
                 if (existing == null) {
                     GbChannel gbChannel = new GbChannel();
                     gbChannel.setGbDeviceId(parentDeviceId);
                     gbChannel.setGbChannelId(channelId);
-                    gbChannel.setChannelName(channel.getName() != null ? channel.getName() : channelId);
+                    gbChannel.setChannelName(StringUtils.hasText(channel.getName()) ? channel.getName() : channelId);
                     gbChannel.setDeviceCode(channelId);
                     gbChannel.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                     gbChannel.setEnableMp4("0");
                     gbChannelService.insertGbChannel(gbChannel);
                     log.info("[GbChannel自动创建] parentDeviceId={}, channelId={}, id={}",
                             parentDeviceId, channelId, gbChannel.getId());
+                } else if (StringUtils.hasText(channel.getName())
+                        && !channel.getName().equals(existing.getChannelName())) {
+                    // 已存在但名称有变化,从目录同步更新名称
+                    existing.setChannelName(channel.getName());
+                    gbChannelService.updateGbChannel(existing);
+                    log.info("[GbChannel更新名称] parentDeviceId={}, channelId={}, {} -> {}",
+                            parentDeviceId, channelId, existing.getChannelName(), channel.getName());
+                }
+            }
+
+            // 如果有真实子通道,删除注册时创建的默认通道(gb_channel_id = gb_device_id)
+            if (hasRealSubChannel) {
+                GbChannel defaultChannel = gbChannelService.selectByGbDeviceIdAndGbChannelId(parentDeviceId, parentDeviceId);
+                if (defaultChannel != null) {
+                    gbChannelService.deleteGbChannelById(defaultChannel.getId());
+                    log.info("[GbChannel清理默认通道] parentDeviceId={}, 有真实子通道,删除默认通道 id={}", parentDeviceId, defaultChannel.getId());
                 }
             }
         } catch (Exception e) {
-            log.error("[GbChannel自动创建] 失败 parentDeviceId={}", device.getDeviceId(), e);
+            log.error("[GbChannel自动创建] 失败 parentDeviceId={}", parentDeviceId, e);
         }
     }
 

--
Gitblit v1.9.3