package com.njcn.serviceimpl.advanced;

import com.njcn.mapper.configuration.DeviceMapper;
import com.njcn.mapper.configuration.LineMapper;
import com.njcn.mapper.data.EventDetailMapper;
import com.njcn.pojo.advanced.ReadWaveData;
import com.njcn.pojo.configuration.Device;
import com.njcn.pojo.configuration.Line;
import com.njcn.pojo.data.EventDetail;
import com.njcn.utils.AnalyWave;
import com.njcn.utils.AppConfig;
import com.njcn.utils.PubUtils;
import jcifs.smb.NtlmPasswordAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class GetWaveNameImpl {
    @Autowired
    private AppConfig appConfig;

    @Autowired
    private LineMapper lineMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private EventDetailMapper eventDetailMapper;

    public ReadWaveData getWaveName(String eventIndex) {
        ReadWaveData readWaveData = new ReadWaveData();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 初始化路径
        String path = appConfig.getWavePath();
        // 根据事件索引获取事件详细数据
        EventDetail eventDetail;
        eventDetail = eventDetailMapper.selectByIndex(eventIndex);
        Long lineId = eventDetail.getLineId();
        Line line = new Line();
        line.setLineIndex(lineId);
        line = lineMapper.selectOne(line);
        Device device = new Device();
        device.setDevIndex(line.getDevIndex());
        device = deviceMapper.selectOne(device);
        // 从终端表获取IP
        String ip = File.separator + device.getIp();
        path = path + ip;
        String fileName = "";
        // 从line表（监测点）获取时间
        boolean flags = PubUtils.judgeWaveName(eventDetail, path, null);
        String timeID = "";
        Date date = eventDetail.getTimeId();
        String dateString = sdf.format(date);
        String year = dateString.substring(0, 10);
        year = year.replaceAll("-", "");
        String hour = dateString.substring(11);
        hour = hour.replaceAll(":", "");
        if (flags) {
            path = path + File.separator + fileName;
        } else {
            timeID = year + "_" + hour + "_" + eventDetail.getMs();
            int lineID = lineId.intValue() % 10;
            timeID = lineID + "_" + timeID;
            fileName=timeID+ ".CFG";
            path = path + File.separator + fileName;
        }
        float f1 = line.getPt1();
        float f2 = line.getPt2();
        readWaveData.setF1(f1);
        readWaveData.setEventIndex(eventIndex);
        readWaveData.setF2(f2);
        readWaveData.setAuth(null);
        readWaveData.setFilePath(path);
        readWaveData.setFileName(fileName);
        readWaveData.setPicName(year + "_" + hour + "_" + eventDetail.getMs());

        return readWaveData;
    }
}
