package com.njcn.influxdb.utils;

import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * pqs
 * 传入终端id集合返回influxdb使用sql
 * @author cdf
 * @date 2022/7/1
 */
@Component
@Validated
public class InfluxDBCommUtils {

    /**
     * 组装为influxdb监测点参数
     *
     * @param lineIds 监测点参数
     * @return StringBuilder
     */
    public static StringBuilder assToInfluxParam(@NotEmpty List<String> lineIds){
        StringBuilder lineIdsStr = new StringBuilder("(");
        for (int i = 0; i < lineIds.size(); i++) {
            if (lineIds.size() - i != 1) {
                lineIdsStr.append("line_id ='").append(lineIds.get(i)).append("' or ");
            } else {
                lineIdsStr.append("line_id ='").append(lineIds.get(i)).append("') ");
            }
        }
        return lineIdsStr;
    }


    /**
     * 组装为influxdb监测点参数
     *
     * @param devIds 设备ids参数
     * @return StringBuilder
     */
    public static StringBuilder assToInfluxParamDev(@NotEmpty List<String> devIds){
        StringBuilder lineIdsStr = new StringBuilder("(");
        for (int i = 0; i < devIds.size(); i++) {
            if (devIds.size() - i != 1) {
                lineIdsStr.append("dev_id ='").append(devIds.get(i)).append("' or ");
            } else {
                lineIdsStr.append("dev_id ='").append(devIds.get(i)).append("') ");
            }
        }
        return lineIdsStr;
    }

    /**
     * 组装为influxdb监测点参数
     *
     * @param devIds 设备ids参数
     * @param name  列明
     * @return StringBuilder
     */
    public static StringBuilder assToInfluxParamAndName(@NotEmpty List<String> devIds,String name){
        StringBuilder lineIdsStr = new StringBuilder("(");
        for (int i = 0; i < devIds.size(); i++) {
            if (devIds.size() - i != 1) {
                lineIdsStr.append(name+" ='").append(devIds.get(i)).append("' or ");
            } else {
                lineIdsStr.append(name+" ='").append(devIds.get(i)).append("') ");
            }
        }
        return lineIdsStr;
    }
}
