zhaowenyu
2026-03-11 2acf49278129e5870d8b775519a2bc1461294fb8
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;
@@ -18,7 +19,7 @@
import com.ard.work.service.IArdMediaService;
import com.ard.work.util.osSelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
@@ -27,18 +28,17 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
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.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@@ -56,12 +56,10 @@
    private ArdMediaPicMapper ardMediaPicMapper;
    @Resource
    private ArdMediaVideoMapper ardMediaVideoMapper;
    @Resource
    private OssServiceContext ossServiceContext;
    @Resource
    private ArdFlightTaskMapper ardFlightTaskMapper;
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    // 调用接口获取架次图片
    private List<BoxPhotosReceiver> getBoxPhotos(String serverId, BoxPhotosDTO boxPhotosDTO) {
@@ -114,90 +112,138 @@
        }
    }
    @Override
    public void updatePhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
    public void syncPicToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
        // 获取本地数据库中已存在的图片记录(类型为 0 表示图片)
        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;
        }
                // 更新任务的视频数量
                flightTask.setPhotoCount(flightTask.getPhotoCount() + newPhotos.size());
                ardFlightTaskMapper.updateArdFlightTask(flightTask);
        // 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);
            }
        }
    }
    @Override
    public void updateVideoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
    public void syncVideoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
        // 获取本地数据库中已存在的视频记录
        // 1️⃣ 拉取远端视频列表
        BoxVideosDTO dto = BoxVideosDTO.builder()
                .taskId(flightTask.getTaskId())
                .boxSn(boxSn)
                .pageIndex(1)
                .pageSize(100)
                .build();
        TyHttpCommonReceiver<List<BoxVideosReceiver>> resp = tyClient.getBoxVideos(serverId, dto);
        List<BoxVideosReceiver> videoList = resp.getContent();
        if (videoList == null || videoList.isEmpty()) {
            return;
        }
        // 2️⃣ 本地视频转 Map(videoId 为唯一键)
        List<ArdMediaVideo> existingVideos = ardMediaVideoMapper.selectArdMediaVideoByTaskId(flightTask.getTaskId());
        int count = existingVideos.size(); //获取本地库视频数量
        // 查询图片
        BoxVideosDTO boxVideosDTO = BoxVideosDTO.builder()
                .taskId(flightTask.getTaskId())
                .boxSn(boxSn)
                .pageIndex(1)
                .pageSize(500)
                .build();
        TyHttpCommonReceiver<List<BoxVideosReceiver>> boxVideos = tyClient.getBoxVideos(serverId, boxVideosDTO);
        List<BoxVideosReceiver> videoList = boxVideos.getContent();
        int size = videoList.size();
        Map<Long, ArdMediaVideo> localVideoMap = existingVideos.stream()
                .filter(v -> v.getId() != null)
                .collect(Collectors.toMap(
                        ArdMediaVideo::getId,  // 远端 videoId
                        v -> v,
                        (a, b) -> a
                ));
        if (size > count) {
            log.info("任务【{}】视频数量不一致,需要更新数据库", flightTask.getTaskId());
            // 需要插入新的视频
            // 提取现有的图片路径(根据 imgUrl 来判断是否重复)
            List<String> existingVideoUrls = existingVideos.stream()
                    .map(ArdMediaVideo::getSourceUrl) // 提取 ArdMedia 中的视频 URL
                    .collect(Collectors.toList());
        // 3️⃣ 遍历远端视频,逐条处理
        for (BoxVideosReceiver remote : videoList) {
            // 筛选出 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());
            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);
            }
        }
    }
    private void addPhotos1(ArdFlightTask flightTask, List<BoxPhotosReceiver> photoList) {
    private void addPhotos(ArdFlightTask flightTask, List<BoxPhotosReceiver> photoList) {
        log.warn("开始添加图片");
        String boxSn = flightTask.getBoxSn();
        for (BoxPhotosReceiver photo : photoList) {
            ArdMediaPic ardMedia = ArdMediaPic.builder()
                    .id(photo.getPhotoId())
@@ -210,284 +256,25 @@
                    .height(photo.getHeight())
                    .captureTime(DateUtils.parseDate(photo.getTimeStamp()))
                    .build();
            String url = "";
            try {
                InputStream inputStream = openUrlInputStream(photo.getImgUrl(), 5000, 10000);
                if (inputStream != null) {
                    url = ossServiceContext.upload(
                            "pic",
                            boxSn + "/" + ardMedia.getId() + ".jpg",
                            inputStream,
                            MimeTypeUtils.IMAGE_JPG
                    );
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            ardMedia.setMediaUrl(url);
            ardMedia.setCreateTime(new Date());
            ardMediaPicMapper.insertPic(ardMedia);
        }
    }
    private void addPhotos(ArdFlightTask flightTask, List<BoxPhotosReceiver> photoList) {
        log.warn("开始添加图片");
        String boxSn = flightTask.getBoxSn();
        for (BoxPhotosReceiver photo : photoList) {
            CompletableFuture.runAsync(() -> {
                ArdMediaPic ardMedia = ArdMediaPic.builder()
                        .id(photo.getPhotoId())
                        .taskId(flightTask.getTaskId())
                        .fileSize(photo.getFileSize())
                        .fileMd5(photo.getFileMd5())
                        .sourceUrl(photo.getImgUrl())
                        .longitude(photo.getLng())
                        .latitude(photo.getLat())
                        .height(photo.getHeight())
                        .captureTime(DateUtils.parseDate(photo.getTimeStamp()))
                        .build();
                String url = "";
                try (InputStream inputStream = openUrlInputStream(photo.getImgUrl(), 5000, 10000)) {
                    if (inputStream != null) {
                        url = ossServiceContext.upload(
                                "pic",
                                boxSn + "/" + ardMedia.getId() + ".jpg",
                                inputStream,
                                MimeTypeUtils.IMAGE_JPG
                        );
                    }
                } catch (Exception e) {
                    log.error("图片上传失败 taskId={}, photoId={}", flightTask.getTaskId(), photo.getPhotoId(), e);
                }
                ardMedia.setMediaUrl(url);
                ardMedia.setCreateTime(new Date());
                ardMediaPicMapper.insertPic(ardMedia);
            }, threadPoolTaskExecutor); // 使用自定义线程池
        }
    }
    private void addVideos1(ArdFlightTask flightTask, List<BoxVideosReceiver> videoList) {
        log.warn("开始添加视频");
        String boxSn = flightTask.getBoxSn();
    private void addVideos(ArdFlightTask flightTask, List<BoxVideosReceiver> videoList) {
        for (BoxVideosReceiver video : videoList) {
            ArdMediaVideo ardMedia = ArdMediaVideo.builder()
                    .id(video.getId())
                    .name(video.getName())
                    .taskId(flightTask.getTaskId())
                    .fileSize(getFileSizeByUrl(video.getImgUrl()))
//                    .fileSize(getFileSizeByUrl(video.getImgUrl()))
                    .sourceUrl(video.getImgUrl())
                    .startTime(DateUtils.parseDate(video.getStartTime()))
                    .endTime(DateUtils.parseDate(video.getEndTime()))
                    .spanSeconds(Integer.parseInt(video.getSpanSeconds()))
                    .build();
            // 视频上传
            String videoUrl = null;
            try (InputStream videoStream = openUrlInputStream(ardMedia.getSourceUrl(), 5000, 10000)) {
                if (videoStream != null) {
                    videoUrl = ossServiceContext.upload("record", boxSn + "/" + ardMedia.getId() + ".mp4", videoStream, MimeTypeUtils.VIDEO_MP4);
                }
            } catch (Exception e) {
                log.error("视频上传失败 taskId={}, videoId={}", flightTask.getTaskId(), video.getId(), e);
            }
            // 缩略图上传(失败不影响视频入库)
            String thumbUrl = null;
            try (ByteArrayInputStream thumbStream = generateThumbnail(ardMedia.getSourceUrl(), 1, 200)) {
                if (thumbStream != null) {
                    thumbUrl = ossServiceContext.upload("record", boxSn + "/" + ardMedia.getId() + ".jpg", thumbStream, MimeTypeUtils.IMAGE_JPEG);
                }
            } catch (Exception e) {
                log.warn("缩略图生成或上传失败 taskId={}, videoId={}", flightTask.getTaskId(), video.getId(), e);
            }
            // 入库(即使缩略图为空也入库)
            ardMedia.setMediaUrl(videoUrl);
            ardMedia.setThumbnailUrl(thumbUrl);
            ardMedia.setCreateTime(new Date());
            ardMediaVideoMapper.insertArdMediaVideo(ardMedia);
        }
    }
    private void addVideos(ArdFlightTask flightTask, List<BoxVideosReceiver> videoList) {
        String boxSn = flightTask.getBoxSn();
        for (BoxVideosReceiver video : videoList) {
            CompletableFuture.runAsync(() -> {
                ArdMediaVideo ardMedia = ArdMediaVideo.builder()
                        .id(video.getId())
                        .name(video.getName())
                        .taskId(flightTask.getTaskId())
                        .fileSize(getFileSizeByUrl(video.getImgUrl()))
                        .sourceUrl(video.getImgUrl())
                        .startTime(DateUtils.parseDate(video.getStartTime()))
                        .endTime(DateUtils.parseDate(video.getEndTime()))
                        .spanSeconds(Integer.parseInt(video.getSpanSeconds()))
                        .build();
                // 视频上传
                String videoUrl = null;
                try (InputStream videoStream = openUrlInputStream(ardMedia.getSourceUrl(), 5000, 10000)) {
                    if (videoStream != null) {
                        videoUrl = ossServiceContext.upload("record",
                                boxSn + "/" + ardMedia.getId() + ".mp4",
                                videoStream,
                                MimeTypeUtils.VIDEO_MP4);
                    }
                } catch (Exception e) {
                    log.error("视频上传失败 taskId={}, videoId={}", flightTask.getTaskId(), video.getId(), e);
                }
                // 缩略图上传(失败不影响视频入库)
                String thumbUrl = null;
                try (ByteArrayInputStream thumbStream = generateThumbnail(ardMedia.getSourceUrl(), 1, 200)) {
                    if (thumbStream != null) {
                        thumbUrl = ossServiceContext.upload("record",
                                boxSn + "/" + ardMedia.getId() + ".jpg",
                                thumbStream,
                                MimeTypeUtils.IMAGE_JPEG);
                    }
                } catch (Exception e) {
                    log.warn("缩略图生成或上传失败 taskId={}, videoId={}", flightTask.getTaskId(), video.getId(), e);
                }
                // 入库(即使缩略图为空也入库)
                ardMedia.setMediaUrl(videoUrl);
                ardMedia.setThumbnailUrl(thumbUrl);
                ardMedia.setCreateTime(new Date());
                ardMediaVideoMapper.insertArdMediaVideo(ardMedia);
            }, threadPoolTaskExecutor);
        }
    }
    // 获取文件大小
    private long getFileSizeByUrl(String fileUrl) {
        try {
            URL url = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("HEAD");
            conn.connect();
            return conn.getContentLengthLong(); // 返回字节大小
        } catch (Exception e) {
            return -1;
        }
    }
    // 生成视频缩略图
    public ByteArrayInputStream generateThumbnail(String videoFile, int skipSeconds, int targetWidth) {
        String ffmpegPath = "";
        if (osSelect.isWindows()) {
            ffmpegPath = "ffmpeg";
        } else if (osSelect.isLinux()) {
            ffmpegPath = "/usr/local/ffmpeg/bin/ffmpeg";
        }
        try {
            List<String> command = new ArrayList<>();
            command.add(ffmpegPath);
            command.add("-timeout");
            command.add("3000000");
            command.add("-rw_timeout");
            command.add("3000000");
            command.add("-ss");
            command.add(String.format("00:00:%02d", skipSeconds));
            command.add("-i");
            command.add(videoFile);
            command.add("-vframes");
            command.add("1");
            // 等比缩放
            command.add("-vf");
            command.add("scale=" + targetWidth + ":-1");
            // **确保输出 JPEG**
            command.add("-f");
            command.add("mjpeg");      // 关键点:输出格式固定为 JPEG
            command.add("-vcodec");
            command.add("mjpeg");
            // 输出到管道
            command.add("pipe:1");
            // ⭐ 打印最终命令(可复制)
            log.info("ffmpeg command: {}", String.join(" ", command));
            ProcessBuilder pb = new ProcessBuilder(command);
            pb.redirectErrorStream(false);
            Process process = pb.start();
            // 读取 FFmpeg 输出(JPEG 数据)到内存
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try (InputStream is = process.getInputStream()) {
                byte[] buffer = new byte[8192];
                int len;
                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
            }
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("FFmpeg 执行失败,退出码: " + exitCode);
            }
            return new ByteArrayInputStream(baos.toByteArray());
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("FFmpeg 生成缩略图失败", e);
        }
    }
    /**
     * 将 URL 转成 InputStream
     *
     * @param fileUrl              文件 URL
     * @param connectTimeoutMillis 连接超时(毫秒)
     * @param readTimeoutMillis    读取超时(毫秒)
     * @return InputStream,如果失败返回 null
     */
    public InputStream openUrlInputStream(String fileUrl, int connectTimeoutMillis, int readTimeoutMillis) {
        try {
            URL url = new URL(fileUrl);
            // 支持 HTTPS 信任所有证书(如果需要访问自签名证书)
            if ("https".equalsIgnoreCase(url.getProtocol())) {
                TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
                            @Override
                            public void checkClientTrusted(X509Certificate[] chain, String authType) {
                            }
                            @Override
                            public void checkServerTrusted(X509Certificate[] chain, String authType) {
                            }
                            @Override
                            public X509Certificate[] getAcceptedIssuers() {
                                return null;
                            }
                        }
                };
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
            }
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(connectTimeoutMillis);
            connection.setReadTimeout(readTimeoutMillis);
            return connection.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}