package com.njcn.prepare.harmonic.service.mysql.Impl.area;


import com.njcn.prepare.harmonic.constant.AlgorithmParam;

import com.njcn.prepare.harmonic.pojo.param.PrimaryGridParam;
import com.njcn.prepare.harmonic.service.mysql.area.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

import static com.njcn.system.pojo.constant.DicDataConstant.NAME_KEY;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/12/13  14:34【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@Deprecated
public class AreaCountServiceImpl implements AreaCountService {


   /* private @Autowired
    PmsMonitorPOService pmsMonitorPOService;


    private @Autowired
    RMpIntegrityDPOService rMpIntegrityDPOService;


    private @Autowired
    RStatAbnormalDPOMapper rStatAbnormalDPOMapper;

    private @Autowired
    DistributionMonitorClient distributionMonitorClient;

    private @Autowired
    RStatOrgDService rStatOrgDService;

    private @Autowired
    RStatOrgMService rStatOrgMService;


    private @Autowired
    RMpTargetWarnDService rMpTargetWarnDService;


    private @Autowired
    RedisUtil redisUtil;*/

    @Override
    @Async("asyncExecutor")
    public void areaCountHanlder(PrimaryGridParam primaryGridParam) {
        if (Objects.equals (primaryGridParam.getDataSource ( ), AlgorithmParam.ORACLE_DATA)) {
            //this.hanlderOracleData (primaryGridParam);
        } else if (Objects.equals (primaryGridParam.getDataSource ( ), AlgorithmParam.INFLUX_DATA)) {
            this.hanlderInfludxbData (primaryGridParam);
        }
    }

    /**
     * @Description: 处理influxdb数据 todo 本次不做处理
     * @Param: [primaryGridParam]
     * @return: void
     * @Author: clam
     * @Date: 2022/11/25
     */
    private void hanlderInfludxbData(PrimaryGridParam primaryGridParam) {
    }

    /**
     * @Description: hanlderOracleData 目前只处理日表
     * @Param: [primaryGridParam]
     * @return: void
     * @Author: clam
     * @Date: 2022/12/13
     */
 /*   private void hanlderOracleData(PrimaryGridParam primaryGridParam) {

        Integer type = primaryGridParam.getType ( );
        switch (type) {
//            case 1:
//                this.hanlderOracleYear (primaryGridParam);
//                break;
//            case 2:
//                this.hanlderOracleQtr (primaryGridParam);
//                break;
            case 3:
                this.hanlderOracleMonth (primaryGridParam);
                break;
            case 5:
                this.hanlderOracleDay (primaryGridParam);
                break;
            default:
                break;
        }
    }*/
/*    @SneakyThrows
    private void hanlderOracleMonth(PrimaryGridParam primaryGridParam) {
        List<String> orgIdList = primaryGridParam.getOrgIdList ( );
        String dataDate = primaryGridParam.getDataDate ( );
        List<RStatOrgMPO> rStatOrgMPOList = new ArrayList<> ( );

        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);
        *//*在运的字典表id，配网1，2，3类监测点字典表id用于for循环中过滤*//*
        String lineTypeOneId = redisUtil.getStringByKey (NAME_KEY +
                SPLIT_SIGN + DicDataTypeEnum.LINE_SORT.getCode ( ) +
                SPLIT_SIGN + DicDataEnum.ONE_LINE.getCode ( ));
        String lineTypeTwoId = redisUtil.getStringByKey (NAME_KEY +
                SPLIT_SIGN + DicDataTypeEnum.LINE_SORT.getCode ( ) +
                SPLIT_SIGN + DicDataEnum.TWO_LINE.getCode ( ));
        String lineTypeThreeId = redisUtil.getStringByKey (NAME_KEY +
                SPLIT_SIGN + DicDataTypeEnum.LINE_SORT.getCode ( ) +
                SPLIT_SIGN + DicDataEnum.THREE_LINE.getCode ( ));
        if (
                StringUtils.isEmpty (lineTypeOneId) ||
                        StringUtils.isEmpty (lineTypeTwoId) ||
                        StringUtils.isEmpty (lineTypeThreeId)) {
            throw new BusinessException (PqsPrepareExceptionEnum.REDIS_NULL_EXCEPTION);
        }
        orgIdList.forEach (orgid -> {
                    *//*1.2.2.1.	主网区域分类有效接入监测点数*//*
                    *//*主网在运监测点*//*
                    List<Monitor> pmsMonitorPOS = pmsMonitorPOService.qurymeasurementRunPoints (orgid);
                    *//* 根据监测点对象类型分组获取监测点*//*
                    Map<String, List<PmsMonitorPO>> collect = pmsMonitorPOS.stream ( ).collect (Collectors.groupingBy (PmsMonitorPO::getMonitorObjectType));
                    for (Map.Entry<String, List<PmsMonitorPO>> entry : collect.entrySet ( )) {
                        *//*计算出当前机构不同监测点对象类型累计稳态超标监测点数*//*
                        List<String> monitorIdList = entry.getValue ( ).stream ( ).map (PmsMonitorPO::getId).collect (Collectors.toList ( ));
                        Integer integer = rMpTargetWarnDService.queryHarmonicMeasurementAccrued (monitorIdList, dataDate);
                        RStatOrgMPO rStatOrgMPO = new RStatOrgMPO ( );
                        rStatOrgMPO.setOrgNo (orgid);
                        rStatOrgMPO.setMeasurementTypeClass (entry.getKey ( ));
                        rStatOrgMPO.setDataDate (LocalDate.parse(dataDate));
                        rStatOrgMPO.setDataType ("01");
                        rStatOrgMPO.setHarmonicMeasurementAccrued (integer);
                        rStatOrgMPOList.add (rStatOrgMPO);
                    }
                    *//*	配网区域分类1.2.3类有效接入监测点数*//*
                    *//*monitorSort=""获取配网监1,2,3类测点全部数据*//*
                    RStatOrgMPO rStatOrgMPOOne = packageRStatOrgMPO (orgid, lineTypeOneId, dataDate);
                    RStatOrgMPO rStatOrgMPOTwo = packageRStatOrgMPO (orgid, lineTypeTwoId, dataDate);
                    RStatOrgMPO rStatOrgMPOThree = packageRStatOrgMPO (orgid, lineTypeThreeId, dataDate);

                    rStatOrgMPOList.add (rStatOrgMPOOne);
                    rStatOrgMPOList.add (rStatOrgMPOTwo);
                    rStatOrgMPOList.add (rStatOrgMPOThree);
                }

        );
        rStatOrgMService.saveOrUpdateBatchByMultiId (rStatOrgMPOList,500);
    }*/


    /**
     * @Description: hanlderOracleDay
     * @Param: [primaryGridParam]
     * @return: void
     * @Author: clam
     * @Date: 2022/12/13
     */
  /*  @SneakyThrows
    private void hanlderOracleDay(PrimaryGridParam primaryGridParam) {

        List<String> orgIdList = primaryGridParam.getOrgIdList ( );
        String dataDate = primaryGridParam.getDataDate ( );
        List<RStatOrgDPO> rStatOrgDPOList = new ArrayList<> ( );

        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);
        *//*在运的字典表id，配网1，2，3类监测点字典表id用于for循环中过滤*//*
        String lineTypeOneId = redisUtil.getStringByKey (NAME_KEY +
                SPLIT_SIGN + DicDataTypeEnum.LINE_SORT.getCode ( ) +
                SPLIT_SIGN + DicDataEnum.ONE_LINE.getCode ( ));
        String lineTypeTwoId = redisUtil.getStringByKey (NAME_KEY +
                SPLIT_SIGN + DicDataTypeEnum.LINE_SORT.getCode ( ) +
                SPLIT_SIGN + DicDataEnum.TWO_LINE.getCode ( ));
        String lineTypeThreeId = redisUtil.getStringByKey (NAME_KEY +
                SPLIT_SIGN + DicDataTypeEnum.LINE_SORT.getCode ( ) +
                SPLIT_SIGN + DicDataEnum.THREE_LINE.getCode ( ));
        if (
                StringUtils.isEmpty (lineTypeOneId) ||
                        StringUtils.isEmpty (lineTypeTwoId) ||
                        StringUtils.isEmpty (lineTypeThreeId)) {
            throw new BusinessException (PqsPrepareExceptionEnum.REDIS_NULL_EXCEPTION);
        }
        orgIdList.forEach (orgid -> {
                    *//*1.2.2.1.	主网区域分类有效接入监测点数*//*
                    *//*主网在运监测点*//*
                    List<Monitor> pmsMonitorPOS = pmsMonitorPOService.qurymeasurementRunPoints (orgid);
                    *//* 根据监测点对象类型分组获取监测点*//*
                    Map<String, List<PmsMonitorPO>> collect = pmsMonitorPOS.stream ( ).collect (Collectors.groupingBy (Monitor::getMonitorObjectType));
                    for (Map.Entry<String, List<PmsMonitorPO>> entry : collect.entrySet ( )) {
                        *//*计算出当前监测点对象类型有效接入监测点个数*//*
                        List<String> monitorIdList = entry.getValue ( ).stream ( ).map (PmsMonitorPO::getId).collect (Collectors.toList ( ));
                        Integer integer = this.queryEffectiveAccessMeasurementCount (monitorIdList, dataDate);
                        RStatOrgDPO rStatOrgDPO = new RStatOrgDPO ( );
                        rStatOrgDPO.setOrgNo (orgid);
                        rStatOrgDPO.setMeasurementTypeClass (entry.getKey ( ));
                        rStatOrgDPO.setDataDate (LocalDate.parse(dataDate));
                        rStatOrgDPO.setDataType ("01");
                        rStatOrgDPO.setEffectiveMeasurement (integer);
                        rStatOrgDPOList.add (rStatOrgDPO);
                    }
                    *//*	配网区域分类1.2.3类有效接入监测点数*//*
                    *//*monitorSort=""获取配网监1,2,3类测点全部数据*//*
                    RStatOrgDPO rStatOrgDPOOne = packageRStatOrgDPO (orgid, lineTypeOneId, dataDate);
                    RStatOrgDPO rStatOrgDPOTwo = packageRStatOrgDPO (orgid, lineTypeTwoId, dataDate);
                    RStatOrgDPO rStatOrgDPOThree = packageRStatOrgDPO (orgid, lineTypeThreeId, dataDate);

                    rStatOrgDPOList.add (rStatOrgDPOOne);
                    rStatOrgDPOList.add (rStatOrgDPOTwo);
                    rStatOrgDPOList.add (rStatOrgDPOThree);
                }

        );
        rStatOrgDService.saveOrUpdateBatchByMultiId (rStatOrgDPOList,500);


    }*/

   /* @SneakyThrows
    private RStatOrgMPO packageRStatOrgMPO(String orgid, String lineTypeId, String dataDate) {
        RStatOrgMPO rStatOrgMPO = new RStatOrgMPO ( );
        List<DistributionMonitor> distributionMonitorList = distributionMonitorClient.distributionMonitorListByCondition (orgid, lineTypeId).getData ( );
        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);

        *//*计算出当前监测点对象类型有效接入监测点个数*//*
        List<String> monitorIdList = distributionMonitorList.stream ( ).map (DistributionMonitor::getMonitorId).collect (Collectors.toList ( ));

        Integer integer = rMpTargetWarnDService.queryHarmonicMeasurementAccrued (monitorIdList, dataDate);
        rStatOrgMPO.setOrgNo (orgid);
        rStatOrgMPO.setMeasurementTypeClass (lineTypeId);
        rStatOrgMPO.setDataDate (LocalDate.parse(dataDate));
        rStatOrgMPO.setDataType ("02");
        rStatOrgMPO.setHarmonicMeasurementAccrued (integer);
        return rStatOrgMPO;
    }

    *//**
     * @Description: 计算1，2，3类配网有效接入监测 后包装成RStatOrgDPO
     * @Param: [orgid, lineTypeId, dataDate]
     * @return: com.njcn.prepare.harmonic.pojo.mysql.po.RStatOrgDPO
     * @Author: clam
     * @Date: 2022/12/14
     *//*
    @SneakyThrows
    private RStatOrgDPO packageRStatOrgDPO(String orgid, String lineTypeId, String dataDate) {
        RStatOrgDPO rStatOrgDPO = new RStatOrgDPO ( );
        List<DistributionMonitor> distributionMonitorList = distributionMonitorClient.distributionMonitorListByCondition (orgid, lineTypeId).getData ( );
        final Date tempDate = new SimpleDateFormat ("yyyy-MM-dd").parse (dataDate);

        *//*计算出当前监测点对象类型有效接入监测点个数*//*
        List<String> monitorIdList = distributionMonitorList.stream ( ).map (DistributionMonitor::getMonitorId).collect (Collectors.toList ( ));
        Integer integer = this.queryEffectiveAccessMeasurementCount (monitorIdList, dataDate);
        rStatOrgDPO.setOrgNo (orgid);
        rStatOrgDPO.setMeasurementTypeClass (lineTypeId);
        rStatOrgDPO.setDataDate (LocalDate.parse(dataDate));
        rStatOrgDPO.setDataType ("02");
        rStatOrgDPO.setEffectiveMeasurement (integer);

        return rStatOrgDPO;
    }

    *//**
     * @Description: 有效接入监测点个数
     * 1.获取pms_monitor表中监测点状态为投运的监测点id列表
     * 2.根据id查询r_mp_integrity_d表中当天各项指标个数，采用以下公式计算数据完整率
     * 3.〖数据完整率〗_日统计=(频率+相电压+线电压+畸变率+不平衡度)/(有效值数据个数*5)*100%
     * 判定监测点数据完整率是否大于95%
     * 4、根据id查询r_stat_abnormal_d表，value_alarm为1。
     * 5、以上两个条件满足表明该监测点为有效监测点。
     * 6、统计有效监测点个数
     * @Param: [indexlist, dataDate]
     * @return: java.lang.String
     * @Author: clam
     * @Date: 2022/11/25
     *//*
    private Integer queryEffectiveAccessMeasurementCount(List<String> indexlist, String dataDate) {

        *//*获取监测点的数据完整性*//*
        List<MonitorDataIntegrityDTO> monitorDataIntegrityDTOList = rMpIntegrityDPOService.calculateDataIntegrity (indexlist, dataDate);
        *//*获取完整率>0.95的监测点id*//*
        List<String> collect = monitorDataIntegrityDTOList.stream ( ).
                filter (temp -> temp.getDataIntegrity ( ).compareTo (new BigDecimal (0.95)) == 1
                ).map (MonitorDataIntegrityDTO::getId).collect (Collectors.toList ( ));
        *//*获取完整率>0.95条件下value_alarm为1的监测点id*//*
        if (CollectionUtils.isEmpty (collect)) {
            return 0;
        }
        QueryWrapper<RStatAbnormalDPO> rStatAbnormalDPOLambdaQueryWrapper = new QueryWrapper ( );
        rStatAbnormalDPOLambdaQueryWrapper.select ("measurement_point_id").
                in ("measurement_point_id", collect).
                eq ("value_alarm", 1).
                eq ("DATE_FORMAT( data_date ,'%Y-%m-%d')", dataDate);
        List<RStatAbnormalDPO> rStatAbnormalDPOS = rStatAbnormalDPOMapper.selectList (rStatAbnormalDPOLambdaQueryWrapper);

        return rStatAbnormalDPOS.size ( );

    }*/
}
