package com.tuoheng.admin.service.third.airport;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.FlightData;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.TaskStatusEnum;
import com.tuoheng.admin.mapper.FlightDataMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.DroneDataVo;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author ChengWang
 * @Date 2023/3/13
 */
@Service
@Slf4j
public class GetDroneDataByIdService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private FlightDataMapper flightDataMapper;


    public JsonResult getDroneDataById(Integer airportId) {

        log.info("获取无人机数据信息,airportId={}",airportId);
        //校验
        if(airportId ==null){
            throw new ServiceException("机场Id为空");
        }
        String tenantId = CurrentUserUtil.getTenantId();
        if(StringUtils.isEmpty(tenantId)){
            throw new ServiceException("当前租户不存在");
        }
        //根据当前租户侠正在飞行的任务获取机场信息
        Inspection inspection = inspectionMapper.selectOne(Wrappers.<Inspection>lambdaQuery()
                .eq(Inspection::getTenantId, tenantId)
                .eq(Inspection::getAirportId, airportId)
                .eq(Inspection::getStatus,10)
                .eq(Inspection::getMark, 1));
        if (ObjectUtils.isNull(inspection)) {
            log.info("获取无人机数据信息，没有正在飞行的任务");
            throw new ServiceException("没有正在飞行的任务");
        }
        //根据当前正在飞行的任务查询实时数据 flight_data表
        Map<String,Object> map = new HashMap<>();
        map.put("inspectionId",inspection.getId());
        map.put("tenantId",tenantId);
        List<FlightData> flightDataList = flightDataMapper.selectListByInspection(map);
        if(CollectionUtils.isEmpty(flightDataList) || flightDataList.size() == 0){
            log.info("获取无人机数据信息，数据为空");
            return JsonResult.success("数据为空");
        }
        //取最新的一条数据给前端
        FlightData flightData = flightDataList.get(0);
        DroneDataVo droneDataVo = new DroneDataVo();
        droneDataVo.setInspectionId(flightData.getInspectionId());
        droneDataVo.setUavPower(79 + "%");
        droneDataVo.setRange(6 + "km");
        //距离机场的距离
        droneDataVo.setTargetLocation(0 + "km");
        droneDataVo.setYsingal(flightData.getYsingal() + "%");

        return JsonResult.success(droneDataVo);
    }
}
