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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionDetailsByIdCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.HttpUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
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;

@Slf4j
@Service
public class DroneHoverService {

    @Autowired
    private InspectionMapper inspectionMapper;

    /**
     * 调用机场平台，原无人机悬停
     *
     * @param inspectionId 任务Id
     */
    public JsonResult hover(String inspectionId) {
        //校验
        JsonResult jsonResult = this.check(inspectionId);
        if(0 != jsonResult.getCode()){
            log.info("无人机悬停, 参数检验失败");
            return JsonResult.error(jsonResult.getCode(),jsonResult.getMsg());
        }
        Inspection inspection = (Inspection) jsonResult.getData();
        Integer airportId = inspection.getAirportId();

        String url = CommonConfig.airportURL + SystemConstant.API_AIRPORT_DRONE_CONTROL;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("zhilin", "01");
        jsonObject.put("airportId", airportId);
        jsonObject.put("taskId", inspectionId);
        jsonObject.put("msg", "悬停");

        log.info("调用机场，无人机悬停：url:{}", url);
        log.info("调用机场，无人机悬停：jsonObject:{}", jsonObject);

        String airPortStr = HttpUtils.doSend(url, jsonObject, null, "POST");
        if (StringUtils.isEmpty(airPortStr)) {
            log.info("调用机场，无人机悬停：机场接口返回数据为空，悬停失败，jsonObject：{}", jsonObject);
            throw new com.aliyun.oss.ServiceException("机场接口返回数据为空，悬停失败");
        }
        jsonResult = JacksonUtil.json2pojo(airPortStr, JsonResult.class);
        if (0 != jsonResult.getCode()) {
            log.info("调用机场，无人机悬停：悬停失败，jsonResult：{}", jsonResult.getMsg());
            throw new ServiceException("机场平台返回，悬停失败");
        }
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private JsonResult check(String id) {
        // 判断任务id是否为空
        if (StringUtils.isEmpty(id)) {
            log.info("无人机悬停，任务ID为空");
            throw new ServiceException("任务ID为空");
        }
        // 判断任务是否存在
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getId, id)
                .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isEmpty(inspection)) {
            log.info("无人机悬停，任务不存在");
            throw new ServiceException("任务不存在");
        }
        return JsonResult.success(inspection);
    }
}
