package com.njcn;

import cn.hutool.core.text.StrPool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.influx.pojo.po.DataFlicker;
import com.njcn.influx.pojo.po.DataInHarmRateV;
import com.njcn.influx.pojo.po.DataV;
import com.njcn.influx.pojo.po.PqsCommunicate;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.manufactinflux.param.JobQueryParam;
import com.njcn.manufactinflux.po.Monitor;
import com.njcn.manufactinflux.service.IInfluxManufactService;
import com.njcn.manufactinflux.service.IMonitorService;
import lombok.SneakyThrows;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年12月14日 12:55
 */
public class DataTest extends BaseJunitTest {

    static InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();


    @Value("${spring.influx.target.database}")
    private String database;

    @Value("${spring.influx.target.url}")
    private String url;

    @Value("${spring.influx.target.user}")
    private String user;

    @Value("${spring.influx.target.password}")
    private String password;

    @Autowired
    private IMonitorService monitorService;

    @Autowired
    private IInfluxManufactService iInfluxManufactService;



    @SneakyThrows
    @Test
    public void test() {
        List<String> lineList = Arrays.asList("e5807e73521aa58dec3cb1cf74805281"
                ,"a78e16cd28a428b1c1ee3af25880281d","ff2d9674c1f1ecce7f33a5bf17fc4f2d"
                ,"f7c58f82fd8ba1f307ef5beec4d462be","b65ef14ed9e25f682840061b1abb464f"
                ,"44ea885554d88eef661c2671f738125a","1aacf83cd01dfa3581438cbecf06977f"
                ,"23b8eb64453fbb0e23d9ff1e913c5843","07ee83be55ec8fa8c8af50cb8a6925d6"
                ,"fbd1201ac204b5f40583a53456ed2de1","692f8201ba6f50a31f1ab99051ed6985"
                ,"b7f94884e69deb54a26414dcb015b88d");
        String startTime = "2023-09-23 00:00:00";
        String endTime ="2023-09-23 01:00:00";
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(DataV.class);
        influxQueryWrapper.between(DataV::getTime, startTime,endTime).regular(DataV::getLineId,lineList);

        System.out.println(influxQueryWrapper.generateSql());
        InfluxDbUtils influxDbUtils = new InfluxDbUtils(user, password, url, database, "");
        QueryResult sqlResult = influxDbUtils.query(influxQueryWrapper.generateSql());
        System.out.println(2222);
        System.out.println(2222);
        System.out.println(2222);
        System.out.println(2222);
        System.out.println(2222);
        List<DataV> list = resultMapper.toPOJO(sqlResult, DataV.class);
        Map<String, List<DataV>> stringListMap = list.stream().collect(Collectors.groupingBy(obj -> obj.getPhaseType().concat(StrPool.UNDERLINE).concat(obj.getValueType())));

        influxQueryWrapper = new InfluxQueryWrapper(DataInHarmRateV.class);
        influxQueryWrapper.between(DataInHarmRateV::getTime, startTime, endTime).regular(DataInHarmRateV::getLineId, lineList);
        QueryResult dataInHarmRateVResult = influxDbUtils.query(influxQueryWrapper.generateSql());
        System.out.println(influxQueryWrapper.generateSql());
        List<DataInHarmRateV>  dataInHarmRateVList = resultMapper.toPOJO(dataInHarmRateVResult, DataInHarmRateV.class);
        System.out.println(11);
        System.out.println(11);
        System.out.println(11);
//        List<DataV> collect = list.stream().filter(dataV -> dataV.getLineId().equalsIgnoreCase("07ee83be55ec8fa8c8af50cb8a6925d6")).collect(Collectors.toList());
        System.out.println(11);

    }

    @SneakyThrows
    @Test
    public void test1() {
        JobQueryParam jobQueryParam = new JobQueryParam();
        jobQueryParam.setSourceType(0);
        iInfluxManufactService.manufactInfluxData(jobQueryParam);
    }


}
