From f4f4fc53260eb67483dce406a963628273786a61 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Sat, 30 May 2026 17:22:17 +0800
Subject: [PATCH] 优化

---
 ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/service/impl/DeviceChannelServiceImpl.java |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 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 9078f9d..1344e69 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
@@ -2,7 +2,10 @@
 
 import com.ard.gb28181.api.domain.Device;
 import com.ard.gb28181.api.domain.DeviceChannel;
+import com.ard.gb28181.api.domain.GbCode;
+import com.ard.gb28181.domain.GbChannel;
 import com.ard.gb28181.service.IDeviceChannelService;
+import com.ard.gb28181.service.IGbChannelService;
 import com.ard.gb28181.service.IRedisCatchStorage;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,6 +24,9 @@
 
     @Autowired
     private IRedisCatchStorage redisCatchStorage;
+
+    @Autowired
+    private IGbChannelService gbChannelService;
 
     /**
      * 根据设备id清楚设备通道
@@ -47,6 +53,55 @@
         List<DeviceChannel> deviceChannelsInRedis = redisCatchStorage.queryAllChannelsForRefresh(device.getDeviceId());
 
         redisCatchStorage.batchAdd(device.getDeviceId(), mergeWithNewChannels(deviceChannelsInRedis, channels, device.isOnLine()));
+
+        // 自动为每个通道创建 GbChannel 记录(若不存在)
+        autoCreateGbChannelForChannels(device, channels);
+    }
+
+    /**
+     * 自动为设备下的每个通道创建 GbChannel 记录
+     */
+    private void autoCreateGbChannelForChannels(Device device, List<DeviceChannel> channels) {
+        try {
+            String parentDeviceId = device.getDeviceId();
+            for (DeviceChannel channel : channels) {
+                String channelId = channel.getDeviceId();
+                if (channelId == null) {
+                    continue;
+                }
+                // 跳过行政区划(设备ID长度 <= 8)
+                if (channelId.length() <= 8) {
+                    continue;
+                }
+                // 跳过业务分组(215)和虚拟组织(216)
+                if (channelId.length() == 20) {
+                    try {
+                        GbCode gbCode = GbCode.decode(channelId);
+                        if (gbCode != null && ("215".equals(gbCode.getTypeCode()) || "216".equals(gbCode.getTypeCode()))) {
+                            continue;
+                        }
+                    } catch (Exception ignored) {
+                    }
+                }
+
+                // 检查是否已存在
+                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.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());
+                }
+            }
+        } catch (Exception e) {
+            log.error("[GbChannel自动创建] 失败 parentDeviceId={}", device.getDeviceId(), e);
+        }
     }
 
     /**

--
Gitblit v1.9.3