f7ea2e7cf9
Motivation: 新增巴菲特激进/稳健策略(PUTEJJ/PUTEWJ)的跟单推送能力。该类信号仅有 rawMessage、无 action 字段,需按原文直接推送,并过滤启动文案、本地化时间字段。 Changes: * 支持无 action 的纯 rawMessage 信号,映射为 trade.message 事件 * 新增 not_contains 条件操作符,用于过滤启动文案 * 新增 replace 模板函数,将 Time: 替换为推送时间: * 补充巴菲特策略模板、规则与渠道配置文档
104 lines
2.0 KiB
Go
104 lines
2.0 KiB
Go
package condition
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"aiaa-notification-service/internal/model"
|
|
)
|
|
|
|
// Evaluate checks all conditions against data. Returns true if ALL conditions pass.
|
|
// An empty conditions slice always returns true.
|
|
func Evaluate(conditions []model.Condition, data map[string]interface{}) bool {
|
|
if len(conditions) == 0 {
|
|
return true
|
|
}
|
|
for _, c := range conditions {
|
|
if !evaluateOne(c, data) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func evaluateOne(c model.Condition, data map[string]interface{}) bool {
|
|
fieldVal, fieldExists := data[c.Field]
|
|
|
|
switch c.Op {
|
|
case "exists":
|
|
return fieldExists
|
|
case "not_exists":
|
|
return !fieldExists
|
|
case "eq":
|
|
if !fieldExists {
|
|
return false
|
|
}
|
|
return fmt.Sprintf("%v", fieldVal) == c.Value
|
|
case "ne":
|
|
if !fieldExists {
|
|
return false
|
|
}
|
|
return fmt.Sprintf("%v", fieldVal) != c.Value
|
|
case "contains":
|
|
if !fieldExists {
|
|
return false
|
|
}
|
|
return strings.Contains(fmt.Sprintf("%v", fieldVal), c.Value)
|
|
case "not_contains":
|
|
if !fieldExists {
|
|
return true
|
|
}
|
|
return !strings.Contains(fmt.Sprintf("%v", fieldVal), c.Value)
|
|
case "gt", "gte", "lt", "lte":
|
|
if !fieldExists {
|
|
return false
|
|
}
|
|
return compareNumeric(fieldVal, c.Value, c.Op)
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func compareNumeric(fieldVal interface{}, value string, op string) bool {
|
|
fv, err := toFloat64(fieldVal)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
cv, err := strconv.ParseFloat(value, 64)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
switch op {
|
|
case "gt":
|
|
return fv > cv
|
|
case "gte":
|
|
return fv >= cv
|
|
case "lt":
|
|
return fv < cv
|
|
case "lte":
|
|
return fv <= cv
|
|
}
|
|
return false
|
|
}
|
|
|
|
func toFloat64(v interface{}) (float64, error) {
|
|
switch val := v.(type) {
|
|
case float64:
|
|
return val, nil
|
|
case float32:
|
|
return float64(val), nil
|
|
case int:
|
|
return float64(val), nil
|
|
case int64:
|
|
return float64(val), nil
|
|
case string:
|
|
return strconv.ParseFloat(val, 64)
|
|
case json.Number:
|
|
return val.Float64()
|
|
default:
|
|
return 0, fmt.Errorf("cannot convert %T to float64", v)
|
|
}
|
|
}
|