package com.ho.business.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.*;
import com.ho.business.feignclient.FlowFeignClient;
import com.ho.business.feignclient.TdFeignClient;
import com.ho.business.mapper.DeviceTypeColMapper;
import com.ho.business.service.*;
import com.ho.business.vo.DeviceTransfer;
import com.ho.business.vo.InverterCurveReqVo;
import com.ho.business.vo.req.DeviceAttrbuteReqVO;
import com.ho.business.vo.req.MonitorQuery;
import com.ho.business.vo.req.TopologyReqVo;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.business.vo.resp.InverterResp.*;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.BigDecimalUtil;
import com.ho.common.tools.util.RedisCommon;
import com.ho.datacollect.api.vo.req.StatisticsRespVO;
import com.ho.datacollect.api.vo.req.add.TdBase;
import com.ho.datacollect.api.vo.req.inverter.InverterCurveListVo;
import com.ho.flow.vo.req.AlarmConfig.AlarmConfigQueryVo;
import com.ho.td.api.entity.TdDeviceIdEntity;
import com.ho.td.api.entity.inverter.InverterTimingGenerate;
import com.ho.td.api.entity.query.DeviceIdAndTypeVo;
import com.ho.td.api.entity.query.TdBaseTimeQuery;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author gyan
 * @desc: 逆变器
 * @DateTime: 2023/1/6 9:25
 */
@Service
@Slf4j
public class InverterServiceImpl implements InverterService {
    @Autowired
    StationService stationService;

    @Autowired
    ModelDeviceService modelDeviceService;

    @Autowired
    DeviceTypeColMapper deviceTypeColMapper;

    @Autowired
    DeviceTypeConfigService deviceTypeConfigService;

    @Autowired
    RedisService redisService;

    @Autowired
    DeviceService deviceService;

    @Autowired
    RedisCommon redisUtil;

    @Autowired
    BigDecimalUtil bigDecimalUtil;

    @Autowired
    TdFeignClient feignClient;

    @Autowired
    DeviceTypeService deviceTypeService;

    @Autowired
    InverterMethodService inverterMethodService;

    @Autowired
    JoinTbSuffixService joinTbSuffixService;

    @Autowired
    PvStationService pvStationService;

    //查询时间间隔
    @Value("${inverter.timeInterval}")
    Integer timeInterval;

    //碳排放因子
    @Value("${inverter.co2Factor}")
    String co2Factor;

    @Value("${station.timeDifference}")
    Integer minute;

    @Autowired
    FlowFeignClient flowFeignClient;

    @Override
    public List<InverterTopologyRespVO> selectByStationId(Integer stationId) {
        //根据电站id查询电站信息
        Station station = stationService.selectById(stationId);
        if (station == null) {
            throw new BusinessException(BaseResponseCode.STATION_YC_NOT_EXISTS);
        }
        //创建返回对象
        List<InverterTopologyRespVO> list = new ArrayList<>();
        //查电站下逆变器数据
        List<DeviceRespVO> deviceRespVOS = pvStationService.getIntervalsByStationId(stationId);
        //判断返回的list是否空
        if (deviceRespVOS.isEmpty()) {
            return list;
        }
        //循环set所需要返回的值
        for (DeviceRespVO date : deviceRespVOS) {
            InverterTopologyRespVO vo = new InverterTopologyRespVO();
            //在进行逆变器类型判断
            Device device = deviceService.selectByStationIdAndSrcId(stationId, date.getSrcId());
            String deviceType = device.getDeviceType();
            vo.setName(date.getDeviceName());
            vo.setSrcId(date.getSrcId());
            vo.setDeviceType(deviceType);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<InverterPvValueRespVO> getPvDate(MonitorQuery monitorQuery) {
        //根据电站id查询电站信息
        Station station = stationService.selectById(monitorQuery.getStationId());
        if (station == null) {
            throw new BusinessException(BaseResponseCode.STATION_YC_NOT_EXISTS);
        }
        //创建返回对象
        List<InverterPvValueRespVO> list = new ArrayList<>();
        //在进行逆变器类型判断
        Device device = deviceService.selectByStationIdAndSrcId(monitorQuery.getStationId(), monitorQuery.getSrcId());
        String deviceType = device.getDeviceType();

        //拼接redis的键
        //先判断缓存中是否存在
        String inverterRedisKey = deviceType + ":" + monitorQuery.getStationId() + ":" + monitorQuery.getSrcId();
        //创建返回对象
        InverterPanelData data = new InverterPanelData();
        ModelNewInverter modelInverter = new ModelNewInverter();
        if (redisService.hasKey(inverterRedisKey)) {
            Map<Object, Object> inverterMap = redisService.hgetall(inverterRedisKey);
            if (inverterMap != null) {
                modelInverter = JSON.parseObject(JSON.toJSONString(inverterMap), ModelNewInverter.class);
            }
            if (deviceType.contains(DeviceTypeConstant.INVERTER_HW)) {
                //禾望
                List<InverterPvValueRespVO> hwList = inverterMethodService.getInverterHW(modelInverter);
                list.addAll(hwList);
            } else if (deviceType.contains(DeviceTypeConstant.INVERTER_YG)) {
                //阳光
                List<InverterPvValueRespVO> ygList = inverterMethodService.getInverterYG(modelInverter);
                list.addAll(ygList);
            } else if (deviceType.contains(DeviceTypeConstant.INVERTER_HUAWEI)) {
                //华为(20)
                List<InverterPvValueRespVO> huaWeiList = inverterMethodService.getInverterHuaWei(modelInverter);
                list.addAll(huaWeiList);
            } else if (deviceType.contains(DeviceTypeConstant.INVERTER_GDW)) {
                //固德威
                List<InverterPvValueRespVO> gdwList = inverterMethodService.getInverterGdw(modelInverter);
                list.addAll(gdwList);
            }
        } else {
            if (deviceType.contains(DeviceTypeConstant.INVERTER_HW)) {
                //禾望
                List<InverterPvValueRespVO> hwList = inverterMethodService.copeNotExistHw();
                list.addAll(hwList);
            } else if (deviceType.contains(DeviceTypeConstant.INVERTER_YG)) {
                //阳光
                List<InverterPvValueRespVO> ygList = inverterMethodService.copeNotExistYG();
                list.addAll(ygList);
            } else if (deviceType.contains(DeviceTypeConstant.INVERTER_HUAWEI)) {
                //华为
                List<InverterPvValueRespVO> huaWeiList = inverterMethodService.copeNotExistHuaWei();
                list.addAll(huaWeiList);

            } else if (deviceType.contains(DeviceTypeConstant.INVERTER_GDW)) {
                //固德威
                List<InverterPvValueRespVO> gdwList = inverterMethodService.copeNotExistGdw();
                list.addAll(gdwList);
            }
        }
        return list;
    }

    @Override
    public InverterPanelData getPanelData(MonitorQuery monitorQuery) {
        //根据电站id查询电站信息
        Station station = stationService.selectById(monitorQuery.getStationId());
        if (station == null) {
            throw new BusinessException(BaseResponseCode.STATION_YC_NOT_EXISTS);
        }
        //在进行逆变器类型判断
        Device device = deviceService.selectByStationIdAndSrcId(monitorQuery.getStationId(), monitorQuery.getSrcId());
        String deviceType = device.getDeviceType();
        //拼接redis的键
        //先判断缓存中是否存在
        String inverterRedisKey = deviceType + ":" + monitorQuery.getStationId() + ":" + monitorQuery.getSrcId();
        //创建返回对象
        InverterPanelData data = new InverterPanelData();
        ModelNewInverter modelInverter = new ModelNewInverter();
        if (redisService.hasKey(inverterRedisKey)) {
            Map<Object, Object> inverterMap = redisService.hgetall(inverterRedisKey);
            if (inverterMap != null) {
                modelInverter = JSON.parseObject(JSON.toJSONString(inverterMap), ModelNewInverter.class);
            }
            InverterPanelData hwData = getData(modelInverter, deviceType, monitorQuery);
            BeanUtils.copyProperties(hwData, data);

        } else {
            //缓存中不存在
            InverterPanelData panelData = (InverterPanelData) bigDecimalUtil.ifIsNUll(data);
            return panelData;
        }
        return data;
    }

    public InverterPanelData getData(ModelNewInverter modelInverter, String deviceType, MonitorQuery monitorQuery) {
        InverterPanelData data = new InverterPanelData();
        // A交流侧电压
        if (modelInverter.getVUa() == null || modelInverter.getVUa().getValue() == null) {
            data.setVUa(BigDecimal.ZERO);
        } else {
            data.setVUa(modelInverter.getVUa().getValue());
        }
        // B交流侧电压
        if (modelInverter.getVUb() == null || modelInverter.getVUb().getValue() == null) {
            data.setVUb(BigDecimal.ZERO);
        } else {
            data.setVUb(modelInverter.getVUb().getValue());
        }
        // C交流侧电压
        if (modelInverter.getVUc() == null || modelInverter.getVUc().getValue() == null) {
            data.setVUc(BigDecimal.ZERO);
        } else {
            data.setVUc(modelInverter.getVUc().getValue());
        }
        // A交流侧电流
        if (modelInverter.getCIa() == null || modelInverter.getCIa().getValue() == null) {
            data.setCIa(BigDecimal.ZERO);
        } else {
            data.setCIa(modelInverter.getCIa().getValue());
        }
        // B交流侧电流
        if (modelInverter.getCIb() == null || modelInverter.getCIb().getValue() == null) {
            data.setCIb(BigDecimal.ZERO);
        } else {
            data.setCIb(modelInverter.getCIb().getValue());
        }
        // C交流侧电流
        if (modelInverter.getCIc() == null || modelInverter.getCIc().getValue() == null) {
            data.setCIc(BigDecimal.ZERO);
        } else {
            data.setCIc(modelInverter.getCIc().getValue());
        }
        // 故障状态
        //1 超过更新 时间和当前时间比较 超过5 认为时故障
        Date updateTime = modelInverter.getUpdateTime();
        String format = DateUtil.format(modelInverter.getUpdateTime(), CommonConstant.DATE);
        if (updateTime != null) {
            long between = DateUtil.between(updateTime, new Date(), DateUnit.MINUTE);
            if (between > minute) {
                data.setCommunicationStatus(CommonConstant.ZERO);
            } else {
                AlarmConfigQueryVo alarmConfigQueryVo = new AlarmConfigQueryVo();
                alarmConfigQueryVo.setStationId(monitorQuery.getStationId());
                alarmConfigQueryVo.setSrcId(monitorQuery.getSrcId());
                alarmConfigQueryVo.setDeviceType(deviceType);
                Boolean isRecovery = flowFeignClient.isRecoveryDevice(alarmConfigQueryVo);
                data.setCommunicationStatus(CommonConstant.ZERO);
                if (isRecovery) {
                    data.setCommunicationStatus(CommonConstant.ONE);
                }
                //故障状态为空或者 状态是正常
//                if (modelInverter.getCommunicationStatus() == null || CommonConstant.ONE.equals(modelInverter.getCommunicationStatus())) {
//                    data.setCommunicationStatus(CommonConstant.ONE);
//                } else {
//                    data.setCommunicationStatus(modelInverter.getCommunicationStatus());
//                }
            }

        }
        // 当日发电量
        if (modelInverter.getDailyPowerGeneration() == null || modelInverter.getDailyPowerGeneration().getValue() == null) {
            data.setDailyPowerGeneration(BigDecimal.ZERO);
        } else {
            data.setDailyPowerGeneration(modelInverter.getDailyPowerGeneration().getValue());
        }
        //累计发电量
        if (modelInverter.getCumulativePowerGeneration() == null || modelInverter.getCumulativePowerGeneration().getValue() == null) {
            data.setCumulativePowerGeneration(BigDecimal.ZERO);
        } else {
            data.setCumulativePowerGeneration(modelInverter.getCumulativePowerGeneration().getValue());
        }
        //二氧化碳减排量
        //if (!DeviceTypeConstant.INVERTER_HW.equals(deviceType)) {
        if (modelInverter.getCumulativePowerGeneration() == null || modelInverter.getCumulativePowerGeneration().getValue() == null) {
            data.setCo2Reductions(BigDecimal.ZERO);
        } else {
            BigDecimal decimal = modelInverter.getCumulativePowerGeneration().getValue();
            BigDecimal Co2multiply = decimal.multiply(new BigDecimal(co2Factor));
            data.setCo2Reductions(Co2multiply);
        }
        //} else {
        // if (modelInverter.getCo2Reductions() == null) {
        //   data.setCo2Reductions(BigDecimal.ZERO);
        // } else {
        //     data.setCo2Reductions(modelInverter.getCo2Reductions());
        // }
        // }

        //有功功率
        if (modelInverter.getAcActivePower() == null || modelInverter.getAcActivePower().getValue() == null) {
            data.setAcActivePower(BigDecimal.ZERO);
        } else {
            data.setAcActivePower(modelInverter.getAcActivePower().getValue());
        }
        //无功功率
        if (modelInverter.getAcReactivePower() == null || modelInverter.getAcReactivePower().getValue() == null) {
            data.setAcReactivePower(BigDecimal.ZERO);
        } else {
            data.setAcReactivePower(modelInverter.getAcReactivePower().getValue());
        }

        //功率因数
        if (modelInverter.getPowerFactor() == null || modelInverter.getPowerFactor().getValue() == null) {
            data.setPowerFactor(BigDecimal.ZERO);
        } else {
            data.setPowerFactor(modelInverter.getPowerFactor().getValue());
        }
        //机箱温度
        if (modelInverter.getMachineTemperature() == null || modelInverter.getMachineTemperature().getValue() == null) {
            data.setMachineTemperature(BigDecimal.ZERO);
        } else {
            data.setMachineTemperature(modelInverter.getMachineTemperature().getValue());
        }
     /*   //逆变器效率
        if (modelInverter.getInverterEfficiency() == null || modelInverter.getInverterEfficiency().getValue() == null) {
            data.setInverterEfficiency(BigDecimal.ZERO);
        } else {
            data.setInverterEfficiency(modelInverter.getInverterEfficiency().getValue());
        }*/
        //总直流输出功率
        if (modelInverter.getTotalOutputPower() == null || modelInverter.getTotalOutputPower().getValue() == null) {
            data.setTotalOutputPower(BigDecimal.ZERO);
        } else {
            data.setTotalOutputPower(modelInverter.getTotalOutputPower().getValue());
        }
        //年发
        if (modelInverter.getYearEnergyOutput() == null || modelInverter.getYearEnergyOutput().getValue() == null) {
            data.setYearEnergyOutput(BigDecimal.ZERO);
        } else {
            data.setYearEnergyOutput(modelInverter.getYearEnergyOutput().getValue());
        }
        //电网频率
        if (modelInverter.getGridFreq() == null || modelInverter.getGridFreq().getValue() == null) {
            data.setGridFreq(BigDecimal.ZERO);
        } else {
            data.setGridFreq(modelInverter.getGridFreq().getValue());
        }

        return data;
    }

    @Override
    public List<InverterCurveRespVo> getCurve(InverterCurveReqVo vo) {
        //先判断电站是否存在
        Station station = stationService.selectById(vo.getStationId());
        if (station == null) {
            return new ArrayList<>();
        }
        String beginTime = null;
        String endTime = null;
        String format = DateUtil.format(DateUtil.beginOfDay(new Date()), CommonConstant.DATE).substring(0, 10);
        if (vo.getTime() == null) {
            //开始时间 当天时间（零点）
            beginTime = DateUtil.format(DateUtil.beginOfDay(new Date()), CommonConstant.DATE);
            //结束时间 现在
            endTime = DateUtil.format(new Date(), CommonConstant.DATE);
        } else if (format.equals(vo.getTime())) {
            beginTime = vo.getTime() + CommonConstant.ZERO_SECOND_MINUTE_HOUR;
            endTime = DateUtil.format(new Date(), CommonConstant.DATE);
        } else {
            beginTime = vo.getTime() + CommonConstant.ZERO_SECOND_MINUTE_HOUR;
            endTime = vo.getTime() + CommonConstant.END_SUFFIX_TIMESTAMP;
        }
        //开始时间和结束时间转换为Date
        DateTime beginDate = DateUtil.parse(beginTime, CommonConstant.DATE);
        DateTime endDate = DateUtil.parse(endTime, CommonConstant.DATE);

        //通过size拿到循环的个数
        List<DateTime> dateTimes = DateUtil.rangeToList(DateUtil.beginOfDay(beginDate), endDate, DateField.MINUTE, 5);
        int endHours = dateTimes.size();

        //创建返回list
        List<InverterCurveRespVo> list = new ArrayList<>();
        //查询时间
        List<DateTime> dateTimeList = DateUtil.rangeToList(DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()), DateField.MINUTE, 5);
        List<String> oneHourRedisKey = dateTimeList.stream().map(s -> {
            return DateUtil.format(s, CommonConstant.DATE);
        }).collect(Collectors.toList());
        //判断入参的时间是否大于当前时间
        boolean isBefore = beginDate.before(new Date());
        if (vo.getSrcId() == null || !isBefore) {
            for (int i = 0; i < endHours; i++) {
                String timeIndex = oneHourRedisKey.get(i);
                InverterCurveRespVo curve = new InverterCurveRespVo();
                String dateString = timeIndex + "";
                String substring = dateString.substring(11, 16);
                curve.setDate(substring);
                curve.setCurrentA(BigDecimal.ZERO);
                curve.setCurrentB(BigDecimal.ZERO);
                curve.setCurrentC(BigDecimal.ZERO);
                curve.setVoltageA(BigDecimal.ZERO);
                curve.setVoltageB(BigDecimal.ZERO);
                curve.setVoltageC(BigDecimal.ZERO);
                curve.setActivePower(BigDecimal.ZERO);
                list.add(curve);
            }
            return list;
        }
        //在进行逆变器类型判断
        Device device = deviceService.selectByStationIdAndSrcId(vo.getStationId(), vo.getSrcId());
        String deviceType = device.getDeviceType();
        //查询逆变器的topology数据
        //封装topology查询条件
        TopologyReqVo topologyReqVo = new TopologyReqVo();
        topologyReqVo.setStationId(vo.getStationId());
        topologyReqVo.setSrcId(vo.getSrcId());
        //根据电站到topology表中查询
        Device topologyDevice = deviceService.selectByStationIdAndSrcId(vo.getStationId(), vo.getSrcId());
        //判断返回的list是否空
        if (topologyDevice == null) {
            return new ArrayList<>();
        }
        //组表名
        TdBase tdAddBase = TdBase.builder()
                .groupId(station.getGroupId())
                .stationId(station.getId())
                .srcId(vo.getSrcId())
                .build();
        //拼接表名后缀
        String tdStackSuffix = joinTbSuffixService.getTdInverterSuffix(tdAddBase);
        tdAddBase.setTableName(tdStackSuffix);
        tdAddBase.setEndTime(endTime);
        tdAddBase.setBeginTime(beginTime);
        //判断类型
        //电流
        Date firstTime = null;
        if (CommonConstant.InverterQueryType.ELEC.equals(vo.getType())) {
            //tdAddBase.setColName("cIa");
            DataResult<InverterCurveListVo> cIaDataResult = new DataResult<>();
            tdAddBase.setDeviceType(deviceType);
            List<DeviceTypeConfig> deviceTypeConfigs = deviceTypeConfigService.selectByType(deviceType, station.getId());
            if (!deviceTypeConfigs.isEmpty()) {
                DeviceTypeConfig deviceTypeConfig = deviceTypeConfigs.get(0);
                tdAddBase.setModelName(deviceTypeConfig.getModelName());

            }
            List<ModelDeviceColComp> cIaCompList = modelDeviceService.getCompListByType("device", deviceType, "cIa");
            if (!cIaCompList.isEmpty()) {
                ModelDeviceColComp cIaModelDeviceCol = cIaCompList.get(0);
                tdAddBase.setColName(cIaModelDeviceCol.getDeviceCol());
                cIaDataResult = feignClient.queryInterval(tdAddBase);
            }

            //tdAddBase.setColName("cIb");
            DataResult<InverterCurveListVo> cIbDataResult = new DataResult<>();
            List<ModelDeviceColComp> cIbCompList = modelDeviceService.getCompListByType("device", deviceType, "cIb");
            if (!cIbCompList.isEmpty()) {
                ModelDeviceColComp cIbModelDeviceCol = cIbCompList.get(0);
                tdAddBase.setColName(cIbModelDeviceCol.getDeviceCol());
                cIbDataResult = feignClient.queryInterval(tdAddBase);
            }

            //tdAddBase.setColName("cIc");
            DataResult<InverterCurveListVo> cIcDataResult = new DataResult<>();
            List<ModelDeviceColComp> cIcCompList = modelDeviceService.getCompListByType("device", deviceType, "cIc");
            if (!cIcCompList.isEmpty()) {
                ModelDeviceColComp cIcModelDeviceCol = cIcCompList.get(0);
                tdAddBase.setColName(cIcModelDeviceCol.getDeviceCol());
                cIcDataResult = feignClient.queryInterval(tdAddBase);
                firstTime = cIcDataResult.getData().getFirstTime();
            }
            // String format = DateUtil.format(firstTime, CommonConstant.DATE);
            if (!cIaDataResult.isSuccess() || cIaDataResult.getData() == null
                    || cIcDataResult.getData() == null || cIbDataResult.getData() == null) {
                for (int i = 0; i < endHours; i++) {
                    String timeIndex = oneHourRedisKey.get(i);
                    InverterCurveRespVo curve = new InverterCurveRespVo();
                    String dateString = timeIndex + "";
                    String substring = dateString.substring(11, 16);
                    curve.setDate(substring);
                    curve.setCurrentA(BigDecimal.ZERO);
                    curve.setCurrentB(BigDecimal.ZERO);
                    curve.setCurrentC(BigDecimal.ZERO);
                    list.add(curve);
                }
            } else if (!cIaDataResult.isSuccess() || cIaDataResult.getData().getStatistics() == null
                    || cIcDataResult.getData().getStatistics() == null || cIbDataResult.getData().getStatistics() == null
                    || firstTime == null) {
                for (int i = 0; i < endHours; i++) {
                    String timeIndex = oneHourRedisKey.get(i);
                    InverterCurveRespVo curve = new InverterCurveRespVo();
                    String dateString = timeIndex + "";
                    String substring = dateString.substring(11, 16);
                    curve.setDate(substring);
                    curve.setCurrentA(BigDecimal.ZERO);
                    curve.setCurrentB(BigDecimal.ZERO);
                    curve.setCurrentC(BigDecimal.ZERO);
                    list.add(curve);
                }

            } else {
                if (CommonConstant.ONE.equals(cIaDataResult.getData().getStatistics().size())) {
                    for (int i = 0; i < endHours; i++) {
                        String timeIndex = oneHourRedisKey.get(i);
                        InverterCurveRespVo curve = new InverterCurveRespVo();
                        String dateString = timeIndex + "";
                        String substring = dateString.substring(11, 16);
                        curve.setDate(substring);
                        //如果时间小于第一条时序记录的时间,这个值就塞0
                        DateTime theTime = DateUtil.parse(timeIndex, CommonConstant.DATE);
                        if (DateUtil.compare(theTime, firstTime) < 0) {
                            curve.setCurrentA(BigDecimal.ZERO);
                        } else {
                            curve.setCurrentA(cIaDataResult.getData().getStatistics().get(0).getDigital());
                        }
                        list.add(curve);
                    }
                    if (CommonConstant.ONE.equals(cIbDataResult.getData().getStatistics().size())) {
                        for (int i = 0; i < endHours; i++) {
                            for (InverterCurveRespVo curveRespVo : list) {
                                //判断CurrentA 是什么值，进行填充
                                if (curveRespVo.getCurrentA().equals(BigDecimal.ZERO)) {
                                    curveRespVo.setCurrentB(curveRespVo.getCurrentA());
                                } else {
                                    curveRespVo.setCurrentB(cIbDataResult.getData().getStatistics().get(0).getDigital());
                                }
                            }
                        }
                    }
                    if (CommonConstant.ONE.equals(cIcDataResult.getData().getStatistics().size())) {
                        for (int i = 0; i < endHours; i++) {
                            for (InverterCurveRespVo curveRespVo : list) {
                                if (curveRespVo.getCurrentA().equals(BigDecimal.ZERO)) {
                                    curveRespVo.setCurrentC(curveRespVo.getCurrentA());
                                } else {
                                    curveRespVo.setCurrentC(cIcDataResult.getData().getStatistics().get(0).getDigital());
                                }

                            }
                        }
                    }
                } else {
                    //取三条数据的date
                    List<StatisticsRespVO> cIaList = cIaDataResult.getData().getStatistics();
                    List<StatisticsRespVO> cIbList = cIbDataResult.getData().getStatistics();
                    List<StatisticsRespVO> cIcList = cIcDataResult.getData().getStatistics();
                    //用cIaList的size进行遍历，原则上三条list的size都一样
                    for (int i = 0; i < cIaList.size(); i++) {
                        String timeIndex = oneHourRedisKey.get(i);
                        StatisticsRespVO cIa = cIaList.get(i);
                        StatisticsRespVO cIb = cIbList.get(i);
                        StatisticsRespVO cIc = cIcList.get(i);
                        InverterCurveRespVo invert = new InverterCurveRespVo();
                        String dateString = timeIndex + "";
                        String substring = dateString.substring(11, 16);
                        String sub = dateString.substring(11, 13);
                        invert.setDate(substring);
                        String tdSubstring = DateUtil.format(firstTime, CommonConstant.DATE).substring(11, 13);
                        //如果时间小于第一条时序记录的时间, 并且不在第一条数据的时间段（小时） 这个值就塞0
                        DateTime theTime = DateUtil.parse(timeIndex, CommonConstant.DATE);
                        if (DateUtil.compare(theTime, firstTime) < 0 && !sub.equals(tdSubstring)) {
                            invert.setCurrentA(BigDecimal.ZERO);
                            invert.setCurrentB(BigDecimal.ZERO);
                            invert.setCurrentC(BigDecimal.ZERO);
                        } else {
                            if (cIa != null) {
                                invert.setCurrentA(cIa.getDigital());

                            } else {
                                invert.setCurrentA(new BigDecimal(0));
                            }
                            if (cIb != null) {
                                invert.setCurrentB(cIb.getDigital());

                            } else {
                                invert.setCurrentB(new BigDecimal(0));
                            }
                            if (cIc != null) {
                                invert.setCurrentC(cIc.getDigital());
                            } else {
                                invert.setCurrentC(new BigDecimal(0));
                            }
                        }
                        list.add(invert);
                    }
                }
            }
        } else if (CommonConstant.InverterQueryType.VOL.equals(vo.getType())) {
            tdAddBase.setDeviceType(deviceType);
            //tdAddBase.setColName("vUa");
            List<DeviceTypeConfig> deviceTypeConfigs = deviceTypeConfigService.selectByType(deviceType, station.getId());
            if (!deviceTypeConfigs.isEmpty()) {
                DeviceTypeConfig deviceTypeConfig = deviceTypeConfigs.get(0);
                tdAddBase.setModelName(deviceTypeConfig.getModelName());

            }
            List<ModelDeviceColComp> vUaCompList = modelDeviceService.getCompListByType("device", deviceType, "vUa");
            DataResult<InverterCurveListVo> vUaDataResult = new DataResult<>();
            if (!vUaCompList.isEmpty()) {
                ModelDeviceColComp vUaModelDeviceCol = vUaCompList.get(0);
                tdAddBase.setColName(vUaModelDeviceCol.getDeviceCol());
                vUaDataResult = feignClient.queryInterval(tdAddBase);
            }

            //tdAddBase.setColName("vUb");
            List<ModelDeviceColComp> vUbCompList = modelDeviceService.getCompListByType("device", deviceType, "vUb");
            DataResult<InverterCurveListVo> vUbDataResult = new DataResult<>();
            if (!vUaCompList.isEmpty()) {
                ModelDeviceColComp vUbModelDeviceCol = vUbCompList.get(0);
                tdAddBase.setColName(vUbModelDeviceCol.getDeviceCol());
                vUbDataResult = feignClient.queryInterval(tdAddBase);
            }
            //tdAddBase.setColName("vUc");
            List<ModelDeviceColComp> vUcCompList = modelDeviceService.getCompListByType("device", deviceType, "vUc");
            DataResult<InverterCurveListVo> vUcDataResult = new DataResult<>();
            if (!vUaCompList.isEmpty()) {
                ModelDeviceColComp vUcModelDeviceCol = vUcCompList.get(0);
                tdAddBase.setColName(vUcModelDeviceCol.getDeviceCol());
                vUcDataResult = feignClient.queryInterval(tdAddBase);
                firstTime = vUcDataResult.getData().getFirstTime();
            }
            if (!vUaDataResult.isSuccess() || vUbDataResult.getData() == null
                    || vUaDataResult.getData() == null || vUcDataResult.getData() == null) {
                for (int i = 0; i < endHours; i++) {
                    String timeIndex = oneHourRedisKey.get(i);
                    InverterCurveRespVo curve = new InverterCurveRespVo();
                    String dateString = timeIndex + "";
                    String substring = dateString.substring(11, 16);
                    curve.setDate(substring);
                    curve.setVoltageA(BigDecimal.ZERO);
                    curve.setVoltageB(BigDecimal.ZERO);
                    curve.setVoltageC(BigDecimal.ZERO);
                    list.add(curve);
                }
            } else if
            (!vUaDataResult.isSuccess() || vUbDataResult.getData().getStatistics() == null
                            || vUaDataResult.getData().getStatistics() == null || vUcDataResult.getData().getStatistics() == null
                            || firstTime == null) {
                for (int i = 0; i < endHours; i++) {
                    String timeIndex = oneHourRedisKey.get(i);
                    InverterCurveRespVo curve = new InverterCurveRespVo();
                    String dateString = timeIndex + "";
                    String substring = dateString.substring(11, 16);
                    curve.setDate(substring);
                    curve.setVoltageA(BigDecimal.ZERO);
                    curve.setVoltageB(BigDecimal.ZERO);
                    curve.setVoltageC(BigDecimal.ZERO);
                    list.add(curve);
                }
            } else {
                if (CommonConstant.ONE.equals(vUaDataResult.getData().getStatistics().size())) {
                    for (int i = 0; i < endHours; i++) {
                        String timeIndex = oneHourRedisKey.get(i);
                        InverterCurveRespVo curve = new InverterCurveRespVo();
                        String dateString = timeIndex + "";
                        String substring = dateString.substring(11, 16);
                        curve.setDate(substring);
                        //如果时间小于第一条时序记录的时间,这个值就塞0
                        DateTime theTime = DateUtil.parse(timeIndex, CommonConstant.DATE);
                        if (DateUtil.compare(theTime, firstTime) < 0) {
                            curve.setVoltageA(BigDecimal.ZERO);
                        } else {
                            curve.setVoltageA(vUaDataResult.getData().getStatistics().get(0).getDigital());
                        }
                        list.add(curve);
                    }
                    if (CommonConstant.ONE.equals(vUbDataResult.getData().getStatistics().size())) {
                        for (int i = 0; i < endHours; i++) {
                            for (InverterCurveRespVo curveRespVo : list) {
                                if (curveRespVo.getVoltageA().equals(BigDecimal.ZERO)) {
                                    curveRespVo.setVoltageB(curveRespVo.getVoltageA());
                                }
                                curveRespVo.setVoltageB(vUbDataResult.getData().getStatistics().get(0).getDigital());
                            }
                        }
                    }
                    if (CommonConstant.ONE.equals(vUcDataResult.getData().getStatistics().size())) {
                        for (int i = 0; i < endHours; i++) {
                            for (InverterCurveRespVo curveRespVo : list) {
                                if (curveRespVo.getVoltageA().equals(BigDecimal.ZERO)) {
                                    curveRespVo.setVoltageC(curveRespVo.getVoltageA());
                                } else {
                                    curveRespVo.setVoltageC(vUcDataResult.getData().getStatistics().get(0).getDigital());
                                }

                            }
                        }
                    }
                } else {
                    //取三条数据的date
                    List<StatisticsRespVO> vUaList = vUaDataResult.getData().getStatistics();
                    List<StatisticsRespVO> vUbList = vUbDataResult.getData().getStatistics();
                    List<StatisticsRespVO> vUcList = vUcDataResult.getData().getStatistics();
                    //用cIaList的size进行遍历，原则上三条list的size都一样
                    for (int i = 0; i < vUaList.size(); i++) {
                        String timeIndex = oneHourRedisKey.get(i);
                        StatisticsRespVO vUa = vUaList.get(i);
                        StatisticsRespVO vUb = vUbList.get(i);
                        StatisticsRespVO vUc = vUcList.get(i);
                        InverterCurveRespVo invert = new InverterCurveRespVo();
                        //如果时间小于第一条时序记录的时间, 并且不在第一条数据的时间段（小时） 这个值就塞0
                        DateTime theTime = DateUtil.parse(timeIndex, CommonConstant.DATE);
                        String dateString = timeIndex + "";
                        String substring = dateString.substring(11, 16);
                        invert.setDate(substring);
                        String sub = dateString.substring(11, 13);
                        invert.setDate(substring);
                        String tdSubstring = DateUtil.format(firstTime, CommonConstant.DATE).substring(11, 13);
                        //如果时间小于第一条时序记录的时间,这个值就塞0
                        if (DateUtil.compare(theTime, firstTime) < 0 && !sub.equals(tdSubstring)) {
                            invert.setVoltageA(BigDecimal.ZERO);
                            invert.setVoltageB(BigDecimal.ZERO);
                            invert.setVoltageC(BigDecimal.ZERO);
                        } else {
                            if (vUa != null) {
                                invert.setVoltageA(vUa.getDigital());
                            } else {
                                invert.setVoltageA(new BigDecimal(0));
                            }
                            if (vUb != null) {
                                invert.setVoltageB(vUb.getDigital());

                            } else {
                                invert.setVoltageB(new BigDecimal(0));
                            }
                            if (vUc != null) {
                                invert.setVoltageC(vUc.getDigital());
                            } else {
                                invert.setVoltageC(new BigDecimal(0));
                            }
                        }
                        list.add(invert);
                    }
                }
            }

        } else if (CommonConstant.InverterQueryType.POWER.equals(vo.getType())) {
            //tdAddBase.setColName("acActivePower");
            tdAddBase.setDeviceType(deviceType);
            List<ModelDeviceColComp> acActivePowerList = modelDeviceService.getCompListByType("device", deviceType, "acActivePower");
            List<DeviceTypeConfig> deviceTypeConfigs = deviceTypeConfigService.selectByType(deviceType, station.getId());
            if (!deviceTypeConfigs.isEmpty()) {
                DeviceTypeConfig deviceTypeConfig = deviceTypeConfigs.get(0);
                tdAddBase.setModelName(deviceTypeConfig.getModelName());

            }
            //当日
            DataResult<InverterCurveListVo> powerDataResult = new DataResult<>();
            //昨日
            DataResult<InverterCurveListVo> lastPowerDataResult = new DataResult<>();
            if (!acActivePowerList.isEmpty()) {
                ModelDeviceColComp acActivePowerDeviceCol = acActivePowerList.get(0);
                tdAddBase.setColName(acActivePowerDeviceCol.getDeviceCol());
                powerDataResult = feignClient.queryInterval(tdAddBase);
            }
            //加一个昨日的曲线

            Calendar c = Calendar.getInstance();
            Date dateTime = null;
            try {
                dateTime = new SimpleDateFormat(CommonConstant.DATE).parse(beginTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            c.setTime(dateTime);
            int day = c.get(Calendar.DATE);
            c.set(Calendar.DATE, day - 1);

            String dayBefore = new SimpleDateFormat(CommonConstant.DATE).format(c
                    .getTime());

            DateTime yesterday = DateUtil.parse(dayBefore, CommonConstant.DATE);
            String yesterdayBeginTime = DateUtil.format(DateUtil.beginOfDay(yesterday), CommonConstant.DATE);
            String yesterdayEndTime = DateUtil.format(DateUtil.endOfDay(yesterday), CommonConstant.DATE);
            tdAddBase.setBeginTime(yesterdayBeginTime);
            tdAddBase.setEndTime(yesterdayEndTime);
            lastPowerDataResult = feignClient.queryInterval(tdAddBase);
            firstTime = powerDataResult.getData().getFirstTime();
            if (!format.equals(vo.getTime())) { //判断所查询的时间是否为今天的时间；如果不是今天的实时数据要返回一整天
                //取数据的date
                List<StatisticsRespVO> powerList = powerDataResult.getData().getStatistics();
                List<StatisticsRespVO> lastPowerList = lastPowerDataResult.getData().getStatistics();
                int pSize = 0;
                if (lastPowerList == null) {
                    pSize = 288;
                } else {
                    pSize = lastPowerList.size();
                }

                //用cIaList的size进行遍历，原则上三条list的size都一样
                for (int i = 0; i < pSize; i++) {
                    StatisticsRespVO power = new StatisticsRespVO();
                    if (powerList == null || i >= powerList.size()) {
                        power = new StatisticsRespVO();
                    } else {
                        power = powerList.get(i);
                    }
                    StatisticsRespVO lastPower = new StatisticsRespVO();
                    if (lastPowerList != null) {
                        lastPower = lastPowerList.get(i);
                    } else {
                        lastPower = new StatisticsRespVO();
                    }

                    String timeIndex = oneHourRedisKey.get(i);
                    InverterCurveRespVo invert = new InverterCurveRespVo();
                    //如果时间小于第一条时序记录的时间, 并且不在第一条数据的时间段（小时） 这个值就塞0
                    DateTime theTime = DateUtil.parse(timeIndex, CommonConstant.DATE);
                    String dateString = timeIndex + "";
                    String substring = dateString.substring(11, 16);
                    invert.setDate(substring);
                    if (power != null) {
                        invert.setActivePower(power.getDigital());
                    } else {
                        invert.setActivePower(new BigDecimal(0));
                    }
                    if (lastPower != null) {
                        invert.setPastActivePower(lastPower.getDigital());
                    } else {
                        invert.setActivePower(new BigDecimal(0));
                    }
                    list.add(invert);
                }
            } else {
                if (!powerDataResult.isSuccess() || powerDataResult.getData() == null
                        || lastPowerDataResult.getData() == null) {
                    for (int i = 0; i < oneHourRedisKey.size(); i++) {
                        String timeIndex = oneHourRedisKey.get(i);
                        InverterCurveRespVo curve = new InverterCurveRespVo();
                        String dateString = timeIndex + "";
                        String substring = dateString.substring(11, 16);
                        curve.setDate(substring);
                        curve.setActivePower(BigDecimal.ZERO);
                        curve.setPastActivePower(BigDecimal.ZERO);
                        list.add(curve);
                    }
                } else if (!powerDataResult.isSuccess() && !lastPowerDataResult.isSuccess() || powerDataResult.getData().getStatistics() == null
                        && lastPowerDataResult.getData().getStatistics() == null
                        || firstTime == null && lastPowerDataResult.getData().getFirstTime() == null) {
                    for (int i = 0; i < oneHourRedisKey.size(); i++) {
                        String timeIndex = oneHourRedisKey.get(i);
                        InverterCurveRespVo curve = new InverterCurveRespVo();
                        String dateString = timeIndex + "";
                        String substring = dateString.substring(11, 16);
                        curve.setDate(substring);
                        curve.setActivePower(BigDecimal.ZERO);
                        curve.setPastActivePower(BigDecimal.ZERO);
                        list.add(curve);
                    }
                } else if (powerDataResult.getData().getStatistics().size() == CommonConstant.ONE ||
                        lastPowerDataResult.getData().getStatistics() != null && lastPowerDataResult.getData().getStatistics().size() == CommonConstant.ONE) {
                    //取数据的date
                    List<StatisticsRespVO> powerList = powerDataResult.getData().getStatistics();
                    List<StatisticsRespVO> lastPowerList = lastPowerDataResult.getData().getStatistics();
                    //创建返回list
                    List<InverterCurveRespVo> newLastList = new ArrayList<>();
                    if (lastPowerList.size() == CommonConstant.ONE) {
                        for (int i = 0; i < oneHourRedisKey.size(); i++) {
                            InverterCurveRespVo invert = new InverterCurveRespVo();
                            if (lastPowerList.get(0).getDigital() != null) {
                                invert.setPastActivePower(lastPowerList.get(0).getDigital());
                            } else {
                                invert.setPastActivePower(new BigDecimal("0"));
                            }
                            invert.setDate(oneHourRedisKey.get(i));
                            newLastList.add(invert);
                        }
                    } else {
                        if (powerList.size() == CommonConstant.ONE) {
                            StatisticsRespVO statisticsRespVO = powerList.get(0);
                            for (int i = 0; i < oneHourRedisKey.size(); i++) {
                                if (statisticsRespVO != null) {
                                    InverterCurveRespVo invert = new InverterCurveRespVo();
                                    invert.setActivePower(statisticsRespVO.getDigital());
                                    invert.setDate(oneHourRedisKey.get(i));
                                    newLastList.add(invert);
                                }
                            }
                        }
                        for (int i = 0; i < oneHourRedisKey.size(); i++) {
                            InverterCurveRespVo curveRespVo = newLastList.get(i);
                            if (lastPowerList.get(i) != null && lastPowerList.get(i).getDigital() != null) {
                                curveRespVo.setPastActivePower(lastPowerList.get(i).getDigital());
                            } else {
                                curveRespVo.setPastActivePower(new BigDecimal("0"));
                            }
                        }

                    }
                    Date newDate = new Date();
                    //不管当天有几条数据，超过当前时间数据全部为0
                    for (int i = 0; i < newLastList.size(); i++) {
                        Date date = DateUtil.parse(newLastList.get(i).getDate(), CommonConstant.DATE);
                        if (date.before(newDate)) {
                            InverterCurveRespVo curveRespVo = newLastList.get(i);
                            curveRespVo.setActivePower(newLastList.get(i).getActivePower());
                        } else {
                            InverterCurveRespVo curveRespVo = newLastList.get(i);
                            curveRespVo.setActivePower(new BigDecimal("0"));
                        }
                    }
                    for (InverterCurveRespVo curveRespVo : newLastList) {
                        String substring = curveRespVo.getDate().substring(11, 16);
                        curveRespVo.setDate(substring);
                    }
                    list = newLastList;
                } else {
                    //取数据的date
                    List<StatisticsRespVO> powerList = powerDataResult.getData().getStatistics();
                    List<StatisticsRespVO> lastPowerList = lastPowerDataResult.getData().getStatistics();
                    int pSize = 0;
                    if (lastPowerList == null) {
                        pSize = 288;
                    } else {
                        pSize = lastPowerList.size();
                    }

                    //用cIaList的size进行遍历，原则上三条list的size都一样
                    for (int i = 0; i < pSize; i++) {
                        StatisticsRespVO power = new StatisticsRespVO();
                        if (i >= powerList.size()) {
                            power = new StatisticsRespVO();
                        } else {
                            power = powerList.get(i);
                        }
                        if(power==null){
                            power = new StatisticsRespVO();
                        }
                        StatisticsRespVO lastPower = new StatisticsRespVO();
                        if (lastPowerList != null) {
                            lastPower = lastPowerList.get(i);
                        } else {
                            lastPower = new StatisticsRespVO();
                        }

                        String timeIndex = oneHourRedisKey.get(i);
                        InverterCurveRespVo invert = new InverterCurveRespVo();
                        //如果时间小于第一条时序记录的时间, 并且不在第一条数据的时间段（小时） 这个值就塞0
                        DateTime theTime = DateUtil.parse(timeIndex, CommonConstant.DATE);
                        String dateString = timeIndex + "";
                        String substring = dateString.substring(11, 16);
                        String sub = dateString.substring(11, 13);
                        invert.setDate(substring);
                        String tdSubstring = DateUtil.format(firstTime, CommonConstant.DATE).substring(11, 13);
                        if (DateUtil.compare(theTime, firstTime) < 0 && !sub.equals(tdSubstring)) {
                            invert.setActivePower(BigDecimal.ZERO);
                        } else {
                            invert.setActivePower(power.getDigital());
                            invert.setPastActivePower(lastPower == null?null:lastPower.getDigital());
                        }
                        list.add(invert);
                    }
                }
            }
        }

        return list;
    }

    /**
     * 集团下所有电站的逆变器发电量间隔数据
     *
     * @param stations
     * @return
     */
    @Override
    public List<InverterTimingGenerate> timingGenerationList(List<Station> stations) {
        //查集团下的电站
        // List<Station> stations = stationService.selectByGroupId(groupId);
        //返回结果集
        List<InverterTimingGenerate> list = new ArrayList<>(128);
        TdBaseTimeQuery timeQuery = new TdBaseTimeQuery();
        //时间间隔,取自配置文件,目前是15分钟
        timeQuery.setTimeInterval(timeInterval);
        //查询实体中的子实体
        List<TdDeviceIdEntity> tdDeviceIds = new ArrayList<>();
/*            //todo 先查一个电站的
            stations = new ArrayList<>();
            Station stationTemp = new Station();
            stationTemp.setId(251);
            stations.add(stationTemp);*/

        //创建一个查询逆变器对应的集合
        Map<String, String> colNameMap = new HashMap<>();
        Map<String, String> modelNameMap = new HashMap<>();
        //查每个电站下的逆变器
        for (Station station : stations) {
            timeQuery.setGroupId(station.getGroupId());
            //查电站下逆变器
            List<DeviceRespVO> intervalsTopologies = pvStationService.getIntervalsByStationId(station.getId());
            //电站下有逆变器的才去查时序库
            if (!intervalsTopologies.isEmpty()) {
                //srcIdList
                List<DeviceIdAndTypeVo> deviceAttributes = new ArrayList<>();
                TdDeviceIdEntity tdDevice = new TdDeviceIdEntity();
                tdDevice.setStationId(station.getId());
                for (DeviceRespVO deviceRespVO : intervalsTopologies) {
                    //查每个逆变器的设备类型
                    //设置每个逆变器id和类型
                    Device device = deviceService.selectByStationIdAndSrcId(station.getId(), deviceRespVO.getSrcId());
                    if (device == null || StringUtils.isEmpty(device.getDeviceType())) {
                        continue;
                    }
                    DeviceIdAndTypeVo deviceIdAndTypeVo = new DeviceIdAndTypeVo();
                    deviceIdAndTypeVo.setSrcId(deviceRespVO.getSrcId());
                    deviceIdAndTypeVo.setDeviceType(device.getDeviceType());
                    deviceAttributes.add(deviceIdAndTypeVo);

                    if (colNameMap.get(device.getDeviceType()) == null) {
                        List<ModelDeviceColComp> acActivePowerList = modelDeviceService.getCompListByType("device", device.getDeviceType(), "cumulativePowerGeneration");
                        if (null != acActivePowerList && acActivePowerList.size() > 0) {
                            ModelDeviceColComp cumulativePowerGenerationCol = acActivePowerList.get(0);
                            colNameMap.put(device.getDeviceType(), cumulativePowerGenerationCol.getDeviceCol());
                        }
                    }
                    List<DeviceTypeConfig> deviceTypeConfigs = deviceTypeConfigService.selectByType(deviceRespVO.getDeviceType(), station.getId());
                    if (null != deviceTypeConfigs && deviceTypeConfigs.size() > 0) {
                        DeviceTypeConfig deviceTypeConfig = deviceTypeConfigs.get(0);
                        modelNameMap.put(device.getDeviceType(), deviceTypeConfig.getModelName());
                    }


                }
                tdDevice.setDeviceIdAndTypeVos(deviceAttributes);
                tdDeviceIds.add(tdDevice);
            }
        }
        timeQuery.setTdDeviceIds(tdDeviceIds);
        timeQuery.setColNameMap(colNameMap);
        timeQuery.setModelNameMap(modelNameMap);

        //调用td层查询数据
        DataResult<List<InverterTimingGenerate>> listDataResult = feignClient.timingGenerateList(timeQuery);
        log.info("listDataResult:" + listDataResult);
        if (!listDataResult.isSuccess()) {
            return list;
        }
        return listDataResult.getData();
    }

    @Override
    public List<DeviceAttributeRespVO> getAttr(DeviceAttrbuteReqVO vo) {
        //查询到该设备
        Device device = deviceService.selectByStationIdAndSrcId(vo.getStationId(), vo.getSrcId());
        //创建返回值
        List<DeviceAttributeRespVO> respVOS = new ArrayList<>();
        if (device != null) {
            if (device.getDeviceType() != null) {
                //查询设备映射字段
                List<ModelDeviceColComp> colMappingList = modelDeviceService.getCompListByType(CommonConstant.ModelDeviceType.DEVICE, device.getDeviceType(), null);
                //查询没有映射字段
                List<DeviceTypeCol> deviceTypeCols = deviceTypeColMapper.selectByDeviceType(device.getDeviceType(),null);
                //拼接缓存key
                String inverterRedisKey = device.getDeviceType() + ":" + vo.getStationId() + ":" + vo.getSrcId();
                //先判断缓存中是否存在
                if (redisService.hasKey(inverterRedisKey)) {
                    //缓存中的字段
                    Map<Object, Object> inverterMap = redisService.hgetall(inverterRedisKey);

                    //把字段集合转化为map
                    Map<String, List<ModelDeviceColComp>> colMappingMap = colMappingList.stream().collect(Collectors.groupingBy(ModelDeviceColComp::getModelCol));
                    Map<String, List<DeviceTypeCol>> deviceTypeColMap = deviceTypeCols.stream().collect(Collectors.groupingBy(DeviceTypeCol::getCol));
                    List<DeviceAttributeRespVO> finalRespVOS = respVOS;
                    inverterMap.forEach((k, v) -> {
                        if (colMappingMap.get((String) k) != null) {
                            DeviceAttributeRespVO deviceAttributeRespVO = new DeviceAttributeRespVO();
                            List<ModelDeviceColComp> modelDeviceColComps = colMappingMap.get((String) k);
                            ModelDeviceColComp modelDeviceColComp = modelDeviceColComps.get(0);
                            deviceAttributeRespVO.setUnit(modelDeviceColComp.getUnit());
                            deviceAttributeRespVO.setColName(modelDeviceColComp.getDeviceCol());
                            deviceAttributeRespVO.setFieldName(modelDeviceColComp.getDeviceColName());
                            DeviceTransfer value = (DeviceTransfer) v;
                            deviceAttributeRespVO.setData(NumberUtil.round(new BigDecimal(value.getValue().toString()), 2));
                            deviceAttributeRespVO.setSensType(modelDeviceColComp.getSensType());
                            finalRespVOS.add(deviceAttributeRespVO);
                        } else if (deviceTypeColMap.get((String) k) != null) {
                            DeviceAttributeRespVO deviceAttributeRespVO = new DeviceAttributeRespVO();
                            List<DeviceTypeCol> modelDeviceColComps = deviceTypeColMap.get((String) k);
                            DeviceTypeCol modelDeviceColComp = modelDeviceColComps.get(0);
                            deviceAttributeRespVO.setUnit(modelDeviceColComp.getUnit());
                            deviceAttributeRespVO.setColName(modelDeviceColComp.getCol());
                            deviceAttributeRespVO.setFieldName(modelDeviceColComp.getColName());
                            DeviceTransfer value = (DeviceTransfer) v;
                            deviceAttributeRespVO.setData(NumberUtil.round(new BigDecimal(value.getValue().toString()), 2));
                            deviceAttributeRespVO.setSensType(modelDeviceColComp.getSensType());
                            finalRespVOS.add(deviceAttributeRespVO);
                        }
                    });

                }
                //通过入参过滤
                respVOS = respVOS.stream().filter(s -> {
                    return vo.getSensType().equals(s.getSensType());
                }).collect(Collectors.toList());
            }
        }
        respVOS = respVOS.stream().sorted(
                Comparator.comparing(DeviceAttributeRespVO::getFieldName, Comparator.nullsLast(String::compareTo))
        ).collect(Collectors.toList());
        //如果不存在直接返回空
        return respVOS;
    }
}


/**
 * 集团下所有电站的逆变器发电量间隔数据
 *
 * @param groupId
 * @return
 */
/*
    @Override
    public List<InverterTimingGenerate> timingGenerationList(Integer groupId) {
        //查集团下的电站
        List<Station> stations = stationService.selectByGroupId(groupId);
        //返回结果集
        List<InverterTimingGenerate> list = new ArrayList<>(128);
        TdBaseTimeQuery timeQuery = new TdBaseTimeQuery();
        timeQuery.setGroupId(groupId);
        //时间间隔,取自配置文件,目前是15分钟
        timeQuery.setTimeInterval(timeInterval);
        //查询实体中的子实体
        List<TdDeviceIdEntity> tdDeviceIds = new ArrayList<>();
        //todo 先查一个电站的
        stations = new ArrayList<>();
        Station stationTemp = new Station();
        stationTemp.setId(251);
        stations.add(stationTemp);

        //查每个电站下的逆变器
        for (Station station : stations) {
            //查电站下逆变器
            List<Topology> intervalsTopologies = pvStationService.getIntervalsByStationId(station.getId());
            //电站下有逆变器的才去查时序库
            if (!intervalsTopologies.isEmpty()) {
                //srcIdList
                List<Integer> srcIdList = new ArrayList<>();
                TdDeviceIdEntity tdDevice = new TdDeviceIdEntity();
                tdDevice.setStationId(station.getId());
                for (Topology topology : intervalsTopologies) {
                    srcIdList.add(topology.getSrcId());
                }
                tdDevice.setSrcIdList(srcIdList);
                tdDeviceIds.add(tdDevice);
            }
        }
        timeQuery.setTdDeviceIds(tdDeviceIds);
        //调用td层查询数据
        DataResult<List<InverterTimingGenerate>> listDataResult = feignClient.timingGenerateList(timeQuery);
        log.info("listDataResult:" + listDataResult);
        if (!listDataResult.isSuccess()) {
            return list;
        }
        return listDataResult.getData();
    }
}
}*/
