package com.gyee.wisdom.taos;

import com.gyee.wisdom.common.data.timeseries.DoubleTsData;
import com.gyee.wisdom.common.data.timeseries.TsData;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class WriteRealValue {

    @Autowired
    TaosConfig taosConfig;
    @Autowired
    ThreadPoolTaskConfig taskConfig;

    public int writeValue(Map<String, TsData> tsDataMap) throws Exception {
        Statement st = taosConfig.getInstance().createStatement();
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ");
        for (String key : tsDataMap.keySet()) {
            long time = tsDataMap.get(key).getTs();
            String point = key;
            DoubleTsData doubleTsData = (DoubleTsData)tsDataMap.get(key);
            double value = doubleTsData.getDoubleValue();
            sb.append(point).append(" values (");
            sb.append(time).append(",").append(value).append(")");
        }
        val i = st.executeUpdate(sb.toString());
        return i==tsDataMap.size()? tsDataMap.size() : 0;
    }

    //测试新建20000个点表
    public int createTable() throws Exception {
        Statement st = taosConfig.getInstance().createStatement();
        StringBuilder sb = new StringBuilder();
        sb.append("create table ");
        for (int i = 1 ;i <= 20000; i++) {
            sb.append("'test_").append(i).append("'").append(" ").append("using").append(" ");
            sb.append("'windturbine'").append(" ").append("tags").append("('").append("test_").append(i).append("')");
        }
        val i = st.executeUpdate(sb.toString());
        return i;
    }

    //测试删除点
    public void removeTable() throws Exception {
        Statement st = taosConfig.getInstance().createStatement();
        StringBuilder sb = new StringBuilder();
        for (int i = 2450 ;i <= 20000; i++) {
            sb.append("drop table ");
            sb.append("test_").append(i);
            int i1 = st.executeUpdate(sb.toString());
            sb.setLength(0);
        }
    }
}
