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; import com.ard.work.domain.dto.BoxPhotosDTO; import com.ard.work.domain.dto.BoxVideosDTO; import com.ard.work.domain.entity.ArdFlightTask; import com.ard.work.domain.entity.ArdMediaPic; import com.ard.work.domain.entity.ArdMediaVideo; import com.ard.work.domain.receiver.BoxPhotosReceiver; import com.ard.work.domain.receiver.BoxVideosReceiver; import com.ard.work.domain.receiver.TyHttpCommonReceiver; import com.ard.work.mapper.ArdFlightTaskMapper; import com.ard.work.mapper.ArdMediaPicMapper; import com.ard.work.mapper.ArdMediaVideoMapper; import com.ard.work.service.IArdMediaService; import com.ard.work.util.osSelect; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; 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.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * Description: * Author: lsy * Date: 2025年11月18日13:17 **/ @Slf4j @Service public class ArdMediaServiceImpl implements IArdMediaService { @Resource private TyClient tyClient; @Resource private ArdMediaPicMapper ardMediaPicMapper; @Resource private ArdMediaVideoMapper ardMediaVideoMapper; @Resource private ArdFlightTaskMapper ardFlightTaskMapper; // 调用接口获取架次图片 private List getBoxPhotos(String serverId, BoxPhotosDTO boxPhotosDTO) { TyHttpCommonReceiver> boxPhotos = tyClient.getBoxPhotos(serverId, boxPhotosDTO); return boxPhotos.getContent(); } // 调用接口获取架次视频 private List getBoxVideos(String serverId, BoxVideosDTO boxVideosDTO) { TyHttpCommonReceiver> boxVideos = tyClient.getBoxVideos(serverId, boxVideosDTO); return boxVideos.getContent(); } @Override public List selectArdMediaPicList(ArdMediaPic ardMediaPic) { return ardMediaPicMapper.selectPicList(ardMediaPic); } @Override public List selectArdMediaVideoList(ArdMediaVideo ardMediaVideo) { return ardMediaVideoMapper.selectArdMediaVideoList(ardMediaVideo); } @Override public void insertPhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) { log.warn("开始获取图片信息"); // 查询图片 BoxPhotosDTO boxPhotosDTO = new BoxPhotosDTO(); boxPhotosDTO.setTaskId(flightTask.getTaskId()); boxPhotosDTO.setBoxSn(boxSn); boxPhotosDTO.setPageIndex(1); boxPhotosDTO.setPageSize(100); List photoList = getBoxPhotos(serverId, boxPhotosDTO); if (photoList != null && !photoList.isEmpty()) { addPhotos(flightTask, photoList); } } @Override public void insertVideoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) { log.warn("开始获取视频信息"); BoxVideosDTO boxVideosDTO = new BoxVideosDTO(); boxVideosDTO.setTaskId(flightTask.getTaskId()); boxVideosDTO.setBoxSn(boxSn); boxVideosDTO.setPageIndex(1); boxVideosDTO.setPageSize(100); List videoList = getBoxVideos(serverId, boxVideosDTO); if (videoList != null && !videoList.isEmpty()) { addVideos(flightTask, videoList); } } @Override public void syncPicToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) { // 1️⃣ 查第三方图片列表 BoxPhotosDTO dto = BoxPhotosDTO.builder() .taskId(flightTask.getTaskId()) .boxSn(boxSn) .pageIndex(1) .pageSize(100) .build(); TyHttpCommonReceiver> resp = tyClient.getBoxPhotos(serverId, dto); List photoList = resp.getContent(); if (photoList == null || photoList.isEmpty()) { return; } // 2️⃣ 本地图片转 Map(用“业务唯一键”) List existingList = ardMediaPicMapper.selectPicByTaskId(flightTask.getTaskId()); Map 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 syncVideoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) { // 1️⃣ 拉取远端视频列表 BoxVideosDTO dto = BoxVideosDTO.builder() .taskId(flightTask.getTaskId()) .boxSn(boxSn) .pageIndex(1) .pageSize(100) .build(); TyHttpCommonReceiver> resp = tyClient.getBoxVideos(serverId, dto); List videoList = resp.getContent(); if (videoList == null || videoList.isEmpty()) { return; } // 2️⃣ 本地视频转 Map(videoId 为唯一键) List existingVideos = ardMediaVideoMapper.selectArdMediaVideoByTaskId(flightTask.getTaskId()); Map localVideoMap = existingVideos.stream() .filter(v -> v.getId() != null) .collect(Collectors.toMap( ArdMediaVideo::getId, // 远端 videoId v -> v, (a, b) -> a )); // 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); } } } private void addPhotos(ArdFlightTask flightTask, List photoList) { log.warn("开始添加图片"); for (BoxPhotosReceiver photo : photoList) { 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(); ardMedia.setCreateTime(new Date()); ardMediaPicMapper.insertPic(ardMedia); } } private void addVideos(ArdFlightTask flightTask, List videoList) { for (BoxVideosReceiver video : videoList) { 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(); ardMedia.setCreateTime(new Date()); ardMediaVideoMapper.insertArdMediaVideo(ardMedia); } } }