package com.njcn;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.IdUtil;
import com.njcn.access.utils.MqttUtil;
import com.njcn.csdevice.pojo.dto.PqsCommunicateDto;
import com.njcn.csdevice.service.DeviceFtpService;
import com.njcn.csdevice.service.IRStatIntegrityDService;
import com.njcn.csdevice.service.IRStatOnlineRateDService;
import com.njcn.csharmonic.pojo.param.StatisticsDataParam;
import com.njcn.influx.imapper.EvtDataMapper;
import com.njcn.influx.imapper.PqsCommunicateMapper;
import com.njcn.influx.pojo.po.PqsCommunicate;
import com.njcn.influx.pojo.po.cs.EntData;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import lombok.SneakyThrows;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;

import java.io.*;
import java.nio.file.Files;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DownloadFileTest extends BaseJunitTest{

    @Autowired
    private DeviceFtpService deviceFtpService;

    @Autowired
    private FileStorageUtil fileStorageUtil;
    @Autowired
    private EvtDataMapper evtDataMapper;
    @Autowired
    private PqsCommunicateMapper pqsCommunicateMapper;
    private final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());

    @Autowired
    private IRStatIntegrityDService statIntegrityDService;
    @Autowired
    private IRStatOnlineRateDService statOnlineRateDService;
    @Autowired
    private MqttUtil mqttUtil;

    @SneakyThrows
    @Test
    public void test() {
        String nDId = "00B78D016AB5";
        String clientName = "NJCN-" + nDId.substring(nDId.length() - 6);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 200; i++) {
            boolean mqttClient = mqttUtil.judgeClientOnline(clientName);
            System.out.println("当前第"+i+"次,结果为:" + mqttClient);
        }
        long end = System.currentTimeMillis();
        System.out.println("时间差==：" + (end - startTime) + "ms");
    }


    /**
     * inflxudb写入
     */
    @SneakyThrows
    @Test
    public void addInfluxdb() {
        PqsCommunicateDto pqsCommunicate = new PqsCommunicateDto();
        pqsCommunicate.setTime("2025-06-23 ");
        pqsCommunicate.setDevId("da7aa071bf89864bedea8833133676b7");
        pqsCommunicate.setType(1);
        pqsCommunicate.setDescription("设备上线");
    }

    /**
     * 测试下载文件
     */
    @SneakyThrows
    @Test
    public void downloadFileTest() {
        String nDid = "00B78D016AB3";
        String name = "/etc/pqs_arm.bin";
        Integer size = 5127376;
        String fileCheck = "E883C579";
        String filePath = "C:\\Users\\徐扬\\Desktop\\download.txt";
        for (int i = 0; i < 10; i++) {
            System.out.println("开始第"+i+"次");
            deviceFtpService.downloadFile(nDid,name,size,fileCheck);
            String dataToAppend = LocalDateTime.now() +  "开始下载,这是第"+i+"次,结果为:" + null + "\r\n";
            // 第二个参数为 true 表示追加模式
            try (FileWriter fileWriter = new FileWriter(filePath, true)) {
                fileWriter.write(dataToAppend); // 写入数据
                System.out.println("数据已成功追加到文件中。");
            } catch (IOException e) {
                System.err.println("写入文件时发生错误: " + e.getMessage());
            }
        }
    }

    @SneakyThrows
    @Test
    public void moveFile() {
        String oldPath = "/db0/cmn/07/min/20240925/line2_avg.bin";
        StringBuilder result = null;
        String[] arr = oldPath.split("/");
        for (String s : arr) {
            s = s + File.separator;
        }
        System.out.println("result==:" + result);
    }

    public static void main(String[] args) {
        String nDid = "00B78D016AB3";
        String oldPath = "/db0/cmn/07/min/20240925/line2_avg.bin";
        String path = getFilePath(oldPath,nDid);
        System.out.println("path==:" + path);
    }

    private static String getFilePath(String path, String nDid) {
        String[] parts = path.split("/");
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (int i = 3; i < parts.length; i++) {
            if (!first) {
                sb.append(File.separator);
            }
            sb.append(parts[i]);
            first = false;
        }

        return nDid + File.separator + sb.toString();
    }

//    private static String getFilePath(String path, String nDid) {
//        path = nDid + "/" + path;
//        String[] parts = path.split("/");
//        StringBuilder sb = new StringBuilder();
//        boolean first = true;
//        for (int i = 4; i < parts.length; i++) {
//            if (!first) {
//                sb.append("/");
//            }
//            sb.append(parts[i]);
//            first = false;
//        }
//        return sb.toString();
//    }

}
