From 51c7de24747ff8b04135d2a6669aa5b9ab795530 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sat, 13 Dec 2025 21:47:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(fill):=20=E6=96=B0=E5=A2=9E=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=BC=A9=E7=95=A5=E5=9B=BE=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现DeviceThumbImageDataSourceStrategy类,支持根据deviceIndex获取设备缩略图 - 支持从过滤后的机位列表或直接通过deviceIndex两种方式查询数据 - 默认使用LATEST排序策略,可配置type类型(默认为图片类型2) - 添加对filteredDeviceIds上下文参数的支持,提升数据筛选灵活性 - 增强日志记录,便于调试和问题追踪 - 在DataSourceType枚举中新增DEVICE_THUMB_IMAGE类型定义 --- .../DeviceThumbImageDataSourceStrategy.java | 104 ++++++++++++++++++ .../puzzle/fill/enums/DataSourceType.java | 1 + 2 files changed, 105 insertions(+) create mode 100644 src/main/java/com/ycwl/basic/puzzle/fill/datasource/DeviceThumbImageDataSourceStrategy.java diff --git a/src/main/java/com/ycwl/basic/puzzle/fill/datasource/DeviceThumbImageDataSourceStrategy.java b/src/main/java/com/ycwl/basic/puzzle/fill/datasource/DeviceThumbImageDataSourceStrategy.java new file mode 100644 index 00000000..b1001cb7 --- /dev/null +++ b/src/main/java/com/ycwl/basic/puzzle/fill/datasource/DeviceThumbImageDataSourceStrategy.java @@ -0,0 +1,104 @@ +package com.ycwl.basic.puzzle.fill.datasource; + +import com.fasterxml.jackson.databind.JsonNode; +import com.ycwl.basic.mapper.SourceMapper; +import com.ycwl.basic.model.pc.source.entity.SourceEntity; +import com.ycwl.basic.puzzle.fill.enums.DataSourceType; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; + +/** + * 设备缩略图数据源策略 + * 根据deviceIndex指定第N个设备的缩略图 + */ +@Slf4j +@Component +public class DeviceThumbImageDataSourceStrategy implements DataSourceStrategy { + + @Autowired + private SourceMapper sourceMapper; + + @Override + public String resolve(JsonNode sourceFilter, String sortStrategy, DataSourceContext context) { + try { + // 默认type=2(图片) + Integer type = 2; + if (sourceFilter != null && sourceFilter.has("type")) { + type = sourceFilter.get("type").asInt(); + } + + // 获取deviceIndex + Integer deviceIndex = 0; + if (sourceFilter != null && sourceFilter.has("deviceIndex")) { + deviceIndex = sourceFilter.get("deviceIndex").asInt(); + } + + // 使用默认策略 + if (sortStrategy == null || sortStrategy.isEmpty()) { + sortStrategy = "LATEST"; + } + + // 1. 检查是否有过滤后的机位列表 + Map extra = context.getExtra(); + if (extra != null && extra.containsKey("filteredDeviceIds")) { + @SuppressWarnings("unchecked") + List filteredDeviceIds = (List) extra.get("filteredDeviceIds"); + + if (filteredDeviceIds != null && !filteredDeviceIds.isEmpty()) { + // 使用过滤后的机位列表 + if (deviceIndex >= filteredDeviceIds.size()) { + log.warn("deviceIndex[{}]超出过滤后的机位列表范围, 最大索引={}", + deviceIndex, filteredDeviceIds.size() - 1); + return null; + } + + Long targetDeviceId = filteredDeviceIds.get(deviceIndex); + log.debug("使用过滤后的机位列表, deviceIndex={}, targetDeviceId={}", + deviceIndex, targetDeviceId); + + SourceEntity source = sourceMapper.getSourceByFaceAndDeviceId( + context.getFaceId(), + targetDeviceId, + type, + sortStrategy + ); + + if (source != null) { + String thumbUrl = source.getThumbUrl(); + log.debug("解析DEVICE_THUMB_IMAGE成功(过滤模式), faceId={}, deviceId={}, type={}, thumbUrl={}", + context.getFaceId(), targetDeviceId, type, thumbUrl); + return thumbUrl; + } + return null; + } + } + + // 2. 降级到原有逻辑(使用deviceIndex直接查询) + SourceEntity source = sourceMapper.getSourceByFaceAndDeviceIndex( + context.getFaceId(), + deviceIndex, + type, + sortStrategy + ); + + if (source != null) { + String thumbUrl = source.getThumbUrl(); + log.debug("解析DEVICE_THUMB_IMAGE成功(索引模式), faceId={}, deviceIndex={}, type={}, thumbUrl={}", + context.getFaceId(), deviceIndex, type, thumbUrl); + return thumbUrl; + } + } catch (Exception e) { + log.error("解析DEVICE_THUMB_IMAGE异常, faceId={}", context.getFaceId(), e); + } + return null; + } + + @Override + public String getSupportedType() { + return DataSourceType.DEVICE_THUMB_IMAGE.getCode(); + } +} diff --git a/src/main/java/com/ycwl/basic/puzzle/fill/enums/DataSourceType.java b/src/main/java/com/ycwl/basic/puzzle/fill/enums/DataSourceType.java index 6214efe9..0983588c 100644 --- a/src/main/java/com/ycwl/basic/puzzle/fill/enums/DataSourceType.java +++ b/src/main/java/com/ycwl/basic/puzzle/fill/enums/DataSourceType.java @@ -27,6 +27,7 @@ public enum DataSourceType { * 设备图片(根据deviceIndex指定第N个设备的图片) */ DEVICE_IMAGE("DEVICE_IMAGE", "设备图片"), + DEVICE_THUMB_IMAGE("DEVICE_THUMB_IMAGE", "设备缩略图片"), /** * 静态值(直接使用fallbackValue)