92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package base
|
|
|
|
import "git.fsdpf.net/go/condition"
|
|
|
|
type ResCondition struct {
|
|
Id int `db:"id"`
|
|
Pid int `db:"pid"`
|
|
Type string `db:"type"`
|
|
CategoryUuid string `db:"category_uuid"`
|
|
Column string `db:"column"`
|
|
ColumnResource string `db:"columnResource"`
|
|
Operator string `db:"operator"`
|
|
ColumnSqlFunc string `db:"columnSqlFunc"`
|
|
ColumnSqlFuncParam string `db:"columnSqlFuncParam"`
|
|
IgnoreEmptyParma bool `db:"ignoreEmptyParma"`
|
|
ValueType string `db:"valueType"`
|
|
Value string `db:"value"`
|
|
UpdatedAt string `db:"updated_at"`
|
|
CreatedAt string `db:"created_at"`
|
|
}
|
|
|
|
type ResWhere struct {
|
|
Type string `json:"type"`
|
|
Exprs []ResWhereExpr `json:"exprs"`
|
|
Children *[]ResWhere `json:"children"`
|
|
}
|
|
|
|
type ResWhereExpr struct {
|
|
Column string `json:"column"`
|
|
ColumnResource string `json:"columnResource"`
|
|
Operator string `json:"operator"`
|
|
ColumnSqlFunc string `json:"columnSqlFunc"`
|
|
ColumnSqlFuncParam string `json:"columnSqlFuncParam"`
|
|
IgnoreEmptyParma int `json:"ignoreEmptyParma"`
|
|
Value string `json:"value"`
|
|
ValueType string `json:"valueType"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
type GetResConditions func(categoryUuid string) []ResCondition
|
|
|
|
// Create Condition By Resource
|
|
func NewConditionByRes(items []ResCondition, opts ...condition.Option) (root *condition.Condition) {
|
|
conditions := map[int]any{}
|
|
|
|
for _, item := range items {
|
|
switch item.Type {
|
|
case "and":
|
|
conditions[item.Id] = condition.New()
|
|
case "or":
|
|
conditions[item.Id] = condition.New(condition.Type(condition.OR))
|
|
case "expr":
|
|
conditions[item.Id] = condition.NewExpr(
|
|
item.ColumnResource, item.Column,
|
|
condition.Token(item.Value, condition.TokenType(item.ValueType)),
|
|
condition.Operator(condition.ConditionOperator(item.Operator)),
|
|
condition.FieldSqlFn(item.ColumnSqlFunc, item.ColumnSqlFuncParam),
|
|
condition.IgnoreEmptyParma(item.IgnoreEmptyParma),
|
|
)
|
|
}
|
|
|
|
if item.Pid == 0 && (item.Type == "and" || item.Type == "or") {
|
|
root = conditions[item.Id].(*condition.Condition)
|
|
}
|
|
}
|
|
|
|
for _, item := range items {
|
|
parent := root
|
|
|
|
if item.Pid == 0 {
|
|
continue
|
|
} else {
|
|
parent = conditions[item.Pid].(*condition.Condition)
|
|
}
|
|
|
|
switch item.Type {
|
|
case "and", "or":
|
|
parent.SetCondition(conditions[item.Id].(*condition.Condition))
|
|
case "expr":
|
|
parent.SetExpr(conditions[item.Id].(*condition.ConditionExpr))
|
|
}
|
|
}
|
|
|
|
if root == nil {
|
|
root = condition.New()
|
|
}
|
|
|
|
root.SetOption(opts...)
|
|
|
|
return root
|
|
}
|