From d306af45519b3d32cc927c93078b8a0d93815dd5 Mon Sep 17 00:00:00 2001 From: lxu75 Date: Thu, 12 Jun 2025 19:26:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83=E6=97=B6=E9=97=B4=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../center/service/SpokesManService.java | 66 +++++++------------ .../center/utils/PublishedTimeFormatter.java | 24 ++++--- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/service/SpokesManService.java b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/service/SpokesManService.java index 5c290d1..05b12db 100644 --- a/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/service/SpokesManService.java +++ b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/service/SpokesManService.java @@ -129,34 +129,8 @@ public class SpokesManService { JSONObject difSpokesManResult = (JSONObject) diFyService.getDiFyObject(diFyReq); difSpokesManResult.put("communityRequestId", spokesManDTO.getCommunityRequestId()); difSpokesManResult.put("aiAnalysisRequestId", aiAnalysisRequestId); - //处理发布时间 - // 获取原始时间字段 - String publishedTime = difSpokesManResult.getJSONObject("ContentAnalysisResult") - .getJSONObject("contentAnalysisReport") - .getJSONObject("publishedContent") - .getJSONObject("publishedContent") - .getString("publishedTime"); - - // 格式化时间 - String formattedPublishedTime = PublishedTimeFormatter.parsePublishedTime(publishedTime); - - // 如果转换成功,则更新回 JSON 对象 - if (formattedPublishedTime != null) { - difSpokesManResult.getJSONObject("ContentAnalysisResult") - .getJSONObject("contentAnalysisReport") - .getJSONObject("publishedContent") - .getJSONObject("publishedContent") - .put("publishedTime", formattedPublishedTime); - } else { - // 可选:设置为空或默认值 - difSpokesManResult.getJSONObject("ContentAnalysisResult") - .getJSONObject("contentAnalysisReport") - .getJSONObject("publishedContent") - .getJSONObject("publishedContent") - .put("publishedTime", ""); - } - + updatePublishedTime(difSpokesManResult); //发送事件网格 CloudEvents cloudEvents = new CloudEvents(); cloudEvents.setData(difSpokesManResult); @@ -165,7 +139,6 @@ public class SpokesManService { cloudEvents.setDatacontenttype("application/json"); cloudEvents.setType("SPOKESMAN"); cloudEvents.setId(aiAnalysisRequestId); - log.info("发送特邀发言官事件消息:{}",difSpokesManResult.toString()); huaWeiEGService.sendEvent(cloudEvents, channelId); //异步更新dify响应日志 syncUpdateDiFyResponse(difSpokesManResult, aiAnalysisRequestId); @@ -180,24 +153,33 @@ public class SpokesManService { } } - public static void main(String[] args) { + public static void updatePublishedTime(JSONObject rootJson) { + String contentAnalysisResultStr = rootJson.getString("ContentAnalysisResult"); + if (contentAnalysisResultStr == null || contentAnalysisResultStr.isEmpty()) return; - String str = "{\n" + - "\t\"ContentAnalysisResult\": \"{\\n \\\"contentAnalysisReport\\\": {\\n \\\"publishedContent\\\": {\\n \\\"publisherAccount\\\": \\\"\\\",\\n \\\"needSubscribe\\\": \\\"No\\\",\\n \\\"publishedContent\\\": {\\n \\\"contentType\\\": \\\"Normal\\\",\\n \\\"matchTheme\\\": \\\"Yes\\\",\\n \\\"publishedTime\\\": \\\"\\\",\\n \\\"contentOverview\\\": \\\"图片展示了一辆沃尔沃全新XC90停在一座传统建筑前,背景为山景和古建筑。\\\",\\n \\\"pictureOverview\\\": \\\"图片内容为一辆沃尔沃全新XC90停在传统建筑前,共有2张图片。\\\",\\n \\\"topicTags\\\": []\\n }\\n },\\n \\\"indicatorData\\\": {\\n \\\"contentTopic\\\": \\\"沃尔沃全新XC90\\\",\\n \\\"countCharacters\\\": 0,\\n \\\"totalNumberOfPictures\\\": 2,\\n \\\"videoDuration\\\": \\\"\\\",\\n \\\"interactionVolume\\\": {\\n \\\"read\\\": \\\"\\\",\\n \\\"like\\\": \\\"\\\",\\n \\\"collection\\\": \\\"\\\",\\n \\\"comment\\\": \\\"\\\",\\n \\\"forward\\\": \\\"\\\"\\n }\\n },\\n \\\"riskDetection\\\": {\\n \\\"sensitiveWords\\\": \\\"NOT\\\",\\n \\\"sensitiveContent\\\": \\\"\\\"\\n }\\n }\\n}\",\n" + - "\t\"auditReport\": \"{\\n \\\"auditReport\\\": {\\n \\\"auditRule\\\": \\\"1. 根据是否需要订阅判定是否为本人发布,需要订阅的为非本人发布;2. 互动量要累计点赞、评论、收藏、转发的总数。\\\",\\n \\\"auditResult\\\": \\\"PASS\\\",\\n \\\"reportsummary\\\": \\\"审核通过。内容为本人发布,无需订阅;互动量数据为空,暂无风险检测异常问题。\\\"\\n }\\n}\"\n" + - "}"; + JSONObject contentAnalysisResult = JSON.parseObject(contentAnalysisResultStr); + JSONObject contentAnalysisReport = contentAnalysisResult.getJSONObject("contentAnalysisReport"); + if (contentAnalysisReport == null) return; - String str1 = "{\n" + - "\t\"ContentAnalysisResult\": \"{\\n\\t\\\"contentAnalysisReport\\\": {\\n\\t\\t\\\"publishedContent\\\": {\\n\\t\\t\\t\\\"publisherAccount\\\": \\\"林宇 325\\\",\\n \\\"needSubscribe\\\": \\\"No\\\",\\n\\t\\t\\t\\\"publishedContent\\\": {\\n \\\"matchTheme\\\": \\\"Yes\\\", \\n\\t\\t\\t\\t\\\"publishedTime\\\": \\\"8天前\\\",\\n\\t\\t\\t\\t\\\"contentOverview\\\": \\\"心向往之 行则将至 #沃尔沃\\\",\\n\\t\\t\\t\\t\\\"pictureOverview\\\": \\\"图片内容为一辆白色汽车停在草地上,背景是多云的天空。图片总数为1张。\\\",\\n\\t\\t\\t\\t\\\"topicTags\\\": [\\\"#沃尔沃\\\"]\\n\\t\\t\\t}\\n\\t\\t},\\n\\t\\t\\\"indicatorData\\\": {\\n\\t\\t\\t\\\"contentTopic\\\": \\\"#沃尔沃\\\",\\n\\t\\t\\t\\\"countCharacters\\\": 14,\\n\\t\\t\\t\\\"totalNumberOfPictures\\\": 1,\\n\\t\\t\\t\\\"videoDuration\\\": \\\"\\\",\\n\\t\\t\\t\\\"interactionVolume\\\": {\\n\\t\\t\\t\\t\\\"like\\\": 12,\\n\\t\\t\\t\\t\\\"collection\\\": 3,\\n\\t\\t\\t\\t\\\"comment\\\": 7,\\n\\t\\t\\t\\t\\\"forward\\\": 7\\n\\t\\t\\t}\\n\\t\\t},\\n\\t\\t\\\"riskDetection\\\": {\\n\\t\\t\\t\\\"sensitiveWords\\\": \\\"NOT\\\",\\n\\t\\t\\t\\\"sensitiveContent\\\": \\\"\\\"\\n\\t\\t}\\n\\t}\\n}\",\n" + - "\t\"auditReport\": \"{\\n \\\"auditReport\\\": {\\n \\\"auditRule\\\": \\\"必须为本人发布,视频时长≥15秒,标题内容不含敏感词,发布时间需在2025年5月31日之前,互动量(点赞+评论+收藏+转发)总和≥10,带沃尔沃相关话题。\\\",\\n \\\"auditResult\\\": \\\"FAIL\\\",\\n \\\"reportsummary\\\": \\\"审核未通过。原因如下:1. 发布内容非视频,且视频时长不足15秒;2. 互动量满足要求(总和为29),发布时间符合要求(8天前),内容匹配主题并带有沃尔沃相关话题;3. 标题内容未检测到敏感词。综上,因缺少视频及视频时长不达标,判定为未通过。\\\"\\n }\\n}\"\n" + - "}"; + JSONObject publishedContentOuter = contentAnalysisReport.getJSONObject("publishedContent"); + if (publishedContentOuter == null) return; - String str2 = "{ \"ContentAnalysisResult\": \"{\\n \\\"contentAnalysisReport\\\": {\\n \\\"publishedContent\\\": {\\n \\\"publisherAccount\\\": \\\"@yoyo\\\",\\n \\\"needSubscribe\\\": \\\"Yes\\\",\\n \\\"publishedContent\\\": {\\n \\\"matchTheme\\\": \\\"Yes\\\",\\n \\\"publishedTime\\\": \\\"4月16日\\\",\\n \\\"contentOverview\\\": \\\"全新XC90耀世登场 ✨ 北欧匠心设计×六座奢适空间 以安全与豪华重新定义出行 \uD83D\uDEE1 邀您共赏旗舰风范\\\",\\n \\\"pictureOverview\\\": \\\"图片内容为沃尔沃全新XC90的发布会现场,包括主持人和活动现场的照片。图片总数为2张,当前展示的是第1张和第2张。\\\",\\n \\\"topicTags\\\": [\\\"#沃尔沃全新XC90\\\", \\\"#全新XC90进口旗舰大六座\\\"]\\n }\\n },\\n \\\"indicatorData\\\": {\\n \\\"contentTopic\\\": \\\"沃尔沃全新XC90\\\",\\n \\\"countCharacters\\\": 63,\\n \\\"totalNumberOfPictures\\\": 2,\\n \\\"videoDuration\\\": \\\"00:33\\\",\\n \\\"interactionVolume\\\": {\\n \\\"like\\\": \\\"4\\\",\\n \\\"collection\\\": \\\"\\\",\\n \\\"comment\\\": \\\"\\\",\\n \\\"forward\\\": \\\"\\\"\\n }\\n },\\n \\\"riskDetection\\\": {\\n \\\"sensitiveWords\\\": \\\"NOT\\\",\\n \\\"sensitiveContent\\\": \\\"\\\"\\n }\\n }\\n}\", \"auditReport\": \"{\\n \\\"auditReport\\\": {\\n \\\"auditRule\\\": \\\"必须为本人发布,视频时长≥15秒,标题内容不含敏感词,发布时间需在2025年5月31日之前,互动量(点赞+评论+收藏+转发)总和≥10,带沃尔沃相关话题。\\\",\\n \\\"auditResult\\\": \\\"FAIL\\\",\\n \\\"reportSummary\\\": \\\"审核未通过。原因:1. 发布账户需要订阅,判定为非本人发布;2. 互动总量不足,当前仅有4次点赞,未达到最低要求的10次互动量。\\\"\\n }\\n}\" }"; - JSONObject difSpokesManResult = JSON.parseObject(str1); - String publishedTime = difSpokesManResult.getJSONObject("ContentAnalysisResult").getJSONObject("contentAnalysisReport").getJSONObject("publishedContent").getJSONObject("publishedContent").getString("publishedTime"); - //处理publishedTime转换为年月日,年默认为当前年 - System.out.println(publishedTime); + JSONObject publishedContentInner = publishedContentOuter.getJSONObject("publishedContent"); + if (publishedContentInner == null) return; + + String publishedTime = publishedContentInner.getString("publishedTime"); + String formattedPublishedTime = PublishedTimeFormatter.parsePublishedTime(publishedTime); + + if (formattedPublishedTime != null) { + publishedContentInner.put("publishedTime", formattedPublishedTime); + publishedContentOuter.put("publishedContent", publishedContentInner); + contentAnalysisReport.put("publishedContent", publishedContentOuter); + contentAnalysisResult.put("contentAnalysisReport", contentAnalysisReport); + rootJson.put("ContentAnalysisResult", contentAnalysisResult.toJSONString()); + } } + + @Async protected void syncSaveRequestLogs(String message, String aiAnalysisRequestId) { aiAnalysisRequestLogsMapper.insert(AiAnalysisRequestLogs.builder() diff --git a/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/utils/PublishedTimeFormatter.java b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/utils/PublishedTimeFormatter.java index 2f32c26..7ad77b2 100644 --- a/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/utils/PublishedTimeFormatter.java +++ b/ai-analytic-center-biz/src/main/java/com/volvo/ai/analytic/center/utils/PublishedTimeFormatter.java @@ -11,8 +11,7 @@ import java.time.temporal.ChronoUnit; @Slf4j @Component public class PublishedTimeFormatter { - private static final DateTimeFormatter MONTH_DAY_FORMAT = DateTimeFormatter.ofPattern("M月d日"); - private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH.mm"); + private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static String parsePublishedTime(String publishedTime) { if (publishedTime == null || publishedTime.isEmpty() || publishedTime.equals("\"\"")) { @@ -22,20 +21,29 @@ public class PublishedTimeFormatter { LocalDate now = LocalDate.now(); try { - // 处理 "8天前" 格式 + // 处理 "X天前" 格式 if (publishedTime.endsWith("天前")) { int daysAgo = Integer.parseInt(publishedTime.replace("天前", "")); return now.minus(daysAgo, ChronoUnit.DAYS).format(DateTimeFormatter.ISO_DATE); } - // 处理 "4月16日" 格式 + // 处理 "X月X日" 格式 if (publishedTime.matches("\\d+月\\d+日")) { - LocalDate parsed = LocalDate.parse(publishedTime, MONTH_DAY_FORMAT); - return LocalDate.of(now.getYear(), parsed.getMonth(), parsed.getDayOfMonth()) + // 提取月份和日期 + int monthStart = 0; + int monthEnd = publishedTime.indexOf("月"); + int dayStart = monthEnd + 1; + int dayEnd = publishedTime.indexOf("日"); + + int monthValue = Integer.parseInt(publishedTime.substring(monthStart, monthEnd)); + int dayValue = Integer.parseInt(publishedTime.substring(dayStart, dayEnd)); + + // 构建当年的 LocalDate 对象 + return LocalDate.of(now.getYear(), monthValue, dayValue) .format(DateTimeFormatter.ISO_DATE); } - // 处理 "2025-05-06 23.00" 格式 + // 处理 "2025-05-06 23:00:00" 格式 if (publishedTime.contains("-")) { LocalDateTime parsed = LocalDateTime.parse(publishedTime, DATE_TIME_FORMAT); return parsed.toLocalDate().format(DateTimeFormatter.ISO_DATE); @@ -43,7 +51,7 @@ public class PublishedTimeFormatter { } catch (Exception e) { // 可记录异常或忽略错误格式 - System.err.println("无法解析时间: " + publishedTime); + log.error("无法解析时间:{} " ,publishedTime); } return null; // 未知格式返回null }