|
|
@@ -0,0 +1,222 @@
|
|
|
+package com.tzld.videoVector.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tzld.videoVector.model.entity.DeconstructResult;
|
|
|
+import com.tzld.videoVector.service.DeconstructService;
|
|
|
+import com.tzld.videoVector.util.http.HttpClientUtils;
|
|
|
+import com.tzld.videoVector.util.http.HttpResponseContent;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 内容解构服务实现类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class DeconstructServiceImpl implements DeconstructService {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 内容解构 API Host
|
|
|
+ */
|
|
|
+ @Value("${deconstruct.api.host:http://supply-content-deconstruction-api.piaoquantv.com}")
|
|
|
+ private String apiHost;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建任务接口路径
|
|
|
+ */
|
|
|
+ private static final String CREATE_TASK_PATH = "/api/v1/content/tasks/decode";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询结果接口路径模板
|
|
|
+ */
|
|
|
+ private static final String QUERY_RESULT_PATH_TEMPLATE = "/api/v1/content/tasks/%s";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String deconstruct(int scene, int contentType, String channelContentId,
|
|
|
+ String videoUrl, List<String> images,
|
|
|
+ String bodyText, String title,
|
|
|
+ String channelAccountId, String channelAccountName) {
|
|
|
+ // 构建 content 对象
|
|
|
+ JSONObject content = new JSONObject();
|
|
|
+ content.put("channel_content_id", channelContentId);
|
|
|
+ content.put("video_url", videoUrl != null ? videoUrl : "");
|
|
|
+ content.put("images", images != null ? images : new java.util.ArrayList<>());
|
|
|
+ content.put("body_text", bodyText != null ? bodyText : "");
|
|
|
+ content.put("title", title != null ? title : "");
|
|
|
+ content.put("channel_account_id", channelAccountId != null ? channelAccountId : "");
|
|
|
+ content.put("channel_account_name", channelAccountName != null ? channelAccountName : "");
|
|
|
+
|
|
|
+ // 构建请求体
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.put("scene", scene);
|
|
|
+ requestBody.put("content_type", contentType);
|
|
|
+ requestBody.put("content", content);
|
|
|
+
|
|
|
+ // 构建完整 URL
|
|
|
+ String url = apiHost + CREATE_TASK_PATH;
|
|
|
+ log.info("调用内容解构 API,url: {}, 请求参数: {}", url, requestBody.toJSONString());
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送 POST 请求
|
|
|
+ HttpResponseContent response = HttpClientUtils.postRequestBody(
|
|
|
+ url,
|
|
|
+ requestBody,
|
|
|
+ null
|
|
|
+ );
|
|
|
+
|
|
|
+ if (response == null) {
|
|
|
+ log.error("调用内容解构 API 失败,响应为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析响应
|
|
|
+ String responseBody = response.getBodyContent();
|
|
|
+ log.info("内容解构 API 响应: {}", responseBody);
|
|
|
+
|
|
|
+ JSONObject jsonResponse = JSONObject.parseObject(responseBody);
|
|
|
+
|
|
|
+ // 检查响应码
|
|
|
+ Integer code = jsonResponse.getInteger("code");
|
|
|
+ if (code == null || code != 0) {
|
|
|
+ String msg = jsonResponse.getString("msg");
|
|
|
+ log.error("内容解构 API 调用失败,code: {}, msg: {}", code, msg);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取 taskId
|
|
|
+ JSONObject data = jsonResponse.getJSONObject("data");
|
|
|
+ if (data == null) {
|
|
|
+ log.error("内容解构 API 响应中 data 为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String taskId = data.getString("taskId");
|
|
|
+ log.info("内容解构任务创建成功,taskId: {}", taskId);
|
|
|
+ return taskId;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用内容解构 API 异常: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String deconstructVideo(String videoId, String videoUrl, String title) {
|
|
|
+ return deconstruct(
|
|
|
+ 0, // scene: 0选题
|
|
|
+ 3, // content_type: 3视频
|
|
|
+ videoId, // channel_content_id
|
|
|
+ videoUrl, // video_url
|
|
|
+ null, // images
|
|
|
+ null, // body_text
|
|
|
+ title, // title
|
|
|
+ null, // channel_account_id
|
|
|
+ null // channel_account_name
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String deconstructArticle(String contentId, String title, String bodyText, List<String> images) {
|
|
|
+ return deconstruct(
|
|
|
+ 0, // scene: 0选题
|
|
|
+ 2, // content_type: 2图文
|
|
|
+ contentId, // channel_content_id
|
|
|
+ null, // video_url
|
|
|
+ images, // images
|
|
|
+ bodyText, // body_text
|
|
|
+ title, // title
|
|
|
+ null, // channel_account_id
|
|
|
+ null // channel_account_name
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DeconstructResult getDeconstructResult(String taskId) {
|
|
|
+ if (taskId == null || taskId.trim().isEmpty()) {
|
|
|
+ log.error("taskId 不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建完整 URL
|
|
|
+ String url = apiHost + String.format(QUERY_RESULT_PATH_TEMPLATE, taskId);
|
|
|
+ log.info("查询解构结果,taskId: {}, url: {}", taskId, url);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送 GET 请求
|
|
|
+ HttpResponseContent response = HttpClientUtils.get(url);
|
|
|
+
|
|
|
+ if (response == null) {
|
|
|
+ log.error("查询解构结果失败,响应为空,taskId: {}", taskId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析响应
|
|
|
+ String responseBody = response.getBodyContent();
|
|
|
+ log.info("查询解构结果响应: {}", responseBody);
|
|
|
+
|
|
|
+ JSONObject jsonResponse = JSONObject.parseObject(responseBody);
|
|
|
+
|
|
|
+ DeconstructResult result = new DeconstructResult();
|
|
|
+ result.setTaskId(taskId);
|
|
|
+
|
|
|
+ // 检查响应码
|
|
|
+ Integer code = jsonResponse.getInteger("code");
|
|
|
+ if (code == null) {
|
|
|
+ log.error("查询解构结果响应中 code 为空,taskId: {}", taskId);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 404 表示任务不存在
|
|
|
+ if (code == 404) {
|
|
|
+ log.warn("任务不存在,taskId: {}", taskId);
|
|
|
+ result.setStatus(3); // FAILED
|
|
|
+ result.setSuccess(false);
|
|
|
+ result.setReason("任务不存在");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 其他非成功状态
|
|
|
+ if (code != 0) {
|
|
|
+ String msg = jsonResponse.getString("msg");
|
|
|
+ log.error("查询解构结果失败,code: {}, msg: {}, taskId: {}", code, msg, taskId);
|
|
|
+ result.setSuccess(false);
|
|
|
+ result.setReason(msg);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析 data
|
|
|
+ JSONObject data = jsonResponse.getJSONObject("data");
|
|
|
+ if (data == null) {
|
|
|
+ log.error("查询解构结果响应中 data 为空,taskId: {}", taskId);
|
|
|
+ result.setSuccess(false);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 填充结果
|
|
|
+ result.setTaskId(data.getString("taskId"));
|
|
|
+ result.setStatus(data.getInteger("status"));
|
|
|
+ result.setResult(data.getString("result"));
|
|
|
+ result.setReason(data.getString("reason"));
|
|
|
+ result.setSuccess(result.getStatus() != null && result.getStatus() == 2);
|
|
|
+
|
|
|
+ // 解析 url 对象
|
|
|
+ JSONObject urlObj = data.getJSONObject("url");
|
|
|
+ if (urlObj != null) {
|
|
|
+ result.setPointUrl(urlObj.getString("pointUrl"));
|
|
|
+ result.setWeightUrl(urlObj.getString("weightUrl"));
|
|
|
+ result.setPatternUrl(urlObj.getString("patternUrl"));
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("查询解构结果成功,taskId: {}, status: {}, success: {}",
|
|
|
+ result.getTaskId(), result.getStatusDesc(), result.isSuccess());
|
|
|
+ return result;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查询解构结果异常,taskId: {}, error: {}", taskId, e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|