package com.muyu.data.config;


import com.muyu.data.domain.CarInformation;
import org.apache.iotdb.isession.SessionDataSet;
import org.apache.iotdb.isession.util.Version;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.read.common.Field;
import org.apache.iotdb.tsfile.read.common.RowRecord;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * iotdb数据库 配置
 * @author WangXIn
 * @package：com.muyu.event.config
 * @name：IOTDBConfig
 * @date：2024/9/28 19:35
 */
@Configuration
public class IoTDBConfig {

    public static final String HOST="127.0.0.1";
    public static final Integer PORT=6667;
    public static final String USERNAME="root";
    public static final String PASSWORD="root";
    public static final String TABLENAME="root.four.car_information";


    /**
     * 初始化连接
     * @return
     */
    public Session initIoTDB(){
        // 初始化与连接
        Session session = new Session.Builder()
                .host(HOST)
                .port(PORT)
                .username(USERNAME)
                .password(PASSWORD)
                .version(Version.V_1_0)
                .build();
        return session;
    };


    /**
     * 实时数据库添加
     * @param list
     */
    public void insertIoTDB(List<String> list){
        Session session = initIoTDB();
        // 开启session Rpc不压缩
        try {
            session.open(false);

            //添加字段名称
            ArrayList<String> measurements = new ArrayList<>();
            //添加字段类型
            ArrayList<TSDataType> types = new ArrayList<>();

            measurements.add("car_vin");
            measurements.add("information");

            session.insertRecord(TABLENAME,System.currentTimeMillis(),measurements,list);
            //关闭连接
            session.close();
        } catch (IoTDBConnectionException e) {
            throw new RuntimeException(e);
        } catch (StatementExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public CarInformation queryIoTDB(String carVin)  {
        Session session = initIoTDB();
        try {
            session.open(false);
        } catch (IoTDBConnectionException e) {
            throw new RuntimeException(e);
        }
        Long timeMillis = System.currentTimeMillis();

        CarInformation carInformation = new CarInformation();

        try(SessionDataSet dataSet= session.executeQueryStatement("select * from root.four.car_information where car_vin='"+carVin+"'")){
            System.out.println(dataSet.getColumnNames());
            dataSet.setFetchSize(1024);
            while (dataSet.hasNext()){
//                List<Field> fields = dataSet.next().getFields();
//                carInformation.setCarVin(String.valueOf(fields.get(0)));
//                carInformation.setInformation(String.valueOf(fields.get(1)));

                String[] fields = dataSet.next().toString().split("\t");
                carInformation.setTime(Long.valueOf(fields[0]));
                carInformation.setCarVin(fields[1]);
                carInformation.setInformation(fields[2]);
            }

        } catch (IoTDBConnectionException e) {
            throw new RuntimeException(e);
        } catch (StatementExecutionException e) {
            throw new RuntimeException(e);
        }

        //关闭连接
        try {
            session.close();
        } catch (IoTDBConnectionException e) {
            throw new RuntimeException(e);
        }
        return carInformation;
    };








}
