package com.dcs.equipment.domain;

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

/**
 * @author: wangpx
 * @date: 2025-03-08 12:00
 */
public class InfluxDBQuery {
    private String bucket;
    private String measurement;
    private String[] fields;
    private String equipmentName;
    private String timeRangeStart;
    private String timeRangeStop;
    private String windowPeriod;
    private String aggregateFunction;

    public InfluxDBQuery() {
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public void setMeasurement(String measurement) {
        this.measurement = measurement;
    }

    public void setFields(String[] fields) {
        this.fields = fields;
    }

    public void setEquipmentName(String equipmentName) {
        this.equipmentName = equipmentName;
    }

    public void setTimeRangeStart(String timeRangeStart) {
        this.timeRangeStart = timeRangeStart;
    }

    public void setTimeRangeStop(String timeRangeStop) {
        this.timeRangeStop = timeRangeStop;
    }

    public void setWindowPeriod(String windowPeriod) {
        this.windowPeriod = windowPeriod;
    }

    public void setAggregateFunction(String aggregateFunction) {
        this.aggregateFunction = aggregateFunction;
    }

    /**
     * SELECT MEAN("LB6bpqdzd"), MEAN("LB6bpsz")
     * FROM "data"
     * WHERE time >= 'timeRangeStart' AND time <= 'timeRangeStop'
     * GROUP BY time(60s)
     * FILL(none)
     */
    // 构造FluxQL语句
    public String buildFluxQL() {
        StringBuilder query = new StringBuilder();
        query.append("SELECT ");
        // 构造数据查询语句
        for (String field : fields) {
            query.append(aggregateFunction + "(\"" + field + "\"),");
        }
        // 去掉最后一个逗号
        query.deleteCharAt(query.length() - 1);
        // 表名
        query.append(" FROM \"").append(measurement).append("\"");
        // 时间条件
        List<String> timeConditions = new ArrayList<>();
        // 开始时间
        if (timeRangeStart != null) {
            timeConditions.add("time >= '" + timeRangeStart + "'");
        }
        // 结束时间
        if (timeRangeStop != null) {
            timeConditions.add("time <= '" + timeRangeStop + "'");
        }
        // 构造时间条件
        if (!timeConditions.isEmpty()) {
            query.append(" WHERE ").append(String.join(" AND ", timeConditions));
        }

        // 时间间隔
        if (windowPeriod != null) {
            query.append(" GROUP BY time(").append(windowPeriod).append(")");
        }
        // 填充方式
        query.append(" FILL(none)");
        return query.toString();
    }


    /**
     * from(bucket: "shanghai_dcs_bucket")
     *   |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
     *   |> filter(fn: (r) => r["_measurement"] == "data")
     *   |> filter(fn: (r) => r["_field"] == "LB6bpqdzd" or r["_field"] == "LB6bpsz" or r["_field"] == "LB6bpztz" or r["_field"] == "LB6yxfkpl")
     *   |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
     *   |> yield(name: "mean")
     */
    // 构造Flux语句
    public String buildFlux() {
        StringBuilder query = new StringBuilder();
        
        // from bucket
        query.append("from(bucket: \"").append(bucket).append("\")\n");
        
        // range
        if (timeRangeStart != null) {
            query.append("  |> range(start: ").append(timeRangeStart);
            if (timeRangeStop != null) {
                query.append(", stop: ").append(timeRangeStop);
            }
            query.append(")\n");
        }
        
        // measurement filter
        if (measurement != null) {
            query.append("  |> filter(fn: (r) => r[\"_measurement\"] == \"")
                 .append(measurement).append("\")\n");
        }
        
        // fields filter
        if (fields != null && fields.length > 0) {
            query.append("  |> filter(fn: (r) => ");
            for (int i = 0; i < fields.length; i++) {
                if (i > 0) {
                    query.append(" or ");
                }
                query.append("r[\"_field\"] == \"").append(fields[i]).append("\"");
            }
            query.append(")\n");
        }
        
        // equipment name filter
        if (equipmentName != null) {
            query.append("  |> filter(fn: (r) => r[\"equipment_name\"] == \"")
                 .append(equipmentName).append("\")\n");
        }
        
        // aggregate window
        if (windowPeriod != null && aggregateFunction != null) {
            query.append("  |> aggregateWindow(every: ").append(windowPeriod)
                 .append(", fn: ").append(aggregateFunction)
                 .append(", createEmpty: false)\n");
        }
        
        // yield
        if (aggregateFunction != null) {
            query.append("  |> yield(name: \"").append(aggregateFunction).append("\")");
        }
        
        return query.toString();
    }
}
