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

import cn.hutool.core.util.StrUtil;
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.event.devcie.mapper.PqLinedetailMapper;
import com.njcn.product.event.devcie.pojo.po.PqDevice;
import com.njcn.product.event.devcie.pojo.po.PqLine;
import com.njcn.product.event.devcie.pojo.po.PqLinedetail;
import com.njcn.product.event.devcie.service.PqDeviceService;
import com.njcn.product.event.devcie.service.PqLineService;
import com.njcn.product.event.transientes.pojo.param.MonitorTerminalParam;
import com.njcn.product.event.transientes.pojo.po.PqsEventdetail;
import com.njcn.product.event.transientes.service.EventGateService;
import com.njcn.product.event.transientes.service.PqsEventdetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Objects;

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

    private final PqsEventdetailService pqsEventdetailService;
    private final PqDeviceService pqDeviceService;
    private final WaveFileComponent waveFileComponent;
    private final PqLineService pqLineService;
    private final PqLinedetailMapper pqLinedetailMapper;
    @Value("${WAVEPATH}")
    private String WAVEPATH;
    @Override
    public WaveDataDTO getTransientAnalyseWave(MonitorTerminalParam param) {
        WaveDataDTO waveDataDTO;
        //获取暂降事件
        PqsEventdetail eventDetail = pqsEventdetailService.getById(param.getId());
        Integer lineid = eventDetail.getLineid();
        PqLine pqLine = pqLineService.getById(lineid);
        PqLinedetail pqLinedetail = pqLinedetailMapper.selectById(lineid);
        PqDevice device = pqDeviceService.getById(pqLine.getDevIndex());
        String waveName = eventDetail.getWavename();
        String cfgPath, datPath;
        if (StrUtil.isBlank(waveName)) {
            throw new BusinessException(WaveFileResponseEnum.ANALYSE_WAVE_NOT_FOUND);
        }
        cfgPath = WAVEPATH+"/"+device.getIp()+"/"+waveName+".CFG";
        datPath = WAVEPATH+"/"+device.getIp()+"/"+waveName+".DAT";
        log.info("本地磁盘波形文件路径----" + cfgPath);
        InputStream cfgStream = waveFileComponent.getFileInputStreamByFilePath(cfgPath);
        InputStream datStream = waveFileComponent.getFileInputStreamByFilePath(datPath);
        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(pqLinedetail.getPttype());
        waveDataDTO.setPt(pqLine.getPt1()/ pqLine.getPt2());
        waveDataDTO.setCt(pqLine.getCt1()/ pqLine.getCt2());
        waveDataDTO.setMonitorName(pqLine.getName());
        return waveDataDTO;

    }
}
