75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package condition
|
|
|
|
import (
|
|
"git.fsdpf.net/go/condition/contracts"
|
|
"git.fsdpf.net/go/contracts/base"
|
|
)
|
|
|
|
// Create Condition By Resource
|
|
func NewConditionByRes(items []base.ResCondition, describe string) (root contracts.Condition) {
|
|
conditions := map[int]any{}
|
|
|
|
for _, item := range items {
|
|
switch item.Type {
|
|
case "and":
|
|
conditions[item.Id] = &Condition{typ: AND}
|
|
case "or":
|
|
conditions[item.Id] = &Condition{typ: OR}
|
|
case "expr":
|
|
conditions[item.Id] = &ConditionExpr{
|
|
field: item.Column,
|
|
fieldResource: item.ColumnResource,
|
|
operator: contracts.ConditionOperator(item.Operator),
|
|
fieldSqlFunc: item.ColumnSqlFunc,
|
|
fieldSqlFuncParam: item.ColumnSqlFuncParam,
|
|
ignoreEmptyParma: item.IgnoreEmptyParma,
|
|
tokenType: contracts.ConditionTokenType(item.ValueType),
|
|
token: item.Value,
|
|
}
|
|
}
|
|
|
|
if item.Pid == 0 && (item.Type == "and" || item.Type == "or") {
|
|
root = conditions[item.Id].(*Condition)
|
|
}
|
|
}
|
|
|
|
for _, item := range items {
|
|
parent := root
|
|
|
|
if item.Pid == 0 {
|
|
continue
|
|
} else {
|
|
parent = conditions[item.Pid].(*Condition)
|
|
}
|
|
|
|
switch item.Type {
|
|
case "and", "or":
|
|
child := conditions[item.Id].(contracts.Condition)
|
|
if len(parent.(*Condition).childrens) == 0 {
|
|
parent.(*Condition).childrens = []contracts.Condition{child}
|
|
} else {
|
|
parent.(*Condition).childrens = append(parent.(*Condition).childrens, child)
|
|
}
|
|
child.AppendTo(parent)
|
|
case "expr":
|
|
expr := conditions[item.Id].(contracts.ConditionExpr)
|
|
if len(parent.(*Condition).exprs) == 0 {
|
|
parent.(*Condition).exprs = []contracts.ConditionExpr{expr}
|
|
} else {
|
|
parent.(*Condition).exprs = append(parent.(*Condition).exprs, expr)
|
|
}
|
|
expr.AppendTo(parent)
|
|
}
|
|
}
|
|
|
|
if root == nil {
|
|
root = &Condition{}
|
|
}
|
|
|
|
if describe != "" {
|
|
root.(*Condition).describe = describe
|
|
}
|
|
|
|
return root
|
|
}
|