package com.njcn.event.service.majornetwork.Impl;

import com.njcn.common.config.GeneralInfo;
import com.njcn.event.file.pojo.dto.AnalogDTO;
import com.njcn.event.file.pojo.dto.WaveDataDTO;
import com.njcn.event.file.component.WaveFileComponent;
import com.njcn.event.service.majornetwork.AutonomeWaveService;
import com.njcn.oss.constant.GeneralConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

/**
 * @version 1.0.0
 * @author: zbj
 * @date: 2023/043/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AutonomeWaveServiceImpl implements AutonomeWaveService {

    private final GeneralInfo generalInfo;

    private final WaveFileComponent waveFileComponent;

    @Override
    public WaveDataDTO uploadFileAndViewWave(MultipartFile file1, MultipartFile file2) throws IOException {
        //测试路径
        // dir = new File("C:\\Users\\User\\Desktop\\wave");
        //真实路径
        File dir = new File(generalInfo.getBusinessTempPath() + File.separator + "wave");

        String cfgPath, datPath;
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String fileName1 = file1.getOriginalFilename();
        String fileName2 = file2.getOriginalFilename();
        //判断文件后缀
        String fileExtension1 = fileName1.substring(fileName1.lastIndexOf(".")).toUpperCase();
        if (Objects.equals(fileExtension1, GeneralConstant.CFG)) {
            //使用本地的波形文件进行测试
            File cfg = new File(dir + File.separator + fileName1);
            File dat = new File(dir + File.separator + fileName2);
            file1.transferTo(cfg);
            file2.transferTo(dat);
            cfgPath = dir + File.separator + fileName1;
            datPath = dir + File.separator + fileName2;
            InputStream cfgInputStream = Files.newInputStream(Paths.get(cfgPath));
            InputStream datInputStream = Files.newInputStream(Paths.get(datPath));
            WaveDataDTO comtrade = waveFileComponent.getComtrade(cfgInputStream, datInputStream, 1);
            WaveDataDTO validData = waveFileComponent.getValidData(comtrade);
            return countPtCt(validData);
        } else {
            //使用本地的波形文件进行测试
            File dat = new File(dir + File.separator + fileName1);
            File cfg = new File(dir + File.separator + fileName2);
            file1.transferTo(dat);
            file2.transferTo(cfg);
            cfgPath = dir + File.separator + fileName2;
            datPath = dir + File.separator + fileName1;
            InputStream cfgInputStream = Files.newInputStream(Paths.get(cfgPath));
            InputStream datInputStream = Files.newInputStream(Paths.get(datPath));
            WaveDataDTO comtrade = waveFileComponent.getComtrade(cfgInputStream, datInputStream, 1);
            WaveDataDTO validData = waveFileComponent.getValidData(comtrade);
            return countPtCt(validData);
        }
    }

    /**
     * 计算pt值和ct值
     */
    private WaveDataDTO countPtCt(WaveDataDTO waveDataDTO) {
        //取出集合
        List<AnalogDTO> lstAnalogDTO = waveDataDTO.getComtradeCfgDTO().getLstAnalogDTO();
        AnalogDTO ptParam = null;
        for (AnalogDTO analogDTO : lstAnalogDTO) {
            if (StringUtils.isNotBlank(analogDTO.getSzUnitName()) && analogDTO.getSzUnitName().contains("V")) {
                ptParam = analogDTO;
                break;
            }
        }
        AnalogDTO ctParam = null;
        for (AnalogDTO analogDTO : lstAnalogDTO) {
            if (StringUtils.isNotBlank(analogDTO.getSzUnitName()) && analogDTO.getSzUnitName().contains("A")) {
                ctParam = analogDTO;
                break;
            }
        }
        double pt1 = Double.parseDouble(ptParam.getFPrimary().toString());
        double pt2 = Double.parseDouble(ptParam.getFSecondary().toString());
        double ct1 = Double.parseDouble(ctParam.getFPrimary().toString());
        double ct2 = Double.parseDouble(ctParam.getFSecondary().toString());
        waveDataDTO.setPt(pt1 / pt2);
        waveDataDTO.setCt(ct1 / ct2);
        return waveDataDTO;
    }

}