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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.domain.Tenant;
import com.tuoheng.admin.entity.vo.airport.AirPortStatusVo;
import com.tuoheng.admin.entity.vo.airport.AirportInfoVo;
import com.tuoheng.admin.mapper.TenantMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.config.common.CommonConfig;
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.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

@Slf4j
@Service
public class GetAirportListByAirportIds {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    public List<AirPortStatusVo> getList(String airportIds) {
        String url = UriComponentsBuilder.fromHttpUrl(CommonConfig.airportURL + SystemConstant.API_AIRPORT_STATUS_BY_AIRPORT_ID)
                .queryParam("airportIds", airportIds)
                .toUriString();
        JsonResult jsonResult;
        try {
            jsonResult = restTemplate.getForObject(url, JsonResult.class);
        } catch (Exception e) {
            log.info("调用机场平台，查询飞行状态异常, url:{}", url);
            return null;
        }
        List<AirPortStatusVo> airPortStatusVOList = JSONObject.parseArray(JSONObject.toJSONString(jsonResult.getData()), AirPortStatusVo.class);
        return airPortStatusVOList;

    }

    public List<AirportInfoVo> getAirportInfo(String airportIds) {
        //读取不同租户的机场平台url
        Tenant tenant = tenantMapper.selectById(CurrentUserUtil.getTenantId());
        if (ObjectUtil.isEmpty(tenant) && StringUtils.isEmpty(tenant.getCode())) {
            return null;
        }
        String tenantCode = tenant.getCode();
        String url = UriComponentsBuilder.fromHttpUrl(CommonConfig.airportURL + SystemConstant.API_AIRPORT_MSG_BY_AIRPORT_ID)
                .queryParam("airportIds", airportIds)
                .queryParam("tenantCode", tenantCode)
                .toUriString();
        JsonResult jsonResult;
        try {
            jsonResult = restTemplate.getForObject(url, JsonResult.class);
        } catch (Exception e) {
            log.info("调用机场平台，查询机场信息异常，url:{}", url);
            return null;
        }
        if (jsonResult.getCode() != 0) {
            log.info("调用机场平台，查询机场信息，失败，jsonResult：{}", jsonResult.getMsg());
            return null;
        }
        List<AirportInfoVo> airPortStatusVOList = JSONObject.parseArray(JSONObject.toJSONString(jsonResult.getData()), AirportInfoVo.class);
        return airPortStatusVOList;
    }
}
