package com.purui.prims.common.pojo;

import com.purui.prims.common.pojo.enums.AggOperatorEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@Data
public class Aggregator {

    @ApiModelProperty(value = "字段名称")
    @NotBlank(message = "Invalid aggregator column")
    private String column;

    @ApiModelProperty(value = "聚合函数")
    private AggOperatorEnum func = AggOperatorEnum.SUM;

    @ApiModelProperty(value = "中文名称")
    private String nameCh;

    @ApiModelProperty(value = "参数")
    private List<String> args;

    @ApiModelProperty(value = "别名")
    private String alias;

    @ApiModelProperty(hidden = true)
    private Integer decimalPlaces;

    List<Castor> castors = new ArrayList<>(2);

    public Aggregator() {
    }

    public Aggregator(String column, AggOperatorEnum func) {
        this.column = column;
        this.func = func;
    }

    public Aggregator(String column, AggOperatorEnum func, List<String> args) {
        this.column = column;
        this.func = func;
        this.args = args;
    }

    public Aggregator(String column, AggOperatorEnum func, String alias) {
        this.column = column;
        this.func = func;
        this.alias = alias;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"column\":\"")
                .append(column).append('\"');
        sb.append(",\"func\":")
                .append(func);
        sb.append(",\"nameCh\":\"")
                .append(nameCh).append('\"');
        sb.append(",\"args\":")
                .append(args);
        sb.append(",\"alias\":")
                .append(alias);
        sb.append('}');
        return sb.toString();
    }
}
