package com.ho.td.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.vo.DeviceTransfer;
import com.ho.business.vo.resp.point.PointCurveResp;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.entity.PointCurveSrcCol;
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.PageResult;
import com.ho.common.tools.util.PageUtils;
import com.ho.common.tools.vo.req.PointData;
import com.ho.common.tools.vo.req.StationHomeRespVo;
import com.ho.common.tools.vo.req.StatisticsCurve;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.datacollect.api.vo.req.add.TdBase;
import com.ho.datacollect.api.vo.req.add.TdDeviceAddReq;
import com.ho.td.api.entity.elecMeter.ElecPowerGeneration;
import com.ho.td.api.entity.query.TdBaseTimeQuery;
import com.ho.td.mapper.DeviceComplexMapper;
import com.ho.td.mapper.DeviceSimpleMapper;
import com.ho.td.service.Device001Service;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author xueweizhi
 * @desc: device001
 * @date 2023/3/14
 */
@RestController
@RequestMapping("device001")
@Slf4j
public class Device001ApiController {

    @Autowired
    DeviceComplexMapper deviceComplexMapper;

    @Autowired
    DeviceSimpleMapper deviceSimpleMapper;

    @Autowired
    BigDecimalUtil bigDecimalUtil;

    @Autowired
    RedisService redisService;

    @Autowired
    Device001Service device001Service;

    @Value("${spring.profiles.active}")
    String env;

    @Value("${openRedis.host}")
    String host;

    @Value("${openRedis.port}")
    String port;

    @Value("${openRedis.pass}")
    String pass;

    @Value("${openTestRedis.host}")
    String testHost;

    @Value("${openTestRedis.port}")
    String testPort;

    @Value("${openTestRedis.pass}")
    String testPass;

    @PostMapping("insertDeviceSimple")
    public DataResult insertDeviceSimple(@RequestBody TdDeviceAddReq device) {
        device001Service.insertDeviceSimple(device);
        return DataResult.success();
    }

    @PostMapping("insertDynamicSimple")
    public DataResult insertDynamicSimple(@RequestBody TdDeviceAddReq device) {
        device001Service.insertDynamicSimple(device);
        return DataResult.success();
    }

    @PostMapping("insertDeviceComplex")
    public DataResult insertDeviceComplex(@RequestBody TdDeviceAddReq device) {
        List<Integer> complexTableList = getComplexTable(device.getDevice());
        device001Service.insertDeviceComplex(complexTableList, device);
        return DataResult.success();
    }

    /**
     * 收益计算 查询单条曲线
     *
     * @param query
     * @return
     */
    @PostMapping("queryPointTableList")
    public DataResult<List<StationHomeRespVo>> queryPointTableList(@RequestBody TdBaseTimeQuery query) {
        // 查询前一天最后一个点数据的查询参数
        TdBaseTimeQuery query1 = new TdBaseTimeQuery();

        //开始时间,结束时间转为数值
        String beginTime = query.getBeginTime();
        String endTime = query.getEndTime();
        DateTime begin = null;
        DateTime end = null;
        Long beginLong = null;
        Long endLong = null;
        if (beginTime != null) {
            begin = DateUtil.parse(beginTime, CommonConstant.DATE);
            beginLong = begin.getTime();
        }
        if (endTime != null) {
            end = DateUtil.parse(endTime, CommonConstant.DATE);
            endLong = end.getTime();
        }
        query.setBeginLong(beginLong);
        query.setEndLong(endLong);
        String modelName = query.getModelName();
        List<StationHomeRespVo> stationHomeRespVos = new ArrayList<StationHomeRespVo>(0);
        String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();

        long delay = 30 * 60 * 1000;
        BeanUtils.copyProperties(query, query1);
        query1.setBeginLong(query.getBeginLong() - delay);
        query1.setEndLong(query.getBeginLong());
        try {
            if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                stationHomeRespVos = deviceSimpleMapper.selectEndPoint(query1, tableName);
                stationHomeRespVos.addAll(deviceSimpleMapper.selectAll(query, tableName));
            } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                stationHomeRespVos = deviceComplexMapper.selectEndPoint(query1, tableName);
                stationHomeRespVos.addAll(deviceComplexMapper.selectAll(query, tableName));
            }
        } catch (Exception e) {
            //e.printStackTrace();
        }

        System.err.println("收益曲线查询 -> " + query.getBeginTime() + "------" + query.getCol() + "-------" + stationHomeRespVos.size());
        return DataResult.success(stationHomeRespVos);
    }


    @PostMapping("queryPointTableComplex")
    public DataResult<List<StatisticsCurve>> queryPointTableComplex(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableComplex.tdBaseTimeQuery:" + query);
        List<StatisticsCurve> list = new ArrayList<>();
        try {
            String tableName;
            if (query.getTableName() == null) {
                tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
            } else {
                tableName = query.getTableName();
            }
            log.info("产生的tableName: {}", tableName);
            //开始时间,结束时间转为数值
            String beginTime = query.getBeginTime();
            String endTime = query.getEndTime();
            DateTime begin = null;
            DateTime end = null;
            Long beginLong = null;
            Long endLong = null;
            if (beginTime != null) {
                begin = DateUtil.parse(beginTime, CommonConstant.DATE);
                beginLong = begin.getTime();
            }
            if (endTime != null) {
                end = DateUtil.parse(endTime, CommonConstant.DATE);
                endLong = end.getTime();
            }
            query.setBeginLong(beginLong);
            query.setEndLong(endLong);
            list = deviceComplexMapper.queryPointTableComplex(query, tableName);

            return DataResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("电表数据查询失败");
        }
        return DataResult.success(list);
    }

    @PostMapping("queryPointTableSimple")
    public DataResult<List<StatisticsCurve>> queryPointTableSimple(@RequestBody TdBaseTimeQuery query) {
        List<StatisticsCurve> list = new ArrayList<>();
        try {
            String tableName;
            if (query.getTableName() == null) {
                tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
            } else {
                tableName = query.getTableName();
            }

            log.info("产生的tableName: {}", tableName);
            //开始时间,结束时间转为数值
            String beginTime = query.getBeginTime();
            String endTime = query.getEndTime();
            DateTime begin = null;
            DateTime end = null;
            Long beginLong = null;
            Long endLong = null;
            if (beginTime != null) {
                begin = DateUtil.parse(beginTime, CommonConstant.DATE);
                beginLong = begin.getTime();
            }
            if (endTime != null) {
                end = DateUtil.parse(endTime, CommonConstant.DATE);
                endLong = end.getTime();
            }
            query.setBeginLong(beginLong);
            query.setEndLong(endLong);
            list = deviceSimpleMapper.queryPointTableSimple(query, tableName);
            return DataResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询电表数据失败");
        }
        return DataResult.success(list);
    }

    @PostMapping("queryPointTable")
    public DataResult<List<PointCurveResp>> queryPointTable(@RequestBody TdBaseTimeQuery query) {
        List<PointCurveResp> pointCurveRespList = new ArrayList<>();
        for (PointCurveSrcCol pointCurveSrcCol : query.getDeviceIdList()) {
            DataResult<List<StatisticsCurve>> listDataResult = new DataResult<>();
            query.setSrcId(pointCurveSrcCol.getSrcId());
            //根据srcId拿到相对应的数据，来判断查询那个数据库
            String modelName = query.getMap().get(pointCurveSrcCol.getSrcId());
            List<String> cols = pointCurveSrcCol.getCols();
            for (String col : cols) {
                //判断yc的colName在那张表中
                query.setCol(col);
                String tableName = getTableName(query);
                query.setTableName(tableName);
                if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                    listDataResult = queryPointTableSimple(query);

                } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                    listDataResult = queryPointTableComplex(query);
                }
                if (listDataResult.isSuccess() && listDataResult.getData() != null) {
                    List<StatisticsCurve> data = listDataResult.getData();
                    DateTime parseTime = DateUtil.parse(query.getBeginTime(), CommonConstant.DATE);
//                    parseTime = DateUtil.offsetMinute(parseTime, -query.getTimeInterval());
                    String time = query.getTime();
                    String s = time.substring(time.length() - 1, time.length());
                    //当传入是秒时，按照秒去计算分割时间
                    if ("s".equals(s)) {
                        parseTime = DateUtil.offsetSecond(parseTime, -query.getTimeInterval());
                    } else {
                        parseTime = DateUtil.offsetMinute(parseTime, -query.getTimeInterval());
                    }
                    for (int i = 0; i < data.size(); i++) {
//                        parseTime = DateUtil.offsetMinute(parseTime, query.getTimeInterval());
                        if ("s".equals(s)) {
                            parseTime = DateUtil.offsetSecond(parseTime, query.getTimeInterval());
                        } else {
                            parseTime = DateUtil.offsetMinute(parseTime, query.getTimeInterval());
                        }
                        StatisticsCurve statisticsRespVO = data.get(i);
                        if (statisticsRespVO == null) {
                            StatisticsCurve statisticsCurve = new StatisticsCurve();
                            statisticsCurve.setDate(DateUtil.format(parseTime, CommonConstant.DATE));
                            statisticsCurve.setDigital(null);
                            data.set(i, statisticsCurve);
                        } else {
                            statisticsRespVO.setDate(DateUtil.format(parseTime, CommonConstant.DATE));
                        }
                    }

                    PointCurveResp pointCurveResp = new PointCurveResp();
                    pointCurveResp.setCol(col);
                    pointCurveResp.setSrcId(pointCurveSrcCol.getSrcId());
                    List<StatisticsCurve> list = new ArrayList<>();
                    for (StatisticsCurve datum : data) {
                        StatisticsCurve statisticsRespVO = new StatisticsCurve();
                        statisticsRespVO.setDigital(datum.getDigital());
                        statisticsRespVO.setDate(datum.getDate());
                        //保留两位
                        bigDecimalUtil.keepTwoDecimalPlaces(statisticsRespVO);
                        list.add(statisticsRespVO);
                    }
                    //
                    pointCurveResp.setStaticCurveList(list);
                    //1:遥测 2:遥信
                    pointCurveResp.setIdentifier(CommonConstant.TWO);
                    pointCurveRespList.add(pointCurveResp);
                }
            }
        }
        return DataResult.success(pointCurveRespList);

    }

    @PostMapping("getTodayPowerGeneration")
    public DataResult<ElecPowerGeneration> getTodayPowerGeneration(@RequestBody TdBaseTimeQuery query) {
        return getDayPowerGeneration(query);
    }

    /**
     * 缓存对象
     *
     * @param query
     * @param beginCharge
     */
    private void cacheObj(TdBaseTimeQuery query, String field, BigDecimal beginCharge) {
        String key = query.getDeviceType() + ":" + query.getStationId() + ":" + query.getSrcId();
        DeviceTransfer deviceTransfer = new DeviceTransfer();
        deviceTransfer.setUpdateTime(query.getBegin());
        deviceTransfer.setValue(beginCharge);
        redisService.hset(key, field, deviceTransfer);
    }

    /**
     * 处理总充总放可能出现的异常情况
     *
     * @param query
     * @param ele
     * @param tableName
     * @param charge
     * @param beginCharge
     * @param endCharge
     */
    private BigDecimal handleSimpleEle(TdBaseTimeQuery query, BigDecimal ele, String tableName, String charge, BigDecimal beginCharge, BigDecimal endCharge) {
        BigDecimal oneVal = BigDecimal.ONE;
        BigDecimal hisMaxVal = BigDecimal.ONE;
        //结束充电小于开始充电
        if (endCharge.compareTo(beginCharge) < 0) {
            //取当日最大
            hisMaxVal = deviceSimpleMapper.queryMaxOneByTime(tableName, charge, query.getBegin(), query.getEnd());
            //结束充电小于1
            if (endCharge.compareTo(oneVal) < 0) {
                //取当日最大充电电量，作为结束充电量
                ele = hisMaxVal.subtract(beginCharge);
            } else {
                //认为是清0后的电量值，取当日最大值-开始充电量+结束充电
                ele = hisMaxVal.subtract(beginCharge).add(endCharge);
            }
            query.setIsError(true);
        } else if (endCharge.compareTo(beginCharge) > 0) {
            //开始充电小于1
            if (beginCharge.compareTo(oneVal) < 0) {
                //结束充电 - 取历史最大充电值
                //结束充电>历史最大
                hisMaxVal = deviceSimpleMapper.queryHisMaxOneByTime(tableName, charge, query.getBegin());
                if (null == hisMaxVal) {
                    hisMaxVal = deviceSimpleMapper.queryMinOneByTime(tableName, charge, query.getBegin(), query.getEnd());
                }
                if (endCharge.compareTo(hisMaxVal) >= 0) {
                    ele = endCharge.subtract(hisMaxVal);
                }
                query.setIsError(true);
            }
        }
        return ele;
    }

    /**
     * 处理总充总放可能出现的异常情况
     *
     * @param query
     * @param ele
     * @param tableName
     * @param charge
     * @param beginCharge
     * @param endCharge
     */
    private BigDecimal handleComplexEle(TdBaseTimeQuery query, BigDecimal ele, String tableName, String charge, BigDecimal beginCharge, BigDecimal endCharge) {
        BigDecimal oneVal = BigDecimal.ONE;
        BigDecimal hisMaxVal = BigDecimal.ONE;
        //结束充电小于开始充电
        if (endCharge.compareTo(beginCharge) < 0) {
            hisMaxVal = deviceComplexMapper.queryMaxOneByTime(tableName, charge, query.getEnd());
            //结束充电小于1
            if (endCharge.compareTo(oneVal) < 0) {
                //取当日最大充电电量，作为结束充电量
                ele = hisMaxVal.subtract(beginCharge);
            } else {
                //认为是清0后的电量值，取当日最大值-开始充电量+结束充电
                ele = hisMaxVal.subtract(beginCharge).add(endCharge);
            }
            query.setIsError(true);
        } else if (endCharge.compareTo(beginCharge) > 0) {
            //开始充电小于1
            if (beginCharge.compareTo(oneVal) < 0) {
                //结束充电 - 取历史最大充电值
                //结束充电>历史最大
                hisMaxVal = deviceComplexMapper.queryMaxOneByTime(tableName, charge, query.getBegin());
                if (null == hisMaxVal) {
                    hisMaxVal = deviceComplexMapper.queryMinOneByTime(tableName, charge, query.getBegin(), query.getEnd());
                }
                if (endCharge.compareTo(hisMaxVal) >= 0) {
                    ele = endCharge.subtract(hisMaxVal);
                }
                query.setIsError(true);
            }
        }
        return ele;
    }

    @PostMapping("getPointTable")
    public DataResult<List<StationHomeRespVo>> getPointTable(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableSimple.getPointTable:" + query);
        DataResult<List<StationHomeRespVo>> listDataResult = new DataResult<>();
        DataResult<List<StatisticsCurve>> dataResult = new DataResult<>();
        for (PointCurveSrcCol pointCurveSrcCol : query.getDeviceIdList()) {
            query.setSrcId(pointCurveSrcCol.getSrcId());
            //根据srcId拿到相对应的数据，来判断查询那个数据库
            String modelName = query.getMap().get(pointCurveSrcCol.getSrcId());
            List<String> cols = pointCurveSrcCol.getCols();
            for (String col : cols) {
                query.setCol(col);
                if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                    dataResult = queryPointTableSimple(query);
                } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                    dataResult = queryPointTableComplex(query);
                }
            }

            List<StationHomeRespVo> list = new ArrayList<>();
            for (int i = 0; i < dataResult.getData().size(); i++) {
                StationHomeRespVo stationHomeRespVo = new StationHomeRespVo();
                if (dataResult.getData().get(i) == null) {
                    StationHomeRespVo vo = new StationHomeRespVo();
                    vo.setDigital(null);
                    vo.setDate(null);
                    list.add(vo);
                } else {
                    stationHomeRespVo.setDate(dataResult.getData().get(i).getDate());
                    if (dataResult.getData().get(i).getDigital() != null) {
                        stationHomeRespVo.setDigital(BigDecimal.valueOf(dataResult.getData().get(i).getDigital()));
                    }
                    list.add(stationHomeRespVo);
                }
                listDataResult.setData(list);
            }
        }
        return listDataResult;
    }


    @PostMapping("getPointDataShow")
    public DataResult<List<PointData>> getPointDataShow(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableSimple.getPointDataShow:" + query);
        List<PointData> pointData = new ArrayList<>();
        query.setSrcId(query.getSrcId());
        //根据srcId拿到相对应的数据，来判断查询那个数据库
        String modelName = query.getMap().get(query.getSrcId());
        query.setCol(query.getCol());
        if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                pointData = deviceSimpleMapper.queryPointDataShowSimple(query, tableName);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
            try {
                String tableName = getTableName(query);
                log.info("产生的tableName: {}", tableName);
                pointData = deviceComplexMapper.queryPointDataShowComplex(query, tableName);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        }
        return DataResult.success(pointData);
    }

    //求一段时间内最大值
    @PostMapping("selectMaxValue")
    public DataResult<PointData> selectMaxValue(@RequestBody TdBaseTimeQuery query) throws ParseException {
        log.info("queryPointTableSimple.getPointDataShow:" + query);
        PointData pointData = new PointData();
        //转换时间
        DateFormat fmt = new SimpleDateFormat(CommonConstant.DATE);
        Date beginTime = fmt.parse(query.getBeginTime());
        Date endTime = fmt.parse(query.getEndTime());
        query.setSrcId(query.getSrcId());
        //根据srcId拿到相对应的数据，来判断查询那个数据库
        String modelName = query.getMap().get(query.getSrcId());
        query.setCol(query.getCol());
        BigDecimal decimal = new BigDecimal(0);
        if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                decimal = deviceSimpleMapper.queryMaxOneByTime(tableName, query.getCol(), beginTime, endTime);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                decimal = deviceComplexMapper.queryMaxValue(tableName, query.getCol(), beginTime, endTime);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        }
        if (decimal == null) {
            decimal = new BigDecimal(0);
        }
        pointData.setDigital(new Double(String.valueOf(decimal)));
        return DataResult.success(pointData);
    }

    @PostMapping("getPointDetails")
    public DataResult<List<PointData>> getPointDetails(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableSimple.getPointDetails:" + query);
        List<PointData> pointDataList = new ArrayList<>();
        query.setSrcId(query.getSrcId());
        //根据srcId拿到相对应的数据，来判断查询那个数据库
        String modelName = query.getMap().get(query.getSrcId());
        query.setCol(query.getCol());
        if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                //开始时间,结束时间转为数值
                String beginTime = query.getBeginTime();
                String endTime = query.getEndTime();
                DateTime begin = null;
                DateTime end = null;
                Long beginLong = null;
                Long endLong = null;
                if (beginTime != null) {
                    begin = DateUtil.parse(beginTime, CommonConstant.DATE);
                    beginLong = begin.getTime();
                }
                if (endTime != null) {
                    end = DateUtil.parse(endTime, CommonConstant.DATE);
                    endLong = end.getTime();
                }
                query.setBeginLong(beginLong);
                query.setEndLong(endLong);
                List<StatisticsCurve> list = deviceSimpleMapper.queryPointTableSimple(query, tableName);
                for (StatisticsCurve statisticsRespVO : list) {
                    PointData pointData = new PointData();
                    pointData.setDate(statisticsRespVO.getDate());
                    pointData.setDigital(statisticsRespVO.getDigital().doubleValue());
                    pointDataList.add(pointData);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                //开始时间,结束时间转为数值
                String beginTime = query.getBeginTime();
                String endTime = query.getEndTime();
                DateTime begin = null;
                DateTime end = null;
                Long beginLong = null;
                Long endLong = null;
                if (beginTime != null) {
                    begin = DateUtil.parse(beginTime, CommonConstant.DATE);
                    beginLong = begin.getTime();
                }
                if (endTime != null) {
                    end = DateUtil.parse(endTime, CommonConstant.DATE);
                    endLong = end.getTime();
                }
                query.setBeginLong(beginLong);
                query.setEndLong(endLong);
                List<StatisticsCurve> list = deviceComplexMapper.queryPointTableComplex(query, tableName);
                for (StatisticsCurve statisticsRespVO : list) {
                    PointData pointData = new PointData();
                    pointData.setDate(statisticsRespVO.getDate());
                    pointData.setDigital(statisticsRespVO.getDigital());
                    pointDataList.add(pointData);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        }
        return DataResult.success(pointDataList);
    }

    /**
     * 统计数量
     *
     * @param query
     * @return
     */
    @PostMapping("getCountPointDataShow")
    public Integer getCountPointDataShow(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableSimple.getPointDataShow:" + query);

        query.setSrcId(query.getSrcId());
        //根据srcId拿到相对应的数据，来判断查询那个数据库
        String modelName = query.getMap().get(query.getSrcId());
        query.setCol(query.getCol());
        int totalNum = 0;
        if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                totalNum = deviceSimpleMapper.countPointTableSimple(query, tableName);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                totalNum = deviceComplexMapper.countQueryPointDataShowComplex(query, tableName);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        }
        return totalNum;
    }


    //不分页查询
    @PostMapping("getPointData")
    public DataResult<List<PointData>> getPointData(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableSimple.getPointDataShow:" + query);
        List<PointData> pointData = new ArrayList<>();
        List<StatisticsCurve> list = new ArrayList<>();
        query.setSrcId(query.getSrcId());
        String modelName = null;
        //根据srcId拿到相对应的数据，来判断查询那个数据库
        if (!query.getMap().isEmpty()) {
            modelName = query.getMap().get(query.getSrcId());
            query.setCol(query.getCol());
        }
        if (modelName != null) {
            if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                try {
                    String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                            .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                    log.info("产生的tableName: {}", tableName);
                    //开始时间,结束时间转为数值
                    String beginTime = query.getBeginTime();
                    String endTime = query.getEndTime();
                    DateTime begin = null;
                    DateTime end = null;
                    Long beginLong = null;
                    Long endLong = null;
                    if (beginTime != null) {
                        begin = DateUtil.parse(beginTime, CommonConstant.DATE);
                        beginLong = begin.getTime();
                    }
                    if (endTime != null) {
                        end = DateUtil.parse(endTime, CommonConstant.DATE);
                        endLong = end.getTime();
                    }
                    query.setBeginLong(beginLong);
                    query.setEndLong(endLong);
                    list = deviceSimpleMapper.queryPointTableSimple(query, tableName);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("电表数据查询失败");
                }
            } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                try {
                    String tableName = getTableName(query);
                    log.info("产生的tableName: {}", tableName);
                    //开始时间,结束时间转为数值
                    String beginTime = query.getBeginTime();
                    String endTime = query.getEndTime();
                    DateTime begin = null;
                    DateTime end = null;
                    Long beginLong = null;
                    Long endLong = null;
                    if (beginTime != null) {
                        begin = DateUtil.parse(beginTime, CommonConstant.DATE);
                        beginLong = begin.getTime();
                    }
                    if (endTime != null) {
                        end = DateUtil.parse(endTime, CommonConstant.DATE);
                        endLong = end.getTime();
                    }
                    query.setBeginLong(beginLong);
                    query.setEndLong(endLong);
                    list = deviceComplexMapper.queryPointTableComplex(query, tableName);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("电表数据查询失败");
                }
            }
        }

        for (int i = 0; i < list.size(); i++) {
            StatisticsCurve statisticsCurve = list.get(i);
            PointData data = new PointData();
            if (statisticsCurve == null) {
                data.setDigital(null);
                data.setDate(null);
                pointData.add(data);
            } else {
                data.setDate(statisticsCurve.getDate());
                data.setDigital(statisticsCurve.getDigital());
                pointData.add(data);
            }

        }
        return DataResult.success(pointData);
    }

    /**
     * 分表获取表名称
     *
     * @param query
     * @return
     */
    private String getTableName(TdBaseTimeQuery query) {
        String colFalg = query.getCol().substring(0, CommonConstant.TWO);
        String substring = query.getCol().substring(CommonConstant.TWO);
        int colNumber = Integer.parseInt(substring);
        int nameSuffix = 0;
        if (CommonConstant.YX.equals(colFalg)) {
            nameSuffix = colNumber / CommonConstant.FIVE_HUNDRED_AND_ONE;
        } else if (CommonConstant.YC.equals(colFalg)) {
            nameSuffix = colNumber / CommonConstant.THREETHOUSAND;
        }
        //将value拼接到表名后面进行查询
        StringBuffer tableName = new StringBuffer();
        tableName.append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId());
        if (nameSuffix != CommonConstant.ZERO) {
            tableName.append(DataCollectConstant.TABLE_LINK).append(nameSuffix);
        }
        return tableName.toString();
    }

    //查询遥信值
    @PostMapping("querySignalValue")
    public DataResult<List<PointCurveResp>> querySignalValue(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableSimple.querySignalValue:" + query);
        List<PointCurveResp> pointCurveRespList = new ArrayList<>();
        //开始时间,结束时间转为数值
        String beginTime = query.getBeginTime();
        String endTime = query.getEndTime();
        DateTime begin = null;
        DateTime end = null;
        Long beginLong = null;
        Long endLong = null;
        if (beginTime != null) {
            begin = DateUtil.parse(beginTime, CommonConstant.DATE);
            beginLong = begin.getTime();
        }
        if (endTime != null) {
            end = DateUtil.parse(endTime, CommonConstant.DATE);
            endLong = end.getTime();
        }
        query.setBeginLong(beginLong);
        query.setEndLong(endLong);
        for (PointCurveSrcCol pointCurveSrcCol : query.getDeviceIdList()) {
            query.setSrcId(pointCurveSrcCol.getSrcId());
            //根据srcId拿到相对应的数据，来判断查询那个数据库
            String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                    .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();

            log.info("产生的tableName: {}", tableName);
            String modelName = query.getMap().get(pointCurveSrcCol.getSrcId());
            List<String> cols = pointCurveSrcCol.getCols();
            if (!cols.isEmpty()) {
                for (String col : cols) {
                    PointCurveResp pointCurveResp = new PointCurveResp();
                    StationHomeRespVo stationHomeVo = new StationHomeRespVo();
                    //根据结束时间查询的最近一条数据
                    StationHomeRespVo stationEnsTimeVo = new StationHomeRespVo();
                    List<StationHomeRespVo> list = new ArrayList<>();
                    query.setCol(col);
                    if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                        try {
                            //先进行是数据的第一条查询，拿到值
                            stationHomeVo = deviceSimpleMapper.selectBeginTime(col, tableName, query.getBeginLong());

                            //在进行该段时间的查询（查询条件：不等于查询的第一条数据的值）拿到一个List中包含时间以及数值
                            if (stationHomeVo != null) {
                                //第一条数据的值
                                BigDecimal firstDigital = stationHomeVo.getDigital();
                                List<StationHomeRespVo> stationHomeRespVoList = deviceSimpleMapper.selectAll(query, tableName);
                                for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                                    if (!firstDigital.equals(stationHomeRespVo.getDigital())) {
                                        //如果不等于第一条变位值的则为拐点,将该条数据添加到List中，并修改初始变位值
                                        firstDigital = stationHomeRespVo.getDigital();
                                        list.add(stationHomeRespVo);
                                    }
                                }
                            } else {
                                BigDecimal firstDigital = null;
                                List<StationHomeRespVo> stationHomeRespVoList = deviceSimpleMapper.selectAll(query, tableName);
                                for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                                    if (stationHomeRespVo.getDigital() != null) {
                                        firstDigital = stationHomeRespVo.getDigital();
                                        list.add(stationHomeRespVo);
                                        break;
                                    }
                                }
                                for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                                    if (!firstDigital.equals(stationHomeRespVo.getDigital())) {
                                        //如果不等于第一条变位值的则为拐点,将该条数据添加到List中，并修改初始变位值
                                        firstDigital = stationHomeRespVo.getDigital();
                                        list.add(stationHomeRespVo);
                                    }
                                }
                            }
                            //根据结束时间查询最后一个拐点
                            stationEnsTimeVo = deviceSimpleMapper.selectEnd(col, tableName, query.getEndLong());

                        } catch (Exception e) {
                            e.printStackTrace();
                            log.error("电表数据查询失败");
                        }
                    } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                        //当遥信值超过最大设定范围后，分表查询
                        tableName = getTableName(query);
                        try {
                            //先进行是数据的第一条查询，拿到值
                            stationHomeVo = deviceComplexMapper.selectBeginTime(col, tableName, query.getBeginLong());
//                            list.add(stationHomeVo);

                            //在进行该段时间的查询（查询条件：不等于查询的第一条数据的值）拿到一个List中包含时间以及数值
                            if (stationHomeVo != null) {
                                //第一条数据的值
                                BigDecimal firstDigital = stationHomeVo.getDigital();
                                List<StationHomeRespVo> stationHomeRespVoList = deviceComplexMapper.selectAll(query, tableName);
                                for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                                    if (!firstDigital.equals(stationHomeRespVo.getDigital())) {
                                        //如果不等于第一条变位值的则为拐点,将该条数据添加到List中，并修改初始变位值
                                        firstDigital = stationHomeRespVo.getDigital();
                                        list.add(stationHomeRespVo);
                                    }
                                }
                            } else {
                                BigDecimal firstDigital = null;
                                List<StationHomeRespVo> stationHomeRespVoList = deviceComplexMapper.selectAll(query, tableName);
                                for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                                    if (stationHomeRespVo.getDigital() != null) {
                                        firstDigital = stationHomeRespVo.getDigital();
                                        list.add(stationHomeRespVo);
                                        break;
                                    }
                                }
                                for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                                    if (!firstDigital.equals(stationHomeRespVo.getDigital())) {
                                        //如果不等于第一条变位值的则为拐点,将该条数据添加到List中，并修改初始变位值
                                        firstDigital = stationHomeRespVo.getDigital();
                                        list.add(stationHomeRespVo);
                                    }
                                }
                            }
                            //根据结束时间查询最后一个拐点
                            stationEnsTimeVo = deviceComplexMapper.selectEnd(col, tableName, query.getEndLong());
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.error("电表数据查询失败");
                        }
                    }
                    if (list.size() != CommonConstant.ZERO && list != null) {
                        //比较结束时间
                        StationHomeRespVo stationHomeRespVo = list.get(list.size() - 1);
                        if (stationHomeRespVo != null && !stationHomeRespVo.getDate().equals(query.getEndTime())) {
                            StationHomeRespVo vo = new StationHomeRespVo();
                            vo.setDate(query.getEndTime());
                            if (stationEnsTimeVo != null) {
                                vo.setDigital(stationEnsTimeVo.getDigital());
                            } else {
                                vo.setDigital(stationHomeRespVo.getDigital());
                            }
                            list.add(vo);
                        }
                        //比较开始时间
                        StationHomeRespVo stationHomeBegin = list.get(0);
                        if (!stationHomeBegin.getDate().equals(query.getBeginTime())) {
                            StationHomeRespVo vo = new StationHomeRespVo();
                            vo.setDate(query.getBeginTime());
                            if (stationHomeVo != null) {
                                vo.setDigital(stationHomeVo.getDigital());
                            } else {
                                vo.setDigital(null);
                            }
                            list.add(0, vo);
                        }
                    } else {
                        StationHomeRespVo beginVo = new StationHomeRespVo();
                        beginVo.setDate(query.getBeginTime());
                        if (stationHomeVo != null) {
                            beginVo.setDigital(stationHomeVo.getDigital());
                        }
                        list.add(beginVo);
                        StationHomeRespVo endVo = new StationHomeRespVo();
                        endVo.setDate(query.getEndTime());
                        if (stationEnsTimeVo != null) {
                            endVo.setDigital(stationEnsTimeVo.getDigital());
                        } else {
                            if (stationHomeVo != null) {
                                endVo.setDigital(stationHomeVo.getDigital());
                            }
                        }
                        list.add(endVo);
                    }
                    pointCurveResp.setCol(col);
                    pointCurveResp.setSrcId(pointCurveSrcCol.getSrcId());
                    List<StatisticsCurve> staticCurveList = new ArrayList<>();
                    for (StationHomeRespVo stationHomeRespVo : list) {
                        StatisticsCurve statisticsRespVO = new StatisticsCurve();
                        statisticsRespVO.setDate(stationHomeRespVo.getDate());
                        if (stationHomeRespVo.getDigital() != null) {
                            statisticsRespVO.setDigital(stationHomeRespVo.getDigital().doubleValue());
                        }
                        staticCurveList.add(statisticsRespVO);
                    }
                    pointCurveResp.setStaticCurveList(staticCurveList);
                    //1:遥测 2:遥信
                    pointCurveResp.setIdentifier(CommonConstant.ONE);
                    pointCurveRespList.add(pointCurveResp);
                }
            }
        }
        return DataResult.success(pointCurveRespList);

    }


    //查表的一段时间内历史值
    @PostMapping("queryHisValueList")
    public DataResult<List<StationHomeRespVo>> queryHisValueList(@RequestBody TdBaseTimeQuery query) {
        log.info("device001.queryHisValueList:" + query);
        List<StationHomeRespVo> stationHomeRespVoList = new ArrayList<>();
        //开始时间,结束时间转为数值
        String beginTime = query.getBeginTime();
        String endTime = query.getEndTime();
        DateTime begin = null;
        DateTime end = null;
        Long beginLong = null;
        Long endLong = null;
        if (beginTime != null) {
            begin = DateUtil.parse(beginTime, CommonConstant.DATE);
            beginLong = begin.getTime();
        }
        if (endTime != null) {
            end = DateUtil.parse(endTime, CommonConstant.DATE);
            endLong = end.getTime();
        }
        query.setBeginLong(beginLong);
        query.setEndLong(endLong);
        //根据srcId拿到相对应的数据，来判断查询那个数据库
        String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
        try {
            stationHomeRespVoList = deviceComplexMapper.selectAll(query, tableName);
        } catch (Exception e) {
            log.error("查表的一段时间内历史值 出错: {}", e.getMessage());
        }
        return DataResult.success(stationHomeRespVoList);

    }

    private static List<Integer> getComplexTable(JSONObject jsonObject) {
        List<Map.Entry<String, Object>> yxList = jsonObject.entrySet().stream().filter(i -> i.getKey().contains("YX")).collect(Collectors.toList());
        List<Map.Entry<String, Object>> ycList = jsonObject.entrySet().stream().filter(i -> i.getKey().contains("YC")).collect(Collectors.toList());
        Set<Integer> resultList = new HashSet<>();
        if (ycList.size() > 0) {
            List<Integer> list = new ArrayList<>();
            for (Map.Entry<String, Object> s : ycList) {
                list.add(Integer.valueOf(s.getKey().substring(2, s.getKey().length())));
            }
            Integer max = Collections.max(list) / CommonConstant.THREETHOUSAND;
            Integer min = Collections.min(list) / CommonConstant.THREETHOUSAND;
            if (Collections.max(list) > CommonConstant.THREE_THOUSAND_AND_FOUR_HUNDRED * (max + 1)) {
                max = max + 1;
            }
            for (int i = min; i <= max; i++) {
                resultList.add(i);
            }
        }
        if (yxList.size() > 0) {
            List<Integer> listYX = new ArrayList<>();
            for (Map.Entry<String, Object> s : yxList) {
                listYX.add(Integer.valueOf(s.getKey().substring(2, s.getKey().length())));
            }
            //当yx大于500,超出第一张超级表所能存储的范围，需要向后扩充
            Integer yxMax = Collections.max(listYX) / CommonConstant.FIVE_HUNDRED_AND_ONE;
            Integer yxMin = Collections.min(listYX) / CommonConstant.FIVE_HUNDRED_AND_ONE;
            if (Collections.max(listYX) > CommonConstant.FIVE_HUNDRED * (yxMax + 1)) {
                yxMax = yxMax + 1;
            }
            for (int i = yxMin; i <= yxMax; i++) {
                resultList.add(i);
            }
        }
        log.info("getComplexTable {}", resultList);
        return resultList.stream().collect(Collectors.toList());
    }

    //查表的一段时间内历史值
    @PostMapping("queryAllValue")
    public PageResult queryAllValue(@RequestBody TdBase query) {
        log.info("device001.queryAllValue:" + query);
        int totalNum = 0;
        //查询总条数
        int num = getCountData(query);
        //每次拿到后判断是否成功
        totalNum = Math.max(totalNum, num);
        log.info("最大条数totalNum:{}", totalNum);
        List<Map<String, Object>> list = new ArrayList<>();
        String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
        if (DeviceTypeConstant.DEVICE_SIMPLE.equals(query.getTableName())) {
            list = deviceSimpleMapper.selectAllValue(query, tableName);

        } else if (DeviceTypeConstant.DEVICE_COMPLEX.equals(query.getTableName())) {
            list = deviceComplexMapper.selectAllValue(query, tableName);
        }
        PageResult pageResult = new PageResult<>();
        if (!list.isEmpty()) {
            pageResult = PageUtils.getPageResult(new PageInfo<>(list));
            pageResult.setTotalRows(totalNum);
            pageResult.setTotalPages(totalNum / query.getPageSize() + 1);
        } else {
            pageResult = PageUtils.getPageResult(new PageInfo<>(new ArrayList<>()));
        }

        return pageResult;

    }

    /**
     * 统计数量
     *
     * @param query
     * @return
     */
    @PostMapping("getCountData")
    public Integer getCountData(@RequestBody TdBase query) {
        log.info("queryPointTableSimple.getCountData:" + query);
        String modelName = query.getTableName();
        int totalNum = 0;
        if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                totalNum = deviceSimpleMapper.countPointAllSimple(query, tableName);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                totalNum = deviceComplexMapper.countPointAllComplex(query, tableName);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        }
        return totalNum;
    }

    /**
     * 自动化测试 查询单条曲线
     *
     * @param query
     * @return
     */
    @PostMapping("queryPointDataList")
    public DataResult<List<StationHomeRespVo>> queryPointDataList(@RequestBody TdBaseTimeQuery query) {
        //开始时间,结束时间转为数值
        String beginTime = query.getBeginTime();
        String endTime = query.getEndTime();
        DateTime begin = null;
        DateTime end = null;
        Long beginLong = null;
        Long endLong = null;
        if (beginTime != null) {
            begin = DateUtil.parse(beginTime, CommonConstant.DATE);
            beginLong = begin.getTime();
        }
        if (endTime != null) {
            end = DateUtil.parse(endTime, CommonConstant.DATE);
            endLong = end.getTime();
        }
        query.setBeginLong(beginLong);
        query.setEndLong(endLong);
        String modelName = query.getModelName();
        List<StationHomeRespVo> stationHomeRespVos = new ArrayList<StationHomeRespVo>(0);
        String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
        try {
            if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                stationHomeRespVos.addAll(deviceSimpleMapper.selectAll(query, tableName));
            } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                stationHomeRespVos.addAll(deviceComplexMapper.selectAll(query, tableName));
            }
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return DataResult.success(stationHomeRespVos);
    }


    /**
     * 火电厂数据冻结
     *
     * @param query
     * @return
     */
    @PostMapping("getGeneratorSetInfo")
    public DataResult<StationHomeRespVo> getGeneratorSetInfo(@RequestBody TdBaseTimeQuery query) {
        //开始时间,结束时间转为数值
        String beginTime = query.getBeginTime();
        String endTime = query.getEndTime();
        DateTime begin = null;
        DateTime end = null;
        Long beginLong = null;
        Long endLong = null;
        if (beginTime != null) {
            begin = DateUtil.parse(beginTime, CommonConstant.DATE);
            beginLong = begin.getTime();
        }
        if (endTime != null) {
            end = DateUtil.parse(endTime, CommonConstant.DATE);
            endLong = end.getTime();
        }
        query.setBeginLong(beginLong);
        query.setEndLong(endLong);
        StationHomeRespVo stationHomeVo = new StationHomeRespVo();
        Map<Integer, String> map = query.getMap();
        String modelName = query.getModelName();
        Integer srcId = query.getSrcId();
        String col = map.get(srcId);
        if (col != null) {
            query.setCol(col);
            if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                try {
                    String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                            .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                    log.info("产生的tableName: {}", tableName);
                    //拿到该段时间的最后一个值
                    stationHomeVo = deviceSimpleMapper.selectOneValue(tableName, query);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("火电厂数据查询失败");
                }
            } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                try {
                    String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                            .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                    log.info("产生的tableName: {}", tableName);
                    stationHomeVo = deviceComplexMapper.selectOneValue(tableName, query);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("火电厂数据查询失败");
                }
            }
        }

        return DataResult.success(stationHomeVo);
    }

    /**
     * 查询遥信值五遥
     *
     * @param query
     * @return
     */
    @PostMapping("querySignal")
    public DataResult<List<PointCurveResp>> querySignal(@RequestBody TdBaseTimeQuery query) {
        log.info("queryPointTableSimple.querySignal:" + query);
        List<PointCurveResp> pointCurveRespList = new ArrayList<>();
        //开始时间,结束时间转为数值
        String beginTime = query.getBeginTime();
        String endTime = query.getEndTime();
        DateTime begin = null;
        DateTime end = null;
        Long beginLong = null;
        Long endLong = null;
        if (beginTime != null) {
            begin = DateUtil.parse(beginTime, CommonConstant.DATE);
            beginLong = begin.getTime();
        }
        if (endTime != null) {
            end = DateUtil.parse(endTime, CommonConstant.DATE);
            endLong = end.getTime();
        }
        query.setBeginLong(beginLong);
        query.setEndLong(endLong);
        for (PointCurveSrcCol pointCurveSrcCol : query.getDeviceIdList()) {
            String tableName = pointCurveSrcCol.getTableName();
            String col = pointCurveSrcCol.getCol();
            log.info("产生的tableName: {}", tableName);
            PointCurveResp pointCurveResp = new PointCurveResp();
            StationHomeRespVo stationHomeVo = new StationHomeRespVo();
            //根据结束时间查询的最近一条数据
            StationHomeRespVo stationEnsTimeVo = new StationHomeRespVo();
            List<StationHomeRespVo> list = new ArrayList<>();
            query.setCol(col);
            try {
                //先进行是数据的第一条查询，拿到值
                stationHomeVo = deviceSimpleMapper.selectColFirst(col, tableName, query.getBeginLong());
                //在进行该段时间的查询（查询条件：不等于查询的第一条数据的值）拿到一个List中包含时间以及数值
                if (stationHomeVo != null) {
                    //第一条数据的值
                    BigDecimal firstDigital = stationHomeVo.getDigital();
                    List<StationHomeRespVo> stationHomeRespVoList = deviceSimpleMapper.selectColAll(query, tableName);
                    for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                        if (!firstDigital.equals(stationHomeRespVo.getDigital())) {
                            //如果不等于第一条变位值的则为拐点,将该条数据添加到List中，并修改初始变位值
                            firstDigital = stationHomeRespVo.getDigital();
                            list.add(stationHomeRespVo);
                        }
                    }
                } else {
                    BigDecimal firstDigital = null;
                    List<StationHomeRespVo> stationHomeRespVoList = deviceSimpleMapper.selectColAll(query, tableName);
                    for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                        if (stationHomeRespVo.getDigital() != null) {
                            firstDigital = stationHomeRespVo.getDigital();
                            list.add(stationHomeRespVo);
                            break;
                        }
                    }
                    for (StationHomeRespVo stationHomeRespVo : stationHomeRespVoList) {
                        if (!firstDigital.equals(stationHomeRespVo.getDigital())) {
                            //如果不等于第一条变位值的则为拐点,将该条数据添加到List中，并修改初始变位值
                            firstDigital = stationHomeRespVo.getDigital();
                            list.add(stationHomeRespVo);
                        }
                    }
                }
                //根据结束时间查询最后一个拐点
                stationEnsTimeVo = deviceSimpleMapper.selectColEnd(col, tableName, query.getEndLong());

            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }

            if (list.size() != CommonConstant.ZERO && list != null) {
                //比较结束时间
                StationHomeRespVo stationHomeRespVo = list.get(list.size() - 1);
                if (stationHomeRespVo != null && !stationHomeRespVo.getDate().equals(query.getEndTime())) {
                    StationHomeRespVo vo = new StationHomeRespVo();
                    vo.setDate(query.getEndTime());
                    if (stationEnsTimeVo != null) {
                        vo.setDigital(stationEnsTimeVo.getDigital());
                    } else {
                        vo.setDigital(stationHomeRespVo.getDigital());
                    }
                    list.add(vo);
                }
                //比较开始时间
                StationHomeRespVo stationHomeBegin = list.get(0);
                if (!stationHomeBegin.getDate().equals(query.getBeginTime())) {
                    StationHomeRespVo vo = new StationHomeRespVo();
                    vo.setDate(query.getBeginTime());
                    if (stationHomeVo != null) {
                        vo.setDigital(stationHomeVo.getDigital());
                    } else {
                        vo.setDigital(null);
                    }
                    list.add(0, vo);
                }
            } else {
                StationHomeRespVo beginVo = new StationHomeRespVo();
                beginVo.setDate(query.getBeginTime());
                if (stationHomeVo != null) {
                    beginVo.setDigital(stationHomeVo.getDigital());
                }
                list.add(beginVo);
                StationHomeRespVo endVo = new StationHomeRespVo();
                endVo.setDate(query.getEndTime());
                if (stationEnsTimeVo != null) {
                    endVo.setDigital(stationEnsTimeVo.getDigital());
                } else {
                    if (stationHomeVo != null) {
                        endVo.setDigital(stationHomeVo.getDigital());
                    }
                }
                list.add(endVo);
            }
            pointCurveResp.setCol(col);
            pointCurveResp.setSrcId(pointCurveSrcCol.getSrcId());
            List<StatisticsCurve> staticCurveList = new ArrayList<>();
            for (StationHomeRespVo stationHomeRespVo : list) {
                StatisticsCurve statisticsRespVO = new StatisticsCurve();
                statisticsRespVO.setDate(stationHomeRespVo.getDate());
                if (stationHomeRespVo.getDigital() != null) {
                    statisticsRespVO.setDigital(stationHomeRespVo.getDigital().doubleValue());
                }
                staticCurveList.add(statisticsRespVO);
            }
            pointCurveResp.setLegend(pointCurveSrcCol.getLegend());
            pointCurveResp.setStaticCurveList(staticCurveList);
            //1:遥测 2:遥信
            pointCurveResp.setIdentifier(CommonConstant.ONE);
            pointCurveRespList.add(pointCurveResp);
        }
        return DataResult.success(pointCurveRespList);
    }

    /**
     * 查询遥测值五遥
     *
     * @param query
     * @return
     */
    @PostMapping("queryMeasure")
    public DataResult<List<PointCurveResp>> queryMeasure(@RequestBody TdBaseTimeQuery query) {
        List<PointCurveResp> pointCurveRespList = new ArrayList<>();
        for (PointCurveSrcCol pointCurveSrcCol : query.getDeviceIdList()) {
            DataResult<List<StatisticsCurve>> listDataResult = new DataResult<>();
            query.setSrcId(pointCurveSrcCol.getSrcId());
            //根据srcId拿到相对应的数据，来判断查询那个数据库
            String col = pointCurveSrcCol.getCol();
            query.setCol(col);
            query.setTableName(pointCurveSrcCol.getTableName());
            //判断yc的colName在那张表中
            listDataResult = queryMeasureTable(query);
            if (listDataResult.isSuccess() && listDataResult.getData() != null) {
                List<StatisticsCurve> data = listDataResult.getData();
                DateTime parseTime = DateUtil.parse(query.getBeginTime(), CommonConstant.DATE);
//                    parseTime = DateUtil.offsetMinute(parseTime, -query.getTimeInterval());
                String time = query.getTime();
                String s = time.substring(time.length() - 1, time.length());
                //当传入是秒时，按照秒去计算分割时间
                if ("s".equals(s)) {
                    parseTime = DateUtil.offsetSecond(parseTime, -query.getTimeInterval());
                } else {
                    parseTime = DateUtil.offsetMinute(parseTime, -query.getTimeInterval());
                }
                for (int i = 0; i < data.size(); i++) {
//                        parseTime = DateUtil.offsetMinute(parseTime, query.getTimeInterval());
                    if ("s".equals(s)) {
                        parseTime = DateUtil.offsetSecond(parseTime, query.getTimeInterval());
                    } else {
                        parseTime = DateUtil.offsetMinute(parseTime, query.getTimeInterval());
                    }
                    StatisticsCurve statisticsRespVO = data.get(i);
                    if (statisticsRespVO == null) {
                        StatisticsCurve statisticsCurve = new StatisticsCurve();
                        statisticsCurve.setDate(DateUtil.format(parseTime, CommonConstant.DATE));
                        statisticsCurve.setDigital(null);
                        data.set(i, statisticsCurve);
                    } else {
                        statisticsRespVO.setDate(DateUtil.format(parseTime, CommonConstant.DATE));
                    }
                }

                PointCurveResp pointCurveResp = new PointCurveResp();
                pointCurveResp.setCol(col);
                pointCurveResp.setSrcId(pointCurveSrcCol.getSrcId());
                List<StatisticsCurve> list = new ArrayList<>();
                for (StatisticsCurve datum : data) {
                    StatisticsCurve statisticsRespVO = new StatisticsCurve();
                    statisticsRespVO.setDigital(datum.getDigital());
                    statisticsRespVO.setDate(datum.getDate());
                    //保留两位
                    bigDecimalUtil.keepTwoDecimalPlaces(statisticsRespVO);
                    list.add(statisticsRespVO);
                }
                pointCurveResp.setLegend(pointCurveSrcCol.getLegend());
                pointCurveResp.setStaticCurveList(list);
                //1:遥测 2:遥信
                pointCurveResp.setIdentifier(CommonConstant.TWO);
                pointCurveRespList.add(pointCurveResp);

            }
        }
        return DataResult.success(pointCurveRespList);
    }

    @PostMapping("queryMeasureTable")
    public DataResult<List<StatisticsCurve>> queryMeasureTable(@RequestBody TdBaseTimeQuery query) {
        List<StatisticsCurve> list = new ArrayList<>();
        try {
            String tableName = query.getTableName();
            log.info("产生的tableName: {}", tableName);
            //开始时间,结束时间转为数值
            String beginTime = query.getBeginTime();
            String endTime = query.getEndTime();
            DateTime begin = null;
            DateTime end = null;
            Long beginLong = null;
            Long endLong = null;
            if (beginTime != null) {
                begin = DateUtil.parse(beginTime, CommonConstant.DATE);
                beginLong = begin.getTime();
            }
            if (endTime != null) {
                end = DateUtil.parse(endTime, CommonConstant.DATE);
                endLong = end.getTime();
            }
            query.setBeginLong(beginLong);
            query.setEndLong(endLong);
            list = deviceSimpleMapper.queryMeasure(query, tableName);
            return DataResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询电表数据失败");
        }
        return DataResult.success(list);
    }

    /**
     * 冻结光伏-Mppt日发电量
     * @param query
     * @return
     */
    @PostMapping("getToDayPVMpptCharge")
    public DataResult<StationHomeRespVo> getToDayPVMpptCharge(@RequestBody TdBaseTimeQuery query){
        //拼接后缀表名
        Map<Integer, String> colNameMap = query.getMap();
        String modelName = query.getModelName();
        StationHomeRespVo stationHomeRespVo = new StationHomeRespVo();
        if (colNameMap != null) {
            //查询发电量
            String charge = colNameMap.get(query.getSrcId());
            if (charge != null) {
                query.setBeginLong(query.getBegin().getTime());
                query.setEndLong(query.getEnd().getTime());
                query.setCol(charge);
                String tableName = getTableName(query);
                log.info("charge != null");
                //计算昨日发电量
               BigDecimal ele = getPvBigDecimal(query, tableName, modelName, charge, DeviceTypeConstant.PV_DAILY_CHARGE_START);
               stationHomeRespVo.setDigital(ele);
                // 缓存今日发电量起始值
               query.setBeginLong(DateUtil.beginOfDay(DateUtil.date()).getTime());
               query.setEndLong(DateUtil.endOfDay(DateUtil.date()).getTime());
                List<StationHomeRespVo> stationHomeRespVos = null;
                if (DeviceTypeConstant.DEVICE_SIMPLE.equals(modelName)) {
                    stationHomeRespVos = deviceSimpleMapper.selectAll(query, tableName);
                } else if (DeviceTypeConstant.DEVICE_COMPLEX.equals(modelName)) {
                    stationHomeRespVos = deviceComplexMapper.selectAll(query, tableName);
                }
                if (stationHomeRespVos != null && stationHomeRespVos.size() > 0) {
                    BigDecimal startEle = stationHomeRespVos.get(0).getDigital();
                    cacheObj(query, DeviceTypeConstant.PV_DAILY_CHARGE_START, startEle);
                }
            }
        }
        return DataResult.success(stationHomeRespVo);
    }
    /**
     * 光伏累计充
     */
    @PostMapping("getToDayPVCharge")
    public DataResult<StationHomeRespVo> getToDayPVCharge(@RequestBody TdBaseTimeQuery query) {
        //开始时间,结束时间转为数值
        String beginTime = query.getBeginTime();
        String endTime = query.getEndTime();
        DateTime begin = null;
        DateTime end = null;
        Long beginLong = null;
        Long endLong = null;
        if (beginTime != null) {
            begin = DateUtil.parse(beginTime, CommonConstant.DATE);
            beginLong = begin.getTime();
        }
        if (endTime != null) {
            end = DateUtil.parse(endTime, CommonConstant.DATE);
            endLong = end.getTime();
        }
        query.setBeginLong(beginLong);
        query.setEndLong(endLong);
        StationHomeRespVo stationHomeVo = new StationHomeRespVo();
        Map<Integer, String> map = query.getMap();
        String modelName = query.getModelName();
        Integer srcId = query.getSrcId();
        String col = map.get(srcId);
        if (col != null) {
            query.setCol(col);
            if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
                try {
                    String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                            .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                    log.info("产生的tableName: {}", tableName);
                    //拿到该段时间的最后一个值
                    stationHomeVo = deviceSimpleMapper.selectOneValue(tableName, query);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("光伏累计充数据查询失败");
                }
            } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
                try {
                    String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                            .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                    log.info("产生的tableName: {}", tableName);
                    stationHomeVo = deviceComplexMapper.selectOneValue(tableName, query);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("光伏累计充查询失败");
                }
            }
        }

        return DataResult.success(stationHomeVo);
    }

    /**
     * 根据参数修改历史值
     *
     * @param query
     * @return
     */
    @PostMapping("modifyHistoryData")
    public DataResult modifyHistoryData(@RequestBody TdBaseTimeQuery query) {
        int result = device001Service.modifyHistoryData(query);
        return DataResult.success(result);
    }

    //求一段时间内最新值
    @PostMapping("selectLastedValue")
    public DataResult<PointData> selectLastedValue(@RequestBody TdBaseTimeQuery query) throws ParseException {
        log.info("queryPointTableSimple.selectLastedValue:" + query);
        PointData pointData = new PointData();
        //转换时间
        DateFormat fmt = new SimpleDateFormat(CommonConstant.DATE);
        Date endTime = new Date();
        query.setSrcId(query.getSrcId());
        //根据srcId拿到相对应的数据，来判断查询那个数据库
        String modelName = query.getMap().get(query.getSrcId());
        query.setCol(query.getCol());
        BigDecimal decimal = new BigDecimal(0);
        if (modelName.equals(DeviceTypeConstant.DEVICE_SIMPLE)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                decimal = deviceSimpleMapper.queryOneByTime(tableName, query.getCol(), endTime);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        } else if (modelName.equals(DeviceTypeConstant.DEVICE_COMPLEX)) {
            try {
                String tableName = new StringBuilder().append(query.getGroupId()).append(DataCollectConstant.TABLE_LINK)
                        .append(query.getStationId()).append(DataCollectConstant.TABLE_LINK).append(query.getSrcId()).toString();
                log.info("产生的tableName: {}", tableName);
                decimal = deviceComplexMapper.queryOneByTime(tableName, query.getCol(), endTime);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("电表数据查询失败");
            }
        }
        if (decimal == null) {
            decimal = new BigDecimal(0);
        }
        pointData.setDigital(new Double(String.valueOf(decimal)));
        return DataResult.success(pointData);
    }

    /**
     * 获取日电量，用来替换原来的 getTodayPowerGeneration 方法
     * 将日电量获取限定在传递的时间范围内，在开始结束范围内分别取最开始头与最后尾作为日电量
     *
     * @param query
     * @return
     */
    @PostMapping("getDayPowerGeneration")
    public DataResult<ElecPowerGeneration> getDayPowerGeneration(@RequestBody TdBaseTimeQuery query) {
        //创建返回对象
        ElecPowerGeneration elecPowerGeneration = new ElecPowerGeneration();
        try {
            //拼接后缀表名
            Map<String, String> colNameMap = query.getColNameMap();
            String modelName = query.getModelName();
            if (colNameMap != null) {
                //查询充电量
                String charge = colNameMap.get(DataCollectConstant.Elec_Meter.TOTAL_CHARGE);
                if (charge != null) {
                    query.setBeginLong(query.getBegin().getTime());
                    query.setEndLong(query.getEnd().getTime());
                    query.setCol(charge);
                    String tableName = getTableName(query);
                    log.info("charge != null");
                    //开始充电量
                    BigDecimal ele = getBigDecimal(query, tableName, modelName, charge, DeviceTypeConstant.DAILY_CHARGE_START);
                    elecPowerGeneration.setTodayChargePowerGeneration(ele);
                }
                //查询放电量
                String disCharge = colNameMap.get(DataCollectConstant.Elec_Meter.TOTAL_DISCHARGE);
                if (disCharge != null) {
                    query.setCol(disCharge);
                    String tableName = getTableName(query);
                    log.info("disCharge != null");
                    //开始放电量
                    BigDecimal ele = getBigDecimal(query, tableName, modelName, disCharge, DeviceTypeConstant.DAILY_DISCHARGE_START);
                    elecPowerGeneration.setTodayDisChargePowerGeneration(ele);
                }
            }
            elecPowerGeneration.setStationId(query.getStationId());
            elecPowerGeneration.setSrcId(query.getSrcId());
        } catch (Exception e) {
            log.error("getDayPowerGeneration query:{},error:{}", query, e.getMessage());
        }
        return DataResult.success(elecPowerGeneration);
    }

    @NotNull
    private BigDecimal getBigDecimal(TdBaseTimeQuery query, String tableName, String modelName, String col, String dayCol) {
        long start = System.currentTimeMillis();
        List<StationHomeRespVo> stationHomeRespVos = null;
        if (DeviceTypeConstant.DEVICE_SIMPLE.equals(modelName)) {
            stationHomeRespVos = deviceSimpleMapper.selectAll(query, tableName);
        } else if (DeviceTypeConstant.DEVICE_COMPLEX.equals(modelName)) {
            stationHomeRespVos = deviceComplexMapper.selectAll(query, tableName);
        }
        BigDecimal ele = BigDecimal.ZERO;
        if (stationHomeRespVos != null && stationHomeRespVos.size() > 0) {
            int len = stationHomeRespVos.size();
            BigDecimal startEle = stationHomeRespVos.get(0).getDigital();
            BigDecimal endEle = stationHomeRespVos.get(len - 1).getDigital();
            ele = endEle.subtract(startEle);
            //处理异常值
            ele = handleEle(ele, startEle, endEle, stationHomeRespVos);
            cacheObj(query, dayCol, startEle);
        } else {
            log.info("getDayPowerGeneration get stationHomeRespVos is empty,col:{},tableName:{},time:{}", col, tableName, new Date());
        }
        long end = System.currentTimeMillis();
        log.info("getBigDecimal used1:" + (end - start));
        return ele;
    }

    @NotNull
    private BigDecimal getPvBigDecimal(TdBaseTimeQuery query, String tableName, String modelName, String col, String dayCol) {
        long start = System.currentTimeMillis();
        List<StationHomeRespVo> stationHomeRespVos = null;
        if (DeviceTypeConstant.DEVICE_SIMPLE.equals(modelName)) {
            stationHomeRespVos = deviceSimpleMapper.selectAll(query, tableName);
        } else if (DeviceTypeConstant.DEVICE_COMPLEX.equals(modelName)) {
            stationHomeRespVos = deviceComplexMapper.selectAll(query, tableName);
        }
        BigDecimal ele = BigDecimal.ZERO;
        if (stationHomeRespVos != null && stationHomeRespVos.size() > 0) {
            int len = stationHomeRespVos.size();
            BigDecimal startEle = stationHomeRespVos.get(0).getDigital();
            BigDecimal endEle = stationHomeRespVos.get(len - 1).getDigital();
            ele = endEle.subtract(startEle);
            //处理异常值
            ele = handleEle(ele, startEle, endEle, stationHomeRespVos);
        } else {
            log.info("getDayPowerGeneration get stationHomeRespVos is empty,col:{},tableName:{},time:{}", col, tableName, new Date());
        }
        long end = System.currentTimeMillis();
        log.info("getBigDecimal used1:" + (end - start));
        return ele;
    }

    /**
     * 异常值处理
     *
     * @param ele
     * @param beginCharge
     * @param endCharge
     * @param stationHomeRespVos
     * @return
     */
    private BigDecimal handleEle(BigDecimal ele, BigDecimal beginCharge, BigDecimal endCharge, List<StationHomeRespVo> stationHomeRespVos) {
        BigDecimal oneVal = BigDecimal.ONE;
        BigDecimal hisMaxVal = BigDecimal.ONE;
        //结束充电小于开始充电
        if (endCharge.compareTo(beginCharge) < 0) {
            //取当日最大
            List<BigDecimal> eleList = stationHomeRespVos.stream().map(StationHomeRespVo::getDigital).collect(Collectors.toList());
            hisMaxVal = Collections.max(eleList);
            //结束充电小于1
            if (endCharge.compareTo(oneVal) < 0) {
                //取当日最大充电电量，作为结束充电量
                ele = hisMaxVal.subtract(beginCharge);
            } else {
                //认为是清0后的电量值，取当日最大值-开始充电量+结束充电
                ele = hisMaxVal.subtract(beginCharge).add(endCharge);
            }
        } else if (endCharge.compareTo(beginCharge) > 0) {
            //结束充电小于1
            if (endCharge.compareTo(oneVal) < 0) {
                //取当日最大
                List<BigDecimal> eleList = stationHomeRespVos.stream().map(StationHomeRespVo::getDigital).collect(Collectors.toList());
                hisMaxVal = Collections.max(eleList);
                //认为是清0后的电量值，取当日最大值-开始充电量+结束充电
                ele = hisMaxVal.subtract(beginCharge).add(endCharge);
            }
        }
        return ele;
    }
}