package com.njcn.product.terminal.event.service.impl;

import cn.hutool.core.util.StrUtil;
import com.njcn.common.config.GeneralInfo;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.event.file.component.WaveFileComponent;
import com.njcn.event.file.pojo.dto.WaveDataDTO;
import com.njcn.event.file.pojo.enums.WaveFileResponseEnum;

import com.njcn.product.terminal.event.pojo.param.MonitorTerminalParam;
import com.njcn.product.terminal.event.service.EventGateService;
import com.njcn.product.terminal.mysqlTerminal.mapper.LedgerScaleMapper;
import com.njcn.product.terminal.mysqlTerminal.mapper.RmpEventDetailMapper;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.LedgerBaseInfo;
import com.njcn.product.terminal.mysqlTerminal.pojo.po.RmpEventDetailPO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: cdf
 * @CreateTime: 2025-06-30
 * @Description:
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class EventGateServiceImpl implements EventGateService {

    private final RmpEventDetailMapper rmpEventDetailMapper;

    private final WaveFileComponent waveFileComponent;

    private final LedgerScaleMapper ledgerScaleMapper;

    private final GeneralInfo generalInfo;

    @Override
    public WaveDataDTO getTransientAnalyseWave(MonitorTerminalParam param) {
        WaveDataDTO waveDataDTO;
        //获取暂降事件
        RmpEventDetailPO eventDetail = rmpEventDetailMapper.selectById(param.getId());
        if(Objects.isNull(eventDetail)){
            throw new BusinessException(CommonResponseEnum.FAIL,"查询事件为空,请检查参数");
        }
        String lineid = eventDetail.getLineId();
        LedgerBaseInfo pqLine = ledgerScaleMapper.getLedgerBaseInfo(Stream.of(lineid).collect(Collectors.toList())).get(0);
        String waveName = eventDetail.getWavePath();
        String cfgPath, datPath,cfgPath1,datPath1;
        InputStream cfgStream,datStream;
        if (StrUtil.isBlank(waveName)) {
            throw new BusinessException(WaveFileResponseEnum.ANALYSE_WAVE_NOT_FOUND);
        }
        try {
            cfgPath = generalInfo.getBusinessWavePath()+ File.separator+pqLine.getIp()+"/"+waveName+".CFG";
            datPath = generalInfo.getBusinessWavePath()+ File.separator+pqLine.getIp()+"/"+waveName+".DAT";
            cfgStream = waveFileComponent.getFileInputStreamByFilePath(cfgPath);
            datStream = waveFileComponent.getFileInputStreamByFilePath(datPath);
            log.info("本地磁盘波形文件路径----" + cfgPath);

        }catch (Exception e){
            cfgPath1 = generalInfo.getBusinessWavePath()+ File.separator+pqLine.getIp()+"/"+waveName+".cfg";
            datPath1 = generalInfo.getBusinessWavePath()+ File.separator+pqLine.getIp()+"/"+waveName+".dat";
            cfgStream = waveFileComponent.getFileInputStreamByFilePath(cfgPath1);
            datStream = waveFileComponent.getFileInputStreamByFilePath(datPath1);
        }



        if (Objects.isNull(cfgStream) || Objects.isNull(datStream)) {

          throw new BusinessException(WaveFileResponseEnum.ANALYSE_WAVE_NOT_FOUND);

        }
        waveDataDTO = waveFileComponent.getComtrade(cfgStream, datStream, 1);

        waveDataDTO = waveFileComponent.getValidData(waveDataDTO);

        waveDataDTO.setPtType(pqLine.getPtType());
        waveDataDTO.setPt(pqLine.getPt1()/ pqLine.getPt2());
        waveDataDTO.setCt(pqLine.getCt1()/ pqLine.getCt2());
        waveDataDTO.setMonitorName(pqLine.getLineName());
        return waveDataDTO;

    }
}
