package com.njcn.gather.script.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.gather.detection.pojo.enums.DetectionCodeEnum;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.device.pojo.enums.CommonEnum;
import com.njcn.gather.device.pojo.po.PqDev;
import com.njcn.gather.device.pojo.vo.PqDevVO;
import com.njcn.gather.device.service.IPqDevService;
import com.njcn.gather.plan.mapper.AdPlanMapper;
import com.njcn.gather.plan.pojo.po.AdPlan;
import com.njcn.gather.report.pojo.enums.PowerIndexEnum;
import com.njcn.gather.script.mapper.PqScriptCheckDataMapper;
import com.njcn.gather.script.mapper.PqScriptDtlsMapper;
import com.njcn.gather.script.mapper.PqScriptMapper;
import com.njcn.gather.script.pojo.param.PqScriptCheckDataParam;
import com.njcn.gather.script.pojo.param.PqScriptDtlsParam;
import com.njcn.gather.script.pojo.param.PqScriptIssueParam;
import com.njcn.gather.script.pojo.param.ScriptParam;
import com.njcn.gather.script.pojo.po.PqScript;
import com.njcn.gather.script.pojo.po.PqScriptCheckData;
import com.njcn.gather.script.pojo.po.PqScriptDtls;
import com.njcn.gather.script.pojo.po.SourceIssue;
import com.njcn.gather.script.pojo.vo.PqScriptDtlDataVO;
import com.njcn.gather.script.service.IPqScriptCheckDataService;
import com.njcn.gather.script.service.IPqScriptDtlsService;
import com.njcn.gather.script.util.ScriptDtlsDesc;
import com.njcn.gather.script.util.ThreePhaseUnbalance;
import com.njcn.gather.source.service.IPqSourceService;
import com.njcn.gather.system.dictionary.pojo.po.DictTree;
import com.njcn.gather.system.dictionary.service.IDictTreeService;
import com.njcn.gather.type.pojo.po.DevType;
import com.njcn.gather.type.service.IDevTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author caozehui
 * @date 2024-11-18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PqScriptDtlsServiceImpl extends ServiceImpl<PqScriptDtlsMapper, PqScriptDtls> implements IPqScriptDtlsService {

    private final static String FREQ = "Freq";
    private final static String VOL = "VOL";
    private final static String CUR = "CUR";
    private final static String HARM_V = "Harm_V";
    private final static String HARM_I = "Harm_I";
    private final static String INHARM_V = "InHarm_V";
    private final static String INHARM_I = "InHarm_I";
    private final static String DIP = "Dip";
    private final static String FLICKER = "Flicker";
    //    @Value("${Dip.fPreTime}")
//    private Double fPreTime;
//    @Value("${Dip.fRampIn}")
//    private Double fRampIn;
//    @Value("${Dip.fRampOut}")
//    private Double fRampOut;
//    @Value("${Dip.fAfterTime}")
//    private Double fAfterTime;
    private static final String waveFluType = "CPM";
    private static final String waveType = "SQU";
    private static final Double fDutyCycle = 50.0;


    private final IPqDevService pqDevService;
    private final PqScriptMapper pqScriptMapper;
    private final IPqScriptCheckDataService pqScriptCheckDataService;
    private final PqScriptCheckDataMapper pqScriptCheckDataMapper;
    private final IDevTypeService devTypeService;
    private final IDictTreeService dictTreeService;
    private final AdPlanMapper adPlanMapper;
    private final IPqSourceService pqSourceService;

    @Override
    @Transactional
    public boolean savePqScriptDtls(PqScriptDtls pqScriptDtls) {
        return this.save(pqScriptDtls);
    }

    @Override
    @Transactional
    public boolean deletePqScriptDtlsByIds(List<String> ids) {
        LambdaQueryWrapper<PqScriptDtls> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PqScriptDtls::getId, ids);
        return this.remove(queryWrapper);
    }

    @Override
    @Transactional
    public boolean deletePqScriptDtlsByScriptId(List<String> scriptIds) {
        LambdaQueryWrapper<PqScriptDtls> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PqScriptDtls::getScriptId, scriptIds);
        return this.remove(queryWrapper);
    }

    @Override
    @Transactional
    public boolean updatePqScriptDtls(PqScriptDtls pqScriptDtls) {
        return this.updateById(pqScriptDtls);
    }

    @Override
    public List<PqScriptDtls> listPqScriptDtlByScriptId(String scriptId) {
        MPJLambdaWrapper<PqScriptDtls> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(PqScriptDtls.class)
                .leftJoin(PqScript.class, PqScript::getId, PqScriptDtls::getScriptId)
                .eq(PqScript::getState, DataStateEnum.ENABLE.getCode())
                .eq(StrUtil.isNotBlank(scriptId), PqScriptDtls::getScriptId, scriptId)
                .eq(PqScriptDtls::getEnable, DataStateEnum.ENABLE.getCode());
        return this.getBaseMapper().selectJoinList(PqScriptDtls.class, queryWrapper);
    }

    @Override
    public List<SourceIssue> listSourceIssue(PqScriptIssueParam param) {
        String dataType;
        if (StrUtil.isNotBlank(param.getPlanId())) {
            AdPlan plan = adPlanMapper.selectById(param.getPlanId());
            dataType = plan.getDatasourceId();
        } else {
            dataType = "real";
        }
        List<SourceIssue> sourceIssues = new ArrayList<>();
        Set<Double> voltSet = new HashSet<>(1);
        Set<Double> currSet = new HashSet<>(1);
        boolean devFly = CollUtil.isNotEmpty(param.getDevIds());
        if (devFly) {
            //校验终端额定电压电流是否相同
            List<PqDev> list = pqDevService.list(new LambdaQueryWrapper<PqDev>()
                    .eq(StrUtil.isNotBlank(param.getPlanId()), PqDev::getPlanId, param.getPlanId())
                    .in(PqDev::getId, param.getDevIds())
                    .eq(PqDev::getState, DataStateEnum.ENABLE.getCode())
            );
            Set<String> devTypeSet = list.stream().map(PqDev::getDevType).collect(Collectors.toSet());
            List<DevType> devTypes = devTypeService.listByIds(devTypeSet);
            //额定电压信息
            voltSet = devTypes.stream().map(DevType::getDevVolt).collect(Collectors.toSet());
            //额定电流信息
            currSet = devTypes.stream().map(DevType::getDevCurr).collect(Collectors.toSet());
        } else {
            if (param.getType() == 0) {
                voltSet.add(1.0);
                currSet.add(1.0);
                param.setIsPhaseSequence("-1");
            } else if (param.getType() == 1) {
                voltSet.add(57.74);
                currSet.add(5.0);
                param.setIsPhaseSequence("-1");
            }
        }

        if (voltSet.size() == 1 && currSet.size() == 1) {
            Double volt = voltSet.stream().collect(Collectors.toList()).stream().findFirst().get();
            Double curr = currSet.stream().collect(Collectors.toList()).stream().findFirst().get();
            List<PqScriptDtls> pqScriptDtls = this.pqScriptDtls(param, volt, curr);
            Boolean isValueType = pqScriptMapper.selectScriptIsValueType(param.getScriptId());
            if (CollUtil.isNotEmpty(pqScriptDtls)) {
                /**
                 * 1.获取全部检测点脚本
                 * 2.根据检测脚本Index,进行分组来确定是第几行数据，理论上基本数据只有七条
                 * 3.根据见树形字典在区分大类下面有多少小类
                 * 3.组装所有检测格式
                 */
                Map<Integer, List<PqScriptDtls>> scriptDtlsMap = pqScriptDtls.stream()
                        .sorted(Comparator.comparing(PqScriptDtls::getScriptIndex))
                        .collect(Collectors.groupingBy(PqScriptDtls::getScriptIndex, LinkedHashMap::new, Collectors.toList()));
                scriptDtlsMap.forEach((key, value) -> {
                    /**
                     * 分组获取有多少小类
                     *  VOL-Base:额定工作条件下的检测
                     *  VOL-Freq:频率对电压测量的影响
                     *  VOL-Harm:谐波对电压测量的影响
                     */
                    List<PqScriptDtls> freq = value.stream().filter(x -> FREQ.equals(x.getValueType())).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(freq)) {
                        PqScriptDtls freqDtls = freq.get(0);
                        SourceIssue issue = new SourceIssue();
                        issue.setId(freqDtls.getId());
                        issue.setEnable(freqDtls.getEnable());
                        issue.setSourceId(param.getSourceId());
                        issue.setType(freqDtls.getScriptCode());
                        issue.setIndex(freqDtls.getScriptIndex());
                        String scriptSubType = freqDtls.getScriptSubType();
                        issue.setSubType(CollUtil.isEmpty(param.getDevIds()) || StrUtil.isBlank(scriptSubType) ? scriptSubType : scriptSubType.replace("Base", "NULL"));
                        PqScriptCheckDataParam checkDataParam = new PqScriptCheckDataParam();
                        checkDataParam.setScriptId(freqDtls.getScriptId());
                        checkDataParam.setIndex(freqDtls.getScriptIndex());
                        checkDataParam.setIsValueTypeName(true);
                        if (PowerIndexEnum.F.getKey().equals(issue.getType()) || PowerIndexEnum.LF.getKey().equals(issue.getType()) || PowerIndexEnum.VOLTAGE.getKey().equals(issue.getType())) {
                            checkDataParam.setDataType("avg");
                            issue.setDataType("avg");
                        } else {
                            checkDataParam.setDataType(dataType);
                            issue.setDataType(dataType);
                        }
                        issue.setDevValueTypeList(pqScriptCheckDataService.getValueType(checkDataParam));
                        issue.setDesc(ScriptDtlsDesc.getStringBuffer(value, true, isValueType).toString());
                        issue.setScriptId(freqDtls.getScriptId());
                        issue.setFUn(volt);
                        issue.setFIn(curr);
                        issue.setFFreq(freqDtls.getValue());
                        List<SourceIssue.ChannelListDTO> channelListDTOS = new ArrayList<>();

                        //1.通道电压(ABC)
                        List<PqScriptDtls> volList = value.stream().filter(x -> VOL.equals(x.getValueType()))
                                .sorted(Comparator.comparing(PqScriptDtls::getPhase))
                                .collect(Collectors.toList());
                        //谐波电压,间谐波
                        List<PqScriptDtls> dtlsVList = value.stream().filter(x -> HARM_V.equals(x.getValueType()) ||
                                        INHARM_V.equals(x.getValueType()) ||
                                        DIP.equals(x.getValueType()) ||
                                        FLICKER.equals(x.getValueType())
                                )
                                .sorted(Comparator.comparing(PqScriptDtls::getPhase))
                                .collect(Collectors.toList());
                        issueAdd(volList, dtlsVList, HARM_V, INHARM_V, freqDtls.getValue(), "U", channelListDTOS, checkDataParam, devFly, isValueType);

                        //2.通道电流(ABC)
                        List<PqScriptDtls> curList = value.stream().filter(x -> CUR.equals(x.getValueType()))
                                .sorted(Comparator.comparing(PqScriptDtls::getPhase))
                                .collect(Collectors.toList());
                        //谐波电流,间谐波电流
                        List<PqScriptDtls> dtlsIList = value.stream().filter(x -> HARM_I.equals(x.getValueType()) ||
                                        INHARM_I.equals(x.getValueType())
                                )
                                .sorted(Comparator.comparing(PqScriptDtls::getPhase))
                                .collect(Collectors.toList());
                        issueAdd(curList, dtlsIList, HARM_I, INHARM_I, freqDtls.getValue(), "I", channelListDTOS, checkDataParam, devFly, isValueType);

                        issue.setChannelList(channelListDTOS);
                        if (PowerIndexEnum.P.getKey().equals(issue.getType()) || PowerIndexEnum.ANGLE.getKey().equals(issue.getType()) || PowerIndexEnum.LF.getKey().equals(issue.getType())) {
                            issue.setOtherType(issue.getType());
                        } else {
                            issue.setOtherType(null);
                        }
                        sourceIssues.add(issue);
                    }
                });
            }

        }

        return sourceIssues;
    }


    @Override
    public List<Integer> getIndexList(String scriptType, String scriptId) {
        MPJLambdaWrapper<PqScriptDtls> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(PqScriptDtls::getScriptIndex)
                .distinct()
                .eq(PqScriptDtls::getScriptType, scriptType)
                .eq(PqScriptDtls::getScriptId, scriptId);
        return this.getBaseMapper().selectJoinList(PqScriptDtls.class, wrapper).stream().map(PqScriptDtls::getScriptIndex).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public Boolean saveScriptDtls(PqScriptDtlsParam sourceIssue) {
        List<PqScriptDtls> info = new ArrayList<>();
        List<PqScriptCheckData> checkList = new ArrayList<>();
        Integer i = sourceIssue.getIndex();
        if (ObjectUtil.isNotNull(sourceIssue.getIndex())) {
            this.remove(new LambdaQueryWrapper<PqScriptDtls>()
                    .eq(PqScriptDtls::getScriptId, sourceIssue.getScriptId())
                    .eq(PqScriptDtls::getScriptIndex, sourceIssue.getIndex())
            );
            pqScriptCheckDataService.remove(new LambdaQueryWrapper<PqScriptCheckData>()
                    .eq(PqScriptCheckData::getScriptId, sourceIssue.getScriptId())
                    .eq(PqScriptCheckData::getScriptIndex, sourceIssue.getIndex())
            );
        } else {
            i = this.baseMapper.selectMaxIndex(sourceIssue.getScriptId()) + 1;
        }
        List<PqScriptDtlsParam.CheckData> checkDataList = sourceIssue.getCheckDataList();
        PqScriptCheckData data;
        if (CollUtil.isNotEmpty(checkDataList)) {
            checkDataList = checkDataList.stream().filter(x -> ObjectUtil.isNotNull(x.getValue())).collect(Collectors.toList());
            for (PqScriptDtlsParam.CheckData checkData : checkDataList) {
                data = new PqScriptCheckData();
                data.setScriptId(sourceIssue.getScriptId());
                data.setScriptIndex(i);
                data.setValueType(checkData.getValueType());
                data.setPhase(checkData.getPhase());
                data.setValue(checkData.getValue());
                data.setHarmNum(checkData.getHarmNum());
                data.setEnable(checkData.getEnable());
                data.setErrorFlag(checkData.getErrorFlag());
                checkList.add(data);
            }
        }

        //频率赋值
        PqScriptDtls dtls = setScriptDtls(sourceIssue, i);
        dtls.setValueType(FREQ);
        dtls.setPhase("T");
        dtls.setValue(sourceIssue.getFFreq());
        dtls.setAngle(0.0);
        info.add(dtls);
        if (CollUtil.isNotEmpty(sourceIssue.getChannelList())) {
            //开始遍历通道信息
            for (PqScriptDtlsParam.ChannelListDTO channelListDTO : sourceIssue.getChannelList()) {
                String phase = channelListDTO.getChannelType().substring(1, 2).toUpperCase();
                //电压或者电流
                if (channelListDTO.getChannelFlag()) {
                    PqScriptDtls uOri = setScriptDtls(sourceIssue, i);
                    uOri.setPhase(phase);
                    if (channelListDTO.getChannelType().contains("U")) {
                        uOri.setValueType(VOL);
                    }
                    if (channelListDTO.getChannelType().contains("I")) {
                        uOri.setValueType(CUR);
                    }
                    uOri.setValue(channelListDTO.getFAmp());
                    uOri.setAngle(channelListDTO.getFPhase());
                    info.add(uOri);
                }
                //谐波值
                if (channelListDTO.getHarmFlag()) {
                    List<PqScriptDtlsParam.ChannelListDTO.HarmModel> harmList = channelListDTO.getHarmList();
                    if (CollUtil.isNotEmpty(harmList)) {
                        for (PqScriptDtlsParam.ChannelListDTO.HarmModel harmModel : harmList) {
                            PqScriptDtls harm = setScriptDtls(sourceIssue, i);
                            if (channelListDTO.getChannelType().contains("U")) {
                                harm.setValueType(HARM_V);
                            }
                            if (channelListDTO.getChannelType().contains("I")) {
                                harm.setValueType(HARM_I);
                            }
                            harm.setPhase(phase);
                            harm.setHarmNum(harmModel.getHarm());
                            harm.setValue(harmModel.getFAmp());
                            harm.setAngle(harmModel.getFPhase());
                            info.add(harm);
                        }
                    }
                }
                //间谐波
                if (channelListDTO.getInHarmFlag()) {
                    List<PqScriptDtlsParam.ChannelListDTO.InharmModel> inharmList = channelListDTO.getInharmList();
                    if (CollUtil.isNotEmpty(inharmList)) {
                        for (PqScriptDtlsParam.ChannelListDTO.InharmModel inharmModel : inharmList) {
                            PqScriptDtls inHarm = setScriptDtls(sourceIssue, i);
                            if (channelListDTO.getChannelType().contains("U")) {
                                inHarm.setValueType(INHARM_V);
                            }
                            if (channelListDTO.getChannelType().contains("I")) {
                                inHarm.setValueType(INHARM_I);
                            }
                            inHarm.setPhase(phase);
                            inHarm.setHarmNum(inharmModel.getInharm());
                            inHarm.setValue(inharmModel.getFAmp());
                            inHarm.setAngle(inharmModel.getFPhase());
                            info.add(inHarm);
                        }
                    }
                }
                //暂态
                if (channelListDTO.getDipFlag()) {
                    if (channelListDTO.getChannelType().contains("U")) {
                        PqScriptDtlsParam.ChannelListDTO.DipDataDTO dipData = channelListDTO.getDipData();
                        if (ObjectUtil.isNotNull(dipData)) {
                            PqScriptDtls dip = setScriptDtls(sourceIssue, i);
                            dip.setValueType(DIP);
                            dip.setPhase(phase);
                            dip.setTransValue(dipData.getFTransValue());
                            dip.setRetainTime(dipData.getRetainTime());
                            dip.setFPreTime(dipData.getFPreTime());
                            dip.setFAfterTime(dipData.getFAfterTime());
                            dip.setFRampIn(dipData.getFRampIn());
                            dip.setFRampOut(dipData.getFRampOut());
                            info.add(dip);
                        }
                    }
                }
                //闪变
                if (channelListDTO.getFlickerFlag()) {
                    if (channelListDTO.getChannelType().contains("U")) {
                        PqScriptDtlsParam.ChannelListDTO.FlickerDataDTO flickerData = channelListDTO.getFlickerData();
                        if (ObjectUtil.isNotNull(flickerData)) {
                            PqScriptDtls dip = setScriptDtls(sourceIssue, i);
                            dip.setValueType(FLICKER);
                            dip.setPhase(phase);
                            if (!"CPM".equals(flickerData.getWaveFluType())) {
                                dip.setChagFre(NumberUtil.round(flickerData.getFChagFre() * 120, -1).doubleValue());
                            } else {
                                dip.setChagFre(flickerData.getFChagFre());
                            }
                            dip.setChagValue(flickerData.getFChagValue());
                            info.add(dip);
                        }
                    }
                }
            }
        }
        if (CollUtil.isNotEmpty(info)) {
            this.saveBatch(info);
        }
        if (CollUtil.isNotEmpty(checkList)) {
            pqScriptCheckDataService.saveBatch(checkList);
        }
        return true;
    }

    @Override
    public List<PqScriptDtlsParam.CheckData> scriptDtlsCheckDataList(PqScriptDtlsParam sourceIssue) {
        Boolean valueType = pqScriptMapper.selectScriptIsValueType(sourceIssue.getScriptId());
        double radio = 1;
        if (valueType) {
            radio = 100;
        }

        List<PqScriptDtlsParam.CheckData> info = new ArrayList<>();
        //获取所有下拉值情况
        List<PqScriptDtlsParam.CheckData> checkDataList = sourceIssue.getCheckDataList();
        if (CollUtil.isNotEmpty(checkDataList)) {
            Map<String, List<PqScriptCheckData>> checkDataOldMap = new HashMap<>();
            if (sourceIssue.getRetryCompute()) {
                PqScriptCheckDataParam param = new PqScriptCheckDataParam();
                param.setScriptId(sourceIssue.getScriptId());
                param.setIndex(sourceIssue.getIndex());
                List<PqScriptCheckData> checkDataOld = pqScriptCheckDataService.listCheckDataCode(param);
                checkDataOldMap = checkDataOld.stream().collect(Collectors.groupingBy(PqScriptCheckData::getValueTypeCode));
            }

            List<String> ids = checkDataList.stream().map(PqScriptDtlsParam.CheckData::getValueType).collect(Collectors.toList());
            List<DictTree> dictTreeById = dictTreeService.getDictTreeById(ids);
            Map<String, DictTree> dicTreeMap = dictTreeById.stream().collect(Collectors.toMap(DictTree::getId, Function.identity()));
            PqScriptDtlsParam.CheckData checkData;
            for (PqScriptDtlsParam.CheckData channelListDTO : checkDataList) {
                if (dicTreeMap.containsKey(channelListDTO.getValueType())) {
                    DictTree dictTree = dicTreeMap.get(channelListDTO.getValueType());
                    channelListDTO.setPid(dictTree.getPid());
                    DetectionCodeEnum codeEnum = DetectionCodeEnum.getDetectionCodeByCode(dictTree.getCode());
                    List<PqScriptDtlsParam.ChannelListDTO> channelList = sourceIssue.getChannelList();
                    List<PqScriptDtlsParam.ChannelListDTO> channelU;
                    List<PqScriptDtlsParam.ChannelListDTO> channelI;
                    List<PqScriptCheckData> checkArchive;
                    if (checkDataOldMap.containsKey(codeEnum.getCode())) {
                        checkArchive = checkDataOldMap.get(codeEnum.getCode());
                    } else {
                        checkArchive = new ArrayList<>();
                    }
                    switch (codeEnum) {
                        case FREQ:
                            checkData = new PqScriptDtlsParam.CheckData();
                            checkData.setErrorFlag(channelListDTO.getErrorFlag());
                            checkData.setEnable(channelListDTO.getEnable());
                            checkData.setPhase("T");
                            if (CollUtil.isNotEmpty(checkArchive)) {
                                checkData.setValue(checkArchive.get(0).getValue());
                            } else {
                                checkData.setValue(sourceIssue.getFFreq());
                            }
                            checkData.setValueType(channelListDTO.getValueType());
                            checkData.setPid(channelListDTO.getPid());
                            info.add(checkData);
                            break;
                        case U1:
                        case VRMS:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                                rmsCheck(info, channelListDTO, channelU, checkArchive, false);
                            }
                            break;
                        case DELTA_V:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                                rmsCheck(info, channelListDTO, channelU, checkArchive, true);
                            }
                            break;
                        case U1A:
                        case VA:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    if (listDTO.getChannelFlag()) {
                                        checkData = new PqScriptDtlsParam.CheckData();
                                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                        checkData.setEnable(channelListDTO.getEnable());
                                        checkData.setValue(listDTO.getFPhase());
                                        checkData.setValueType(channelListDTO.getValueType());
                                        checkData.setPid(channelListDTO.getPid());
                                        setCheckValue(checkArchive, checkData, listDTO);
                                        info.add(checkData);
                                    }
                                }
                            }
                            break;
                        case V2_50:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                                harmCheck(info, channelListDTO, channelU, checkArchive);
                            }
                            break;
                        case I2_50:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelI = channelList.stream().filter(x -> x.getChannelType().contains("I")).collect(Collectors.toList());
                                harmCheck(info, channelListDTO, channelI, checkArchive);
                            }
                            break;
                        case P2_50:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    if (listDTO.getHarmFlag()) {
                                        //获取电流通道
                                        channelI = channelList.stream()
                                                .filter(PqScriptDtlsParam.ChannelListDTO::getHarmFlag)
                                                .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                                .collect(Collectors.toList());
                                        if (CollUtil.isNotEmpty(channelI)) {
                                            List<PqScriptDtlsParam.ChannelListDTO.HarmModel> harmI = channelI.stream()
                                                    .flatMap(x -> x.getHarmList().stream()).collect(Collectors.toList());
                                            Map<Double, PqScriptDtlsParam.ChannelListDTO.HarmModel> harmIMap = harmI.stream()
                                                    .collect(Collectors.toMap(PqScriptDtlsParam.ChannelListDTO.HarmModel::getHarm, Function.identity()));
                                            List<PqScriptDtlsParam.ChannelListDTO.HarmModel> harmList = listDTO.getHarmList();
                                            if (CollUtil.isNotEmpty(harmList)) {
                                                for (PqScriptDtlsParam.ChannelListDTO.HarmModel harmModel : harmList) {
                                                    checkData = new PqScriptDtlsParam.CheckData();
                                                    checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                                    checkData.setEnable(channelListDTO.getEnable());
                                                    checkData.setHarmNum(harmModel.getHarm());
                                                    if (harmIMap.containsKey(harmModel.getHarm())) {
                                                        PqScriptDtlsParam.ChannelListDTO.HarmModel i = harmIMap.get(harmModel.getHarm());
                                                        //电压*电流*cos（电压角度-电流角度）
                                                        checkData.setValue(i.getFAmp() * harmModel.getFAmp() / 100 * Math.cos((harmModel.getFPhase() - i.getFPhase()) * Math.PI / 180));
                                                    }
                                                    setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        case SV_1_49:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                                inHarmCheck(info, channelListDTO, channelU, checkArchive);
                            }
                            break;
                        case SI_1_49:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelI = channelList.stream().filter(x -> x.getChannelType().contains("I")).collect(Collectors.toList());
                                inHarmCheck(info, channelListDTO, channelI, checkArchive);
                            }
                            break;
                        case MAG:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                if (listDTO.getDipFlag()) {
                                    checkData = new PqScriptDtlsParam.CheckData();
                                    checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                    checkData.setEnable(channelListDTO.getEnable());
                                    checkData.setValue(listDTO.getDipData().getFTransValue());
                                    setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                }
                            }
                            break;
                        case DUR:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                if (listDTO.getDipFlag()) {
                                    checkData = new PqScriptDtlsParam.CheckData();
                                    checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                    checkData.setEnable(channelListDTO.getEnable());
                                    checkData.setValue(listDTO.getDipData().getRetainTime());
                                    setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                }
                            }
                            break;
                        case IRMS:
                        case I1A:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelI = channelList.stream().filter(x -> x.getChannelType().contains("I")).collect(Collectors.toList());
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelI) {
                                    if (listDTO.getChannelFlag()) {
                                        checkData = new PqScriptDtlsParam.CheckData();
                                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                        checkData.setEnable(channelListDTO.getEnable());
                                        checkData.setValue(listDTO.getFPhase());
                                        checkData.setValueType(channelListDTO.getValueType());
                                        checkData.setPid(channelListDTO.getPid());
                                        setCheckValue(checkArchive, checkData, listDTO);
                                        info.add(checkData);
                                    }
                                }
                            }
                            break;
                        case IA:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelI = channelList.stream().filter(x -> x.getChannelType().contains("I")).collect(Collectors.toList());
                                rmsCheck(info, channelListDTO, channelI, checkArchive, false);
                            }
                            break;
                        case V_UNBAN:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            unbanCheck(info, channelListDTO, channelU, checkArchive);
                            break;
                        case SeqV:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            unbanSeqCheck(info, channelListDTO, channelU, checkArchive);
                            break;
                        case I_UNBAN:
                            channelI = channelList.stream().filter(x -> x.getChannelType().contains("I")).collect(Collectors.toList());
                            unbanCheck(info, channelListDTO, channelI, checkArchive);
                            break;
                        case SeqA:
                            channelI = channelList.stream().filter(x -> x.getChannelType().contains("I")).collect(Collectors.toList());
                            unbanSeqCheck(info, channelListDTO, channelI, checkArchive);
                            break;
                        case PST:
                        case PLT:
                            if (CollUtil.isNotEmpty(channelList)) {
                                channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    if (listDTO.getFlickerFlag()) {
                                        checkData = new PqScriptDtlsParam.CheckData();
                                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                        checkData.setEnable(channelListDTO.getEnable());
                                        if (ObjectUtil.isNotNull(listDTO.getFlickerData())) {
                                            checkData.setValue(listDTO.getFlickerData().getFlickerValue());
                                        }
                                        setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                    }
                                }
                            }
                            break;
                        case I1:
                            break;
                        case W:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
//                                            .filter(PqScriptDtlsParam.ChannelListDTO::getHarmFlag)
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        checkData = new PqScriptDtlsParam.CheckData();
                                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                        checkData.setEnable(channelListDTO.getEnable());
                                        //电压*电流*cos（电压角度-电流角度）
                                        double v = listDTO.getFPhase() - channelI.get(0).getFPhase();
                                        if (v == 90 || v == -90 || v == 270 || v == -270) {
                                            checkData.setValue(0.0);
                                        } else {
                                            checkData.setValue(channelI.get(0).getFAmp() * listDTO.getFAmp() / radio * Math.cos(v * Math.PI / 180));
                                        }
                                        setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                    }
                                }
                            }
                            break;
                        case VARW:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
//                                            .filter(PqScriptDtlsParam.ChannelListDTO::getHarmFlag)
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        checkData = new PqScriptDtlsParam.CheckData();
                                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                        checkData.setEnable(channelListDTO.getEnable());
                                        //电压*电流*sin（电压角度-电流角度）
                                        double v = listDTO.getFPhase() - channelI.get(0).getFPhase();
                                        if (v == 0 || v == 360 || v == 180 || v == -180) {
                                            checkData.setValue(0.0);
                                        } else {
                                            checkData.setValue(channelI.get(0).getFAmp() * listDTO.getFAmp() / radio * Math.sin(v * Math.PI / 180));
                                        }
//                                        if (valueType) {
//                                        checkData.setValue(checkData.getValue() * 57.74 * 5);
//                                        }
                                        setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                    }
                                }
                            }
                            break;
                        case VAW:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
//                                            .filter(PqScriptDtlsParam.ChannelListDTO::getHarmFlag)
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        checkData = new PqScriptDtlsParam.CheckData();
                                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                        checkData.setEnable(channelListDTO.getEnable());
                                        checkData.setValue(channelI.get(0).getFAmp() * listDTO.getFAmp() / radio);
                                        setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                    }
                                }
                            }
                            break;
                        case PF:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
//                                            .filter(PqScriptDtlsParam.ChannelListDTO::getHarmFlag)
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        checkData = new PqScriptDtlsParam.CheckData();
                                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                        checkData.setEnable(channelListDTO.getEnable());
                                        //cos（电压角度-电流角度）
                                        double v = listDTO.getFPhase() - channelI.get(0).getFPhase();
                                        if (v == 90 || v == -90 || v == 270 || v == -270) {
                                            checkData.setValue(0.0);
                                        } else {
                                            checkData.setValue(Math.cos(v * Math.PI / 180));
                                        }
                                        setCheck(info, checkData, channelListDTO, checkArchive, listDTO);
                                    }
                                }
                            }
                            break;
                        case TOTW:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                Double value = 0.0;
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
//                                            .filter(PqScriptDtlsParam.ChannelListDTO::getHarmFlag)
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        //cos（电压角度-电流角度）
                                        double v = listDTO.getFPhase() - channelI.get(0).getFPhase();
                                        if (v == 90 || v == -90 || v == 270 || v == -270) {
                                            value += 0.0;
                                        } else {
                                            value += channelI.get(0).getFAmp() * listDTO.getFAmp() / radio * Math.cos(v * Math.PI / 180);
                                        }
                                    }
                                }
                                checkData = new PqScriptDtlsParam.CheckData();
                                checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                checkData.setEnable(channelListDTO.getEnable());
                                checkData.setValue(value);
                                checkData.setPhase("T");
                                checkData.setPid(channelListDTO.getPid());
                                checkData.setValueType(channelListDTO.getValueType());
                                info.add(checkData);
                            }
                            break;
                        case TOTVAR:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                Double value = 0.0;
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        //电压*电流*sin（电压角度-电流角度）
                                        double v = listDTO.getFPhase() - channelI.get(0).getFPhase();
                                        if (v == 0 || v == 360 || v == 180 || v == -180) {
                                            value += 0.0;
                                        } else {
                                            value += channelI.get(0).getFAmp() * listDTO.getFAmp() / radio * Math.sin(v * Math.PI / 180);
                                        }
                                    }
                                }
                                checkData = new PqScriptDtlsParam.CheckData();
                                checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                checkData.setEnable(channelListDTO.getEnable());
                                checkData.setValue(value);
                                checkData.setPhase("T");
                                checkData.setPid(channelListDTO.getPid());
                                checkData.setValueType(channelListDTO.getValueType());
                                info.add(checkData);
                            }
                            break;
                        case TOTVA:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                Double value = 0.0;
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        value += channelI.get(0).getFAmp() * listDTO.getFAmp() / radio;
                                    }
                                }
                                checkData = new PqScriptDtlsParam.CheckData();
                                checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                checkData.setEnable(channelListDTO.getEnable());
                                checkData.setValue(value);
                                checkData.setPhase("T");
                                checkData.setPid(channelListDTO.getPid());
                                checkData.setValueType(channelListDTO.getValueType());
                                info.add(checkData);
                            }
                            break;
                        case TOTPF:
                            channelU = channelList.stream().filter(x -> x.getChannelType().contains("U")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(channelU)) {
                                Double valueTotW = 0.0;
                                Double valueTotVA = 0.0;
                                for (PqScriptDtlsParam.ChannelListDTO listDTO : channelU) {
                                    //获取电流通道
                                    channelI = channelList.stream()
//                                            .filter(PqScriptDtlsParam.ChannelListDTO::getHarmFlag)
                                            .filter(x -> x.getChannelType().contains("I" + listDTO.getChannelType().substring(1, 2)))
                                            .collect(Collectors.toList());
                                    if (CollUtil.isNotEmpty(channelI)) {
                                        //cos（电压角度-电流角度）
                                        double v = listDTO.getFPhase() - channelI.get(0).getFPhase();
                                        if (v == 90 || v == -90 || v == 270 || v == -270) {
                                            valueTotW += 0.0;
                                        } else {
                                            valueTotW += channelI.get(0).getFAmp() * listDTO.getFAmp() / radio * Math.cos(v * Math.PI / 180);
                                        }
                                        valueTotVA += channelI.get(0).getFAmp() * listDTO.getFAmp() / radio;
                                    }
                                }
                                checkData = new PqScriptDtlsParam.CheckData();
                                checkData.setErrorFlag(channelListDTO.getErrorFlag());
                                checkData.setEnable(channelListDTO.getEnable());
                                checkData.setValue(valueTotW / valueTotVA);
                                checkData.setPhase("T");
                                checkData.setPid(channelListDTO.getPid());
                                checkData.setValueType(channelListDTO.getValueType());
                                info.add(checkData);
                            }
                            break;
                        case UNKNOWN_ERROR:
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        return info;
    }

    private void setCheck(List<PqScriptDtlsParam.CheckData> info, PqScriptDtlsParam.CheckData checkData, PqScriptDtlsParam.CheckData channelListDTO, List<PqScriptCheckData> checkArchive, PqScriptDtlsParam.ChannelListDTO listDTO) {
        checkData.setPhase(listDTO.getChannelType().substring(1, 2).toUpperCase());
        checkData.setValueType(channelListDTO.getValueType());
        checkData.setPid(channelListDTO.getPid());
        setCheckValue(checkArchive, checkData, listDTO);
        info.add(checkData);
    }

    @Override
    public Boolean deleteDtls(ScriptParam param) {
        this.remove(new LambdaQueryWrapper<PqScriptDtls>()
                .eq(PqScriptDtls::getScriptId, param.getScriptId())
                .eq(PqScriptDtls::getScriptIndex, param.getIndex())
        );
        return pqScriptCheckDataService.remove(new LambdaQueryWrapper<PqScriptCheckData>()
                .eq(PqScriptCheckData::getScriptId, param.getScriptId())
                .eq(PqScriptCheckData::getScriptIndex, param.getIndex())
        );
    }

    @Override
    public Boolean updateDtls(ScriptParam param) {
        return this.update(new LambdaUpdateWrapper<PqScriptDtls>()
                .set(PqScriptDtls::getEnable, param.getEnable())
                .eq(PqScriptDtls::getScriptId, param.getScriptId())
                .eq(ObjectUtil.isNotNull(param.getIndex()), PqScriptDtls::getScriptIndex, param.getIndex())
                .eq(ObjectUtil.isNotNull(param.getScriptType()), PqScriptDtls::getScriptType, param.getScriptType())
        );
    }

    @Override
    public List<PqScriptCheckData> checkDataList(ScriptParam param) {
        List<PqScriptDtls> list = this.list(new LambdaQueryWrapper<PqScriptDtls>()
                .eq(PqScriptDtls::getScriptId, param.getScriptId())
                .eq(PqScriptDtls::getScriptType, param.getScriptType())
        );
        List<Integer> indexList = list.stream().map(PqScriptDtls::getScriptIndex).distinct().collect(Collectors.toList());
        if (CollUtil.isNotEmpty(indexList)) {
            return pqScriptCheckDataMapper.selectJoinList(PqScriptCheckData.class, new MPJLambdaWrapper<PqScriptCheckData>()
                    .select(PqScriptCheckData::getValueType, PqScriptCheckData::getEnable, PqScriptCheckData::getErrorFlag)
                    .distinct()
                    .eq(PqScriptCheckData::getScriptId, param.getScriptId())
                    .in(PqScriptCheckData::getScriptIndex, indexList)
            );

        }
        return Collections.emptyList();
    }

    @Override
    public String getScriptDtlsDesc(ScriptParam.Desc param) {
        Boolean isValueType = pqScriptMapper.selectScriptIsValueType(param.getScriptId());
        MPJLambdaWrapper<PqScriptDtls> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(PqScriptDtls.class)
                .selectAs(DictTree::getCode, PqScriptDtls::getScriptCode)
                .leftJoin(DictTree.class, DictTree::getId, PqScriptDtls::getScriptType)
                .in(PqScriptDtls::getScriptIndex, param.getIndex())
                .eq(PqScriptCheckData::getEnable, DataStateEnum.ENABLE.getCode())
                .orderByAsc(PqScriptCheckData::getScriptIndex)
                .eq(PqScriptDtls::getScriptId, param.getScriptId());

        //先获取检测脚本类型是否相对值  true相对值 false绝对值（相对值要乘额定值,绝对值不需要处理)
        List<PqScriptDtls> pqScriptDtls = this.getBaseMapper().selectJoinList(PqScriptDtls.class, queryWrapper);
        if (isValueType) {
            for (PqScriptDtls pqScriptDtl : pqScriptDtls) {
                if (VOL.equals(pqScriptDtl.getValueType())) {
                    BigDecimal volValue = new BigDecimal(pqScriptDtl.getValue());
                    BigDecimal result = volValue.multiply(param.getDevVolt())
                            .divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);
                    pqScriptDtl.setValue(result.doubleValue());
                }
                if (CUR.equals(pqScriptDtl.getValueType())) {
                    BigDecimal volValue = new BigDecimal(pqScriptDtl.getValue());
                    BigDecimal result = volValue.multiply(param.getDevCurr())
                            .divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);
                    pqScriptDtl.setValue(result.doubleValue());
                }
            }
        }
        return ScriptDtlsDesc.getStringBuffer(pqScriptDtls, true, isValueType).toString();
    }

    @Override
    public Set<String> getScriptToIcdCheckInfo(PreDetectionParam param) {
        PqScriptIssueParam issueParam = new PqScriptIssueParam();
        issueParam.setSourceId(param.getSourceName());
        issueParam.setDevIds(param.getDevIds());
        issueParam.setScriptId(param.getScriptId());
        issueParam.setIsPhaseSequence(CommonEnum.FORMAL_TEST.getValue());
        List<SourceIssue> sourceIssues = this.listSourceIssue(issueParam);
        Set<String> dataTypeSet = new HashSet<>();
        sourceIssues.forEach(x -> {
            dataTypeSet.addAll(x.getDevValueTypeList());
        });
        return dataTypeSet;
    }

    @Override
    public List<PqScriptDtlDataVO> getScriptDtlsDataList(String scriptId) {
        return this.baseMapper.getScriptDtlsDataList(scriptId);
    }

    private void unbanCheck(List<PqScriptDtlsParam.CheckData> info,
                            PqScriptDtlsParam.CheckData channelListDTO,
                            List<PqScriptDtlsParam.ChannelListDTO> list,
                            List<PqScriptCheckData> checkArchive
    ) {
        PqScriptDtlsParam.CheckData checkData;
        if (CollUtil.isNotEmpty(list)) {
            checkData = new PqScriptDtlsParam.CheckData();
            checkData.setErrorFlag(channelListDTO.getErrorFlag());
            checkData.setEnable(channelListDTO.getEnable());
            checkData.setPhase("T");
            checkData.setValueType(channelListDTO.getValueType());
            checkData.setPid(channelListDTO.getPid());
            List<PqScriptDtlsParam.ChannelListDTO> channelA = list.stream().filter(x -> x.getChannelType().contains("a")).collect(Collectors.toList());
            List<PqScriptDtlsParam.ChannelListDTO> channelB = list.stream().filter(x -> x.getChannelType().contains("b")).collect(Collectors.toList());
            List<PqScriptDtlsParam.ChannelListDTO> channelC = list.stream().filter(x -> x.getChannelType().contains("c")).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(channelA) && CollUtil.isNotEmpty(channelB) && CollUtil.isNotEmpty(channelC)) {
                PqScriptDtlsParam.ChannelListDTO a = channelA.get(0);
                PqScriptDtlsParam.ChannelListDTO b = channelB.get(0);
                PqScriptDtlsParam.ChannelListDTO c = channelC.get(0);
                checkData.setValue(ThreePhaseUnbalance.calculateUnbalance(a.getFAmp(), a.getFPhase(), b.getFAmp(), b.getFPhase(), c.getFAmp(), c.getFPhase()));
            }
            if (CollUtil.isNotEmpty(checkArchive)) {
                checkData.setValue(checkArchive.get(0).getValue());
            }
            info.add(checkData);
        }
    }

    private void unbanSeqCheck(List<PqScriptDtlsParam.CheckData> info,
                               PqScriptDtlsParam.CheckData channelListDTO,
                               List<PqScriptDtlsParam.ChannelListDTO> list,
                               List<PqScriptCheckData> checkArchive
    ) {
        PqScriptDtlsParam.CheckData checkData;
        if (CollUtil.isNotEmpty(list)) {
            List<PqScriptDtlsParam.ChannelListDTO> channelA = list.stream().filter(x -> x.getChannelType().contains("a")).collect(Collectors.toList());
            List<PqScriptDtlsParam.ChannelListDTO> channelB = list.stream().filter(x -> x.getChannelType().contains("b")).collect(Collectors.toList());
            List<PqScriptDtlsParam.ChannelListDTO> channelC = list.stream().filter(x -> x.getChannelType().contains("c")).collect(Collectors.toList());
            if (CollUtil.isEmpty(checkArchive)) {
                if (CollUtil.isNotEmpty(channelA) && CollUtil.isNotEmpty(channelB) && CollUtil.isNotEmpty(channelC)) {
                    PqScriptDtlsParam.ChannelListDTO a = channelA.get(0);
                    PqScriptDtlsParam.ChannelListDTO b = channelB.get(0);
                    PqScriptDtlsParam.ChannelListDTO c = channelC.get(0);

                    BigDecimal vaMagnitude = new BigDecimal(a.getFAmp());
                    BigDecimal vaAngle = new BigDecimal(a.getFPhase());
                    BigDecimal vbMagnitude = new BigDecimal(b.getFAmp());
                    BigDecimal vbAngle = new BigDecimal(b.getFPhase());
                    BigDecimal vcMagnitude = new BigDecimal(c.getFAmp());
                    BigDecimal vcAngle = new BigDecimal(c.getFPhase());

                    // 将幅值和角度转换为复数
                    ThreePhaseUnbalance.Complex va = ThreePhaseUnbalance.fromPolar(vaMagnitude, vaAngle);
                    ThreePhaseUnbalance.Complex vb = ThreePhaseUnbalance.fromPolar(vbMagnitude, vbAngle);
                    ThreePhaseUnbalance.Complex vc = ThreePhaseUnbalance.fromPolar(vcMagnitude, vcAngle);

                    // 计算对称分量
                    ThreePhaseUnbalance.Complex[] symmetricalComponents = ThreePhaseUnbalance.calculateSymmetricalComponents(va, vb, vc);
                    ThreePhaseUnbalance.Complex v0 = symmetricalComponents[0]; // 零序分量
                    ThreePhaseUnbalance.Complex v1 = symmetricalComponents[1]; // 正序分量
                    ThreePhaseUnbalance.Complex v2 = symmetricalComponents[2]; // 负序分量

                    checkData = new PqScriptDtlsParam.CheckData();
                    checkData.setErrorFlag(channelListDTO.getErrorFlag());
                    checkData.setEnable(channelListDTO.getEnable());
                    checkData.setPhase("A");
                    checkData.setValueType(channelListDTO.getValueType());
                    checkData.setPid(channelListDTO.getPid());
                    checkData.setValue(v1.magnitude().doubleValue());
                    info.add(checkData);

                    checkData = new PqScriptDtlsParam.CheckData();
                    checkData.setErrorFlag(channelListDTO.getErrorFlag());
                    checkData.setEnable(channelListDTO.getEnable());
                    checkData.setPhase("B");
                    checkData.setValueType(channelListDTO.getValueType());
                    checkData.setPid(channelListDTO.getPid());
                    checkData.setValue(v2.magnitude().doubleValue());
                    info.add(checkData);

                    checkData = new PqScriptDtlsParam.CheckData();
                    checkData.setErrorFlag(channelListDTO.getErrorFlag());
                    checkData.setEnable(channelListDTO.getEnable());
                    checkData.setPhase("C");
                    checkData.setValueType(channelListDTO.getValueType());
                    checkData.setPid(channelListDTO.getPid());
                    checkData.setValue(v0.magnitude().doubleValue());
                    info.add(checkData);
                }
            } else {
                for (PqScriptCheckData checkArciveData : checkArchive) {
                    checkData = new PqScriptDtlsParam.CheckData();
                    checkData.setErrorFlag(checkArciveData.getErrorFlag());
                    checkData.setEnable(checkArciveData.getEnable());
                    checkData.setPhase(checkArciveData.getPhase());
                    checkData.setValueType(checkArciveData.getValueType());
                    checkData.setPid(channelListDTO.getPid());
                    checkData.setValue(checkArciveData.getValue());
                    info.add(checkData);
                }
            }
        }
    }

    private void rmsCheck(List<PqScriptDtlsParam.CheckData> info,
                          PqScriptDtlsParam.CheckData channelListDTO,
                          List<PqScriptDtlsParam.ChannelListDTO> list,
                          List<PqScriptCheckData> checkArchive,
                          Boolean flyDeltaV
    ) {
        PqScriptDtlsParam.CheckData checkData;
        for (PqScriptDtlsParam.ChannelListDTO listDTO : list) {
            if (listDTO.getChannelFlag()) {
                checkData = new PqScriptDtlsParam.CheckData();
                checkData.setErrorFlag(channelListDTO.getErrorFlag());
                checkData.setEnable(channelListDTO.getEnable());
                checkData.setPhase(listDTO.getChannelType().substring(1, 2).toUpperCase());
                checkData.setValueType(channelListDTO.getValueType());
                checkData.setPid(channelListDTO.getPid());
                List<PqScriptCheckData> collect = checkArchive.stream().filter(x -> x.getPhase().equals(listDTO.getChannelType().substring(1, 2).toUpperCase())).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(collect)) {
                    checkData.setValue(collect.get(0).getValue());
                } else {
                    if (flyDeltaV) {
                        checkData.setValue(0.0);
                    } else {
                        // 在这里重新根据谐波、间谐波含有率计算新的幅值
                        Double fAmp = listDTO.getFAmp();
                        if (listDTO.getHarmFlag()) {

                            double thd = listDTO.getHarmList().stream().map(harmModel -> harmModel.getFAmp() * harmModel.getFAmp() / 10000).mapToDouble(x -> x).sum();
                            fAmp = Math.sqrt(1 + thd) * fAmp;
                        }
                        if (listDTO.getInHarmFlag()) {
                            double thd = listDTO.getInharmList().stream().map(harmModel -> harmModel.getFAmp() * harmModel.getFAmp() / 10000).mapToDouble(x -> x).sum();
                            fAmp = Math.sqrt(1 + thd) * fAmp;
                        }
                        checkData.setValue(fAmp);
                    }
                }
                info.add(checkData);
            }

        }
    }

    private void inHarmCheck(List<PqScriptDtlsParam.CheckData> info,
                             PqScriptDtlsParam.CheckData channelListDTO,
                             List<PqScriptDtlsParam.ChannelListDTO> uList,
                             List<PqScriptCheckData> checkArchive
    ) {
        PqScriptDtlsParam.CheckData checkData;
        for (PqScriptDtlsParam.ChannelListDTO listDTO : uList) {
            if (listDTO.getInHarmFlag()) {
                List<PqScriptDtlsParam.ChannelListDTO.InharmModel> inharmList = listDTO.getInharmList();
                if (CollUtil.isNotEmpty(inharmList)) {
                    for (PqScriptDtlsParam.ChannelListDTO.InharmModel inharmModel : inharmList) {
                        checkData = new PqScriptDtlsParam.CheckData();
                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                        checkData.setEnable(channelListDTO.getEnable());
                        checkData.setValue(inharmModel.getFAmp());
                        checkData.setHarmNum(inharmModel.getInharm());
                        checkData.setValueType(channelListDTO.getValueType());
                        checkData.setPid(channelListDTO.getPid());
                        setCheckValue(checkArchive, checkData, listDTO);
                        info.add(checkData);
                    }
                }
            }
        }
    }

    private void setCheckValue(List<PqScriptCheckData> checkArchive, PqScriptDtlsParam.CheckData checkData, PqScriptDtlsParam.ChannelListDTO listDTO) {
        List<PqScriptCheckData> collect = checkArchive.stream().filter(x -> x.getPhase().equals(listDTO.getChannelType().substring(1, 2).toUpperCase())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            checkData.setValue(collect.get(0).getValue());
        }
        checkData.setPhase(listDTO.getChannelType().substring(1, 2).toUpperCase());
    }

    private void harmCheck(List<PqScriptDtlsParam.CheckData> info,
                           PqScriptDtlsParam.CheckData channelListDTO,
                           List<PqScriptDtlsParam.ChannelListDTO> uList,
                           List<PqScriptCheckData> checkArchive
    ) {
        PqScriptDtlsParam.CheckData checkData;
        for (PqScriptDtlsParam.ChannelListDTO listDTO : uList) {
            if (listDTO.getHarmFlag()) {
                List<PqScriptDtlsParam.ChannelListDTO.HarmModel> harmList = listDTO.getHarmList();
                if (CollUtil.isNotEmpty(harmList)) {
                    for (PqScriptDtlsParam.ChannelListDTO.HarmModel harmModel : harmList) {
                        checkData = new PqScriptDtlsParam.CheckData();
                        checkData.setErrorFlag(channelListDTO.getErrorFlag());
                        checkData.setEnable(channelListDTO.getEnable());
                        checkData.setValue(harmModel.getFAmp());
                        checkData.setPhase(listDTO.getChannelType().substring(1, 2).toUpperCase());
                        checkData.setHarmNum(harmModel.getHarm());
                        checkData.setValueType(channelListDTO.getValueType());
                        checkData.setPid(channelListDTO.getPid());
                        setCheckValue(checkArchive, checkData, listDTO);
                        info.add(checkData);
                    }
                }
            }
        }
    }


    private PqScriptDtls setScriptDtls(PqScriptDtlsParam sourceIssue, Integer i) {
        PqScriptDtls dtls = new PqScriptDtls();
        dtls.setScriptId(sourceIssue.getScriptId());
        dtls.setScriptIndex(i);
        dtls.setScriptType(sourceIssue.getScriptType());
        dtls.setScriptSubType(sourceIssue.getSubType());
        dtls.setEnable(DataStateEnum.ENABLE.getCode());
        return dtls;
    }

    public List<PqScriptDtls> pqScriptDtls(PqScriptIssueParam param, Double volt, Double curr) {
        List<PqScriptDtls> pqScriptDtls;
        MPJLambdaWrapper<PqScriptDtls> queryWrapper = new MPJLambdaWrapper<>();
        List<Integer> index = new ArrayList<>();
        if (StrUtil.isNotBlank(param.getScriptSubType())) {
            String[] split = param.getScriptSubType().split("_");
            if (split.length > 1) {
                param.setScriptSubType(split[0]);
                List<PqScriptDtls> pqScriptDtls1 = this.getBaseMapper().selectList(new LambdaQueryWrapper<PqScriptDtls>()
                        .select(PqScriptDtls::getScriptIndex)
                        .eq(StrUtil.isNotBlank(param.getScriptType()), PqScriptDtls::getScriptType, param.getScriptType())
                        .eq(StrUtil.isNotBlank(param.getScriptSubType()), PqScriptDtls::getScriptSubType, param.getScriptSubType())
                        .eq(PqScriptDtls::getScriptId, param.getScriptId())
                        .ge(PqScriptDtls::getTransValue, split[1])
                        .le(PqScriptDtls::getTransValue, split[2]));
                index = pqScriptDtls1.stream().map(PqScriptDtls::getScriptIndex).distinct().collect(Collectors.toList());
            }
        }
        queryWrapper.selectAll(PqScriptDtls.class)
                .selectAs(DictTree::getCode, PqScriptDtls::getScriptCode)
                .leftJoin(DictTree.class, DictTree::getId, PqScriptDtls::getScriptType)
                .eq(StrUtil.isNotBlank(param.getScriptType()), PqScriptDtls::getScriptType, param.getScriptType())
                .eq(StrUtil.isNotBlank(param.getScriptSubType()), PqScriptDtls::getScriptSubType, param.getScriptSubType())
                .in(CollUtil.isNotEmpty(index), PqScriptDtls::getScriptIndex, index)
                .eq(CollUtil.isNotEmpty(param.getDevIds()), PqScriptCheckData::getEnable, DataStateEnum.ENABLE.getCode())
                .orderByAsc(PqScriptCheckData::getScriptIndex)
        ;
        if (param.getIsPhaseSequence().equals(CommonEnum.PHASE_TEST.getValue())) {
            //相序
            queryWrapper.eq(PqScriptDtls::getScriptIndex, -1)
                    .eq(PqScriptDtls::getEnable, 1);
            pqScriptDtls = this.list(queryWrapper);
            // 相序校验中电流需加量需要依据企标10650.2中章节5.5.3的描述过载能力：2 倍额定电流连续，10 倍额定电流持续 1 s。
            // 考虑到有可能存在1A的额定电流，本处做特殊处理，加量分别为额定电流的0.2/0.4/0.6的标幺乘积加量
            // 电压暂不做处理，原因：1、电压的企标描述过载能力为4倍，空间较大；2、额定电压比如57.74V为浮点数，存在不确定小数位，避免引起算术误差；
            // 1. 获取额定电流,前端已做限制，相同额定电流才能一起检测
            String deviceId = param.getDevIds().get(0);
            PqDevVO pqDev = pqDevService.getPqDevById(deviceId);
            String devTypeId = pqDev.getDevType();
            DevType devType = devTypeService.getById(devTypeId);
            Double devCurr = devType.getDevCurr();
            for (int i = 0; i < pqScriptDtls.size(); i++) {
                PqScriptDtls scriptDtls = pqScriptDtls.get(i);
                // 注意此处scriptDtls.getValue() < 1.0,考虑到有些已经投入运行的地方，可能没有改库，避免不必要的异常
                if (scriptDtls.getValueType().equalsIgnoreCase("CUR") && scriptDtls.getValue() < 1.0) {
                    scriptDtls.setValue(devCurr * scriptDtls.getValue());
                }
            }
        } else if (param.getIsPhaseSequence().equals(CommonEnum.COEFFICIENT_TEST.getValue())) {
            //系数
            queryWrapper.in(PqScriptDtls::getScriptIndex, param.getIndexList())
                    .eq(PqScriptDtls::getEnable, 1);
            pqScriptDtls = this.list(queryWrapper);
        } else {
            //先获取检测脚本类型是否相对值  true相对值 false绝对值（相对值要乘额定值,绝对值不需要处理)
            queryWrapper.eq(PqScriptDtls::getScriptId, param.getScriptId())
                    .in(CollUtil.isNotEmpty(param.getIndexList()), PqScriptDtls::getScriptIndex, param.getIndexList());
            pqScriptDtls = this.getBaseMapper().selectJoinList(PqScriptDtls.class, queryWrapper);
            if (CollUtil.isNotEmpty(param.getDevIds()) || (ObjectUtil.isNotNull(param.getType()) && param.getType() == 1)) {
                Boolean isValueType = pqScriptMapper.selectScriptIsValueType(param.getScriptId());
                if (isValueType) {
                    for (PqScriptDtls pqScriptDtl : pqScriptDtls) {
                        if (VOL.equals(pqScriptDtl.getValueType())) {
                            BigDecimal volValue = new BigDecimal(pqScriptDtl.getValue());
                            BigDecimal result = volValue.multiply(new BigDecimal(volt.toString()))
                                    .divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);
                            pqScriptDtl.setValue(result.doubleValue());
                        }
                        if (CUR.equals(pqScriptDtl.getValueType())) {
                            BigDecimal volValue = new BigDecimal(pqScriptDtl.getValue());
                            BigDecimal result = volValue.multiply(new BigDecimal(curr.toString()))
                                    .divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);
                            pqScriptDtl.setValue(result.doubleValue());
                        }
                    }
                }
            }
        }

        return pqScriptDtls;
    }


    /**
     * @param dtlsList        检测集合
     * @param dtlsList        其他类型检测集合(谐波，间谐波等)
     * @param harm            谐波
     * @param inHarm          间谐波
     * @param fFreq           频率
     * @param code            U,I
     * @param channelListDTOS 通道信息
     * @param channelListDTOS 通道信息
     * @Description: 通道信息组装
     * @Author: wr
     * @Date: 2024/12/16 15:51
     */
    private void issueAdd(List<PqScriptDtls> dtlsList,
                          List<PqScriptDtls> dtlsOtherList,
                          String harm,
                          String inHarm,
                          Double fFreq,
                          String code,
                          List<SourceIssue.ChannelListDTO> channelListDTOS,
                          PqScriptCheckDataParam checkDataParam,
                          Boolean devFly,
                          Boolean isValueType) {
        List<String> info = new ArrayList<>();
        info.add("A");
        info.add("B");
        info.add("C");
        for (String phase : info) {
            List<PqScriptDtls> collect = dtlsList.stream().filter(x -> x.getPhase().equals(phase)).collect(Collectors.toList());
            SourceIssue.ChannelListDTO channelListDTO = new SourceIssue.ChannelListDTO();
            channelListDTO.setChannelFlag(false);
            channelListDTO.setHarmFlag(false);
            channelListDTO.setInHarmFlag(false);
            channelListDTO.setDipFlag(false);
            channelListDTO.setFlickerFlag(false);
            channelListDTO.setChannelType(code + phase.toLowerCase());
            channelListDTO.setHarmList(new ArrayList<>());
            channelListDTO.setInharmList(new ArrayList<>());
            //暂态数据
            SourceIssue.ChannelListDTO.DipDataDTO dipDataDTO = new SourceIssue.ChannelListDTO.DipDataDTO();
            dipDataDTO.setFTransValue(0.0);
            dipDataDTO.setFRetainTime(0.0);

//            dipDataDTO.setFPreTime(fPreTime);
//            dipDataDTO.setFRampIn(fRampIn);
//            dipDataDTO.setFRampOut(fRampOut);
//            dipDataDTO.setFAfterTime(fAfterTime);


            channelListDTO.setDipData(dipDataDTO);
            //闪变数据
            SourceIssue.ChannelListDTO.FlickerDataDTO flickerDataDTO = new SourceIssue.ChannelListDTO.FlickerDataDTO();
            SourceIssue.ChannelListDTO.FlickerDataDTO flickerData = channelListDTO.getFlickerData();
            if (ObjectUtil.isNotNull(flickerData)) {
                flickerDataDTO.setWaveFluType(flickerData.getWaveFluType());
                flickerDataDTO.setWaveType(flickerData.getWaveType());
                flickerDataDTO.setFDutyCycle(flickerData.getFDutyCycle());
            } else {
                flickerDataDTO.setWaveFluType(waveFluType);
                flickerDataDTO.setWaveType(waveType);
                flickerDataDTO.setFDutyCycle(fDutyCycle);
            }

            flickerDataDTO.setFChagFre(0.0);
            flickerDataDTO.setFChagValue(0.0);
            channelListDTO.setFlickerData(flickerDataDTO);

            if (CollUtil.isNotEmpty(collect)) {
                PqScriptDtls dtls = collect.get(0);
                channelListDTO.setChannelFlag(true);
                channelListDTO.setFAmp(dtls.getValue());
                channelListDTO.setFPhase(dtls.getAngle());
                if (CollUtil.isNotEmpty(dtlsOtherList)) {
                    List<PqScriptDtls> phaseList = dtlsOtherList.stream().filter(x -> dtls.getPhase().equals(x.getPhase()))
                            .collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(phaseList)) {
                        //处理谐波类型
                        List<PqScriptDtls> harmList = phaseList.stream().filter(x -> harm.equals(x.getValueType()))
                                .filter(x -> ObjectUtil.isNotNull(x.getHarmNum()))
                                .sorted(Comparator.comparing(PqScriptDtls::getHarmNum))
                                .collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(harmList)) {
                            channelListDTO.setHarmList(getHarmModels(harmList));
                            channelListDTO.setHarmFlag(true);
                        }
                        //处理间谐波类型
                        List<PqScriptDtls> inHarmList = phaseList.stream().filter(x -> inHarm.equals(x.getValueType()))
                                .filter(x -> ObjectUtil.isNotNull(x.getHarmNum()))
                                .sorted(Comparator.comparing(PqScriptDtls::getHarmNum))
                                .collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(inHarmList)) {
                            channelListDTO.setInharmList(getInHarmModels(inHarmList));
                            channelListDTO.setInHarmFlag(true);
                        }
                        //处理暂态数据
                        List<PqScriptDtls> dipList = phaseList.stream().filter(x -> DIP.equals(x.getValueType()))
                                .collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(dipList)) {
                            PqScriptDtls dip = dipList.get(0);
                            dipDataDTO.setFTransValue(dip.getTransValue());
                            dipDataDTO.setFPreTime(dip.getFPreTime());
                            dipDataDTO.setFAfterTime(dip.getFAfterTime());
                            dipDataDTO.setFRampIn(dip.getFRampIn());
                            dipDataDTO.setFRampOut(dip.getFRampOut());
                            if (devFly) {
//                                if (isValueType) {
                                dipDataDTO.setFTransValue(dip.getTransValue());
//                                }
//                                else {
//                                    dipDataDTO.setFTransValue(NumberUtil.round(dip.getTransValue() / dtls.getValue(), 4).doubleValue());
//                                }
                            }
                            //  1.0/频率*持续时间（周波）= 暂态持续时间（s）
                            dipDataDTO.setFRetainTime(NumberUtil.round(1.0 / fFreq * dip.getRetainTime(), 3).doubleValue());
                            dipDataDTO.setRetainTime(dip.getRetainTime());
                            channelListDTO.setDipFlag(true);
                            channelListDTO.setDipData(dipDataDTO);
                        }
                        //处理闪变数据
                        List<PqScriptDtls> flickeRList = phaseList.stream().filter(x -> FLICKER.equals(x.getValueType()))
                                .collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(flickeRList)) {
                            PqScriptDtls flicker = flickeRList.get(0);

                            flickerDataDTO.setFChagFre(flicker.getChagFre());
                            flickerDataDTO.setFChagValue(flicker.getChagValue());

                            DictTree dictTreeByCode = dictTreeService.getDictTreeByCode(DetectionCodeEnum.PST.getCode());
                            if (ObjectUtil.isNotNull(dictTreeByCode)) {
                                checkDataParam.setValueType(dictTreeByCode.getId());
                                checkDataParam.setPhase(dtls.getPhase());
                                flickerDataDTO.setFlickerValue(pqScriptCheckDataService.getCheckDataValue(checkDataParam));
                            }
                            channelListDTO.setFlickerFlag(true);
                            channelListDTO.setFlickerData(flickerDataDTO);
                        }

                    }
                }
            }
            channelListDTOS.add(channelListDTO);
        }
    }

    /**
     * 处理谐波信息
     *
     * @param phaseHarmVList
     * @return
     */
    private List<SourceIssue.ChannelListDTO.HarmModel> getHarmModels(List<PqScriptDtls> phaseHarmVList) {
        List<SourceIssue.ChannelListDTO.HarmModel> info = new ArrayList<>();
        SourceIssue.ChannelListDTO.HarmModel harmModel;
        for (PqScriptDtls pqScriptDtls : phaseHarmVList) {
            harmModel = new SourceIssue.ChannelListDTO.HarmModel();
            harmModel.setHarm(pqScriptDtls.getHarmNum().doubleValue());
            harmModel.setFAmp(pqScriptDtls.getValue());
            harmModel.setFPhase(pqScriptDtls.getAngle());
            info.add(harmModel);
        }
        return info;
    }

    /**
     * 处理间谐波谐波信息
     *
     * @param phaseHarmVList
     * @return
     */
    private List<SourceIssue.ChannelListDTO.InharmModel> getInHarmModels(List<PqScriptDtls> phaseHarmVList) {
        List<SourceIssue.ChannelListDTO.InharmModel> info = new ArrayList<>();
        SourceIssue.ChannelListDTO.InharmModel inHarmModel;
        for (PqScriptDtls pqScriptDtls : phaseHarmVList) {
            inHarmModel = new SourceIssue.ChannelListDTO.InharmModel();
            inHarmModel.setInharm(pqScriptDtls.getHarmNum());
            inHarmModel.setFAmp(pqScriptDtls.getValue());
            inHarmModel.setFPhase(pqScriptDtls.getAngle());
            info.add(inHarmModel);
        }
        return info;
    }
}
