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

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.modules.nh.factory.common.TimeWindow;
import org.springblade.modules.nh.factory.goodwe.config.GoodweApiRestTemplate;
import org.springblade.modules.nh.factory.goodwe.dto.getInventersdatas.GetInventersDatasData;
import org.springblade.modules.nh.factory.goodwe.dto.getInventersdatas.GetInventersDatasReq;
import org.springblade.modules.nh.factory.goodwe.dto.getInverterpacbyday.GetInverterPacByDayR;
import org.springblade.modules.nh.factory.goodwe.dto.getInverterpacbyday.GetInverterPacByDayReq;
import org.springblade.modules.nh.factory.goodwe.dto.getinventersmoredetailinfobysn.GetInventersMoreDetailInfoBySnReq;
import org.springblade.modules.nh.factory.goodwe.dto.getinventersmoredetailinfobysn.GetInventersMoreDetailInfoBySnResp;
import org.springblade.modules.nh.factory.goodwe.dto.getinverterdatabycolumn.GetInverterDataByColumnReq;
import org.springblade.modules.nh.factory.goodwe.dto.getinverterdatabycolumn.GetInverterDataByColumnResp;
import org.springblade.modules.nh.factory.goodwe.dto.getinverterpower.GetInverterPowerR;
import org.springblade.modules.nh.factory.goodwe.dto.getinverterpower.GetInverterPowerReq;
import org.springblade.modules.nh.factory.goodwe.dto.getplantspowers.GetPlantsPowersData;
import org.springblade.modules.nh.factory.goodwe.dto.getplantspowers.GetPlantsPowersReq;
import org.springblade.modules.nh.factory.goodwe.dto.getplantsstaticdatas.GetPlantsStaticDatasData;
import org.springblade.modules.nh.factory.goodwe.dto.getplantsstaticdatas.GetPlantsStaticDatasReq;
import org.springblade.modules.nh.factory.goodwe.dto.getplantsstaticdatas.GetPlantsStaticDatasReqByTime;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationpacbyday.GetPowerStationPacByDayR;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationpacbyday.GetPowerStationPacByDayReq;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationpower.GetPowerStationPowerR;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationpower.GetPowerStationPowerReq;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationwarininginfobymulticondition.GetPowerStationWariningInfoByMultiConditionData;
import org.springblade.modules.nh.factory.goodwe.dto.getpowerstationwarininginfobymulticondition.GetPowerStationWariningInfoByMultiConditionReq;
import org.springblade.modules.nh.factory.goodwe.dto.gettoken.GetTokenR;
import org.springblade.modules.nh.factory.goodwe.dto.gettoken.GetTokenReq;
import org.springblade.modules.nh.factory.goodwe.dto.queryinventers.QueryInventersData;
import org.springblade.modules.nh.factory.goodwe.dto.queryinventers.QueryInventersReq;
import org.springblade.modules.nh.factory.goodwe.dto.querypowerstationmonitor.QueryPowerStationMonitorData;
import org.springblade.modules.nh.factory.goodwe.dto.querypowerstationmonitor.QueryPowerStationMonitorReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class GoodweApi {

    private final GoodweApiRestTemplate goodweApiRestTemplate;

	/**
	 * 通过sn获取逆变器详情信息
	 */
	public GoodweRet<GetInventersMoreDetailInfoBySnResp> getInventersDetailBySn(int index,String sn) {
		if (StringUtils.isEmpty(sn)) {
			return null;
		}
		Map<String,String> req = Maps.newHashMap();
		String url = "http://openapi.sems.com.cn/api/OpenApi/GetInventersMoreDetailInfoBySn?sn=" + sn;
		return goodweApiRestTemplate.get(index,url, req, new ParameterizedTypeReference<GoodweRet<GetInventersMoreDetailInfoBySnResp>>() {
		});
	}

    /**
     * 查询当前登录者当前组织和下级组织电站,无设备电站将被过滤.
     */
    public GoodweRet<GetTokenR> getToken(GetTokenReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetToken";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<GetTokenR>>() {
        });
    }

    /**
     * 查询当前登录者当前组织和下级组织电站,无设备电站将被过滤.
     */
    public GoodweRet<QueryPowerStationMonitorData> queryPowerStationMonitor(QueryPowerStationMonitorReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/QueryPowerStationMonitor";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<QueryPowerStationMonitorData>>() {
        });
    }

    /**
     * 根据电站 id 获取电站日发电量,
     */
    public GoodweRet<List<GetPowerStationPowerR>> getPowerStationPower(GetPowerStationPowerReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetPowerStationPower";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<List<GetPowerStationPowerR>>>() {
        });
    }

    /**
     * 根据电站 id 和日期获取日功率数据。
     */
    public GoodweRet<List<GetPowerStationPacByDayR>> getPowerStationPacByDay(GetPowerStationPacByDayReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetPowerStationPacByDay";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<List<GetPowerStationPacByDayR>>>() {
        });
    }

    /**
     * 根据电站 id 集合批量获取电站指定日期各时间点的功率值。,
     */
    public GoodweRet<GetPlantsPowersData> getPlantsPowers(GetPlantsPowersReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetPlantsPowers";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<GetPlantsPowersData>>() {
        });
    }

    /**
     * 批量获取电站日月年发电量,当前接口主要用于指定日期的查询。,
     */
    public GoodweRet<GetPlantsStaticDatasData> getPlantsStaticDatas(GetPlantsStaticDatasReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetPlantsStaticDatas";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<GetPlantsStaticDatasData>>() {
        });
    }

    /**
     * 批量获取电站日期范围内日月年发电量,当前接口主要用于时间段数据查询。,时间范围不能超过一个月
     */
    public GoodweRet<GetPlantsStaticDatasData> getPlantsStaticDatas(GetPlantsStaticDatasReqByTime req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetPlantsStaticDatas";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<GetPlantsStaticDatasData>>() {
        });
    }

    /**
     * 电站设备列表
     */
    public GoodweRet<QueryInventersData> queryInventers(QueryInventersReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/QueryInventers";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<QueryInventersData>>() {
        });
    }

    /**
     * 获取当前用户组织自有电站的告警信息,最多查询 60 天内的告警信息
     */
    public GoodweRet<GetPowerStationWariningInfoByMultiConditionData> getPowerStationWariningInfoByMultiCondition(GetPowerStationWariningInfoByMultiConditionReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetPowerStationWariningInfoByMultiCondition";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<GetPowerStationWariningInfoByMultiConditionData>>() {
        });
    }

    /**
     * 根据指定逆变器序列号集合获取逆变器实时数据,该接口允许最大逆变器数量为 50.
     * 注意:批量获取不支持多线程并发获取.
     */
    public GoodweRet<GetInventersDatasData> getInventersDatas(GetInventersDatasReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetInventersDatas?";
        List<String> sns = req.getSns();
        List<String> params = new ArrayList<>();
        for (String sn : sns) {
            params.add("sns[]=" + sn);
        }
        url += StringUtils.join(params, "&");
        return goodweApiRestTemplate.get(req.getIndex(),url, new HashMap<>(), new ParameterizedTypeReference<GoodweRet<GetInventersDatasData>>() {
        });
    }

    /**
     * 根据 sn 获取指定时间的发电量
     */
    public GoodweRet<List<GetInverterPowerR>> getInverterPower(GetInverterPowerReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetInverterPower";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<List<GetInverterPowerR>>>() {
        });
    }

    /**
     * 根据电站 sn 和选定类型获取电站在某个时间的曲线报表数据。
     */
    public GoodweRet<GetInverterDataByColumnResp> getInverterDataByColumn(GetInverterDataByColumnReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetInverterDataByColumn";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<GetInverterDataByColumnResp>>() {
        });
    }

    /**
     * 根据设备 sn 获取某天的功率曲线。
     */
    public GoodweRet<List<GetInverterPacByDayR>> getInverterPacByDay(GetInverterPacByDayReq req) {
        String url = "http://openapi.sems.com.cn/api/OpenApi/GetInverterPacByDay";
        return goodweApiRestTemplate.postJson(req.getIndex(),url, req, new ParameterizedTypeReference<GoodweRet<List<GetInverterPacByDayR>>>() {
        });
    }
}
