package com.njcn;

import cn.hutool.core.date.DatePattern;
import com.njcn.csdevice.CsDeviceBootApplication;
import com.njcn.csdevice.pojo.dto.PqsCommunicateDto;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import com.njcn.csdevice.service.*;
import com.njcn.influx.pojo.po.PqsCommunicate;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.influx.utils.InfluxDbUtils;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertTrue;

/**
 * Unit test for simple App.
 */
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = CsDeviceBootApplication.class)
@Slf4j
@AllArgsConstructor
public class AppTest{

    @Autowired
    private DeviceFtpService deviceFtpService;

    @Autowired
    private ICsCommunicateService csCommunicateService;

    private final IRStatIntegrityDService statIntegrityDService;
    private final IRStatOnlineRateDService statOnlineRateDService;


    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }


    /**
     * 测试下载文件
     */
    @SneakyThrows
    @Test
    public void downloadFileTest() {
        String nDid = "00B78D016AB5";
        String name = "/etc/pqs_arm.bin";
        Integer size = 5123552;
        String fileCheck = "859E36E8";
        for (int i = 0; i < 10; i++) {
            log.info("开始第{}次", i);
            deviceFtpService.downloadFile(nDid,name,size,fileCheck);
            Thread.sleep(1000 * 60 * 4);
            log.info("这是第{}次询问装置,结果为:{}", i, null);
        }
    }

    @SneakyThrows
    @Test
    public void test1() {
    }

    @SneakyThrows
    @Test
    public void writeData() {
        PqsCommunicateDto pqsCommunicateDto = new PqsCommunicateDto();
        pqsCommunicateDto.setTime("2025-08-04 18:00:00");
        pqsCommunicateDto.setDevId("da7aa071bf89864bedea8833133676b7");
        pqsCommunicateDto.setType(0);
        pqsCommunicateDto.setDescription("通讯中断");
        csCommunicateService.insertion(pqsCommunicateDto);
    }


    public static void main(String[] args) {
        InfluxDbUtils influxDbUtils = new InfluxDbUtils("admin", "123456", "http://192.168.1.24:8086", "pqsbase_zl", "");
        Map<String, String> tags = new HashMap<>();
        tags.put("dev_id","da7aa071bf89864bedea8833133676b7");
        Map<String,Object> fields = new HashMap<>();
        fields.put("type",1);
        fields.put("description","通讯正常");
        long time = LocalDateTime.parse("2025-08-02 18:00:00",  DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        Point point = influxDbUtils.pointBuilder("pqs_communicate", time, TimeUnit.MILLISECONDS, tags, fields);
        BatchPoints batchPoints = BatchPoints.database(influxDbUtils.getDbName()).retentionPolicy("").consistency(InfluxDB.ConsistencyLevel.ALL).build();
        batchPoints.point(point);
        influxDbUtils.batchInsert(influxDbUtils.getDbName(), "", InfluxDB.ConsistencyLevel.ALL, TimeUnit.MILLISECONDS, Collections.singletonList(batchPoints.lineProtocol()));

    }
}
