package com.njcn.prepare.harmonic.service.mysql.upload.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.commApi.CommLineClient;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.pojo.param.MonitorParam;
import com.njcn.device.pms.pojo.po.GeneratrixWire;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pms.pojo.po.PmsMidLedger;
import com.njcn.device.pms.pojo.po.StatationStat;
import com.njcn.device.pq.pojo.po.RStatIntegrityD;
import com.njcn.harmonic.annotaion.HarVoltage;
import com.njcn.harmonic.pojo.po.RStatDataVD;
import com.njcn.harmonic.pojo.po.day.RStatDataVDPO;
import com.njcn.harmonic.pojo.po.day.RStatLimitRateDPO;
import com.njcn.harmonic.pojo.po.upload.RUploadCommPointBus;

import com.njcn.prepare.harmonic.mapper.mysql.day.RStatDataFlickerDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.day.RStatDataVDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.day.RStatIntegrityDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.day.RStatLimitRateDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.dim.GeneratrixWireMapper;
import com.njcn.prepare.harmonic.mapper.mysql.dim.PmsMidLedgerMapper;
import com.njcn.prepare.harmonic.mapper.mysql.upload.PmsStatationStatMapper;
import com.njcn.prepare.harmonic.mapper.mysql.upload.RUploadCommPointBusMapper;
import com.njcn.prepare.harmonic.service.mysql.upload.IRUploadCommPointBusService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 公共连接点母线电能质量统计 服务实现类
 * </p>
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RUploadCommPointBusServiceImpl extends MppServiceImpl<RUploadCommPointBusMapper, RUploadCommPointBus> implements IRUploadCommPointBusService {

    private final MonitorClient monitorClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final DeptFeignClient deptFeignClient;

    private final RStatIntegrityDMapper rStatIntegrityDMapper;

    private final GeneratrixWireMapper generatrixWireMapper;

    private final PmsMidLedgerMapper pmsMidLedgerMapper;

    private final PmsStatationStatMapper pmsStatationStatMapper;

    private final RStatDataVDMapper rStatDataVDMapper;

    private final RStatDataFlickerDMapper rStatDataFlickerDMapper;

    private final RStatLimitRateDMapper rStatLimitRateDMapper;


    @Override
    public void handlerDay(String date){
        List<RUploadCommPointBus> poLIst = new ArrayList<>();

        DictData dictDataStatus = dicDataFeignClient.getDicDataByCodeAndType(DicDataEnum.RUN.getCode(), DicDataTypeEnum.LINE_STATE.getCode()).getData();
        DictData dictDataType = dicDataFeignClient.getDicDataByCodeAndType(DicDataEnum.Pub_Connect_Point.getCode(), DicDataTypeEnum.LINE_TYPE.getCode()).getData();

        //电压
        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        Map<String,DictData> voltageMap = dictDataList.stream().collect(Collectors.toMap(DictData::getId,Function.identity()));

        String rootDeptId = deptFeignClient.getRootDept().getData().getId();
        List<Dept> deptList = deptFeignClient.getDirectSonSelf(rootDeptId).getData();
        Optional<Dept> optional = deptList.stream().filter(item->!item.getId().equals(rootDeptId)).findFirst();
        Dept provinceOrg;
        if(optional.isPresent()){
            provinceOrg = optional.get();
        }else {
            log.error("公共连接点算法执行异常,获取部门异常,请联系管理员排查");
            return;
        }

        List<Dept> deptL = deptFeignClient.getAllDept().getData();
        Map<String,Dept> deptIdMap = deptL.stream().collect(Collectors.toMap(Dept::getId,Function.identity()));
        Map<String,Dept> deptCodeMap = deptL.stream().collect(Collectors.toMap(Dept::getCode,Function.identity()));

        MonitorParam monitorParam = new MonitorParam();
        monitorParam.setIsUpToGrid(DataStateEnum.ENABLE.getCode());
        monitorParam.setMonitorState(dictDataStatus.getId());
        monitorParam.setMonitorType(dictDataType.getId());
        List<Monitor> monitorList = monitorClient.getMonitorListByParam(monitorParam).getData();
        if(CollUtil.isNotEmpty(monitorList)){
            //筛选所有测点
           List<String> monitorIds = monitorList.stream().map(Monitor::getId).distinct().collect(Collectors.toList());

           //获取灿能母线
            List<String> lineIds = monitorList.stream().map(Monitor::getLineId).distinct().collect(Collectors.toList());
            List<GeneratrixWire> generatrixWireList = generatrixWireMapper.selectBatchIds(lineIds);
            Map<String,GeneratrixWire> cnBusMap = generatrixWireList.stream().collect(Collectors.toMap(GeneratrixWire::getId,Function.identity()));

            //获取同源母线
            List<String> midBusIds = generatrixWireList.stream().map(GeneratrixWire::getMidBusId).distinct().collect(Collectors.toList());
            Map<String,PmsMidLedger> midMap = pmsMidLedgerMapper.selectBatchIds(midBusIds).stream().collect(Collectors.toMap(PmsMidLedger::getId,Function.identity()));

            //灿能电站
            List<String> stationIds = monitorList.stream().map(Monitor::getPowerrId).distinct().collect(Collectors.toList());
            List<StatationStat> stationList = pmsStatationStatMapper.selectBatchIds(stationIds);
            Map<String,StatationStat> cnStatMap = stationList.stream().collect(Collectors.toMap(StatationStat::getPowerId,Function.identity()));
            List<String> midStationIds = stationList.stream().map(StatationStat::getMidStationId).distinct().collect(Collectors.toList());
            Map<String,PmsMidLedger> midStationMap = pmsMidLedgerMapper.selectBatchIds(midStationIds).stream().collect(Collectors.toMap(PmsMidLedger::getId,Function.identity()));

            //监测点完整率
            List<RStatIntegrityD> rStatIntegrityDList = rStatIntegrityDMapper.selectList(new LambdaQueryWrapper<RStatIntegrityD>().eq(RStatIntegrityD::getTimeId,date).in(RStatIntegrityD::getLineIndex,monitorIds));
            Map<String,RStatIntegrityD> inteMap = rStatIntegrityDList.stream().collect(Collectors.toMap(RStatIntegrityD::getLineIndex, Function.identity()));

            //循环母线
            Map<String,List<Monitor>> mapMonitor = monitorList.stream().collect(Collectors.groupingBy(Monitor::getLineId));

            mapMonitor.forEach((busId,pointList)->{
                RUploadCommPointBus rUploadCommPointBus = new RUploadCommPointBus();
                GeneratrixWire generatrixWire = cnBusMap.get(busId);
                rUploadCommPointBus.setBusId(generatrixWire.getMidBusId());
                PmsMidLedger busMid = midMap.get(generatrixWire.getMidBusId());
                rUploadCommPointBus.setBusName(busMid.getName());
                rUploadCommPointBus.setBusVoltageLevel(String.format("%02d",voltageMap.get(busMid.getVoltageLevel()).getAlgoDescribe()));
                rUploadCommPointBus.setStatisticalType(DicDataEnum.STATISTICAL_TYPE_D.getCode());
                rUploadCommPointBus.setStatisticalDate(date);

                Monitor point;
                //rUploadCommPointBus.setCityOrg();
                if(pointList.size()>1){
                    //筛选最优测点进行数据统计
                    point = goodMonitor(pointList,inteMap);
                }else {
                    point = pointList.get(0);
                }


                rUploadCommPointBus.setProvinceOrg(provinceOrg.getCode());
                rUploadCommPointBus.setProvinceOrgName(provinceOrg.getName());

                Dept deptTem = deptIdMap.get(deptCodeMap.get(point.getOrgId()).getPid());
                rUploadCommPointBus.setCityOrg(deptTem.getCode());
                rUploadCommPointBus.setCityOrgName(deptTem.getName());

                rUploadCommPointBus.setMaintOrg(point.getOperationId());
                rUploadCommPointBus.setMaintOrgName(point.getOperationName());
                rUploadCommPointBus.setStationType("zf01");
                rUploadCommPointBus.setStationId(cnStatMap.get(point.getPowerrId()).getMidStationId());
                PmsMidLedger midStation = midStationMap.get(cnStatMap.get(point.getPowerrId()).getMidStationId());
                rUploadCommPointBus.setStationName(midStation.getName());
                rUploadCommPointBus.setStationVoltageLevel(String.format("%02d",voltageMap.get(midStation.getVoltageLevel()).getAlgoDescribe()));
                rUploadCommPointBus.setMonitorId(point.getMonitorId());
                rUploadCommPointBus.setDataSource("01");
                rUploadCommPointBus.setCnMonitorId(point.getId());
                rUploadCommPointBus.setUploadStatus(DataStateEnum.DELETED.getCode());
                poLIst.add(rUploadCommPointBus);
            });

            List<String> cnIds = poLIst.stream().map(RUploadCommPointBus::getCnMonitorId).distinct().collect(Collectors.toList());
            QueryWrapper<RStatDataVDPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("avg(rms) as rms,line_id").eq("time",date).in("line_id",cnIds).eq("value_type","AVG").groupBy("line_id");
            List<RStatDataVDPO> rStatDataVDList = rStatDataVDMapper.selectList(queryWrapper);
            Map<String,RStatDataVDPO> avgMap = rStatDataVDList.stream().collect(Collectors.toMap(RStatDataVDPO::getLineId,Function.identity()));

            queryWrapper.clear();
            queryWrapper.select("max(rms) as rms,line_id").eq("time",date).in("line_id",cnIds).eq("value_type","CP95").groupBy("line_id");
            List<RStatDataVDPO> cpList = rStatDataVDMapper.selectList(queryWrapper);
            Map<String,RStatDataVDPO> cpMap = cpList.stream().collect(Collectors.toMap(RStatDataVDPO::getLineId,Function.identity()));

            List<RStatLimitRateDPO> rStatLimitRateDPOS = rStatLimitRateDMapper.selectList(new LambdaQueryWrapper<RStatLimitRateDPO>().in(RStatLimitRateDPO::getLineId,cnIds).eq(RStatLimitRateDPO::getTime,date).eq(RStatLimitRateDPO::getPhasicType,"T"));
            Map<String,RStatLimitRateDPO> rStatLimitRateDPOMap = rStatLimitRateDPOS.stream().collect(Collectors.toMap(RStatLimitRateDPO::getLineId,Function.identity()));

            for(RUploadCommPointBus item: poLIst){
                item.setObjId(IdUtil.simpleUUID());
                if(avgMap.containsKey(item.getCnMonitorId())){
                   RStatDataVDPO rStatDataVDPO =  avgMap.get(item.getCnMonitorId());
                   item.setAvgVrms(rStatDataVDPO.getRms());
                }
                if(cpMap.containsKey(item.getCnMonitorId())){
                    RStatDataVDPO rStatDataVDPO =  cpMap.get(item.getCnMonitorId());
                    item.setgVrms(rStatDataVDPO.getRms());
                }

                Integer limitDay = 0;
                if(rStatLimitRateDPOMap.containsKey(item.getCnMonitorId())){
                    RStatLimitRateDPO r = rStatLimitRateDPOMap.get(item.getCnMonitorId());
                    int limit = maxOverTime(r, HarVoltage.class);
                    if(r.getAllTime()>0){
                        int i= 1440/r.getAllTime();
                        if(limit>0){
                            item.setHarmVOvDuration(limit*i);
                            item.setHarmVOvDays(1);
                            limitDay = 1;
                        }else {
                            item.setHarmVOvDuration(0);
                            item.setHarmVOvDays(0);
                        }

                        //三相不平衡
                        if(r.getUbalanceOvertime()>0){
                            item.setVunbanOvDuration(r.getUbalanceOvertime()*i);
                            item.setVunbanOvDays(1);
                            limitDay = 1;
                        }else {
                            item.setVunbanOvDuration(0);
                            item.setVunbanOvDays(0);
                        }
                    }

                    if(r.getFlickerAllTime() > 0){
                        int i= 1440/r.getFlickerAllTime();
                        //
                        if(r.getFlickerOvertime()>0){
                            item.setPltOvDuration(r.getFlickerOvertime()*i);
                            item.setPltOvDays(1);
                            limitDay = 1;
                        }else {
                            item.setPltOvDuration(0);
                            item.setPltOvDays(0);
                        }
                    }
                    item.setOvDays(limitDay);

                }
            }
            if(CollUtil.isNotEmpty(poLIst)){
                this.saveOrUpdateBatchByMultiId(poLIst);
            }
        }

    }


    public void handlerMonth(String date){
        DateTime begin = DateUtil.beginOfMonth(DateUtil.parse(date, DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.parse(date, DatePattern.NORM_DATE_PATTERN);

        String month = DateUtil.format(end,DatePattern.NORM_MONTH_PATTERN);

        LambdaQueryWrapper<RUploadCommPointBus> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(RUploadCommPointBus::getStatisticalDate,begin,end).eq(RUploadCommPointBus::getStatisticalType,DicDataEnum.STATISTICAL_TYPE_D.getCode());
        List<RUploadCommPointBus> rUploadCommPointBusList = this.list(lambdaQueryWrapper);

        List<RUploadCommPointBus> poLIst = new ArrayList<>();
        Map<String,List<RUploadCommPointBus>> map = rUploadCommPointBusList.stream().collect(Collectors.groupingBy(RUploadCommPointBus::getBusId));
        map.forEach((busId,list)->{
            RUploadCommPointBus one = list.get(0);
            RUploadCommPointBus rUploadCommPointBus = new RUploadCommPointBus();
            rUploadCommPointBus.setObjId(IdUtil.simpleUUID());
            rUploadCommPointBus.setBusId(busId);
            rUploadCommPointBus.setBusName(one.getBusName());
            rUploadCommPointBus.setBusVoltageLevel(one.getBusVoltageLevel());
            rUploadCommPointBus.setStatisticalType(DicDataEnum.STATISTICAL_TYPE_M.getCode());
            rUploadCommPointBus.setStatisticalDate(month);
            rUploadCommPointBus.setProvinceOrg(one.getProvinceOrg());
            rUploadCommPointBus.setProvinceOrgName(one.getProvinceOrgName());

            rUploadCommPointBus.setCityOrg(one.getCityOrg());
            rUploadCommPointBus.setCityOrgName(one.getCityOrgName());

            rUploadCommPointBus.setMaintOrg(one.getMaintOrg());
            rUploadCommPointBus.setMaintOrgName(one.getMaintOrgName());
            rUploadCommPointBus.setStationType("zf01");
            rUploadCommPointBus.setStationId(one.getStationId());
            rUploadCommPointBus.setStationName(one.getStationName());
            rUploadCommPointBus.setStationVoltageLevel(one.getStationVoltageLevel());

            rUploadCommPointBus.setDataSource("01");


            List<String> temList = list.stream().map(RUploadCommPointBus::getMonitorId).collect(Collectors.toList());
            String point = findMostFrequentString(temList);

            rUploadCommPointBus.setMonitorId(point);

            rUploadCommPointBus.setAvgVrms(list.stream().filter(item->Objects.nonNull(item.getAvgVrms())).mapToDouble(RUploadCommPointBus::getAvgVrms).average().orElse(0.0));
            rUploadCommPointBus.setgVrms(list.stream().filter(item->Objects.nonNull(item.getgVrms())).mapToDouble(RUploadCommPointBus::getgVrms).average().orElse(0.0));

            rUploadCommPointBus.setHarmVOvDuration(list.stream().filter(item->Objects.nonNull(item.getHarmVOvDuration())).mapToInt(RUploadCommPointBus::getHarmVOvDuration).sum());
            rUploadCommPointBus.setHarmVOvDays(list.stream().filter(item->Objects.nonNull(item.getHarmVOvDays())).mapToInt(RUploadCommPointBus::getHarmVOvDays).sum());
            rUploadCommPointBus.setVunbanOvDuration(list.stream().filter(item->Objects.nonNull(item.getVunbanOvDuration())).mapToInt(RUploadCommPointBus::getVunbanOvDuration).sum());
            rUploadCommPointBus.setVunbanOvDays(list.stream().filter(item->Objects.nonNull(item.getVunbanOvDays())).mapToInt(RUploadCommPointBus::getVunbanOvDays).sum());
            rUploadCommPointBus.setPltOvDuration(list.stream().filter(item->Objects.nonNull(item.getPltOvDuration())).mapToInt(RUploadCommPointBus::getPltOvDuration).sum());
            rUploadCommPointBus.setPltOvDays(list.stream().filter(item->Objects.nonNull(item.getPltOvDays())).mapToInt(RUploadCommPointBus::getPltOvDays).sum());
            rUploadCommPointBus.setOvDays(list.stream().filter(item->Objects.nonNull(item.getOvDays())).mapToInt(RUploadCommPointBus::getOvDays).sum());
            rUploadCommPointBus.setUploadStatus(DataStateEnum.DELETED.getCode());
            poLIst.add(rUploadCommPointBus);
        });

        if(CollUtil.isNotEmpty(poLIst)){
            this.saveOrUpdateBatchByMultiId(poLIst);
        }
    }


    public void handlerYear(String date){
        DateTime begin = DateUtil.beginOfYear(DateUtil.parse(date, DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.parse(date, DatePattern.NORM_DATE_PATTERN);

        String year = DateUtil.format(end,DatePattern.NORM_YEAR_PATTERN);

        LambdaQueryWrapper<RUploadCommPointBus> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(RUploadCommPointBus::getStatisticalDate,begin,end).eq(RUploadCommPointBus::getStatisticalType,DicDataEnum.STATISTICAL_TYPE_D.getCode());
        List<RUploadCommPointBus> rUploadCommPointBusList = this.list(lambdaQueryWrapper);

        List<RUploadCommPointBus> poLIst = new ArrayList<>();
        Map<String,List<RUploadCommPointBus>> map = rUploadCommPointBusList.stream().collect(Collectors.groupingBy(RUploadCommPointBus::getBusId));
        map.forEach((busId,list)->{
            RUploadCommPointBus one = list.get(0);

            RUploadCommPointBus rUploadCommPointBus = new RUploadCommPointBus();
            rUploadCommPointBus.setStatisticalType(DicDataEnum.STATISTICAL_TYPE_Y.getCode());
            rUploadCommPointBus.setStatisticalDate(year);

            rUploadCommPointBus.setObjId(IdUtil.simpleUUID());
            rUploadCommPointBus.setBusId(busId);
            rUploadCommPointBus.setBusName(one.getBusName());
            rUploadCommPointBus.setBusVoltageLevel(one.getBusVoltageLevel());
            rUploadCommPointBus.setProvinceOrg(one.getProvinceOrg());
            rUploadCommPointBus.setProvinceOrgName(one.getProvinceOrgName());

            rUploadCommPointBus.setCityOrg(one.getCityOrg());
            rUploadCommPointBus.setCityOrgName(one.getCityOrgName());

            rUploadCommPointBus.setMaintOrg(one.getMaintOrg());
            rUploadCommPointBus.setMaintOrgName(one.getMaintOrgName());
            rUploadCommPointBus.setStationType("zf01");
            rUploadCommPointBus.setStationId(one.getStationId());
            rUploadCommPointBus.setStationName(one.getStationName());
            rUploadCommPointBus.setStationVoltageLevel(one.getStationVoltageLevel());

            rUploadCommPointBus.setDataSource("01");

            List<String> temList = list.stream().map(RUploadCommPointBus::getMonitorId).collect(Collectors.toList());
            String point = findMostFrequentString(temList);

            rUploadCommPointBus.setMonitorId(point);

            rUploadCommPointBus.setAvgVrms(list.stream().filter(item->Objects.nonNull(item.getAvgVrms())).mapToDouble(RUploadCommPointBus::getAvgVrms).average().orElse(0.0));
            rUploadCommPointBus.setgVrms(list.stream().filter(item->Objects.nonNull(item.getgVrms())).mapToDouble(RUploadCommPointBus::getgVrms).average().orElse(0.0));

            rUploadCommPointBus.setHarmVOvDuration(list.stream().filter(item->Objects.nonNull(item.getHarmVOvDuration())).mapToInt(RUploadCommPointBus::getHarmVOvDuration).sum());
            rUploadCommPointBus.setHarmVOvDays(list.stream().filter(item->Objects.nonNull(item.getHarmVOvDays())).mapToInt(RUploadCommPointBus::getHarmVOvDays).sum());
            rUploadCommPointBus.setVunbanOvDuration(list.stream().filter(item->Objects.nonNull(item.getVunbanOvDuration())).mapToInt(RUploadCommPointBus::getVunbanOvDuration).sum());
            rUploadCommPointBus.setVunbanOvDays(list.stream().filter(item->Objects.nonNull(item.getVunbanOvDays())).mapToInt(RUploadCommPointBus::getVunbanOvDays).sum());
            rUploadCommPointBus.setPltOvDuration(list.stream().filter(item->Objects.nonNull(item.getPltOvDuration())).mapToInt(RUploadCommPointBus::getPltOvDuration).sum());
            rUploadCommPointBus.setPltOvDays(list.stream().filter(item->Objects.nonNull(item.getPltOvDays())).mapToInt(RUploadCommPointBus::getPltOvDays).sum());
            rUploadCommPointBus.setOvDays(list.stream().filter(item->Objects.nonNull(item.getOvDays())).mapToInt(RUploadCommPointBus::getOvDays).sum());
            rUploadCommPointBus.setUploadStatus(DataStateEnum.DELETED.getCode());
            poLIst.add(rUploadCommPointBus);
        });
        if(CollUtil.isNotEmpty(poLIst)){
            this.saveOrUpdateBatchByMultiId(poLIst);
        }
    }


    public String findMostFrequentString(List<String> strings) {
        if (strings == null || strings.isEmpty()) {
            throw new IllegalArgumentException("输入的列表不能为空");
        }

        // 使用HashMap来记录每个字符串及其出现次数
        Map<String, Integer> frequencyMap = new HashMap<>();
        for (String str : strings) {
            frequencyMap.put(str, frequencyMap.getOrDefault(str, 0) + 1);
        }

        // 初始化最大出现次数和对应的字符串
        int maxCount = 0;
        String mostFrequent = strings.get(0); // 假设列表不为空，则取第一个字符串作为初始值

        // 遍历HashMap找到出现次数最多的字符串
        for (Map.Entry<String, Integer> entry : frequencyMap.entrySet()) {
            if (entry.getValue() > maxCount) {
                maxCount = entry.getValue();
                mostFrequent = entry.getKey();
            }
        }

        return mostFrequent;
    }


    /***
     * 获取越限最大值
     * @param object
     * @param annotation
     * @return
     */
    private Integer maxOverTime(Object object, Class annotation) {
        Integer maxValue = 0;
        try {
            Class objClass = object.getClass();
            Field[] fields = objClass.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                boolean isAnon = field.isAnnotationPresent(annotation);
                if (isAnon) {
                    Object objValue = field.get(object);
                    if (objValue instanceof Integer) {
                        if ((Integer) objValue > maxValue) {
                            maxValue = (Integer) objValue;
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("获取越限最大值异常：{}", e.toString());
        }
        return maxValue;
    }


    /**
     * 选择最优测点，数据完整性最高
     * @param monitorList
     * @param inteMap
     * @return
     */
    private Monitor goodMonitor(List<Monitor> monitorList,Map<String,RStatIntegrityD> inteMap){
        Float val = 0.00f;
        Monitor monitorRes = monitorList.get(0);
        for(Monitor monitor:monitorList){
            if(inteMap.containsKey(monitor.getId())){
                RStatIntegrityD rStatIntegrityD = inteMap.get(monitor.getId());
                float tem = (float)rStatIntegrityD.getRealTime()/rStatIntegrityD.getDueTime();
                if(tem>val){
                    val = tem;
                    monitorRes = monitor;
                }
            }
        }
        return monitorRes;
    }



}
