package com.tuoheng.admin.service.flightdata.query;

import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.admin.entity.FlightData;
import com.tuoheng.admin.mapper.FlightDataMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 根据巡检id查询最新一条遥测数据
 *
 * @Author ChengWang
 * @Date 2022/12/2
 */
@Service
@Slf4j
public class QueryFindFlightDataByInspectionIdService {

    @Autowired
    private FlightDataMapper flightDataMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private RedisUtils redisUtils;

    public FlightData getFlightData(String inspectionId) {
        //拉取最新一条经纬度信息
//        FlightData flightData = flightDataMapper.selectOne(Wrappers.<FlightData>lambdaQuery()
//                .eq(FlightData::getInspectionId, inspectionId)
//                .orderByDesc(FlightData::getCreateTime)
//                .last("limit 1"));
        //从缓存中拿出遥测数据
        Object obj = redisUtils.get(inspectionId);
        FlightData flightData = null;
        if (ObjectUtil.isNull(obj)) {
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        } else {
            flightData = (FlightData) obj;
        }
        return flightData;


    }
}
