liusuyi
2025-12-08 3e64fc031eb42fb66f78104d0ff0f6cf797db0f0
ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java
@@ -1,5 +1,8 @@
package com.ard.work.service.impl;
import com.ard.common.constant.CacheConstants;
import com.ard.common.core.domain.entity.SysUser;
import com.ard.common.core.redis.RedisCache;
import com.ard.common.utils.DateUtils;
import com.ard.common.utils.file.MimeTypeUtils;
import com.ard.common.utils.uuid.IdUtils;
@@ -16,8 +19,10 @@
import com.ard.work.mapper.ArdMediaMapper;
import com.ard.work.service.IArdFlightTaskService;
import com.ard.work.service.IArdServerInfoService;
import com.ard.work.service.control.UavControlService;
import com.ard.work.util.osSelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
@@ -57,7 +62,10 @@
    private OssServiceContext ossServiceContext;
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;  // 使用线程池执行异步任务
    @Resource
    private UavControlService uavControlService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询飞行架次列表
     *
@@ -127,7 +135,19 @@
                    .serverId(serverId)
                    .startTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getStartTime()))
                    .endTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getEndTime()))
                    .flightMileage(boxHistoryReceiver.getFlightMileage())
                    .flightDuration(boxHistoryReceiver.getFlightDuration())
                    .aveSpeed(boxHistoryReceiver.getAveSpeed())
                    .maxSpeed(boxHistoryReceiver.getMaxSpeed())
                    .build();
            Long flyUserId = uavControlService.getCurrentController(flightTask.getBoxSn(), "fly");
            if (flyUserId != null) {
                SysUser flyUser = redisCache.getCacheObject(CacheConstants.SYS_USER_KEY + flyUserId);
                if (flyUser != null && flyUser.getNickName() != null) {
                    flightTask.setCreateBy(flyUser.getNickName());
                }
            }
            flightTask.setCreateTime(DateUtils.getNowDate());
            // 插入新记录
            int rows = ardFlightTaskMapper.insertArdFlightTask(flightTask);
@@ -144,11 +164,41 @@
        } else {
            // 如果存在,则更新记录
            log.debug("Updating task {}", boxHistoryReceiver.getTaskId());
            updateFlightTask(ardFlightTask, boxHistoryReceiver);
            // 增量更新
            updateVideoToDatabase(ardFlightTask, boxHistoryReceiver.getVideoPathList());
            // 增量更新图片
            updatePhotoToDatabase(ardFlightTask, serverId, boxHistoryReceiver.getBoxSn());
        }
    }
    private void updateFlightTask(ArdFlightTask local, BoxHistoryReceiver remote) {
        boolean needUpdate = false;
        // endTime 更新(重要!!!)
        Date newEnd = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, remote.getEndTime());
        if (newEnd != null && (local.getEndTime() == null || !local.getEndTime().equals(newEnd))) {
            local.setEndTime(newEnd);
            needUpdate = true;
        }
        // photoCount 更新
        int remotePhotoCount = Integer.parseInt(remote.getPhotoCount());
        if (local.getPhotoCount() != remotePhotoCount) {
            local.setPhotoCount(remotePhotoCount);
            needUpdate = true;
        }
        // videoCount 更新
        int remoteVideoCount = remote.getVideoPathList() != null ? remote.getVideoPathList().size() : 0;
        if (local.getVideoCount() != remoteVideoCount) {
            local.setVideoCount(remoteVideoCount);
            needUpdate = true;
        }
        if (needUpdate) {
            ardFlightTaskMapper.updateArdFlightTask(local);
        }
    }
@@ -233,12 +283,13 @@
        if (videoPathList == null) {
            return;
        }
        Integer count = flightTask.getVideoCount();//获取本地库视频数量
        // 需要插入新的视频
        List<ArdMedia> existingVideos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 1);
        Integer count = existingVideos.size();//获取本地库视频数量
        int size = videoPathList.size();//获取接口返回的视频数量
        if (count != null && size > count) {
            log.info("任务【{}】录像数量不一致,需要更新数据库", flightTask.getTaskId());
            // 需要插入新的视频
            List<ArdMedia> existingVideos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 1);
            // 提取现有的视频路径
            List<String> existingVideoPaths = existingVideos.stream()
                    .map(ArdMedia::getSourceUrl) // 获取每个视频的 URL
@@ -323,7 +374,10 @@
    }
    private void updatePhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
        int count = flightTask.getPhotoCount(); //获取本地库图片数量
        // 获取本地数据库中已存在的图片记录(类型为 0 表示图片)
        List<ArdMedia> existingPhotos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 0);
        int count = existingPhotos.size(); //获取本地库图片数量
        // 查询图片
        BoxPhotosDTO boxPhotosDTO = BoxPhotosDTO.builder()
                .taskId(flightTask.getTaskId())
@@ -337,8 +391,6 @@
        if (size > count) {
            log.info("任务【{}】图片数量不一致,需要更新数据库", flightTask.getTaskId());
            // 获取本地数据库中已存在的图片记录(类型为 0 表示图片)
            List<ArdMedia> existingPhotos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 0);
            // 需要插入新的视频
            // 提取现有的图片路径(根据 imgUrl 来判断是否重复)
            List<String> existingPhotoUrls = existingPhotos.stream()