package cn.wise.sc.energy.power.plant.business.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.wise.sc.energy.power.plant.business.bean.DataPower;
import cn.wise.sc.energy.power.plant.business.domain.DeviceInfo;
import cn.wise.sc.energy.power.plant.business.domain.PlantInfo;
import cn.wise.sc.energy.power.plant.business.domain.vo.CharacterParamInfoVo;
import cn.wise.sc.energy.power.plant.business.opentsdb.OpentsdbOkHttpClient;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.QueryExt;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.QueryRequestExt;
import cn.wise.sc.energy.power.plant.business.repository.DeviceInfoRepository;
import cn.wise.sc.energy.power.plant.business.service.ICharacterParamService;
import cn.wise.sc.energy.power.plant.business.service.IPlantInfoService;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import cn.wise.sc.energy.power.plant.common.core.exception.ResponseEnum;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import net.opentsdb.client.api.query.response.QueryResponse;
import net.opentsdb.client.bean.Aggregator;
import net.opentsdb.client.bean.QueryResult;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @description: 电厂信息服务层实现
 * @author: qh
 * @create: 2020-07-14 16:38
 **/
@Service
public class PlantInfoServiceImpl extends BaseServiceImpl<String, PlantInfo> implements IPlantInfoService {

    final
    DeviceInfoRepository deviceInfoRepository;
    final
    ICharacterParamService iCharacterParamService;

    public PlantInfoServiceImpl(DeviceInfoRepository deviceInfoRepository,
                                ICharacterParamService iCharacterParamService) {
        this.deviceInfoRepository = deviceInfoRepository;
        this.iCharacterParamService = iCharacterParamService;
    }

    @Override
    public BaseResponse<Double> getTotalPower(String plantId) {

        List<DeviceInfo> deviceInfos = deviceInfoRepository.findAll((root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("plantId"), plantId));
        if (deviceInfos.size() == 0) {
            return BaseResponse.okData(10D);
        } else {
            //设备不为空
            double totalPower = 0D;
            for (DeviceInfo deviceInfo : deviceInfos) {
                ResponseEnum.LICENCE_NOT_FOUND.assertNotEmpty(deviceInfo.getDeviceParam());
                DeviceParam deviceParam = JSON.parseObject(deviceInfo.getDeviceParam(), DeviceParam.class);
                ResponseEnum.LICENCE_NOT_FOUND.assertNotEmpty(deviceParam.getRatepower());
                double power = Double.parseDouble(deviceParam.getRatepower().replace("MW", ""));
                totalPower += power;
            }
            return BaseResponse.okData(totalPower);
        }
    }

    @Override
    public DataPower getInitPower(String plantId, String deviceId) {

        ResponseEnum.LICENCE_NOT_FOUND.assertNotEmpty(plantId);

        if (StrUtil.isBlank(deviceId)){
            deviceId = "";
        }

        List<String> cpName = new ArrayList<>(1);
        cpName.add("燃机发电机有功功率");
        List<CharacterParamInfoVo> characterParamInfoVos = iCharacterParamService
                .getCharacterByName(cpName, plantId, deviceId);

        QueryRequestExt queryRequestExt = QueryRequestExt.builder()
                .start("1597456800000")
                .end("1597460400000")
                .build();

        for (CharacterParamInfoVo characterParamInfo : characterParamInfoVos) {
            QueryExt build = QueryExt.builder()
                    .aggregator(Aggregator.AVG)
                    .downsample("1m-max-zero")
                    .metric(characterParamInfo.getKksCode())
                    .build();
            queryRequestExt.addQuery(build);
        }
        QueryResponse response;
        try {
            response = OpentsdbOkHttpClient.query(queryRequestExt);
            if (response.getResults().size() == 0){
                return null;
            }
        } catch (IOException | URISyntaxException e) {
            return null;
        }

        return buildDataPower(characterParamInfoVos, response);
    }

    private DataPower buildDataPower(List<CharacterParamInfoVo> characterParamInfos,
                                     QueryResponse response) {

        DataPower rts = new DataPower();
        List<Object> times = rts.getXAxis();
        List<QueryResult> results = response.getResults();
        //添加时间轴
        if (results.size() > 0) {
            times.addAll(results.get(0).getDps().keySet());
        }
        //关联kksCode和名字
        for (QueryResult queryResult : results) {
            Optional<CharacterParamInfoVo> first = characterParamInfos.stream()
                    .filter(arg -> arg.getKksCode().equals(queryResult.getMetric()))
                    .findFirst();
            if (!first.isPresent()) {
                continue;
            }
            String cpName = first.get().getCpName();
            DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
            powerPoints.setName(cpName);
            queryResult.getDps().keySet().forEach(arg -> {
                Number number = queryResult.getDps().get(arg);
                powerPoints.getValue().add(number + "");
            });
            rts.getDataList().add(powerPoints);
        }
        return rts;
    }


    @Data
    public static class DeviceParam {

        private String ratepower;

        private String raterpm;
    }
}
