package com.muyu.processing.config;

import lombok.extern.log4j.Log4j2;
import org.apache.iotdb.isession.SessionDataSet;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.read.common.Field;
import org.apache.iotdb.tsfile.read.common.RowRecord;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

/**
 * iotdb配置类
 * @Author：杨鹏
 * @Package：com.muyu.processing.config
 * @Project：car-cloud-server
 * @name：IotDBConfig
 * @Date：2024/9/30 15:13
 */
@Log4j2
@Component
@Configuration
public class IotDBConfig {

    /**
     * 创建session
     */
    @Bean
    public static Session session(){
        Session session = null;
        try {
            session = new Session(
                    "47.101.49.53",
                    6667,
                    "root",
                    "root"
            );
            session.open(false);
            session.setFetchSize(100);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return session;
    }

    /**
     * 新增数据
     */
    public void execute(String deviceId, Long time, List<String> measurement, List<String> values){
        if (!CollectionUtils.isEmpty(measurement) && !CollectionUtils.isEmpty(values)){
            try {
                session().insertAlignedRecord(deviceId,time,measurement,values);
            } catch (IoTDBConnectionException e) {
                throw new RuntimeException(e);
            } catch (StatementExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 查询数据
     */
    public List<HashMap<String, Object>> executeQuery(String sql){
        log.info("sql:{}",sql);
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();

        try {
            SessionDataSet sessionDataSet = session().executeQueryStatement(sql);
            int fetchSize = sessionDataSet.getFetchSize();
            List<String> columnNames = sessionDataSet.getColumnNames();
            List<String> columnTypes = sessionDataSet.getColumnTypes();
            System.out.println(columnNames);
            System.out.println(columnTypes);
            if (fetchSize > 0){
               while (sessionDataSet.hasNext()){
                   HashMap<String, Object> map = new HashMap<>();
                   RowRecord next = sessionDataSet.next();
                   List<Field> fields = next.getFields();
                   // 查询结果第一个为时间戳
                   long timestamp = next.getTimestamp();
                   for (int i = 0; i < fields.size(); i++) {
                       Field field = fields.get(i);
                       String key = field.getStringValue();
                       // 这里的需要按照类型获取
                       Object value = field.getObjectValue(field.getDataType());
                       map.put(key, value);
                   }
                   list.add(map);
               }
            }
            sessionDataSet.closeOperationHandle();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }


}
