159 lines
5.3 KiB
Java
159 lines
5.3 KiB
Java
|
|
package com.ruoyi.device.service.impl;
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.stereotype.Service;
|
|||
|
|
import org.springframework.transaction.annotation.Transactional;
|
|||
|
|
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.HashMap;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Map;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 飞行服务实现类
|
|||
|
|
*
|
|||
|
|
* @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;
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public FlightEntity getOrCreateCurrentFlight(String deviceSn) {
|
|||
|
|
FlightEntity flight = flightMapper.selectLatestFlightByDeviceSn(deviceSn);
|
|||
|
|
|
|||
|
|
if (flight == null || "已返航".equals(flight.getStatus())) {
|
|||
|
|
flight = createFlight(deviceSn);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return flight;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
@Transactional(rollbackFor = Exception.class)
|
|||
|
|
public FlightEntity getOrCreateFlightByMessageId(String deviceSn, String messageId) {
|
|||
|
|
FlightEntity flight = flightMapper.selectLatestFlightByDeviceSn(deviceSn);
|
|||
|
|
|
|||
|
|
if (flight == null) {
|
|||
|
|
flight = createFlight(deviceSn);
|
|||
|
|
if (messageId != null && !messageId.isEmpty()) {
|
|||
|
|
updateFlightIdExternal(flight.getFlightId(), messageId);
|
|||
|
|
}
|
|||
|
|
return flight;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
String existingFlightIdExternal = flight.getFlightIdExternal();
|
|||
|
|
if (existingFlightIdExternal == null || existingFlightIdExternal.isEmpty()) {
|
|||
|
|
if (messageId != null && !messageId.isEmpty()) {
|
|||
|
|
updateFlightIdExternal(flight.getFlightId(), messageId);
|
|||
|
|
}
|
|||
|
|
return flight;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (existingFlightIdExternal.equals(messageId)) {
|
|||
|
|
return flight;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
flight = createFlight(deviceSn);
|
|||
|
|
if (messageId != null && !messageId.isEmpty()) {
|
|||
|
|
updateFlightIdExternal(flight.getFlightId(), messageId);
|
|||
|
|
}
|
|||
|
|
log.info("messageId不同,创建新飞行 - deviceSn={}, flightId={}, oldMessageId={}, newMessageId={}",
|
|||
|
|
deviceSn, flight.getFlightId(), existingFlightIdExternal, messageId);
|
|||
|
|
return flight;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public FlightEntity getLatestFlight(String deviceSn) {
|
|||
|
|
return flightMapper.selectLatestFlightByDeviceSn(deviceSn);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
@Transactional(rollbackFor = Exception.class)
|
|||
|
|
public FlightEntity createFlight(String deviceSn) {
|
|||
|
|
FlightEntity flight = new FlightEntity();
|
|||
|
|
flight.setDeviceSn(deviceSn);
|
|||
|
|
flight.setStatus("自检中");
|
|||
|
|
flightMapper.insertFlight(flight);
|
|||
|
|
log.info("创建新的飞行记录: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
|
|||
|
|
return flight;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void updateFlightStatus(Long flightId, String status) {
|
|||
|
|
flightMapper.updateFlightStatus(flightId, status);
|
|||
|
|
log.info("更新飞行状态: flightId={}, status={}", flightId, status);
|
|||
|
|
|
|||
|
|
if ("已返航".equals(status)) {
|
|||
|
|
flightMapper.updateReturnTime(flightId);
|
|||
|
|
log.info("更新返航时间: flightId={}", flightId);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void updateReturnTime(Long flightId) {
|
|||
|
|
flightMapper.updateReturnTime(flightId);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@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());
|
|||
|
|
result.put("preCheckLogs", preCheckLogs);
|
|||
|
|
|
|||
|
|
List<FlightLogEntity> flightLogs = flightLogMapper.selectFlightLogListByFlightId(flight.getFlightId());
|
|||
|
|
result.put("flightLogs", flightLogs);
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void insertPreCheckLog(PreCheckLogEntity logEntity) {
|
|||
|
|
preCheckLogMapper.insertPreCheckLog(logEntity);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void insertFlightLog(FlightLogEntity logEntity) {
|
|||
|
|
flightLogMapper.insertFlightLog(logEntity);
|
|||
|
|
}
|
|||
|
|
}
|