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

import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.constant.SystemConstant;
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.stereotype.Service;

@Slf4j
@Service
public class DroneContinueService {

    /**
     * 调用机场平台，原无人机继续巡检
     *
     * @param airportId    机场Id
     * @param inspectionId 巡检任务Id
     */
    public JsonResult continuee(Integer airportId, String inspectionId) {
        String url = CommonConfig.airportURL + SystemConstant.API_AIRPORT_DRONE_CONTROL;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("zhilin", "04");
        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 jsonResult = JacksonUtil.json2pojo(airPortStr, JsonResult.class);
        if (0 != jsonResult.getCode()) {
            log.info("调用机场，原无人机继续巡检：飞行失败，jsonResult：{}", jsonResult.getMsg());
            throw new ServiceException("机场平台返回，飞行失败");
        }
        return JsonResult.success();
    }

}
