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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.tuoheng.admin.dto.AirportDto;
import com.tuoheng.admin.tzhl.constant.TZHLConstant;
import com.tuoheng.admin.tzhl.request.TZHLAirportLineListRequest;
import com.tuoheng.admin.tzhl.service.CallTianYiPlatformService;
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.Arrays;
import java.util.List;

/**
 * 获取机场列表
 * @Author ChengWang
 * @Date 2023/8/9
 */
@Service
@Slf4j
public class TZHLGetAirportListService {

    @Autowired
    private CallTianYiPlatformService callTianYiPlatformService;

    public JsonResult getAirportList(List<Long> idList){
        Long[] longs = idList.stream().toArray(Long[]::new);
        String result = Arrays.toString(longs);

        TZHLAirportLineListRequest request = new TZHLAirportLineListRequest();
        request.setIds(result);


        //读取全量机场平台列表
        String url = TZHLConstant.TIAN_YI_API_AIRPORT_LIST ;

        String data = callTianYiPlatformService.callGet(url, request);
        List<AirportDto> airportDtoList = JSON.parseArray(data, AirportDto.class);
        if(ObjectUtil.isNull(airportDtoList)){
            return JsonResult.error("查询机场列表数据为空");
        }
        return JsonResult.success(airportDtoList);
    }

    public AirportDto getAirport(Long id){
        Long[] idArr = new Long[ ]{id};
        TZHLAirportLineListRequest request = new TZHLAirportLineListRequest();
        request.setIds(Arrays.toString(idArr));

        //读取全量机场平台列表
        String url = TZHLConstant.TIAN_YI_API_AIRPORT_LIST ;

        String data = callTianYiPlatformService.callGet(url, request);
        List<AirportDto> airportDtoList = JSON.parseArray(data, AirportDto.class);
        if(ObjectUtil.isNull(airportDtoList)){
            log.info("查询机场列表数据为空");
            return null;
        }
        return airportDtoList.get(0);
    }
}
