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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.FlightData;
import com.tuoheng.admin.entity.FlightDataHl;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionInfoByIdCodeEnum;
import com.tuoheng.admin.mapper.FlightDataMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.LiveChannelMapper;
import com.tuoheng.admin.vo.AirportMsgVo;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.CommonNumberTypeUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.RedisUtils;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 根据Id查询巡检任务业务层处理
 *
 * @author chengwang
 * @team tuoheng
 * @date 2022-11-25
 */
@Slf4j
@Service
public class QueryAirportMsgByIdService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private LiveChannelMapper liveChannelMapper;

    @Autowired
    private RedisUtils redisUtils;


    public JsonResult airportMsg(String inspectionId) {
        //任务id校验
        if (StringUtils.isEmpty(inspectionId)) {
            return JsonResult.error(QueryInspectionInfoByIdCodeEnum.INSPECTION_ID_IS_NULL.getCode(),
                    QueryInspectionInfoByIdCodeEnum.INSPECTION_ID_IS_NULL.getMsg());
        }
        Inspection inspection = inspectionMapper.selectById(inspectionId);
        if (ObjectUtil.isEmpty(inspection)) {
            return JsonResult.error("任务不存在");
        }
        if (inspection.getStatus() != InspectionStatusEnum.IN_FLIGHT.getCode()) {
            return JsonResult.success("当前任务不是正在飞行中,无直播和无人机信息");
        }
        //获取遥测信号最新的一条数据
        Object obj = redisUtils.get(inspectionId);
        FlightData flightDataHl = null;
        if (ObjectUtil.isNull(obj)) {
            return JsonResult.success();
        } else {
            flightDataHl = (FlightData) obj;
        }
        AirportMsgVo vo = new AirportMsgVo();

        vo.setHt(flightDataHl.getHt() == null ? "" : flightDataHl.getHt());
        vo.setDuration(flightDataHl.getDuration() == null ? "" : flightDataHl.getDuration());
        vo.setBat(flightDataHl.getBat() == null ? "" : flightDataHl.getBat());
        vo.setSatellite(flightDataHl.getSatellite() == null ? "" : flightDataHl.getSatellite());
        vo.setGps(flightDataHl.getGpssingal() == null ? "" : flightDataHl.getGpssingal());

        //剩余飞行时间
        String remainingFlightTime = flightDataHl.getRemainingFlightTime();
        if (null == remainingFlightTime) {
            vo.setRemainingFlightTime("");
        }
        Double temp;
        temp = Double.parseDouble(remainingFlightTime) / 60;
        double flightTime = CommonNumberTypeUtils.subDecimalFormat(temp, "#.00");
        vo.setRemainingFlightTime(String.valueOf(flightTime));
        //飞行速度
        if (null == flightDataHl.getAbsp()) {
            vo.setAbsp("");
        }
        double v = Double.parseDouble(flightDataHl.getAbsp());
        double absp = CommonNumberTypeUtils.round(v, 2);
        vo.setAbsp(String.valueOf(absp));

        return JsonResult.success(vo);
    }
}
