package com.ruoyi.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.domain.vo.AisShipInfo;
import com.ruoyi.system.domain.vo.ShipEepReportRecInfo;
import com.ruoyi.system.service.IAisInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: huangcheng
 * @Date: 2021/9/26
 * @Version V1.0
 */
@Service
@Slf4j
public class AisInfoServiceImpl implements IAisInfoService {

    @Value("${spring.profiles.active}")
    private String profile;
    @Value("${third.shipEepReportRecInfo}")
    private String shipEepReportRecInfoUrl;
    @Value("${third.shipBaseInfo}")
    private String shipBaseInfo;
    @Value("${third.shipDynamicInfo}")
    private String shipDynamicInfo;

    /**
     * 获取船舶进出港记录
     *
     * @param startTime
     * @param endTime
     * @param mmsi
     * @param shipId
     * @param shipNameCn
     * @return
     */
    @Override
    public ShipEepReportRecInfo getShipEepReportRecInfo(String startTime, String endTime, String mmsi, String shipId, String shipNameCn) {
        if ("prod".equals(profile) && StrUtil.isNotBlank(mmsi) && mmsi.length() == 9) {
            Map<String, Object> params = new HashMap<>();
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            params.put("mmsi", mmsi);
//            params.put("shipId", shipId);
//            params.put("shipNameCn", shipNameCn);
            try {
                String para = HttpUtil.toParams(params);
                String getResult = HttpUtil.get(shipEepReportRecInfoUrl + "?" + para);
                log.info("入参：{}，获取进出港数据：{}", para, getResult);
                JSONObject jsonObject = JSON.parseObject(getResult);
                if (jsonObject != null && 200 == jsonObject.getInteger("code")) {
                    JSONArray result = jsonObject.getJSONArray("result");
                    if (result != null && !result.isEmpty()) {
                        Map<String, String> stringStringMap = Convert.toMap(String.class, String.class, result.get(0));
                        return ShipEepReportRecInfo.builder().berthName(stringStringMap.get("berth_name")).nextPortName(stringStringMap.get("next_port_name")).build();
                    }
                }
            } catch (Exception e) {
                log.error("获取船舶停靠信息异常");
            }

        }
        return null;
    }

    @Override
    public AisShipInfo getShipInfo(String shipnameCn, String mmsi, String shipId, String shipNo) {
        log.info("当前环境：{}", profile);
        if ("prod".equals(profile) && StrUtil.isNotBlank(mmsi) && mmsi.length() == 9) {
            Map<String, Object> params = new HashMap<>();
//            params.put("shipnameCn", shipnameCn);
            params.put("mmsi", mmsi);
//            params.put("shipId", shipId);
//            params.put("shipNo", shipNo);
            try {
                String url = shipBaseInfo + "?" + HttpUtil.toParams(params);
                String getResult = HttpUtil.get(url);
                log.info("调用地址：{}。获取船舶数据：{}", url, getResult);
                JSONObject jsonObject = JSON.parseObject(getResult);
                if (jsonObject != null && 200 == jsonObject.getInteger("code")) {
                    JSONArray result = jsonObject.getJSONArray("data");
                    if (result != null && !result.isEmpty()) {
                        Map<String, String> stringStringMap = Convert.toMap(String.class, String.class, result.get(0));
                        return AisShipInfo.builder().shipName(stringStringMap.get("shipnameCn")).shipRegionType(stringStringMap.get("shipRegionType")).build();
                    }
                }
            } catch (Exception e) {
                log.error("获取船舶基本信息异常");
            }
        }
        return null;
    }

    /**
     * 推送需要获取的船舶实时数据
     *
     * @param mmsis
     */
    @Override
    public void getDynamicShipInfo(String mmsis) {
        if ("prod".equals(profile)) {
            try {
                if (StrUtil.isNotBlank(mmsis)) {
                    String s = HttpUtil.get(shipDynamicInfo + mmsis);
                    log.info("推送船舶动态信息,返回结果：{}", s);
                }
            } catch (Exception e) {
                log.error("推送船舶动态信息异常");
            }
        }
    }

}