package org.springblade.modules.nh.factory.saj;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.modules.nh.factory.saj.req.QueryDeviceHistoryReq;
import org.springblade.modules.nh.factory.saj.req.QueryPlantEnergyReq;
import org.springblade.modules.nh.factory.saj.req.SajPageReq;
import org.springblade.modules.nh.factory.saj.resp.*;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@AllArgsConstructor
public class SajApi {

    public static final String baseUrl = "https://developer.saj-electric.cn/prod-api";
    private static final Logger log = LoggerFactory.getLogger(SajApi.class);

	private final SajApiRestTemplate sajApiRestTemplate;

	/**
	 * 分页查询电站列表
	 * @param req
	 * @return
	 */
	public SajPageRet<BasePlant> queryPlantPage(SajPageReq req) {
		String url = baseUrl+"/open/api/developer/plant/page";
		return sajApiRestTemplate.get(assembleUrlParam(url, BeanUtil.beanToMap(req)), BeanUtil.beanToMap(req) ,new ParameterizedTypeReference<SajPageRet<BasePlant>>() {
		});
	}

	/**
	 * 分页查询设备列表
	 * @param req
	 * @return
	 */
	public SajPageRet<SajDevice> queryDevicePage(SajPageReq req) {
		String url = baseUrl+"/open/api/developer/device/page";
		return sajApiRestTemplate.get(assembleUrlParam(url, BeanUtil.beanToMap(req)), BeanUtil.beanToMap(req) ,new ParameterizedTypeReference<SajPageRet<SajDevice>>() {
		});
	}

	/**
	 * 获取电站的当前发电量接口
	 * @param req
	 * @return
	 */
	public SajRet<SajPlantCurrent> currentPlantData(QueryPlantEnergyReq req) {
		String url = baseUrl+"/open/api/plant/energy";
		return sajApiRestTemplate.get(assembleUrlParam(url, BeanUtil.beanToMap(req)), BeanUtil.beanToMap(req) ,new ParameterizedTypeReference<SajRet<SajPlantCurrent>>() {
		});
	}

	/**
	 * 获取电站的当前发电量接口
	 * @param req
	 * @return
	 */
	public SajRet<SajPlantData> getPlantStatisticsData(QueryPlantEnergyReq req) {
		String url = baseUrl+"/open/api/plant/getPlantStatisticsData";
		return sajApiRestTemplate.get(assembleUrlParam(url, BeanUtil.beanToMap(req)), BeanUtil.beanToMap(req) ,new ParameterizedTypeReference<SajRet<SajPlantData>>() {
		});
	}

	/**
	 * 获取电站详细信息
	 * @param plantId
	 * @return
	 */
	public SajRet<SajPlantDetail> getPlantDetail(String plantId) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("plantId",plantId);
		String url = baseUrl+"/open/api/plant/details";
		return sajApiRestTemplate.get(assembleUrlParam(url, params), params ,new ParameterizedTypeReference<SajRet<SajPlantDetail>>() {
		});
	}

	/**
	 * 获取设备上传数据信息
	 * @param req
	 * @return
	 */
	public SajRet<SajDeviceData> getDeviceHistory(QueryDeviceHistoryReq req) {
		Map<String,Object> param = BeanUtil.beanToMap(req);
		String url = baseUrl+"/open/api/device/uploadData";
		return sajApiRestTemplate.get(assembleUrlParam(url, param), param ,new ParameterizedTypeReference<SajRet<SajDeviceData>>() {
		});
	}

	/**
	 * 获取设备实时数据
	 * @param sn
	 * @return
	 */
	public SajRet<DeviceRealtimeData> getDeviceRealtime(String sn) {
		Map<String, Object> param = Maps.newHashMap();
		param.put("deviceSn",sn);
		String url = baseUrl+"/open/api/device/realtimeDataCommon";
		return sajApiRestTemplate.get(assembleUrlParam(url, param), param ,new ParameterizedTypeReference<SajRet<DeviceRealtimeData>>() {
		});
	}


	public String assembleUrlParam(String url,Map<String,Object> param) {
		url = url+"?";
		for(Map.Entry<String,Object> entry:param.entrySet()){
			if(entry.getValue()!=null){
				url+=entry.getKey()+"="+entry.getValue()+"&";
			}
		}
		String result = url.substring(0,url.length()-1);
		return result;
	}

    public static void main(String[] args) {
        String url = "https://api.ginlong.com:13333/v1/api/userStationList";
        System.out.println(url.substring("https://api.ginlong.com:13333".length()));
    }

}
