liusuyi
2026-04-01 99647c423596c86a314cb43edb5a6fb70817cca6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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.work.component.forest.TyClient;
import com.ard.work.domain.dto.BoxHistoryDTO;
import com.ard.work.domain.entity.ArdFlightTask;
import com.ard.work.domain.receiver.BoxHistoryReceiver;
import com.ard.work.domain.receiver.TyHttpCommonReceiver;
import com.ard.work.mapper.ArdFlightTaskMapper;
import com.ard.work.service.IArdFlightTaskService;
import com.ard.work.service.IArdMediaService;
import com.ard.work.service.IArdServerInfoService;
import com.ard.work.service.control.UavControlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
@Slf4j
@Service
public class ArdFlightTaskServiceImpl implements IArdFlightTaskService {
 
    @Resource
    private ArdFlightTaskMapper ardFlightTaskMapper;
    @Resource
    private IArdServerInfoService ardServerInfoService;
    @Resource
    private UavControlService uavControlService;
    @Resource
    private IArdMediaService ardMediaService;
    @Resource
    private RedisCache redisCache;
    @Resource
    private TyClient tyClient;
 
    /**
     * 查询飞行架次列表
     *
     * @param ardFlightTask 飞行架次
     * @return 飞行架次集合
     */
    @Override
    public List<ArdFlightTask> selectArdFlightTask(ArdFlightTask ardFlightTask) {
        return ardFlightTaskMapper.selectArdFlightTask(ardFlightTask);
    }
 
    /**
     * 同步所有服务的飞行历史
     */
    @Scheduled(fixedDelay = 5000)
    public void syncAllFlightHistory() {
        try {
            List<String> serverIds = ardServerInfoService.getServerIdsList();
            for (String serverId : serverIds) {
                // 获取上次同步的时间(即数据库中最大 startTime)
                Date lastSyncTime = getLastSyncTimeForServer(serverId);
                // 根据上次同步时间同步飞行历史
                syncFlightHistory(serverId, lastSyncTime);
            }
        } catch (Exception e) {
            log.error("同步飞行历史失败", e);
        }
    }
 
    // 获取上次同步时间(获取最大 startTime)
    private Date getLastSyncTimeForServer(String serverId) {
        Date maxStartTime = ardFlightTaskMapper.findMaxStartTimeByServerId(serverId);
        return maxStartTime != null ? maxStartTime : new Date(0);  // 如果没有数据,返回 Unix 纪元时间作为起点
    }
 
    // 调用接口获取飞行历史数据
    private List<BoxHistoryReceiver> getBoxHistory(String serverId, BoxHistoryDTO boxHistoryDTO) {
        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(serverId, boxHistoryDTO);
        return boxHistory.getContent();
    }
 
    // 根据上次同步时间同步飞行历史
    public void syncFlightHistory(String serverId, Date startTime) {
        // 创建查询条件 BoxHistoryDTO,并设置 startTime 作为过滤条件
        BoxHistoryDTO boxHistoryDTO = BoxHistoryDTO.builder()
                .pageIndex(1)
                .pageSize(20)
                .startTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, startTime))
                .build();
 
        // 获取飞行历史
        List<BoxHistoryReceiver> historyList = getBoxHistory(serverId, boxHistoryDTO);
 
        // 如果获取到数据,处理并同步到本地数据库
        if (historyList != null && !historyList.isEmpty()) {
            for (BoxHistoryReceiver boxHistory : historyList) {
                syncToLocalDatabase(serverId, boxHistory);
            }
        }
    }
 
    // 将数据同步到本地数据库
    private void syncToLocalDatabase(String serverId, BoxHistoryReceiver boxHistoryReceiver) {
        // 检查是否已经存在相同任务ID的记录
        ArdFlightTask ardFlightTask = ardFlightTaskMapper.getArdFlightTaskById(boxHistoryReceiver.getTaskId());
        if (ardFlightTask == null) {
            // 如果不存在,则插入新记录
            log.info("insert task {}", boxHistoryReceiver.getTaskId());
            Date endTime = null;
            if (boxHistoryReceiver.getEndTime() != null) {
                endTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getEndTime());
            }
            // 创建FlightTask实体对象并填充数据
            ArdFlightTask flightTask = ArdFlightTask.builder()
                    .taskId(boxHistoryReceiver.getTaskId())
                    .boxSn(boxHistoryReceiver.getBoxSn())
                    .boxName(boxHistoryReceiver.getBoxName())
                    .taskName(boxHistoryReceiver.getTaskName())
                    .photoCount(Integer.parseInt(boxHistoryReceiver.getPhotoCount()))
                    .videoCount((boxHistoryReceiver.getVideoPathList() != null) ?
                            boxHistoryReceiver.getVideoPathList().size() : 0)
                    .videoPath(boxHistoryReceiver.getVideoPath())
                    .serverId(serverId)
                    .startTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getStartTime()))
                    .endTime(endTime)
                    .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.getCacheMapValue(CacheConstants.SYS_USER, flyUserId.toString());
                if (flyUser != null && flyUser.getNickName() != null) {
                    flightTask.setCreateBy(flyUser.getNickName());
                }
            }
            flightTask.setCreateTime(DateUtils.getNowDate());
            // 插入新记录
            int rows = ardFlightTaskMapper.insertArdFlightTask(flightTask);
            if (rows > 0) {
                // 插入视频
                ardMediaService.insertVideoToDatabase(flightTask, serverId, boxHistoryReceiver.getBoxSn());
                // 插入图片
                ardMediaService.insertPhotoToDatabase(flightTask, serverId, boxHistoryReceiver.getBoxSn());
            }
        } else {
            // 如果存在,则更新记录
            log.debug("sync task {}", boxHistoryReceiver.getTaskId());
            updateFlightTask(ardFlightTask, boxHistoryReceiver);
            // 同步任务图片
            ardMediaService.syncPicToDatabase(ardFlightTask, serverId, boxHistoryReceiver.getBoxSn());
            // 同步任务视频
            ardMediaService.syncVideoToDatabase(ardFlightTask, serverId, boxHistoryReceiver.getBoxSn());
        }
    }
 
    private void updateFlightTask(ArdFlightTask local, BoxHistoryReceiver remote) {
 
        boolean needUpdate = false;
 
        // endTime 更新(重要!!!)
        if (remote.getEndTime() != null) {
            Date newEnd = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, remote.getEndTime());
            if (local.getEndTime() == null || !local.getEndTime().equals(newEnd)) {
                local.setEndTime(newEnd);
                local.setAveSpeed(remote.getAveSpeed());
                local.setMaxSpeed(remote.getMaxSpeed());
                local.setFlightDuration(remote.getFlightDuration());
                local.setFlightMileage(remote.getFlightMileage());
                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);
        }
    }
 
}