a-tuoheng-device/src/main/java/com/ruoyi/device/service/impl/FlightServiceImpl.java

199 lines
7.4 KiB
Java
Raw Normal View History

2026-02-25 13:06:56 +08:00
package com.ruoyi.device.service.impl;
2026-03-10 10:47:15 +08:00
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
2026-02-25 13:06:56 +08:00
import com.ruoyi.device.mapper.FlightLogMapper;
import com.ruoyi.device.mapper.FlightMapper;
import com.ruoyi.device.mapper.PreCheckLogMapper;
import com.ruoyi.device.mapper.entity.FlightEntity;
import com.ruoyi.device.mapper.entity.FlightLogEntity;
import com.ruoyi.device.mapper.entity.PreCheckLogEntity;
import com.ruoyi.device.service.FlightService;
2026-03-10 10:47:15 +08:00
import com.ruoyi.task.api.RemoteTaskService;
import com.ruoyi.task.api.domain.TaskDTO;
import com.ruoyi.task.api.enums.ExecuteTypeEnum;
import com.ruoyi.task.api.enums.StatusEnum;
import com.ruoyi.task.api.enums.TaskCategoryEnum;
2026-02-25 13:06:56 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
2026-03-10 10:47:15 +08:00
import java.util.*;
2026-02-25 13:06:56 +08:00
/**
* 飞行服务实现类
*
* @author ruoyi
* @date 2026-02-25
*/
@Slf4j
@Service
public class FlightServiceImpl implements FlightService
{
@Autowired
private FlightMapper flightMapper;
@Autowired
private PreCheckLogMapper preCheckLogMapper;
@Autowired
private FlightLogMapper flightLogMapper;
2026-03-10 10:47:15 +08:00
@Autowired
private RemoteTaskService remoteTaskService;
public Long onClickTakeOff(String sn,String routeUrl){
TaskDTO taskDTO = new TaskDTO();
taskDTO.setActualStartTime(new Date());
taskDTO.setStartTime(new Date());
taskDTO.setExecuteType(ExecuteTypeEnum.ONCE);
taskDTO.setTaskCategory(TaskCategoryEnum.MANUAL_FLIGHT);
taskDTO.setRouteId(-1L);
taskDTO.setUavId(sn);
taskDTO.setStatus(StatusEnum.RUNNING);
taskDTO.setRouteUrl(routeUrl);
R<Long> taskId = remoteTaskService.createTaskWithoutPlan(taskDTO,SecurityConstants.INNER);
return taskId.getData();
}
2026-02-25 13:06:56 +08:00
@Override
@Transactional(rollbackFor = Exception.class)
public FlightEntity getOrCreateFlightByMessageId(String deviceSn, String messageId) {
2026-02-27 15:30:09 +08:00
// 先查询是否存在相同 deviceSn 和 flight_id_extern 的记录
FlightEntity flight = flightMapper.selectFlightByDeviceSnAndFlightIdExternal(deviceSn, messageId);
2026-02-25 13:06:56 +08:00
2026-02-27 15:30:09 +08:00
if (flight != null) {
// 如果存在,直接返回
log.info("找到已存在的飞行记录: deviceSn={}, messageId={}, flightId={}",
deviceSn, messageId, flight.getFlightId());
2026-02-25 13:06:56 +08:00
return flight;
}
2026-02-27 15:30:09 +08:00
// 如果不存在,创建新的飞行记录
flight = new FlightEntity();
flight.setDeviceSn(deviceSn);
flight.setFlightIdExternal(messageId);
flight.setStatus("CHECKING");
flightMapper.insertFlight(flight);
2026-02-25 13:06:56 +08:00
2026-02-27 15:30:09 +08:00
log.info("创建新的飞行记录: deviceSn={}, messageId={}, flightId={}, status=CHECKING",
deviceSn, messageId, flight.getFlightId());
2026-02-25 13:06:56 +08:00
return flight;
}
2026-03-10 10:47:15 +08:00
// @Override
// public FlightEntity getLatestFlight(String deviceSn) {
// return flightMapper.selectLatestFlightByDeviceSn(deviceSn);
// }
2026-02-28 10:05:38 +08:00
2026-02-25 13:06:56 +08:00
2026-03-10 10:47:15 +08:00
//
// @Override
// public void updateFlightIdExternal(Long flightId, String flightIdExternal) {
// FlightEntity flight = new FlightEntity();
// flight.setFlightId(flightId);
// flight.setFlightIdExternal(flightIdExternal);
// flightMapper.updateFlight(flight);
// log.info("更新飞行ID: flightId={}, flightIdExternal={}", flightId, flightIdExternal);
// }
2026-02-25 13:06:56 +08:00
@Override
public void updateFlightStatus(Long flightId, String status) {
flightMapper.updateFlightStatus(flightId, status);
log.info("更新飞行状态: flightId={}, status={}", flightId, status);
2026-02-28 10:05:38 +08:00
if ("HOME".equals(status) ||"ERROR".equals(status)) {
2026-02-25 13:06:56 +08:00
flightMapper.updateReturnTime(flightId);
log.info("更新返航时间: flightId={}", flightId);
}
}
2026-03-10 10:47:15 +08:00
// @Override
// public void updateReturnTime(Long flightId) {
// flightMapper.updateReturnTime(flightId);
// }
2026-02-25 13:06:56 +08:00
@Override
public Map<String, Object> getLatestFlightWithLogs(String deviceSn) {
FlightEntity flight = flightMapper.selectLatestFlightByDeviceSn(deviceSn);
if (flight == null) {
return null;
}
Map<String, Object> result = new HashMap<>();
result.put("flightId", flight.getFlightId());
result.put("deviceSn", flight.getDeviceSn());
result.put("flightIdExternal", flight.getFlightIdExternal());
result.put("status", flight.getStatus());
result.put("returnTime", flight.getReturnTime());
result.put("createTime", flight.getCreateTime());
List<PreCheckLogEntity> preCheckLogs = preCheckLogMapper.selectPreCheckLogListByFlightId(flight.getFlightId());
2026-02-28 09:42:28 +08:00
List<Map<String, Object>> preCheckLogsWithExecTime = new ArrayList<>();
Date prevTime = flight.getCreateTime();
for (PreCheckLogEntity log : preCheckLogs) {
Map<String, Object> logMap = new HashMap<>();
logMap.put("logId", log.getLogId());
logMap.put("flightId", log.getFlightId());
logMap.put("logContent", log.getLogContent());
logMap.put("success", log.getSuccess());
logMap.put("createTime", log.getCreateTime());
logMap.put("updateTime", log.getUpdateTime());
if (log.getCreateTime() != null && prevTime != null) {
logMap.put("execTime", log.getCreateTime().getTime() - prevTime.getTime());
prevTime = log.getCreateTime();
}
preCheckLogsWithExecTime.add(logMap);
}
result.put("preCheckLogs", preCheckLogsWithExecTime);
2026-02-25 13:06:56 +08:00
List<FlightLogEntity> flightLogs = flightLogMapper.selectFlightLogListByFlightId(flight.getFlightId());
result.put("flightLogs", flightLogs);
return result;
}
@Override
public void insertPreCheckLog(PreCheckLogEntity logEntity) {
2026-02-27 08:58:36 +08:00
log.info("【FlightServiceImpl】准备插入自检日志: flightId={}, logContent={}, success={}",
logEntity.getFlightId(), logEntity.getLogContent(), logEntity.getSuccess());
int rows = preCheckLogMapper.insertPreCheckLog(logEntity);
if (rows > 0) {
log.info("【FlightServiceImpl】成功插入自检日志: flightId={}, logId={}, rows={}",
logEntity.getFlightId(), logEntity.getLogId(), rows);
} else {
log.error("【FlightServiceImpl】插入自检日志失败影响行数为0: flightId={}, logContent={}",
logEntity.getFlightId(), logEntity.getLogContent());
}
2026-02-25 13:06:56 +08:00
}
@Override
public void insertFlightLog(FlightLogEntity logEntity) {
2026-02-27 08:58:36 +08:00
log.info("【FlightServiceImpl】准备插入飞行日志: flightId={}, logContent={}",
logEntity.getFlightId(), logEntity.getLogContent());
int rows = flightLogMapper.insertFlightLog(logEntity);
if (rows > 0) {
log.info("【FlightServiceImpl】成功插入飞行日志: flightId={}, logId={}, rows={}",
logEntity.getFlightId(), logEntity.getLogId(), rows);
} else {
log.error("【FlightServiceImpl】插入飞行日志失败影响行数为0: flightId={}, logContent={}",
logEntity.getFlightId(), logEntity.getLogContent());
}
2026-02-25 13:06:56 +08:00
}
2026-02-27 15:30:09 +08:00
@Override
public boolean hasFlightLog(Long flightId) {
int count = flightLogMapper.countFlightLogByFlightId(flightId);
return count > 0;
}
2026-02-25 13:06:56 +08:00
}