contracts/base/res_condition.go

92 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-04-12 16:56:55 +08:00
package base
2024-05-09 13:15:24 +08:00
import "git.fsdpf.net/go/condition"
2023-04-12 16:56:55 +08:00
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"`
}
2023-04-12 19:50:25 +08:00
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"`
}
2023-04-12 19:50:25 +08:00
type GetResConditions func(categoryUuid string) []ResCondition
2024-05-09 13:15:24 +08:00
// 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
}