package com.njcn.event.influxdb;

import com.njcn.event.pojo.po.PqsOnlinerate;
import com.njcn.influx.config.InfluxDbConfig;
import com.njcn.influx.deprecated.InfluxDBPublicParam;
import com.njcn.influx.utils.InfluxDbUtils;
import org.influxdb.dto.QueryResult;
import org.influxdb.querybuilder.SelectQueryImpl;
import org.influxdb.querybuilder.SelectionQueryImpl;
import org.influxdb.querybuilder.clauses.Clause;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.eq;

@Component
public class PqsOnlinerateQuery extends QueryBuilder {

    @Resource
    private InfluxDbConfig influxDbConfig;
    
    protected PqsOnlinerateQuery(InfluxDbUtils influxDbUtils) {
        super(influxDbUtils);
    }
    
    /**
     * 指定查询数据库和表
     *
     * @param column select count 条件
     * @see SelectQueryImpl
     */
    private SelectQueryImpl fromTable(SelectionQueryImpl column) {
        return column.from(influxDbConfig.getDatabase(), InfluxDBPublicParam.PQS_ONLINERATE);
    }
    
    /**
     * dev_id or条件
     *
     * @param devIds dev_id
     * @return dev_id or条件
     */
    public static List<Clause> devIdOr(List<String> devIds) {
        List<Clause> devIdOr = new ArrayList<>();
        for (String lineId : devIds) {
            Clause clause = eq("dev_id", lineId);
            devIdOr.add(clause);
        }
        return devIdOr;
    }
    
    /**
     * 查询方法
     * SELECT在包括一个tag时，必须至少指定一个field
     *
     * @param columnNames 显示的字段名，查询需要指定，减少数据加载
     * @param or          or条件
     * @param and         and条件
     * @return 结果
     */
    public List<PqsOnlinerate> selectList(List<String> columnNames, List<Clause> or, List<Clause> and) {
        SelectionQueryImpl selectAppendColumnNames = selectAppendColumnNames(columnNames);
        SelectQueryImpl selectAppendTable = fromTable(selectAppendColumnNames);
        QueryResult query = getQueryResult(or, and, selectAppendTable);
        return toPOJO(query, PqsOnlinerate.class);
    }
    
}
