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

import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.config.SystemConstant;
import com.tuoheng.admin.entity.request.airport.DroneControlRequest;
import com.tuoheng.admin.mapper.TenantMapper;
import com.tuoheng.common.config.CommonConfig;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.HttpUtils;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DroneControlService {

    @Autowired
    private TenantMapper tenantMapper;

    public JsonResult execute(DroneControlRequest request) {
        log.info("进入调用机场平台，操作无人机");
        this.check(request);
        String url = CommonConfig.airportURL + SystemConstant.API_AIRPORT_DRONE_CONTROL;
        JSONObject jsonObject = new JSONObject().parseObject(request.getValue());

        log.info("调用机场平台，操作无人机: url:{}", url);
        log.info("调用机场平台，操作无人机: jsonObject:{}", jsonObject);

        String airPortStr = HttpUtils.doSend(url, jsonObject, null, "POST");
        log.info("调用机场平台，操作无人机: airPortStr:{}", airPortStr);
        if (StringUtils.isEmpty(airPortStr)) {
            log.info("执行定点飞行：机场接口返回数据为空，飞行失败，jsonObject：{}", jsonObject);
            return JsonResult.error(-1, "机场接口返回数据为空");
        }

        log.info("调用机场平台方法: 操作无人机结束");
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param request
     * @return
     */
    private void check(DroneControlRequest request) {
        if (StringUtils.isEmpty(request.getValue())) {
            throw new ServiceException("操作无人机，传参空");
        }
    }

}