146 lines
3.0 KiB
Go
146 lines
3.0 KiB
Go
package engine
|
|
|
|
import "git.kingecg.top/kingecg/gomog/pkg/types"
|
|
|
|
// concat 字符串连接
|
|
func (e *AggregationEngine) concat(operand interface{}, data map[string]interface{}) string {
|
|
arr, ok := operand.([]interface{})
|
|
if !ok {
|
|
return ""
|
|
}
|
|
|
|
result := ""
|
|
for _, item := range arr {
|
|
if str, ok := item.(string); ok {
|
|
result += str
|
|
} else {
|
|
result += toString(item)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// substr 子字符串
|
|
func (e *AggregationEngine) substr(operand interface{}, data map[string]interface{}) string {
|
|
arr, ok := operand.([]interface{})
|
|
if !ok || len(arr) < 2 {
|
|
return ""
|
|
}
|
|
|
|
str := e.getFieldValueStr(types.Document{Data: data}, arr[0])
|
|
start := int(toFloat64(arr[1]))
|
|
|
|
if start < 0 {
|
|
start = 0
|
|
}
|
|
if start >= len(str) {
|
|
return ""
|
|
}
|
|
|
|
end := len(str)
|
|
if len(arr) > 2 {
|
|
length := int(toFloat64(arr[2]))
|
|
if length > 0 {
|
|
end = start + length
|
|
if end > len(str) {
|
|
end = len(str)
|
|
}
|
|
}
|
|
}
|
|
|
|
return str[start:end]
|
|
}
|
|
|
|
// add 加法
|
|
func (e *AggregationEngine) add(operand interface{}, data map[string]interface{}) float64 {
|
|
arr, ok := operand.([]interface{})
|
|
if !ok {
|
|
return 0
|
|
}
|
|
|
|
sum := 0.0
|
|
for _, item := range arr {
|
|
sum += toFloat64(e.evaluateExpression(data, item))
|
|
}
|
|
return sum
|
|
}
|
|
|
|
// multiply 乘法
|
|
func (e *AggregationEngine) multiply(operand interface{}, data map[string]interface{}) float64 {
|
|
arr, ok := operand.([]interface{})
|
|
if !ok {
|
|
return 0
|
|
}
|
|
|
|
product := 1.0
|
|
for _, item := range arr {
|
|
product *= toFloat64(e.evaluateExpression(data, item))
|
|
}
|
|
return product
|
|
}
|
|
|
|
// divide 除法
|
|
func (e *AggregationEngine) divide(operand interface{}, data map[string]interface{}) float64 {
|
|
arr, ok := operand.([]interface{})
|
|
if !ok || len(arr) < 2 {
|
|
return 0
|
|
}
|
|
|
|
dividend := toFloat64(e.evaluateExpression(data, arr[0]))
|
|
divisor := toFloat64(e.evaluateExpression(data, arr[1]))
|
|
|
|
if divisor == 0 {
|
|
return 0
|
|
}
|
|
|
|
return dividend / divisor
|
|
}
|
|
|
|
// ifNull IF NULL 表达式
|
|
func (e *AggregationEngine) ifNull(operand interface{}, data map[string]interface{}) interface{} {
|
|
arr, ok := operand.([]interface{})
|
|
if !ok || len(arr) < 2 {
|
|
return nil
|
|
}
|
|
|
|
value := e.evaluateExpression(data, arr[0])
|
|
if value == nil {
|
|
return e.evaluateExpression(data, arr[1])
|
|
}
|
|
return value
|
|
}
|
|
|
|
// cond 条件表达式
|
|
func (e *AggregationEngine) cond(operand interface{}, data map[string]interface{}) interface{} {
|
|
switch op := operand.(type) {
|
|
case map[string]interface{}:
|
|
ifCond, ok1 := op["if"]
|
|
thenCond, ok2 := op["then"]
|
|
elseCond, ok3 := op["else"]
|
|
|
|
if ok1 && ok2 && ok3 {
|
|
if isTrue(e.evaluateExpression(data, ifCond)) {
|
|
return thenCond
|
|
}
|
|
return elseCond
|
|
}
|
|
case []interface{}:
|
|
if len(op) >= 3 {
|
|
if isTrue(e.evaluateExpression(data, op[0])) {
|
|
return op[1]
|
|
}
|
|
return op[2]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// getFieldValueStr 获取字段值的字符串形式
|
|
func (e *AggregationEngine) getFieldValueStr(doc types.Document, field interface{}) string {
|
|
val := e.getFieldValue(doc, field)
|
|
if str, ok := val.(string); ok {
|
|
return str
|
|
}
|
|
return toString(val)
|
|
}
|