...
|
...
|
@@ -53,10 +53,10 @@ type ( |
|
|
}
|
|
|
Expr struct {
|
|
|
// 操作符
|
|
|
OpChar OpType `json:"oc"`
|
|
|
OpChar OpType `json:"op"`
|
|
|
// 如果 OpChar=range ,LeftOp、RightOp需要有值
|
|
|
LeftOp OpType `json:"loc"` // "<= <"
|
|
|
RightOp OpType `json:"roc"` // ">= >"
|
|
|
LeftOp OpType `json:"lop"` // "<= <"
|
|
|
RightOp OpType `json:"rop"` // ">= >"
|
|
|
// 值
|
|
|
Value []interface{} `json:"values"`
|
|
|
}
|
...
|
...
|
@@ -98,6 +98,10 @@ type ( |
|
|
valueType ValueType
|
|
|
OpType OpType
|
|
|
}
|
|
|
LessGreaterExprCompute struct {
|
|
|
expr []Expr
|
|
|
valueType ValueType
|
|
|
}
|
|
|
)
|
|
|
|
|
|
// in合并
|
...
|
...
|
@@ -151,7 +155,7 @@ func combine(arr []interface{}) []interface{} { |
|
|
func NewRangeExprCompute(expr []Expr, valueType ValueType) exprCompute {
|
|
|
rec := RangeNumberExprCompute{valueType: valueType}
|
|
|
for i := range expr {
|
|
|
if expr[i].OpChar == Range || expr[i].OpChar == LessThanEqual || expr[i].OpChar == GreaterThanEqual || expr[i].OpChar == LessThan || expr[i].OpChar == GreaterThan {
|
|
|
if expr[i].OpChar == Range { // || expr[i].OpChar == LessThanEqual || expr[i].OpChar == GreaterThanEqual || expr[i].OpChar == LessThan || expr[i].OpChar == GreaterThan
|
|
|
rec.expr = append(rec.expr, expr[i])
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -178,14 +182,14 @@ func (ex RangeNumberExprCompute) Append(result []ExprResult) []ExprResult { |
|
|
arr.RightOp = ex.expr[0].RightOp
|
|
|
}
|
|
|
for i := 1; i < len(ex.expr); i++ {
|
|
|
if !arr.NumberCompare(ex.expr[i]) {
|
|
|
if !arr.Compare(ex.expr[i]) {
|
|
|
result = append(result, *arr)
|
|
|
arr.Value = ex.expr[i].Value
|
|
|
arr.LeftOp = ex.expr[i].OpChar
|
|
|
arr.RightOp = ex.expr[i].OpChar
|
|
|
//if len(ex.expr[0].LeftOp) != 0 {
|
|
|
// arr.LeftOp = ex.expr[0].LeftOp
|
|
|
//}
|
|
|
if len(ex.expr[0].LeftOp) != 0 {
|
|
|
arr.LeftOp = ex.expr[0].LeftOp
|
|
|
}
|
|
|
if len(ex.expr[0].RightOp) != 0 {
|
|
|
arr.RightOp = ex.expr[0].RightOp
|
|
|
}
|
...
|
...
|
@@ -196,6 +200,60 @@ func (ex RangeNumberExprCompute) Append(result []ExprResult) []ExprResult { |
|
|
return result
|
|
|
}
|
|
|
|
|
|
//范围合并
|
|
|
func NewLessGreaterExprCompute(expr []Expr, valueType ValueType) exprCompute {
|
|
|
rec := LessGreaterExprCompute{valueType: valueType}
|
|
|
for i := range expr {
|
|
|
if expr[i].OpChar == LessThanEqual || expr[i].OpChar == LessThan || expr[i].OpChar == GreaterThanEqual || expr[i].OpChar == GreaterThan {
|
|
|
rec.expr = append(rec.expr, expr[i])
|
|
|
}
|
|
|
}
|
|
|
if len(rec.expr) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
var exprSort = exprSortable(rec.expr)
|
|
|
sort.Sort(exprSort)
|
|
|
rec.expr = exprSort
|
|
|
return rec
|
|
|
}
|
|
|
func (ex LessGreaterExprCompute) Append(result []ExprResult) []ExprResult {
|
|
|
arr := &ExprResult{
|
|
|
OpChar: Range,
|
|
|
ValueType: ex.valueType,
|
|
|
Value: ex.expr[0].Value,
|
|
|
LeftOp: ex.expr[0].OpChar,
|
|
|
RightOp: ex.expr[0].OpChar,
|
|
|
}
|
|
|
x0, x1 := toFloat64(arr.Value[0], arr.Value[1])
|
|
|
for i := 1; i < len(ex.expr); i++ {
|
|
|
compare := ex.expr[i]
|
|
|
y0, y1 := toFloat64(compare.Value[0], compare.Value[1])
|
|
|
if compare.OpChar == LessThanEqual || compare.OpChar == LessThan {
|
|
|
if isInfinity(x1) {
|
|
|
arr.Value[1] = y0
|
|
|
arr.RightOp = compare.OpChar
|
|
|
} else if x1 > y1 {
|
|
|
arr.Value[0] = y0
|
|
|
arr.LeftOp = compare.OpChar
|
|
|
}
|
|
|
}
|
|
|
if compare.OpChar == GreaterThan || compare.OpChar == GreaterThanEqual {
|
|
|
if isInfinity(x0) {
|
|
|
arr.Value[0] = y0
|
|
|
arr.LeftOp = compare.OpChar
|
|
|
} else if y0 > x0 {
|
|
|
arr.Value[0] = y0
|
|
|
arr.LeftOp = compare.OpChar
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if _, ok := less(arr.Value[0], arr.Value[1]); !ok {
|
|
|
arr.Value[0], arr.Value[1] = Infinity, Infinity
|
|
|
}
|
|
|
result = append(result, *arr)
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
// recent范围
|
|
|
func NewRecentDateExprCompute(expr []Expr, valueType ValueType) exprCompute {
|
|
|
inExpr := RecentDateExprCompute{valueType: valueType}
|
...
|
...
|
@@ -268,18 +326,24 @@ func (ex NotEqualExprCompute) Append(result []ExprResult) []ExprResult { |
|
|
return result
|
|
|
}
|
|
|
|
|
|
func (er *ExprResult) NumberCompare(expr Expr) bool {
|
|
|
if len(expr.Value) != 2 {
|
|
|
// Compare 比较两个范围的值
|
|
|
// eg: [80,&] [&,200]
|
|
|
// 排序 [&,200]
|
|
|
// [80,&]
|
|
|
// false:开启新的范围
|
|
|
// true:继续这个范围
|
|
|
func (er *ExprResult) Compare(compare Expr) bool {
|
|
|
if len(compare.Value) != 2 {
|
|
|
return false
|
|
|
}
|
|
|
_, x2 := toFloat64(er.Value[0], er.Value[1])
|
|
|
y1, _ := toFloat64(expr.Value[0], expr.Value[1])
|
|
|
if y1 <= x2 {
|
|
|
er.Value[1] = max(er.Value[1], expr.Value[1])
|
|
|
if isEqual(er.Value[1], expr.Value[1]) {
|
|
|
er.RightOp = expr.OpChar
|
|
|
if len(expr.RightOp) != 0 {
|
|
|
er.RightOp = expr.RightOp
|
|
|
_, x1 := toFloat64(er.Value[0], er.Value[1])
|
|
|
y0, _ := toFloat64(compare.Value[0], compare.Value[1])
|
|
|
if y0 <= x1 {
|
|
|
er.Value[1] = max(er.Value[1], compare.Value[1])
|
|
|
if isEqual(er.Value[1], compare.Value[1]) {
|
|
|
er.RightOp = compare.OpChar
|
|
|
if len(compare.RightOp) != 0 {
|
|
|
er.RightOp = compare.RightOp
|
|
|
}
|
|
|
}
|
|
|
return true
|
...
|
...
|
@@ -349,6 +413,20 @@ func max(x, y interface{}) interface{} { |
|
|
return math.Max(fx, fy)
|
|
|
}
|
|
|
|
|
|
func less(x, y interface{}) (interface{}, bool) {
|
|
|
if isInfinity(x) {
|
|
|
return x, true
|
|
|
}
|
|
|
if isInfinity(y) {
|
|
|
return y, true
|
|
|
}
|
|
|
fx, fy := toFloat64(x, y)
|
|
|
if fx < fy {
|
|
|
return x, true
|
|
|
}
|
|
|
return fy, false
|
|
|
}
|
|
|
|
|
|
func isInfinity(val interface{}) bool {
|
|
|
v := fmt.Sprintf("%v", val)
|
|
|
inf := fmt.Sprintf("%v", Infinity)
|
...
|
...
|
|