query_set_components_aggregation_rule.go 2.2 KB
package domain

import (
	"bytes"
	"fmt"
)

type AggregationRule struct {
	Aggregation
}

type Aggregation struct {
	// 行
	RowFields []*AggregationField `json:"rowFields"`
	// 值
	ValueFields []*AggregationField `json:"valueFields"`
	// 选择列表 【字段名称】
	SelectFields []string `json:"selectFields"`
}

type AggregationField struct {
	Id              string    `json:"id"`
	Field           *Field    `json:"field"`
	DisplayName     string    `json:"displayName"`
	AggregationFlag string    `json:"aggregationFlag"` // 行:row 列:column 值:value
	Order           string    `json:"order"`           // 降序:desc 升序asc 默认:"" 无排序
	Expr            FieldExpr `json:"expr"`
}

func (ar *AggregationRule) Fields() []*Field {
	fields := make([]*Field, 0)
	for _, f := range ar.RowFields {
		fields = append(fields, f.Field)
	}
	for _, f := range ar.ValueFields {
		fields = append(fields, f.Field)
	}
	return fields
}

func (ar *AggregationRule) AggregationFields() []*AggregationField {
	fields := make([]*AggregationField, 0)
	for _, f := range ar.RowFields {
		fields = append(fields, f)
	}
	for _, f := range ar.ValueFields {
		fields = append(fields, f)
	}
	return fields
}

func (ar *AggregationRule) OrderFields() []*Field {
	aggregationFields := ar.AggregationFields()
	var fields = make([]*Field, 0)
	for _, f := range aggregationFields {
		if f.Order != "" {
			copyField := f.Field.Copy()
			copyField.Order = f.Order
			fields = append(fields, copyField)
		}
	}
	return fields
}

func (ar *AggregationField) Diff(c *AggregationField) bool {
	if ar.Expr.ExprSql != c.Expr.ExprSql {
		return true
	}
	if ar.Order != c.Order {
		return true
	}
	if ar.AggregationFlag != c.AggregationFlag {
		return true
	}
	return false
}

func (ar *AggregationField) Info() string {
	buf := bytes.NewBuffer(nil)
	buf.WriteString("(")
	buf.WriteString(fmt.Sprintf("%s|", ar.DisplayName))
	buf.WriteString(fmt.Sprintf("%s,", ar.Expr.ExprHuman))
	if ar.AggregationFlag == "row" {
		buf.WriteString("分组,")
	} else {
		buf.WriteString("不分组,")
	}
	if ar.Order == "asc" {
		buf.WriteString("升序")
	} else if ar.Order == "desc" {
		buf.WriteString("降序")
	} else {
		buf.WriteString("默认")
	}
	buf.WriteString(")")
	return buf.String()
}