From 86edacc57b954565e81ee8755849023f6e79e1ec Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Tue, 30 Dec 2025 09:32:18 +0800
Subject: [PATCH] 优化

---
 ard-work/src/main/resources/mapper/ArdMediaPicMapper.xml                  |   35 ++++++-
 ard-quartz/src/main/java/com/ard/quartz/task/MediaTask.java               |   82 +++++++++++----
 ard-work/src/main/java/com/ard/work/service/impl/ArdMediaServiceImpl.java |  157 +++++++++++++++++++++----------
 3 files changed, 194 insertions(+), 80 deletions(-)

diff --git a/ard-quartz/src/main/java/com/ard/quartz/task/MediaTask.java b/ard-quartz/src/main/java/com/ard/quartz/task/MediaTask.java
index deef01b..1e5258a 100644
--- a/ard-quartz/src/main/java/com/ard/quartz/task/MediaTask.java
+++ b/ard-quartz/src/main/java/com/ard/quartz/task/MediaTask.java
@@ -113,7 +113,12 @@
     }
 
     private void processOneVideo(ArdMediaVideo video) {
-        try {
+        int maxRetries = 3;       // 最大重试次数
+        int retryDelay = 3000;    // 每次重试等待 3 秒
+        int attempt = 0;
+
+        while (attempt < maxRetries) {
+            attempt++;
             try (InputStream is = openUrlInputStream(video.getSourceUrl())) {
                 if (is != null) {
                     String mediaUrl = ossServiceContext.upload(
@@ -123,26 +128,46 @@
                             MimeTypeUtils.VIDEO_MP4
                     );
                     video.setMediaUrl(mediaUrl);
+
+                    // 生成视频缩略图
+                    processVideoThumbnail(video);
+
+                    video.setSyncStatus(2); // 成功
+                    break; // 上传成功,退出循环
+                } else {
+                    log.warn("视频尚未生成,尝试重试 {}/{}", attempt, maxRetries);
                 }
+            } catch (Exception e) {
+                log.error("视频同步异常 videoId={}, 尝试 {}/{}", video.getId(), attempt, maxRetries, e);
             }
 
-            processVideoThumbnail(video);
-            video.setSyncStatus(2);
-        } catch (Exception e) {
-            video.setSyncStatus(3);
-            log.error("视频处理失败 videoId={}", video.getId(), e);
-        } finally {
-            video.setUpdateTime(new Date());
-            try {
-                ardMediaVideoMapper.updateArdMediaVideo(video);
-            } catch (Exception e) {
-                log.error("更新视频状态失败 videoId={}", video.getId(), e);
+            if (attempt < maxRetries) {
+                try {
+                    Thread.sleep(retryDelay);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    break;
+                }
+            } else {
+                video.setSyncStatus(3); // 达到最大重试仍失败
             }
+        }
+
+        video.setUpdateTime(new Date());
+        try {
+            ardMediaVideoMapper.updateArdMediaVideo(video);
+        } catch (Exception e) {
+            log.error("更新视频状态失败 videoId={}", video.getId(), e);
         }
     }
 
     private void processOnePic(ArdMediaPic pic) {
-        try {
+        int maxRetries = 3; // 最大重试次数
+        int retryDelay = 2000; // 每次重试等待 2 秒
+        int attempt = 0;
+
+        while (attempt < maxRetries) {
+            attempt++;
             try (InputStream inputStream = openUrlInputStream(pic.getSourceUrl())) {
                 if (inputStream != null) {
                     String url = ossServiceContext.upload(
@@ -153,20 +178,31 @@
                     );
                     pic.setMediaUrl(url);
                     pic.setSyncStatus(2); // 成功
+                    break; // 上传成功,退出循环
                 } else {
-                    pic.setSyncStatus(3); // 下载失败
+                    log.warn("图片尚未生成,尝试重试 {}/{}", attempt, maxRetries);
                 }
-            }
-        } catch (Exception e) {
-            log.error("图片同步失败 taskId={}, photoId={}", pic.getTaskId(), pic.getId(), e);
-            pic.setSyncStatus(3); // 异常
-        } finally {
-            pic.setUpdateTime(new Date());
-            try {
-                ardMediaPicMapper.updatePic(pic);
             } catch (Exception e) {
-                log.error("更新图片状态失败 photoId={}", pic.getId(), e);
+                log.error("图片同步异常 taskId={}, photoId={}, 尝试 {}/{}", pic.getTaskId(), pic.getId(), attempt, maxRetries, e);
             }
+
+            if (attempt < maxRetries) {
+                try {
+                    Thread.sleep(retryDelay);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    break;
+                }
+            } else {
+                pic.setSyncStatus(3); // 达到最大重试仍失败
+            }
+        }
+
+        pic.setUpdateTime(new Date());
+        try {
+            ardMediaPicMapper.updatePic(pic);
+        } catch (Exception e) {
+            log.error("更新图片状态失败 photoId={}", pic.getId(), e);
         }
     }
 
diff --git a/ard-work/src/main/java/com/ard/work/service/impl/ArdMediaServiceImpl.java b/ard-work/src/main/java/com/ard/work/service/impl/ArdMediaServiceImpl.java
index 87a7abb..8b84da8 100644
--- a/ard-work/src/main/java/com/ard/work/service/impl/ArdMediaServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/work/service/impl/ArdMediaServiceImpl.java
@@ -1,6 +1,7 @@
 package com.ard.work.service.impl;
 
 import com.ard.common.utils.DateUtils;
+import com.ard.common.utils.StringUtils;
 import com.ard.common.utils.file.MimeTypeUtils;
 import com.ard.oss.service.impl.OssServiceContext;
 import com.ard.work.component.forest.TyClient;
@@ -34,10 +35,7 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
@@ -117,35 +115,65 @@
     @Override
     public void syncPicToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
 
-        // 获取本地数据库中已存在的图片记录
-        List<ArdMediaPic> existingPhotos = ardMediaPicMapper.selectPicByTaskId(flightTask.getTaskId());
-        int count = existingPhotos.size(); //获取本地库图片数量
-        // 查询图片
-        BoxPhotosDTO boxPhotosDTO = BoxPhotosDTO.builder()
+        // 1️⃣ 查第三方图片列表
+        BoxPhotosDTO dto = BoxPhotosDTO.builder()
                 .taskId(flightTask.getTaskId())
                 .boxSn(boxSn)
                 .pageIndex(1)
-                .pageSize(500)
+                .pageSize(100)
                 .build();
-        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(serverId, boxPhotosDTO);
-        List<BoxPhotosReceiver> photoList = boxPhotos.getContent();
-        int size = photoList.size();
 
-        if (size > count) {
-            log.info("任务【{}】图片数量不一致,需要更新数据库", flightTask.getTaskId());
-            // 需要插入新的视频
-            // 提取现有的图片路径(根据 imgUrl 来判断是否重复)
-            List<String> existingPhotoUrls = existingPhotos.stream()
-                    .map(ArdMediaPic::getSourceUrl) // 提取 ArdMediaPic 中的图片 URL
-                    .collect(Collectors.toList());
+        TyHttpCommonReceiver<List<BoxPhotosReceiver>> resp = tyClient.getBoxPhotos(serverId, dto);
 
-            // 筛选出 photoList 中没有出现在 existingPhotos 中的新图片
-            List<BoxPhotosReceiver> newPhotos = photoList.stream()
-                    .filter(photo -> !existingPhotoUrls.contains(photo.getImgUrl())) // 过滤出新图片
-                    .collect(Collectors.toList());
-            if (!newPhotos.isEmpty()) {
-                // 插入新的图片记录
-                addPhotos(flightTask, newPhotos);
+        List<BoxPhotosReceiver> photoList = resp.getContent();
+        if (photoList == null || photoList.isEmpty()) {
+            return;
+        }
+
+        // 2️⃣ 本地图片转 Map(用“业务唯一键”)
+        List<ArdMediaPic> existingList = ardMediaPicMapper.selectPicByTaskId(flightTask.getTaskId());
+
+        Map<Long, ArdMediaPic> localPicMap = existingList.stream()
+                .filter(p -> p.getId() != null)
+                .collect(Collectors.toMap(
+                        ArdMediaPic::getId,   // ✅ 远端 photoId
+                        p -> p,
+                        (a, b) -> a
+                ));
+
+
+        // 3️⃣ 逐条处理(新增 or 补全)
+        for (BoxPhotosReceiver remote : photoList) {
+
+            String imgUrl = remote.getImgUrl();
+            if (StringUtils.isBlank(imgUrl)) {
+                continue; // 第三方字段还没准备好
+            }
+            ArdMediaPic local = localPicMap.get(remote.getPhotoId());
+            if (local == null) {
+                // ✅ 新图片 → 插入
+                addPhotos(flightTask, List.of(remote));
+                continue;
+            }
+
+            // ✅ 已存在 → 补字段
+            boolean needUpdate = false;
+
+            if (local.getCaptureTime() == null && remote.getTimeStamp() != null) {
+                local.setCaptureTime(DateUtils.parseDate(remote.getTimeStamp()));
+                needUpdate = true;
+            }
+            if (local.getFileSize() == 0 && remote.getFileSize() != 0) {
+                local.setFileSize(remote.getFileSize());
+                needUpdate = true;
+            }
+            if ("0".equals(local.getFileMd5())&& !"0".equals(remote.getFileMd5())) {
+                local.setFileMd5(remote.getFileMd5());
+                needUpdate = true;
+            }
+            if (needUpdate) {
+                local.setUpdateTime(new Date());
+                ardMediaPicMapper.updatePic(local);
             }
         }
     }
@@ -153,38 +181,63 @@
     @Override
     public void syncVideoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
 
-        // 获取本地数据库中已存在的视频记录
-        List<ArdMediaVideo> existingVideos = ardMediaVideoMapper.selectArdMediaVideoByTaskId(flightTask.getTaskId());
-        int count = existingVideos.size(); //获取本地库视频数量
-        // 查询图片
-        BoxVideosDTO boxVideosDTO = BoxVideosDTO.builder()
+        // 1️⃣ 拉取远端视频列表
+        BoxVideosDTO dto = BoxVideosDTO.builder()
                 .taskId(flightTask.getTaskId())
                 .boxSn(boxSn)
                 .pageIndex(1)
-                .pageSize(500)
+                .pageSize(100)
                 .build();
-        TyHttpCommonReceiver<List<BoxVideosReceiver>> boxVideos = tyClient.getBoxVideos(serverId, boxVideosDTO);
-        List<BoxVideosReceiver> videoList = boxVideos.getContent();
-        int size = videoList.size();
+        TyHttpCommonReceiver<List<BoxVideosReceiver>> resp = tyClient.getBoxVideos(serverId, dto);
+        List<BoxVideosReceiver> videoList = resp.getContent();
+        if (videoList == null || videoList.isEmpty()) {
+            return;
+        }
 
-        if (size > count) {
-            log.info("任务【{}】视频数量不一致,需要更新数据库", flightTask.getTaskId());
-            // 需要插入新的视频
-            // 提取现有的图片路径(根据 imgUrl 来判断是否重复)
-            List<String> existingVideoUrls = existingVideos.stream()
-                    .map(ArdMediaVideo::getSourceUrl) // 提取 ArdMedia 中的视频 URL
-                    .collect(Collectors.toList());
+        // 2️⃣ 本地视频转 Map(videoId 为唯一键)
+        List<ArdMediaVideo> existingVideos = ardMediaVideoMapper.selectArdMediaVideoByTaskId(flightTask.getTaskId());
+        Map<Long, ArdMediaVideo> localVideoMap = existingVideos.stream()
+                .filter(v -> v.getId() != null)
+                .collect(Collectors.toMap(
+                        ArdMediaVideo::getId,  // 远端 videoId
+                        v -> v,
+                        (a, b) -> a
+                ));
 
-            // 筛选出 photoList 中没有出现在 existingPhotos 中的新图片
-            List<BoxVideosReceiver> newVideos = videoList.stream()
-                    .filter(photo -> !existingVideoUrls.contains(photo.getImgUrl())) // 过滤出新视频
-                    .collect(Collectors.toList());
-            if (!newVideos.isEmpty()) {
-                // 插入新的视频记录
-                addVideos(flightTask, newVideos);
-                // 更新任务的视频数量
-                flightTask.setVideoCount(flightTask.getVideoCount() + newVideos.size());
+        // 3️⃣ 遍历远端视频,逐条处理
+        for (BoxVideosReceiver remote : videoList) {
+
+            ArdMediaVideo local = localVideoMap.get(remote.getId());
+            if (local == null) {
+                // 新视频 → 立即插入
+                addVideos(flightTask, List.of(remote));
+                localVideoMap.put(remote.getId(), new ArdMediaVideo()); // 占位
+                flightTask.setVideoCount(flightTask.getVideoCount() + 1);
                 ardFlightTaskMapper.updateArdFlightTask(flightTask);
+                continue;
+            }
+
+            // 已存在 → 补字段
+            boolean needUpdate = false;
+            if (local.getName() == null && remote.getName() != null) {
+                local.setName(remote.getName());
+                needUpdate = true;
+            }
+            if (local.getStartTime() == null && remote.getStartTime() != null) {
+                local.setStartTime(DateUtils.parseDate(remote.getStartTime()));
+                needUpdate = true;
+            }
+            if (local.getEndTime() == null && remote.getEndTime() != null) {
+                local.setEndTime(DateUtils.parseDate(remote.getEndTime()));
+                needUpdate = true;
+            }
+            if (local.getSpanSeconds() == null && remote.getSpanSeconds() != null) {
+                local.setSpanSeconds(Integer.parseInt(remote.getSpanSeconds()));
+                needUpdate = true;
+            }
+            if (needUpdate) {
+                local.setUpdateTime(new Date());
+                ardMediaVideoMapper.updateArdMediaVideo(local);
             }
         }
     }
diff --git a/ard-work/src/main/resources/mapper/ArdMediaPicMapper.xml b/ard-work/src/main/resources/mapper/ArdMediaPicMapper.xml
index dc7b7f2..986ade0 100644
--- a/ard-work/src/main/resources/mapper/ArdMediaPicMapper.xml
+++ b/ard-work/src/main/resources/mapper/ArdMediaPicMapper.xml
@@ -59,15 +59,40 @@
                      #{captureTime}, #{createTime},0
                  )
     </insert>
-    <!-- 更新图片 -->
+
+    <!-- 更新图片(按需补全字段) -->
     <update id="updatePic" parameterType="ArdMediaPic">
         UPDATE ard_media_pic
-        SET
-            media_url   = #{mediaUrl},
-            sync_status = #{syncStatus},
+        <set>
+            <if test="mediaUrl != null">
+                media_url = #{mediaUrl},
+            </if>
+            <if test="fileSize != null">
+                file_size = #{fileSize},
+            </if>
+            <if test="fileMd5 != null">
+                file_md5 = #{fileMd5},
+            </if>
+            <if test="captureTime != null">
+                capture_time = #{captureTime},
+            </if>
+            <if test="longitude != null">
+                longitude = #{longitude},
+            </if>
+            <if test="latitude != null">
+                latitude = #{latitude},
+            </if>
+            <if test="height != null">
+                height = #{height},
+            </if>
+            <if test="syncStatus != null">
+                sync_status = #{syncStatus},
+            </if>
             update_time = #{updateTime}
-        WHERE id = #{id} AND media_url IS NULL
+        </set>
+        WHERE id = #{id}
     </update>
+
     <!-- 标记为处理中 -->
     <update id="markProcessing">
         UPDATE ard_media_pic SET sync_status = 1 WHERE id = #{id} AND sync_status = 0

--
Gitblit v1.9.3