package com.gyee.wisdom.taos;

import com.gyee.wisdom.common.data.timeseries.BooleanTsData;
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.*;
import java.util.stream.Collectors;

@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();

        List<StringBuilder> sbList = new ArrayList<>();
        int mark = 0;
        StringBuilder sb = new StringBuilder();
        sbList.add(sb);
        sb.append("insert into ");
        for (String key : tsDataMap.keySet()) {
            if (mark > 0 && mark % 2999 == 0) {
                sb = new StringBuilder();
                sbList.add(sb);
                sb.append("insert into ");
            }
            long time = tsDataMap.get(key).getTs();
            String point = key;
            if (point.contains("DI")) {
                BooleanTsData booleanTsData = (BooleanTsData) tsDataMap.get(key);
                boolean value = booleanTsData.getBooleanValue();
                sb.append(point).append(" values (");
                sb.append(time).append(",").append(String.valueOf(value)).append(")");
            } else {
                DoubleTsData doubleTsData = (DoubleTsData) tsDataMap.get(key);
                double value = doubleTsData.getDoubleValue();
                sb.append(point).append(" values (");
                sb.append(time).append(",").append(value).append(")");
            }
            mark += 1;

        }

        int updateCount = 0;
        for (StringBuilder s :
                sbList) {
            val i = st.executeUpdate(s.toString());
            updateCount = updateCount + i;
        }
        log.info("taos-write" + updateCount );
        return updateCount;
    }

    //测试新建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);
        }
    }
}
