package com.tuoheng.admin.service.airportline.query.inspection;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.code.AriportCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.vo.airport.AirPortLineLocationVo;
import com.tuoheng.admin.vo.airport.AirPortStatusVO;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 根据任务ID获取航线，从机场平台获取
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-04-26
 */
@Slf4j
@Service
public class QueryAirportLineByAirportService {

    /**
     * 根据任务ID获取航线，从机场平台获取
     *
     * @return
     */
    public JsonResult getAirportLineList(Inspection inspection) {
        log.info("进入根据任务ID获取航线，从机场平台获取");
        Integer inspectionLine = inspection.getInspectionLine();
        JsonResult result = this.checkParam(inspectionLine);
        if (0 != result.getCode()) {
            log.info("根据任务ID获取航线：校验参数失败：{}", result.getMsg());
            return result;
        }
        List<AirPortLineLocationVo> airPortLineLocationVoList = this.getAirPortLocation(inspectionLine);
        return JsonResult.success(airPortLineLocationVoList);
    }

    private JsonResult checkParam(Integer inspectionLine) {
        if (ObjectUtil.isEmpty(inspectionLine)) {
            log.info("进入根据任务ID获取航线，从机场平台获取业务，航线ID为空，inspectionLine={}", inspectionLine);
            throw new ServiceException("航线ID为空");
        }
        return JsonResult.success();
    }

    private List<AirPortLineLocationVo> getAirPortLocation(Integer inspectionLine) {
        String url = CommonConfig.airportURL + SystemConstant.API_AIRPORT_LOCATION;
        String param = "id=" + inspectionLine;
        String airPortLineLocationStr = HttpUtils.sendGet(url, param);
        if (StringUtils.isEmpty(airPortLineLocationStr)) {
            log.info("调用机场平台，查询航线：机场接口返回数据为空");
            return null;
        }
        JsonResult jsonResult = JacksonUtil.json2pojo(airPortLineLocationStr, JsonResult.class);
        if (0 != jsonResult.getCode()) {
            log.info("调用机场平台，查询航线：机场接口返回状态失败");
            return null;
        }
        if (ObjectUtil.isEmpty(jsonResult.getData())) {
            log.info("调用机场平台，查询航线：机场接口返回数据为空");
            return null;
        }
        List<AirPortLineLocationVo> airPortLineLocationVoList = JSONObject.parseArray(JSONObject.toJSONString(jsonResult.getData()), AirPortLineLocationVo.class);
        return airPortLineLocationVoList;
    }

}
