package com.ruoyi.system.service.impl;

import cn.hutool.core.convert.Convert;
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 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
public class AisInfoServiceImpl implements IAisInfoService {

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

    /**
     * 获取船舶进出港记录
     *
     * @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)) {
            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);
            String getResult = HttpUtil.get(shipEepReportRecInfoUrl, params);
            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();
                }
            }
        }
        return null;
    }

    @Override
    public AisShipInfo getShipInfo(String shipnameCn, String mmsi, String shipId, String shipNo) {
        if ("prod".equals(profile)) {
            Map<String, Object> params = new HashMap<>();
            params.put("shipnameCn", shipnameCn);
            params.put("mmsi", mmsi);
            params.put("shipId", shipId);
            params.put("shipNo", shipNo);
            String getResult = HttpUtil.get(shipBaseInfo, params);
            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();
                }
            }
        }
        return null;
    }
}