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

import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.dto.AirportDetailDto;
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.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Author xiaoying
 * @Date 2023/4/14 16:05
 */
@Service
@Slf4j
public class GetAirportDetailService {

    public JsonResult getAirportDetail(Integer airportId) {
        String url = CommonConfig.airportURL + "/api/airportInterface/getAirportStatus";
        String param = "airportId=" + airportId;
        String result = HttpUtils.sendGet(url, param);
        JsonResult jsonResult = JacksonUtil.json2pojo(result, JsonResult.class);
        AirportDetailDto vo = new AirportDetailDto();
        JSONObject dataObject = (JSONObject) JSONObject.toJSON(jsonResult.getData());
        log.info("响应信息:{}", dataObject.toJSONString());
        JSONObject wthJson = dataObject.getJSONObject("WTH");
        if (Objects.nonNull(wthJson)) {
            log.info("参数信息:{}", wthJson.toJSONString());
            JSONObject parmJson = wthJson.getJSONObject("parmNew");
            log.info("参数信息parmJson:{}", parmJson.toJSONString());
            vo.setWspd(parmJson.getString("WSPD") + "m/s")
                    .setWdir(parmJson.getString("WDIRNAME"))
                    .setHpa(parmJson.getString("Hpa") + "Mpa")
                    .setHum(parmJson.getString("Hum") + "rh")
                    .setRainfull(parmJson.getString("Rainfull"))
                    .setTmp(parmJson.getString("Tmp") + "℃");
        }
        JSONObject tahJson = dataObject.getJSONObject("TAH");
        if (Objects.nonNull(tahJson)) {
            JSONObject parmJson = tahJson.getJSONObject("parmNew");
            vo.setHum(parmJson.getString("Hum") + "rh")
                    .setTmp(parmJson.getString("Tmp") + "℃");

        }
        JSONObject mountJson = dataObject.getJSONObject("mount");
        if (Objects.nonNull(mountJson)) {
            vo.setMountName(mountJson.getString("cameraName") + "、" +
                    mountJson.getString("megaphoneName") + "、" +
                    mountJson.getString("searchlightName")
            );
        }
        vo.setStatus(dataObject.getString("status"));
        return JsonResult.success(vo);
    }
}
