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

import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.admin.entity.domain.FlightData;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionInfoByIdCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.LiveChannelMapper;
import com.tuoheng.admin.entity.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 flightData = null;
        if (ObjectUtil.isNull(obj)) {
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        } else {
            flightData = (FlightData) obj;
        }
        AirportMsgVo vo = new AirportMsgVo();
        BeanUtils.copyProperties(flightData,vo);
        //属性赋值
        //高度
        Double.parseDouble(flightData.getAlt());
        double round = CommonNumberTypeUtils.round(Double.parseDouble(flightData.getAlt()), 2);
        vo.setAlt(round + "m");
        //gps
        vo.setGpssingal(flightData.getGpssingal() + "%");
        //电量
        Double Umax = 26.1;
        Double Umin = 22.2;
        if(StringUtils.isNotEmpty(flightData.getVoltage())){
            double temp = Double.parseDouble(flightData.getVoltage());
            if(temp>=Umin){
                //实时电压值大于现在电压最小值
                double vol = (temp - Umin) / (Umax - Umin);
                String percent = CommonNumberTypeUtils.getPercentFormat(vol, Integer.MAX_VALUE, 2);
                vo.setVoltage(percent);
            }else {
                vo.setVoltage(0 + "%");
            }
        }
        //速度
        double hSpeed = CommonNumberTypeUtils.round(Double.parseDouble(flightData.getHspeed()), 2);
        vo.setHspeed(hSpeed+ "m/s");
        //里程
        double mileageTemp = Double.parseDouble(flightData.getMileage()) / 1000;
        double mileage = CommonNumberTypeUtils.round(mileageTemp, 2);
        vo.setMileage(mileage+ "km");
        //机场距离
        double distToHome = CommonNumberTypeUtils.round(Double.parseDouble(flightData.getDistToHome()), 2);
        vo.setDistToHome(distToHome + "m");
        return JsonResult.success(vo);
    }
}
