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

import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.domain.Tenant;
import com.tuoheng.admin.enums.code.AriportCodeEnum;
import com.tuoheng.admin.mapper.TenantMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.entity.vo.AirLineVO;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.HttpUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class GetAirLineListService {

    @Autowired
    private TenantMapper tenantMapper;

    public JsonResult getAirLineList(Integer droneId) {
        //读取不同租户的机场平台url
        Tenant tenant = tenantMapper.selectById(CurrentUserUtil.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            return JsonResult.error(AriportCodeEnum.GET_NO_TENANT.getCode(), AriportCodeEnum.GET_NO_TENANT.getMsg());
        }
        String url = CommonConfig.airportURL + SystemConstant.API_AIRPORT_LINE_LIST;
        String param = "page=1&limit=100&droneId=" + droneId;
        String airPortStr = HttpUtils.sendGet(url, param);
        log.info("调用机场平台,获取航线列表：airPortStr={}",airPortStr);
        //string----->JsonResult
        JsonResult<AirLineVO> jsonResult = JacksonUtil.json2pojo(airPortStr, JsonResult.class);

        if (ObjectUtil.isEmpty(jsonResult) || (!ObjectUtil.isEmpty(Objects.requireNonNull(jsonResult).getData()) && jsonResult.getCode() != 0)) {
            return JsonResult.error(AriportCodeEnum.BAD_REQUEST.getCode(), AriportCodeEnum.BAD_REQUEST.getMsg());
        }
        return jsonResult;
    }

}
