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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.harmonic.pojo.po.day.RStatLimitRateDPO;
import com.njcn.harmonic.pojo.po.day.RStatLimitTargetDPO;
import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.prepare.harmonic.mapper.mysql.day.RStatLimitTargetDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.day.IRStatLimitRateDService;
import com.njcn.prepare.harmonic.service.mysql.line.LimitTargetService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 生成limit_target表
 *
 * @author xiaoyao
 * @version 1.0.0
 * @createTime 2022/10/26 13:41
 */
@Slf4j
@Service
@AllArgsConstructor
public class LimitTargetServiceImpl extends MppServiceImpl<RStatLimitTargetDMapper, RStatLimitTargetDPO> implements LimitTargetService {


    private final IRStatLimitRateDService statLimitRateDService;


    /***********************************************新算法*******************************************************************/
    @Override
    public void limitTargetData(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"r_stat_limit_target_d开始执行=======》");
        List<RStatLimitTargetDPO> list = new ArrayList<>();
        List<RStatLimitRateDPO> limitRateData = new ArrayList<>();
        String startTime = LocalDateTimeUtil.format(
                LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)),
                DatePattern.NORM_DATETIME_PATTERN
        );
        String endTime = LocalDateTimeUtil.format(
                LocalDateTimeUtil.endOfDay(LocalDateTimeUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)),
                DatePattern.NORM_DATETIME_PATTERN
        );
        List<String> lineIds = calculatedParam.getIdList();
        LocalDate dateOut = LocalDate.parse(calculatedParam.getDataDate(),DatePattern.NORM_DATE_FORMATTER);
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(lineIds,5);
        for (List<String> pendingId : pendingIds) {
            limitRateData.addAll(getLimitRateDataFromMysql(pendingId,startTime,endTime));
        }
        if (!CollectionUtils.isEmpty(limitRateData)){
            for (RStatLimitRateDPO limitRate : limitRateData){
                RStatLimitTargetDPO limitTarget = new RStatLimitTargetDPO();
                limitTarget.setTime(dateOut);
                limitTarget.setLineId(limitRate.getLineId());
                limitTarget.setPhasicType(limitRate.getPhasicType());
                chanelData(limitRate,limitTarget);
                list.add(limitTarget);
            }
        }
        this.saveOrUpdateBatchByMultiId(list,100);
    }

    private List<RStatLimitRateDPO> getLimitRateDataFromMysql(List<String> lineList,String startTime,String endTime){
        return statLimitRateDService.findList(lineList,startTime,endTime);
    }

    /***********************************************新算法结束*******************************************************************/


    private void chanelData(RStatLimitRateDPO limitRate, RStatLimitTargetDPO limitTarget){
        //有一个指标超标，allTime则为1
        int allTime = limitRate.getFreqDevOvertime() + limitRate.getVoltageDevOvertime() + limitRate.getUbalanceOvertime() + limitRate.getUaberranceOvertime() + limitRate.getINegOvertime();
        try {
            Class<?> clazz = limitRate.getClass();
            for (int i = 2; i <= 25; i++) {
                String methodName = "getUharm" + i + "Overtime";
                String methodName2 = "getIharm" + i + "Overtime";
                Method method = clazz.getMethod(methodName);
                int overtime = (int) method.invoke(limitRate);
                allTime += overtime;
                Method method2 = clazz.getMethod(methodName2);
                int overtime2 = (int) method2.invoke(limitRate);
                allTime += overtime2;
            }
            for (int i = 1; i <= 16; i++) {
                String methodName = "getInuharm" + i + "Overtime";
                Method method = clazz.getMethod(methodName);
                int overtime = (int) method.invoke(limitRate);
                allTime += overtime;
            }
        } catch (Exception e) {
            e.getMessage();
        }
        limitTarget.setAllTime((allTime>0)?1:0);
        //闪变指标超标，flickerAllTime则为1
        limitTarget.setFlickerAllTime((limitRate.getFlickerOvertime()>0)?1:0);
        limitTarget.setFreqDevOvertime((limitRate.getFreqDevOvertime()>0)?1:0);
        limitTarget.setVoltageDevOvertime((limitRate.getVoltageDevOvertime()>0)?1:0);
        limitTarget.setUbalanceOvertime((limitRate.getUbalanceOvertime()>0)?1:0);
        limitTarget.setFlickerOvertime((limitRate.getFlickerOvertime()>0)?1:0);
        limitTarget.setUaberranceOvertime((limitRate.getUaberranceOvertime()>0)?1:0);
        limitTarget.setINegOvertime((limitRate.getINegOvertime()>0)?1:0);

        limitTarget.setUharm2Overtime((limitRate.getUharm2Overtime()>0)?1:0);
        limitTarget.setUharm3Overtime((limitRate.getUharm3Overtime()>0)?1:0);
        limitTarget.setUharm4Overtime((limitRate.getUharm4Overtime()>0)?1:0);
        limitTarget.setUharm5Overtime((limitRate.getUharm5Overtime()>0)?1:0);
        limitTarget.setUharm6Overtime((limitRate.getUharm6Overtime()>0)?1:0);
        limitTarget.setUharm7Overtime((limitRate.getUharm7Overtime()>0)?1:0);
        limitTarget.setUharm8Overtime((limitRate.getUharm8Overtime()>0)?1:0);
        limitTarget.setUharm9Overtime((limitRate.getUharm9Overtime()>0)?1:0);
        limitTarget.setUharm10Overtime((limitRate.getUharm10Overtime()>0)?1:0);
        limitTarget.setUharm11Overtime((limitRate.getUharm11Overtime()>0)?1:0);
        limitTarget.setUharm12Overtime((limitRate.getUharm12Overtime()>0)?1:0);
        limitTarget.setUharm13Overtime((limitRate.getUharm13Overtime()>0)?1:0);
        limitTarget.setUharm14Overtime((limitRate.getUharm14Overtime()>0)?1:0);
        limitTarget.setUharm15Overtime((limitRate.getUharm15Overtime()>0)?1:0);
        limitTarget.setUharm16Overtime((limitRate.getUharm16Overtime()>0)?1:0);
        limitTarget.setUharm17Overtime((limitRate.getUharm17Overtime()>0)?1:0);
        limitTarget.setUharm18Overtime((limitRate.getUharm18Overtime()>0)?1:0);
        limitTarget.setUharm19Overtime((limitRate.getUharm19Overtime()>0)?1:0);
        limitTarget.setUharm20Overtime((limitRate.getUharm20Overtime()>0)?1:0);
        limitTarget.setUharm21Overtime((limitRate.getUharm21Overtime()>0)?1:0);
        limitTarget.setUharm22Overtime((limitRate.getUharm22Overtime()>0)?1:0);
        limitTarget.setUharm23Overtime((limitRate.getUharm23Overtime()>0)?1:0);
        limitTarget.setUharm24Overtime((limitRate.getUharm24Overtime()>0)?1:0);
        limitTarget.setUharm25Overtime((limitRate.getUharm25Overtime()>0)?1:0);

        limitTarget.setIharm2Overtime((limitRate.getIharm2Overtime()>0)?1:0);
        limitTarget.setIharm3Overtime((limitRate.getIharm3Overtime()>0)?1:0);
        limitTarget.setIharm4Overtime((limitRate.getIharm4Overtime()>0)?1:0);
        limitTarget.setIharm5Overtime((limitRate.getIharm5Overtime()>0)?1:0);
        limitTarget.setIharm6Overtime((limitRate.getIharm6Overtime()>0)?1:0);
        limitTarget.setIharm7Overtime((limitRate.getIharm7Overtime()>0)?1:0);
        limitTarget.setIharm8Overtime((limitRate.getIharm8Overtime()>0)?1:0);
        limitTarget.setIharm9Overtime((limitRate.getIharm9Overtime()>0)?1:0);
        limitTarget.setIharm10Overtime((limitRate.getIharm10Overtime()>0)?1:0);
        limitTarget.setIharm11Overtime((limitRate.getIharm11Overtime()>0)?1:0);
        limitTarget.setIharm12Overtime((limitRate.getIharm12Overtime()>0)?1:0);
        limitTarget.setIharm13Overtime((limitRate.getIharm13Overtime()>0)?1:0);
        limitTarget.setIharm14Overtime((limitRate.getIharm14Overtime()>0)?1:0);
        limitTarget.setIharm15Overtime((limitRate.getIharm15Overtime()>0)?1:0);
        limitTarget.setIharm16Overtime((limitRate.getIharm16Overtime()>0)?1:0);
        limitTarget.setIharm17Overtime((limitRate.getIharm17Overtime()>0)?1:0);
        limitTarget.setIharm18Overtime((limitRate.getIharm18Overtime()>0)?1:0);
        limitTarget.setIharm19Overtime((limitRate.getIharm19Overtime()>0)?1:0);
        limitTarget.setIharm20Overtime((limitRate.getIharm20Overtime()>0)?1:0);
        limitTarget.setIharm21Overtime((limitRate.getIharm21Overtime()>0)?1:0);
        limitTarget.setIharm22Overtime((limitRate.getIharm22Overtime()>0)?1:0);
        limitTarget.setIharm23Overtime((limitRate.getIharm23Overtime()>0)?1:0);
        limitTarget.setIharm24Overtime((limitRate.getIharm24Overtime()>0)?1:0);
        limitTarget.setIharm25Overtime((limitRate.getIharm25Overtime()>0)?1:0);

        limitTarget.setInuharm1Overtime((limitRate.getInuharm1Overtime()>0)?1:0);
        limitTarget.setInuharm2Overtime((limitRate.getInuharm2Overtime()>0)?1:0);
        limitTarget.setInuharm3Overtime((limitRate.getInuharm3Overtime()>0)?1:0);
        limitTarget.setInuharm4Overtime((limitRate.getInuharm4Overtime()>0)?1:0);
        limitTarget.setInuharm5Overtime((limitRate.getInuharm5Overtime()>0)?1:0);
        limitTarget.setInuharm6Overtime((limitRate.getInuharm6Overtime()>0)?1:0);
        limitTarget.setInuharm7Overtime((limitRate.getInuharm7Overtime()>0)?1:0);
        limitTarget.setInuharm8Overtime((limitRate.getInuharm8Overtime()>0)?1:0);
        limitTarget.setInuharm9Overtime((limitRate.getInuharm9Overtime()>0)?1:0);
        limitTarget.setInuharm10Overtime((limitRate.getInuharm10Overtime()>0)?1:0);
        limitTarget.setInuharm11Overtime((limitRate.getInuharm11Overtime()>0)?1:0);
        limitTarget.setInuharm12Overtime((limitRate.getInuharm12Overtime()>0)?1:0);
        limitTarget.setInuharm13Overtime((limitRate.getInuharm13Overtime()>0)?1:0);
        limitTarget.setInuharm14Overtime((limitRate.getInuharm14Overtime()>0)?1:0);
        limitTarget.setInuharm15Overtime((limitRate.getInuharm15Overtime()>0)?1:0);
        limitTarget.setInuharm16Overtime((limitRate.getInuharm16Overtime()>0)?1:0);
    }
}
