package com.njcn.harmonic.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.dataProcess.param.LineCountEvaluateParam;
import com.njcn.dataProcess.pojo.dto.AbnormalData;
import com.njcn.dataProcess.pojo.dto.DataLimitRateDetailTimeDto;
import com.njcn.dataProcess.pojo.po.RStatLimitRateDetailD;
import com.njcn.harmonic.service.IDataLimitRateDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: cdf
 * @CreateTime: 2026-07-01
 * @Description:
 */
@Service()
@RequiredArgsConstructor
public class RelationDataLimitRateDetailImpl extends MppServiceImpl<RStatLimitRateDetailRelationMapper, RStatLimitRateDetailD> implements IDataLimitRateDetailService {



    @Override
    public List<DataLimitRateDetailTimeDto> getLimitRateDetailTime(LineCountEvaluateParam lineParam) {
        List<DataLimitRateDetailTimeDto> info = new ArrayList<>();
        LambdaQueryWrapper<RStatLimitRateDetailD> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CollUtil.isNotEmpty(lineParam.getLineId()), RStatLimitRateDetailD::getLineId, lineParam.getLineId())
                .ge(RStatLimitRateDetailD::getTime, lineParam.getStartTime())
                .le(RStatLimitRateDetailD::getTime, lineParam.getEndTime())
                .orderByAsc(RStatLimitRateDetailD::getTime)
        ;

        List<RStatLimitRateDetailD> list = this.list(lambdaQueryWrapper);
        DataLimitRateDetailTimeDto dto;
        for (RStatLimitRateDetailD detailD : list) {
            dto = new DataLimitRateDetailTimeDto();
            dto.setLineId(detailD.getLineId());
            dto.setTime(detailD.getTime().format((DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))));
            dto.setFlickerOvertime(toList(detailD.getFlickerOvertime()));
            dto.setFreqDevOvertime(toList(detailD.getFreqDevOvertime()));
            dto.setVoltageDevOvertime(toList(detailD.getVoltageDevOvertime()));
            dto.setUbalanceOvertime(toList(detailD.getUbalanceOvertime()));
            dto.setUaberranceOvertime(toList(detailD.getUaberranceOvertime()));
            dto.setINegOvertime(toList(detailD.getINegOvertime()));
            dto.setUharmOvertime(toList(detailD,2,25,"getUharm"));
            dto.setIharmOvertime(toList(detailD,2,25,"getIharm"));
            dto.setInuharmOvertime(toList(detailD,1,16,"getInuharm"));
            info.add(dto);
        }
        return info;
    }

    private List<String> toList(RStatLimitRateDetailD detailD,Integer start, Integer end, String targetName){
        List<AbnormalData.Json> json = new ArrayList<>();
        for (int i = start; i <= end; i++) {
            // 构造方法名
            String methodName = targetName + i + "Overtime";
            try {
                // 获取 DataHarmDto 类的 getVx 方法
                Method getVMethod = RStatLimitRateDetailD.class.getMethod(methodName);
                String value = (String) getVMethod.invoke(detailD);
                if(StrUtil.isNotBlank(value)){
                    json.addAll(JSON.parseArray(value, AbnormalData.Json.class));
                }
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return getString(json);
    }

    private List<String> toList(String json){
        List<AbnormalData.Json> jsons = JSON.parseArray(json, AbnormalData.Json.class);
        return getString(jsons);
    }

    private List<String> getString(List<AbnormalData.Json> jsons) {
        if (CollUtil.isNotEmpty(jsons)){
            List<String> times = jsons.stream().map(AbnormalData.Json::getTime).collect(Collectors.toList());
            String join = String.join(",", times);
            String[] split = join.split(",");
            return Arrays.stream(split).distinct().collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
