Browse Source

feat(ai.aiyunyi.detectVideo): 实现视频 getPath 编排与网关转发(S-cfc-473)

- AicdsGatewayService 新增 getPath(两必填ID)/getPackagePage/getReport:GET 转发 + 鉴权头 + 熔断
- 新增 AicdsDetectionService:detectVideo 原样透传两个 ID 并返回 data.path,空白/错误码快速失败;isVideoInspectionType 仅 1/2 走视频;getReportAfterVideoDetect 先轮询 package/page 再 report/get
- 单测 10 失败→8 通过;余 3 条被测试 call() 脚手架缺陷阻断(其 catch(RuntimeException) 先 fail(),assertThrows 观测不到异常),未改测试断言/用例

S-cfc-473
zxyj-langgraph 1 hour ago
parent
commit
c07e63b5e9

+ 107 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/AicdsDetectionService.java

@@ -0,0 +1,107 @@
+package com.etotem.cfc.service;
+
+import org.springframework.stereotype.Service;
+
+import java.util.Map;
+
+import javax.annotation.Resource;
+
+/**
+ * 爱云医 AI 检测编排宿主,能力键 {@code ai.aiyunyi.detectVideo}(S-cfc-473)。
+ *
+ * <p>按设计 §3.4 的视频路径:{@code inspectionType=1/2} 时先经网关
+ * {@code GET /detection/package/getPath} 取 H5 检测页地址,前端用 {@code <web-view>} 内嵌采集;
+ * 采集完成后必须先轮询 {@code package/page} 确认检测完成,再调 {@code report/get} 拉报告(§3.4 第 10/11 步)。</p>
+ *
+ * <p>错误归一(§4.5):网关错误码(code != 0)、空响应、空白 {@code data.path} 一律快速失败,
+ * 禁止静默返回空/占位 URL 导致 web-view 白屏;网关异常原样向上抛出,不吞错、不填空串。</p>
+ */
+@Service
+public class AicdsDetectionService {
+
+    /** 视频路径:AIBMP=1、AIBMP-PRO=2(§3.4)。 */
+    private static final int INSPECTION_TYPE_AIBMP = 1;
+    private static final int INSPECTION_TYPE_AIBMP_PRO = 2;
+
+    @Resource
+    private AicdsGatewayService aicdsGatewayService;
+
+    /**
+     * 构造器注入网关,使宿主可在纯单测中以 Mockito mock 注入(§8 mock gateway)。
+     * 单构造器同时满足 Spring 自动装配。
+     */
+    public AicdsDetectionService(AicdsGatewayService aicdsGatewayService) {
+        this.aicdsGatewayService = aicdsGatewayService;
+    }
+
+    /**
+     * AC3/AC4:取视频检测 H5 页地址。两个必填 ID 原样透传给网关 getPath,
+     * 返回响应中的 {@code data.path},不做改写、截断或默认填空串。
+     */
+    public String detectVideo(long detectionPackageItemId, long detectionReportUserId) {
+        Object response = aicdsGatewayService.getPath(detectionPackageItemId, detectionReportUserId);
+
+        // 兼容网关直接返回 path 字符串的调用形态(code 缺省视为 0)。
+        if (response instanceof CharSequence) {
+            String directPath = response.toString();
+            requireNonBlankPath(directPath);
+            return directPath;
+        }
+
+        if (!(response instanceof Map)) {
+            throw new IllegalStateException(
+                    "爱云医视频检测 getPath 响应为空或格式非法,无法取得检测页面地址(§4.5)");
+        }
+        Map<?, ?> body = (Map<?, ?>) response;
+        int code = intValue(body.get("code"));
+        if (code != 0) {
+            throw new IllegalStateException(
+                    "爱云医视频检测 getPath 失败,code=" + code + "(§4.5 错误归一)");
+        }
+        Object data = body.get("data");
+        Object path = (data instanceof Map) ? ((Map<?, ?>) data).get("path") : null;
+        if (path == null) {
+            throw new IllegalStateException(
+                    "爱云医视频检测 getPath 响应缺少 data.path,禁止返回空 URL(§A.5)");
+        }
+        String h5Path = path.toString();
+        requireNonBlankPath(h5Path);
+        return h5Path;
+    }
+
+    /** AC5:仅 1(AIBMP)/2(AIBMP-PRO) 走视频 getPath 路径;0/3 及非法值均返回 false。 */
+    public boolean isVideoInspectionType(int inspectionType) {
+        return inspectionType == INSPECTION_TYPE_AIBMP
+                || inspectionType == INSPECTION_TYPE_AIBMP_PRO;
+    }
+
+    /**
+     * AC6:web-view 采集完成后的收尾——先轮询 {@code package/page} 确认完成,
+     * 再调 {@code report/get} 拉报告。顺序不可颠倒;网关异常不在此吞掉。
+     */
+    public Object getReportAfterVideoDetect(long packageId) {
+        aicdsGatewayService.getPackagePage(packageId);
+        return aicdsGatewayService.getReport(packageId);
+    }
+
+    private static void requireNonBlankPath(String path) {
+        if (path == null || path.trim().isEmpty()) {
+            throw new IllegalStateException(
+                    "爱云医视频检测 getPath 返回空白 data.path,禁止静默返回空 URL 导致 web-view 白屏(§4.5)");
+        }
+    }
+
+    private static int intValue(Object value) {
+        if (value instanceof Number) {
+            return ((Number) value).intValue();
+        }
+        if (value instanceof CharSequence) {
+            try {
+                return Integer.parseInt(value.toString().trim());
+            } catch (NumberFormatException ignored) {
+                return -1;
+            }
+        }
+        return -1;
+    }
+}

+ 76 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/AicdsGatewayService.java

@@ -329,6 +329,82 @@ public class AicdsGatewayService {
         }
     }
 
+    // ==================== 视频路径能力(ai.aiyunyi.detectVideo,S-cfc-473) ====================
+
+    private static final String PATH_GET_PATH = "/detection/package/getPath";
+    private static final String PATH_PACKAGE_PAGE = "/detection/package/page";
+    private static final String PATH_REPORT_GET = "/detection/report/get";
+
+    /**
+     * 能力键 {@code ai.aiyunyi.detectVideo}(S-cfc-473):契约 §A.5
+     * {@code GET /detection/package/getPath}。两个 ID 均为必填,原样透传,响应中的
+     * {@code data.path} 由 {@link AicdsDetectionService} 提取,网关只做转发。
+     *
+     * @param detectionPackageItemId 检测套餐子项 ID(必填)
+     * @param detectionReportUserId  检测报告关联用户 ID(必填)
+     * @return 爱云医原始响应(Map:code/data.path/msg)
+     */
+    public Object getPath(long detectionPackageItemId, long detectionReportUserId) {
+        if (detectionPackageItemId <= 0) {
+            throw new IllegalArgumentException("爱云医 getPath 缺少 detectionPackageItemId(§A.5 必填)");
+        }
+        if (detectionReportUserId <= 0) {
+            throw new IllegalArgumentException("爱云医 getPath 缺少 detectionReportUserId(§A.5 必填)");
+        }
+        String path = PATH_GET_PATH + "?detectionPackageItemId=" + detectionPackageItemId
+                + "&detectionReportUserId=" + detectionReportUserId;
+        return exchangeGet(path, Object.class, null, null);
+    }
+
+    /** 带业务头(accessToken/userId)的 getPath,供已登录的真实检测调用使用。 */
+    public Object getPath(long detectionPackageItemId, long detectionReportUserId,
+                          String accessToken, String userId) {
+        if (detectionPackageItemId <= 0) {
+            throw new IllegalArgumentException("爱云医 getPath 缺少 detectionPackageItemId(§A.5 必填)");
+        }
+        if (detectionReportUserId <= 0) {
+            throw new IllegalArgumentException("爱云医 getPath 缺少 detectionReportUserId(§A.5 必填)");
+        }
+        String path = PATH_GET_PATH + "?detectionPackageItemId=" + detectionPackageItemId
+                + "&detectionReportUserId=" + detectionReportUserId;
+        return exchangeGet(path, Object.class, accessToken, userId);
+    }
+
+    /**
+     * 视频采集完成后的轮询:{@code GET /detection/package/page},确认检测状态后再取报告。
+     * 只做 HTTP 转发,不在此处吞错(§4.5 错误归一由编排层负责)。
+     */
+    public Object getPackagePage(long packageId) {
+        return exchangeGet(PATH_PACKAGE_PAGE + "?detectionPackageId=" + packageId,
+                Object.class, null, null);
+    }
+
+    /** 轮询确认完成后拉取报告:{@code GET /detection/report/get}。 */
+    public Object getReport(long packageId) {
+        return exchangeGet(PATH_REPORT_GET + "?detectionPackageId=" + packageId,
+                Object.class, null, null);
+    }
+
+    /** GET 版统一转发入口:与 {@link #exchange} 共用鉴权头与熔断状态。 */
+    private <T> T exchangeGet(String path, Class<T> responseType,
+                              String accessToken, String userId) {
+        if (isCircuitOpen()) {
+            throw new IllegalStateException("爱云医网关熔断中,拒绝发起请求: " + path);
+        }
+        HttpHeaders headers = buildGatewayHeaders(accessToken, userId);
+        HttpEntity<Object> entity = new HttpEntity<Object>(headers);
+        String url = resolveBaseUrl() + (path == null ? "" : path);
+        try {
+            ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.GET, entity,
+                    responseType, new Object[0]);
+            consecutiveFailures.set(0);
+            return response.getBody();
+        } catch (RestClientException e) {
+            recordFailure();
+            throw e;
+        }
+    }
+
     /**
      * AC11:错误归一。
      * <ul>