package com.dcs.equipment.domain;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author: wangpx
 * @date: 2025-03-08 12:00
 */
@Data
public class InfluxDBQuery {
    @ApiModelProperty(value = "库名")
    private String bucket;
    @ApiModelProperty(value = "表名")
    private String measurement;
    @ApiModelProperty(value = "查询字段")
    private String[] fields;
    @ApiModelProperty(value = "按值查询")
    private String equipmentName;
    @ApiModelProperty(value = "开始时间")
    private String timeRangeStart;
    @ApiModelProperty(value = "结束时间")
    private String timeRangeStop;
    @ApiModelProperty(value = "窗口期")
    private String windowPeriod;
    @ApiModelProperty(value = "聚合函数")
    private String aggregateFunction;
    @ApiModelProperty(value = "填充值")
    private Double fill;

    /**
     * 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: true)\n");
        }
        query.append("  |> fill(");
        if (fill == null) { // 无填充值
            // 默认使用上一个值填充
            query.append("usePrevious: true");
        } else {
            query.append("value: ").append(fill);
        }
        query.append(")\n");
        
        // yield
        if (aggregateFunction != null) {
            query.append("  |> yield(name: \"").append(aggregateFunction).append("\")");
        }
        
        return query.toString();
    }
}
