|
|
@@ -1,17 +1,16 @@
|
|
|
-package query_rule
|
|
|
+package rule
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "git.sxidc.com/go-framework/baize/convenient/domain/query_rule/definition"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/domain"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/tag/rule"
|
|
|
- "git.sxidc.com/go-tools/utils/reflectutils"
|
|
|
"git.sxidc.com/go-tools/utils/strutils"
|
|
|
"github.com/pkg/errors"
|
|
|
- "reflect"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
@@ -28,14 +27,13 @@ type Rule struct {
|
|
|
Left *Rule `json:"left"`
|
|
|
Right *Rule `json:"right"`
|
|
|
|
|
|
- FieldName string `json:"fieldName"`
|
|
|
- FieldType string `json:"fieldType"`
|
|
|
- ColumnName string `json:"columnName"`
|
|
|
- Operator string `json:"operator"`
|
|
|
- Value any `json:"value"`
|
|
|
+ FieldName string `json:"fieldName"`
|
|
|
+ FieldType string `json:"fieldType"`
|
|
|
+ Operator string `json:"operator"`
|
|
|
+ Value any `json:"value"`
|
|
|
}
|
|
|
|
|
|
-func (r Rule) Check() error {
|
|
|
+func (r Rule) check() error {
|
|
|
if strutils.IsStringNotEmpty(r.LogicalOperator) {
|
|
|
return r.checkAsNode()
|
|
|
} else {
|
|
|
@@ -92,27 +90,22 @@ func (r Rule) checkAsLeaf() error {
|
|
|
return errors.New("字段类型不支持")
|
|
|
}
|
|
|
|
|
|
- if strutils.IsStringEmpty(r.ColumnName) {
|
|
|
- return errors.New("列名为空")
|
|
|
- }
|
|
|
-
|
|
|
- if !IsSupportedOperator(r.Operator) {
|
|
|
+ if !definition.IsSupportedOperator(r.Operator) {
|
|
|
return errors.New("操作符不支持")
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func Lint(ruleStr string) error {
|
|
|
- r := new(Rule)
|
|
|
- err := json.Unmarshal([]byte(ruleStr), r)
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- return r.Check()
|
|
|
-}
|
|
|
-
|
|
|
+// HasRule 检查是否存在规则
|
|
|
+// 参数:
|
|
|
+// - dbSchema: 数据库schema
|
|
|
+// - scope: 范围
|
|
|
+// - domainName: 领域名称
|
|
|
+// - i: 基础设施
|
|
|
+// 返回值:
|
|
|
+// - 是否存在规则
|
|
|
+// - 错误
|
|
|
func HasRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (bool, error) {
|
|
|
dbExecutor := i.DBExecutor()
|
|
|
|
|
|
@@ -125,7 +118,7 @@ func HasRule(dbSchema string, scope string, domainName string, i *infrastructure
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-// GetRulesAndFormConditionClause 获取规则并返回条件语句
|
|
|
+// FormConditionClause 获取规则并返回条件语句
|
|
|
// 参数:
|
|
|
// - dbSchema: 数据库schema
|
|
|
// - scope: 范围
|
|
|
@@ -135,13 +128,13 @@ func HasRule(dbSchema string, scope string, domainName string, i *infrastructure
|
|
|
// 返回值:
|
|
|
// - 语句接口,当错误为nil时,语句接口也可能是nil,代表该规则并没有被赋值,所以是没有条件的
|
|
|
// - 错误
|
|
|
-func GetRulesAndFormConditionClause(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure, ruleParams map[string]any) (clause.Clause, error) {
|
|
|
- r, err := getRule(dbSchema, scope, domainName, i)
|
|
|
+func FormConditionClause(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure, ruleParams map[string]any) (clause.Clause, error) {
|
|
|
+ r, err := getEnabledRule(dbSchema, scope, domainName, i)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- c, err := formConditionClause(r, ruleParams)
|
|
|
+ c, err := formConditionClause(domainName, r, ruleParams)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -149,7 +142,7 @@ func GetRulesAndFormConditionClause(dbSchema string, scope string, domainName st
|
|
|
return c, nil
|
|
|
}
|
|
|
|
|
|
-func getRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (Rule, error) {
|
|
|
+func getEnabledRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (Rule, error) {
|
|
|
dbExecutor := i.DBExecutor()
|
|
|
|
|
|
result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
|
|
|
@@ -172,10 +165,15 @@ func getRule(dbSchema string, scope string, domainName string, i *infrastructure
|
|
|
return *r, nil
|
|
|
}
|
|
|
|
|
|
-func formConditionClause(r Rule, ruleParams map[string]any) (clause.Clause, error) {
|
|
|
+func formConditionClause(domainName string, r Rule, ruleParams map[string]any) (clause.Clause, error) {
|
|
|
if strutils.IsStringEmpty(r.LogicalOperator) {
|
|
|
+ columnName, err := definition.GetColumnName(domainName, r.FieldName)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
ruleValue := r.Value
|
|
|
- ruleParamValue, ok := ruleParams[r.ColumnName]
|
|
|
+ ruleParamValue, ok := ruleParams[columnName]
|
|
|
if ok {
|
|
|
ruleValue = ruleParamValue
|
|
|
}
|
|
|
@@ -186,65 +184,20 @@ func formConditionClause(r Rule, ruleParams map[string]any) (clause.Clause, erro
|
|
|
|
|
|
conditions := clause.NewConditions()
|
|
|
|
|
|
- switch r.Operator {
|
|
|
- case opEqual:
|
|
|
- conditions.Equal(r.ColumnName, ruleValue)
|
|
|
- case opNotEqual:
|
|
|
- conditions.Not(r.ColumnName, ruleValue)
|
|
|
- case opLessThan:
|
|
|
- conditions.LessThan(r.ColumnName, ruleValue)
|
|
|
- case opLessThanOrEqual:
|
|
|
- conditions.LessThanAndEqual(r.ColumnName, ruleValue)
|
|
|
- case opGreaterThan:
|
|
|
- conditions.GreaterThan(r.ColumnName, ruleValue)
|
|
|
- case opGreaterThanOrEqual:
|
|
|
- conditions.GreaterThanAndEqual(r.ColumnName, ruleValue)
|
|
|
- case opIn:
|
|
|
- if reflect.TypeOf(ruleValue).Kind() != reflect.Slice {
|
|
|
- return nil, errors.New("使用\"包含在列表\"操作符必须使用列表")
|
|
|
- }
|
|
|
-
|
|
|
- conditions.In(r.ColumnName, ruleValue)
|
|
|
- case opNotIn:
|
|
|
- if reflect.TypeOf(ruleValue).Kind() != reflect.Slice {
|
|
|
- return nil, errors.New("使用\"不包含在列表\"操作符必须使用列表")
|
|
|
- }
|
|
|
-
|
|
|
- conditions.NotIn(r.ColumnName, ruleValue)
|
|
|
- case opPrefix:
|
|
|
- strValue, err := reflectutils.ToString(ruleValue)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- conditions.Like(r.ColumnName, strValue+"%")
|
|
|
- case opSuffix:
|
|
|
- strValue, err := reflectutils.ToString(ruleValue)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- conditions.Like(r.ColumnName, "%"+strValue)
|
|
|
- case opContains:
|
|
|
- strValue, err := reflectutils.ToString(ruleValue)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- conditions.Like(r.ColumnName, "%"+strValue+"%")
|
|
|
- default:
|
|
|
- return nil, errors.Errorf("不支持的操作符%v", r.Operator)
|
|
|
+ err = definition.AddCondition(conditions, r.Operator, columnName, ruleValue)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
return conditions.And(), nil
|
|
|
}
|
|
|
|
|
|
- leftClause, err := formConditionClause(*r.Left, ruleParams)
|
|
|
+ leftClause, err := formConditionClause(domainName, *r.Left, ruleParams)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- rightClause, err := formConditionClause(*r.Right, ruleParams)
|
|
|
+ rightClause, err := formConditionClause(domainName, *r.Right, ruleParams)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|