package com.ho.business.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.*;
import com.ho.business.mapper.DeviceCallMapper;
import com.ho.business.mapper.DeviceTypeMapper;
import com.ho.business.service.*;
import com.ho.business.vo.DeviceTransfer;
import com.ho.business.vo.req.DeviceReqVO;
import com.ho.business.vo.req.device.DeviceIntervalReqVo;
import com.ho.business.vo.req.device.DeviceTreeReq;
import com.ho.business.vo.req.device.DeviceTypeQuery;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.business.vo.resp.VirtualDeviceColResp;
import com.ho.business.vo.resp.cabin.TemperatureVoltage;
import com.ho.business.vo.resp.cabin.TemperatureVoltageResp;
import com.ho.business.vo.resp.chargepile.ChargePileRespVO;
import com.ho.business.vo.resp.point.DevicePointResp;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import com.ho.datacollect.api.constant.DataCollectConstant;
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 java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xwz
 * @description 针对表【device_call】的数据库操作Service实现
 * @createDate 2023-05-06
 */
@Service
public class DeviceCallServiceImpl implements DeviceCallService {

    @Autowired
    DeviceCallMapper deviceCallMapper;

    @Autowired
    StationService stationService;

    @Autowired
    DeviceTypeMapper deviceTypeMapper;

    @Autowired
    DeviceTypeConfigService deviceTypeConfigService;

    @Autowired
    VirtualDeviceColService virtualDeviceColService;

    @Autowired
    DeviceTypeColService deviceTypeColService;

    @Autowired
    RedisService redisService;

    @Override
    public PageResult<DeviceRespVO> getPageListByInfo(DeviceReqVO deviceReqVO) {
        //是否是多个电站
        Map<Integer, String> stationMap = new HashMap<>();
        if (deviceReqVO.getStationIds() != null && !deviceReqVO.getStationIds().isEmpty()) {
            for (Integer stationId : deviceReqVO.getStationIds()) {
                Station station = stationService.selectById(stationId);
                stationMap.put(stationId, station.getName());
            }
        }
        //原来的
        /*Station station = stationService.selectById(deviceReqVO.getStationId());
        if (station == null) {
            return PageUtils.getPageResult(new PageInfo<>(new ArrayList<>()));
        }*/

        if (deviceReqVO.getStationId() == null && ArrayUtil.isEmpty(deviceReqVO.getStationIds())) {
            return PageUtils.getPageResult(new PageInfo<>(new ArrayList<>()));
        }

        //如果入参的电站id不为空
        if (deviceReqVO.getStationId() != null) {
            Station station = stationService.selectById(deviceReqVO.getStationId());
            if (station == null) {
                throw new BusinessException(BaseResponseCode.STATION_YC_NOT_EXISTS);
            }
            stationMap.put(deviceReqVO.getStationId(), station.getName());
        }
        //只查询deviceTypeConfig中定义的设备类型数据
        DeviceTypeQuery deviceTypeQuery = new DeviceTypeQuery();
        deviceTypeQuery.setGroupId(deviceReqVO.getGroupId());
        List<DeviceTypeConfig> deviceTypeConfigs = deviceTypeConfigService.queryListByCondition(deviceTypeQuery);
        //按deviceType为key转为map
        Map<String, String> deviceTypeMap = deviceTypeConfigs.stream().collect(Collectors.toMap(DeviceTypeConfig::getDeviceType, DeviceTypeConfig::getName));
        List<String> deviceTypeList = new ArrayList<>();
        for (DeviceTypeConfig deviceTypeConfig : deviceTypeConfigs) {
            deviceTypeList.add(deviceTypeConfig.getDeviceType());
        }
        //构造查询条件
        DeviceCall deviceQuery = new DeviceCall();
        BeanUtil.copyProperties(deviceReqVO, deviceQuery);
        deviceQuery.setNeedFilter(CommonConstant.ONE);
        //因为有多个stationId了,所以不需要groupId和deptId了
        //deviceQuery.setGroupId(station.getGroupId());
        //deviceQuery.setDeptId(station.getDeptId());
        PageHelper.startPage(deviceReqVO.getPageNum(), deviceReqVO.getPageSize());
        //deviceTypeList 不限制值
        //List<DeviceRespVO> deviceList = deviceMapper.selectByInfo(deviceTypeList, deviceQuery, deviceReqVO.getStationIds());
        List<DeviceRespVO> deviceList = deviceCallMapper.selectByInfo(null, deviceQuery, deviceReqVO.getStationIds());
//        deviceList = getDevice(deviceList);
        if (!deviceList.isEmpty()) {
            for (DeviceRespVO device : deviceList) {
                //根据缓存中设备上送数据更新时间判断设备是否在线
                DeviceCall deviceCall = new DeviceCall();
                BeanUtils.copyProperties(device,deviceCall);
                if (!isOnLine(deviceCall)) {
                    device.setStatus(CommonConstant.TWO);
                }
                //device.setStationName(station.getName());
                device.setStationName(stationMap.get(device.getStationId()));
                //查询设备类型 ,改为直接从device表中拿到
                //String deviceType = topologyService.findDeviceType(device.getStationId(), device.getSrcId());
                String deviceType = device.getDeviceType();
                if (deviceType != null) {
//                    String deviceTypeName = DeviceTypeConstant.getDeviceName(deviceType);
                    String deviceTypeName = deviceTypeMap.get(deviceType);
                    device.setDeviceType(deviceType);
                    device.setDeviceTypeName(deviceTypeName);
                    device.setStationName(stationMap.get(device.getStationId()));
                    String pileName = DeviceTypeConstant.judgeChargePile(deviceType);
                    //所有设备都可以编辑
                    //device.setEditable(1);
                    /*if (pileName.equals(DeviceTypeConstant.CHARGE_PILE)) {
                        device.setEditable(1);
                    } else {
                        device.setEditable(0);
                    }*/
                }

            }
        }
        return PageUtils.getPageResult(new PageInfo<>(deviceList));
    }

    @Override
    public List<DeviceRespVO> getListByDeviceType(DeviceReqVO deviceReqVO) {
        List<DeviceRespVO> deviceList = getList(deviceReqVO);
        return deviceList;
    }

    /**
     * 根据device去获取deviceType中的数字，并形成deviceType为key,name为value的map
     *
     * @param deviceList
     * @return
     */
    private Map<String, String> getDeviceTypeNameMap(List<DeviceRespVO> deviceList) {
        Map<String, String> deviceTypeNameMap = new HashMap<>();
        if (deviceList.isEmpty()) {
            return deviceTypeNameMap;
        }
        List<String> dtList = new ArrayList<>();
        for (DeviceRespVO de : deviceList) {
            dtList.add(de.getDeviceType());
        }
        List<DeviceType> deviceTypes = deviceTypeMapper.selectName(dtList);
        deviceTypeNameMap = deviceTypes.stream().collect(Collectors.toMap(DeviceType::getDeviceType, DeviceType::getName));
        return deviceTypeNameMap;
    }

    @Override
    public List<DeviceRespVO> getListByInfo(List<String> deviceTypeList, DeviceReqVO deviceReqVO) {
        //是否是多个电站
        Map<Integer, String> stationMap = new HashMap<>();
        if (deviceReqVO.getStationIds() != null && !deviceReqVO.getStationIds().isEmpty()) {
            for (Integer stationId : deviceReqVO.getStationIds()) {
                Station station = stationService.selectById(stationId);
                stationMap.put(stationId, station.getName());
            }
        }
        //原来的
        /*Station station = stationService.selectById(deviceReqVO.getStationId());
        if (station == null) {
            return PageUtils.getPageResult(new PageInfo<>(new ArrayList<>()));
        }*/

        if (deviceReqVO.getStationId() == null && ArrayUtil.isEmpty(deviceReqVO.getStationIds())) {
            return new ArrayList<>();
        }
        //构造查询条件
        DeviceCall deviceQuery = new DeviceCall();
        BeanUtil.copyProperties(deviceReqVO, deviceQuery);
        //因为有多个stationId了,所以不需要groupId和deptId了
        //deviceQuery.setGroupId(station.getGroupId());
        //deviceQuery.setDeptId(station.getDeptId());
        List<DeviceRespVO> deviceList = deviceCallMapper.selectByInfo(deviceTypeList, deviceQuery, deviceReqVO.getStationIds());
        deviceList = getDeviceRespVOS(deviceList);
        Map<String, String> deviceTypeNameMap = getDeviceTypeNameMap(deviceList);
        if (!deviceList.isEmpty()) {
            for (DeviceRespVO device : deviceList) {
                //device.setStationName(station.getName());
                device.setStationName(stationMap.get(device.getStationId()));
                //查询设备类型 ,改为直接从device表中拿到
                //String deviceType = topologyService.findDeviceType(device.getStationId(), device.getSrcId());
                String deviceType = device.getDeviceType();
                if (deviceType != null) {
//                    String deviceTypeName = DeviceTypeConstant.getDeviceName(deviceType);
                    String deviceTypeName = deviceTypeNameMap.get(deviceType);
                    device.setDeviceType(deviceType);
                    device.setDeviceTypeName(deviceTypeName);
                    device.setStationName(stationMap.get(device.getStationId()));
                    String pileName = DeviceTypeConstant.judgeChargePile(deviceType);
                    if (pileName.equals(DeviceTypeConstant.CHARGE_PILE)) {
                        device.setEditable(1);
                    } else {
                        device.setEditable(0);
                    }
                }

            }
        }
        return deviceList;
    }

    @Override
    public List<DeviceRespVO> getList(DeviceReqVO deviceReqVO) {
        List<DeviceRespVO> deviceList = deviceCallMapper.selectByCondition(deviceReqVO);
        return deviceList;
    }

    @Override
    public List<DeviceRespVO> getListByDeviceType(Integer stationId, String deviceType) {
        List<String> deviceTypes = new ArrayList<>();
        deviceTypes.add(deviceType);
        deviceTypes.add(deviceType + "_" + stationId);
        DeviceReqVO deviceReqVO = new DeviceReqVO();
        deviceReqVO.setStationId(stationId);
        deviceReqVO.setNeedFilter(CommonConstant.ONE);
        deviceReqVO.setDeviceTypeList(deviceTypes);
        List<DeviceRespVO> pcsDeviceList = getList(deviceReqVO);
        return pcsDeviceList;
    }

    @Override
    public List<DeviceRespVO> getListByDeviceTypeAndPid(Integer stationId, Integer pid, String deviceType) {
        List<String> deviceTypes = new ArrayList<>();
        deviceTypes.add(deviceType);
        deviceTypes.add(deviceType + "_" + stationId);
        DeviceReqVO deviceReqVO = new DeviceReqVO();
        deviceReqVO.setStationId(stationId);
        //父设备
        deviceReqVO.setPId(pid);
        deviceReqVO.setNeedFilter(CommonConstant.ONE);
        deviceReqVO.setDeviceTypeList(deviceTypes);
        List<DeviceRespVO> pcsDeviceList = getList(deviceReqVO);
        return pcsDeviceList;
    }


    @Override
    public int insertSelective(DeviceCall device) {
        int i = deviceCallMapper.insertSelective(device);
        return i;
    }

    @Override
    public int updateDevice(DeviceCall device) {
        int i = deviceCallMapper.updateByPrimaryKeySelective(device);
        return i;
    }


    @Override
    public int updateChargePile(DeviceCall device) {
        int i = deviceCallMapper.updateByPrimaryKeySelective(device);
        return i;
    }

    @Override
    public DeviceCall selectById(Integer id) {
        DeviceCall device = deviceCallMapper.selectByPrimaryKey(id);
        return device;
    }

    @Override
    public List<ChargePileRespVO> selectBySrcIds(Integer stationId, List<Integer> srcIds) {
        List<ChargePileRespVO> devices = deviceCallMapper.selectBySrcIds(stationId, srcIds);
        return devices;
    }

    @Override
    public DeviceRespVO getDeviceRespVO(Integer id) {
        DeviceCall device = deviceCallMapper.selectByPrimaryKey(id);

        DeviceRespVO deviceRespVO = new DeviceRespVO();
        Station station = stationService.selectById(device.getStationId());
        deviceRespVO.setStationName(station.getName());
        //查询设备类型
//        String deviceType = topologyService.findDeviceType(device.getStationId(), device.getSrcId());
        DeviceTypeConfig deviceTypeConfig = deviceTypeConfigService.selectByDeviceType(device.getDeviceType(), device.getGroupId());
        if (deviceTypeConfig != null) {
//            String deviceTypeName = DeviceTypeConstant.getDeviceName(deviceType);
            deviceRespVO.setDeviceType(deviceTypeConfig.getDeviceType());
            deviceRespVO.setDeviceTypeName(deviceTypeConfig.getName());
            String pileName = DeviceTypeConstant.judgeChargePile(device.getDeviceType());
            if (pileName.equals(DeviceTypeConstant.CHARGE_PILE)) {
                deviceRespVO.setEditable(1);
            } else {
                deviceRespVO.setEditable(0);
            }
        }

        return deviceRespVO;
    }

    //启用设备缓存
    @Override
//    @Cacheable(value = "device", sync = true)
    public DeviceCall selectByStationIdAndSrcId(Integer stationId, Integer srcId) {
        DeviceCall device = deviceCallMapper.selectByStationIdAndSrcId(stationId, srcId);
        return device;
    }

    @Override
    public List<DeviceRespVO> selectByGroup(Integer groupId) {
        DeviceCall device = new DeviceCall();
        device.setGroupId(groupId);
        List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByInfo(null, device, null);
        return deviceRespVOS;
    }

    @Override
    public List<String> selectDistinctDeviceTypeByStationId(Integer stationId) {
        List<String> deviceTypeList = deviceCallMapper.selectDistinctDeviceTypeByStationId(stationId);
        return deviceTypeList;
    }

    @Override
    public List<DeviceRespVO> getDeviceByStationId(Integer stationId) {
        DeviceReqVO reqVO = new DeviceReqVO();
        reqVO.setStationId(stationId);
        List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByCondition(reqVO);
        deviceRespVOS = getDeviceRespVOS(deviceRespVOS);
        return deviceRespVOS;
    }

    /**
     * 排除掉电站,间隔
     *
     * @param deviceRespVOS
     * @return
     */
    private List<DeviceRespVO> getDeviceRespVOS(List<DeviceRespVO> deviceRespVOS) {
        //排除掉电站,间隔
        deviceRespVOS = deviceRespVOS.stream().filter(s -> {
                    Integer category = s.getCategory();
                    if (category != null) {
                        return category > CommonConstant.TWO;
                    } else {
                        return false;
                    }
                }
        ).collect(Collectors.toList());
        return deviceRespVOS;
    }

    @Override
    public List<DeviceRespVO> getDeviceByStationIds(List<Integer> stationIds) {
        DeviceReqVO reqVO = new DeviceReqVO();
        reqVO.setStationIds(stationIds);
        List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByCondition(reqVO);
        return deviceRespVOS;
    }

    @Override
    public List<DeviceRespVO> selectByIds(List<Integer> deviceIds) {
        DeviceReqVO reqVO = new DeviceReqVO();
        reqVO.setDeviceIds(deviceIds);
        List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByCondition(reqVO);
        return deviceRespVOS;
    }

    @Override
    public List<DeviceRespVO> selectByStation(Integer stationId) {
        DeviceReqVO reqVO = new DeviceReqVO();
        reqVO.setStationId(stationId);
        List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByCondition(reqVO);
        return deviceRespVOS;
    }

    /**
     * 根据电站id和设备的pid查询设备
     *
     * @param stationId 电站id
     * @param pId       设备的父id
     * @return
     */
    @Override
    public List<DeviceRespVO> selectByStationIdAndPId(Integer stationId, Integer pId) {
        DeviceReqVO reqVO = new DeviceReqVO();
        reqVO.setStationId(stationId);
        reqVO.setPId(pId);
        List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByCondition(reqVO);
        return deviceRespVOS;
    }


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

    /**
     * 判断设备是否在线
     *
     * @param device 设备
     * @return
     */
    @Override
    public boolean isOnLine(DeviceCall device) {
        /**
         * 先判断心跳，心跳不在  说明云平台和装置断了  所有设备都应该是离线
         * 心跳在，去判断设备的通讯状态，通讯状态故障则离线
         */
        boolean flag = false;
        String accesspointRedisKey = RedisKeyConstant.HEARTBEAT + device.getSerialNo();
        //判断接入点缓存是否存在
        if (redisService.hasKey(accesspointRedisKey)) {
            if (redisService.hasKey(accesspointRedisKey)) {
                Map<Object, Object> accesspointMap = redisService.hgetall(accesspointRedisKey);
                Date updateTime = (Date) accesspointMap.get(DataCollectConstant.UPDATE_TIME);
                if (updateTime != null) {
                    long between = DateUtil.between(updateTime, new Date(), DateUnit.MINUTE);
                    //如果时间差值为5 ,则设备的离线个数+1
                    if (between < minute) {
                        String deviceRedisKey = device.getDeviceType() + ":" + device.getStationId() + ":" + device.getSrcId();
                        if (redisService.hasKey(deviceRedisKey)) {
                            Map<Object, Object> map = redisService.hgetall(deviceRedisKey);
                            Object comm = map.get(DataCollectConstant.COMMUNICATION_STATUS);
                            Integer communicationStatus = null;
                            if (comm instanceof Integer) {
                                communicationStatus = (Integer) comm;
                            } else if (comm instanceof DeviceTransfer) {
                                DeviceTransfer deviceTransfer = (DeviceTransfer) map.get(DataCollectConstant.COMMUNICATION_STATUS);
                                communicationStatus = deviceTransfer.getValue().intValue();
                            }
                            //如果/状态为1或者不存在这个字段,查接入点的状态
                            if (communicationStatus == null || CommonConstant.ONE.equals(communicationStatus)) {
                                flag = true;
                            }
                        }
                    }
                }
            }
        }
        return flag;
    }

    @Override
    public List<DeviceRespVO> getIntervalDevice(DeviceIntervalReqVo vo) {
        DeviceReqVO reqVO = new DeviceReqVO();
        reqVO.setStationId(vo.getStationId());
        reqVO.setPId(vo.getSrcId());
        reqVO.setCategory(vo.getCategory());
        List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByCondition(reqVO);
        //设备需要过滤设备类型为空的，间隔不需要过滤
        if (CommonConstant.THREE.equals(vo.getCategory())) {
            deviceRespVOS = deviceRespVOS.stream().filter(s -> s.getDeviceType() != null).collect(Collectors.toList());
        }
        return deviceRespVOS;
    }

    @Override
    public List<DeviceRespVO> selectByStationIdAndSrcIds(Map<Integer, List<Integer>> map) {
        List<DeviceRespVO> list = new ArrayList<>();
        //循环查询设备
        map.forEach((k, v) -> {
            List<DeviceRespVO> deviceRespVOS = deviceCallMapper.selectByStationIdAndSrcIds(k, v);
            list.addAll(deviceRespVOS);
        });
        return list;
    }

    @Override
    public List<DeviceRespVO> selectByIdAndSrcIdNotZero(Integer stationId) {
        List<DeviceRespVO> list = deviceCallMapper.selectByIdAndSrcIdNotZero(stationId,null);
        return list;
    }

    @Override
    public List<DeviceRespVO> selectDeviceByStationId(Integer stationId) {
        List<DeviceRespVO> list = deviceCallMapper.selectByIdAndSrcIdNotZero(stationId,CommonConstant.ONE);
        return list;
    }

    @Override
    public TemperatureVoltageResp getTemperatureVoltageData(DeviceTreeReq vo) {
        List<DeviceRespVO> deviceRespVOS = selectByIdAndSrcIdNotZero(vo.getStationId());
        List<DevicePointResp> treeDevices = new ArrayList<>();
        if (!deviceRespVOS.isEmpty()) {
            treeDevices = deviceTypeConfigService.getTreeDevices(deviceRespVOS, vo.getSrcId());
        }
        //查询当前设备下所以电池包的srcId合集
        Map<Integer, Integer> srcIds = new HashMap<>();
        getAllSrcId(srcIds, treeDevices);
        //关联关系表
        VirtualDeviceCol virtualDeviceCol = new VirtualDeviceCol();
        virtualDeviceCol.setStationId(vo.getStationId());
        List<VirtualDeviceColResp> virtualList = virtualDeviceColService.getList(virtualDeviceCol);
        //筛选关联关系中属于电池包的设备id
        List<VirtualDeviceColResp> packList = virtualList.stream().filter(i -> srcIds.get(i.getSrcId()) != null).collect(Collectors.toList());
        //Map<Integer, VirtualDeviceColResp> colIdMap = packList.stream().collect(Collectors.toMap(VirtualDeviceColResp::getDeviceColId, VirtualDeviceColResp -> VirtualDeviceColResp));

        TemperatureVoltageResp temperatureVoltageResp = new TemperatureVoltageResp();
        if(srcIds.size()>0){
            Map.Entry<Integer, Integer> entry = srcIds.entrySet().iterator().next();
            //获取真实设备srcId
            Integer srcId = entry.getValue();
            DeviceCall device = selectByStationIdAndSrcId(vo.getStationId(), srcId);
            //取设备类型
            String deviceType = device.getDeviceType();
            //取缓存
            String redisKey = deviceType + ":" + vo.getStationId() + ":" + srcId;
            //电压合集
            List<DeviceTransfer> vList = new ArrayList<>();
            //温度合集
            List<DeviceTransfer> tList = new ArrayList<>();
            if(redisService.hasKey(redisKey)){
                Map<Object, Object> hgetall = redisService.hgetall(redisKey);
                for (VirtualDeviceColResp col:packList) {
                    DeviceTransfer deviceTransfer = (DeviceTransfer)hgetall.get(col.getCol());
                    if(col.getType().contains(DeviceTypeConstant.VOLTAGE)){
                        vList.add(deviceTransfer);
                    }else if (col.getType().contains(DeviceTypeConstant.TEMPERATURE)){
                        tList.add(deviceTransfer);
                    }
                }
                Map<BigDecimal, List<DeviceTransfer>> tMap = tList.stream().collect(Collectors.groupingBy(DeviceTransfer::getValue));
                List<TemperatureVoltage> temperatureList = new ArrayList<>();
                tMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> {
                    TemperatureVoltage temperatureVoltage = new TemperatureVoltage();
                    temperatureVoltage.setXValue(x.getKey());
                    temperatureVoltage.setYValue(x.getValue().size());
                    temperatureList.add(temperatureVoltage);
                });

                Map<BigDecimal, List<DeviceTransfer>> vMap = vList.stream().collect(Collectors.groupingBy(DeviceTransfer::getValue));
                List<TemperatureVoltage> voltageTempList = new ArrayList<>();
                vMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> {
                    TemperatureVoltage temperatureVoltage = new TemperatureVoltage();
                    temperatureVoltage.setXValue(x.getKey());
                    temperatureVoltage.setYValue(x.getValue().size());
                    voltageTempList.add(temperatureVoltage);
                });
                //计算电压X轴值
                int len = voltageTempList.size();
                List<TemperatureVoltage> voltageList = new ArrayList<>();
                if(len>1){
                    TemperatureVoltage minV = voltageTempList.get(0);
                    TemperatureVoltage maxV = voltageTempList.get(len-1);
                    BigDecimal diff = maxV.getXValue().subtract(minV.getXValue()).divide(new BigDecimal(9),BigDecimal.ROUND_CEILING);
                    TemperatureVoltage temperatureVoltage = null;
                    for (int i = 0; i < 10; i++) {
                        temperatureVoltage = new TemperatureVoltage();
                        temperatureVoltage.setXValue(minV.getXValue().add(diff.multiply(new BigDecimal(i))));
                        temperatureVoltage.setYValue(CommonConstant.ZERO);
                        voltageList.add(temperatureVoltage);
                    }
                    int num = 0;
                    BigDecimal minValue ;
                    BigDecimal maxValue ;
                    for (TemperatureVoltage temp:voltageTempList) {
                        minValue = voltageList.get(num).getXValue();
                        maxValue = voltageList.get(num+1).getXValue();
                        if(!(temp.getXValue().compareTo(minValue)>=0 &&  temp.getXValue().compareTo(maxValue)<0)){
                            num++;
                        }
                        voltageList.get(num).setYValue(voltageList.get(num).getYValue()+temp.getYValue());
                    }
                }
                temperatureVoltageResp.setTemperatureList(temperatureList);
                temperatureVoltageResp.setVoltageList(voltageList);
            }else{
                temperatureVoltageResp.setTemperatureList(new ArrayList<>());
                temperatureVoltageResp.setVoltageList(new ArrayList<>());
            }
        }
        return temperatureVoltageResp;
    }

    private Map<Integer, Integer> getAllSrcId(Map<Integer, Integer> srcIds, List<DevicePointResp> treeDevices) {
        if (treeDevices != null) {
            for (DevicePointResp resp : treeDevices) {
                if (resp.getDeviceType() != null && resp.getDeviceType().contains(DeviceTypeConstant.STACK)) {
                    srcIds.put(resp.getSrcId(), resp.getFromId());
                }
                getAllSrcId(srcIds, resp.getChildren());
            }
        }
        return srcIds;
    }

}