|
|
@@ -1,6 +1,7 @@
|
|
|
package query_rule
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"git.sxidc.com/go-framework/baize/convenient/domain/query_rule/definition"
|
|
|
"git.sxidc.com/go-framework/baize/convenient/domain/query_rule/rule"
|
|
|
"git.sxidc.com/go-framework/baize/framework/binding"
|
|
|
@@ -15,7 +16,6 @@ import (
|
|
|
"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-tools/utils/strutils"
|
|
|
- "github.com/goccy/go-json"
|
|
|
"github.com/pkg/errors"
|
|
|
)
|
|
|
|
|
|
@@ -40,20 +40,25 @@ func Bind(app *application.App, simple *Simple) {
|
|
|
simple.bind(binder)
|
|
|
}
|
|
|
|
|
|
+type QueryRuleParseJsonBody struct {
|
|
|
+ Rule string `json:"rule" assign:"-"`
|
|
|
+}
|
|
|
+
|
|
|
type AdvanceQueryParams interface {
|
|
|
- GetRule() string
|
|
|
+ GetRuleConditions() (string, []any)
|
|
|
GetPageNo() int
|
|
|
GetPageSize() int
|
|
|
}
|
|
|
|
|
|
type BaseAdvanceQueryParams struct {
|
|
|
- Rule string `json:"rule" assign:"-"`
|
|
|
- PageNo int `json:"pageNo" assign:"-"`
|
|
|
- PageSize int `json:"pageSize" assign:"-"`
|
|
|
+ RuleClause string `json:"ruleClause" assign:"-"`
|
|
|
+ RuleClauseArgs []any `json:"ruleClauseArgs" assign:"-"`
|
|
|
+ PageNo int `json:"pageNo" assign:"-"`
|
|
|
+ PageSize int `json:"pageSize" assign:"-"`
|
|
|
}
|
|
|
|
|
|
-func (queryParams *BaseAdvanceQueryParams) GetRule() string {
|
|
|
- return queryParams.Rule
|
|
|
+func (queryParams *BaseAdvanceQueryParams) GetRuleConditions() (string, []any) {
|
|
|
+ return queryParams.RuleClause, queryParams.RuleClauseArgs
|
|
|
}
|
|
|
|
|
|
func (queryParams *BaseAdvanceQueryParams) GetPageNo() int {
|
|
|
@@ -80,7 +85,24 @@ type AddUseQueryRuleQueryRouteParams struct {
|
|
|
QueryMiddlewares []binding.Middleware
|
|
|
}
|
|
|
|
|
|
-func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQueryRuleQueryRouteParams) {
|
|
|
+func (params *AddUseQueryRuleQueryRouteParams) check() error {
|
|
|
+ if params.DBSchema == "" {
|
|
|
+ return errors.New("没有传递DBSchema")
|
|
|
+ }
|
|
|
+
|
|
|
+ if params.Object == nil {
|
|
|
+ return errors.New("没有传递Object")
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQueryRuleQueryRouteParams) error {
|
|
|
+ err := addParams.check()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
domainPath := domain.RelativeDomainPath(addParams.Object)
|
|
|
|
|
|
var queryParams AdvanceQueryParams
|
|
|
@@ -158,7 +180,9 @@ func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQ
|
|
|
additionalConditions = conditions
|
|
|
}
|
|
|
|
|
|
- if strutils.IsStringEmpty(advanceQueryParams.GetRule()) {
|
|
|
+ ruleClause, ruleClauseArgs := advanceQueryParams.GetRuleConditions()
|
|
|
+
|
|
|
+ if strutils.IsStringEmpty(ruleClause) {
|
|
|
if additionalConditions != nil {
|
|
|
selectOtherClauses = append(selectOtherClauses, clause.NewWhere(additionalConditions), limitClause)
|
|
|
countOtherClauses = append(countOtherClauses, clause.NewWhere(additionalConditions))
|
|
|
@@ -166,16 +190,8 @@ func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQ
|
|
|
selectOtherClauses = append(selectOtherClauses, limitClause)
|
|
|
}
|
|
|
} else {
|
|
|
- queryRule := new(rule.Rule)
|
|
|
- err := json.Unmarshal([]byte(advanceQueryParams.GetRule()), queryRule)
|
|
|
- if err != nil {
|
|
|
- return errResponse, err
|
|
|
- }
|
|
|
-
|
|
|
- conditionClause, err := rule.FormConditionClauseByRules(e.DomainCamelName(), *queryRule, nil)
|
|
|
- if err != nil {
|
|
|
- return errResponse, err
|
|
|
- }
|
|
|
+ var conditionClause clause.Clause
|
|
|
+ conditionClause = clause.NewConditions().AddCondition(ruleClause, ruleClauseArgs...).And()
|
|
|
|
|
|
if additionalConditions != nil {
|
|
|
conditionClause = clause.NewConditionJoin(conditionClause, additionalConditions).And()
|
|
|
@@ -231,4 +247,44 @@ func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQ
|
|
|
}, nil
|
|
|
},
|
|
|
}, addParams.QueryMiddlewares...)
|
|
|
+
|
|
|
+ binding.PostBind(binder, &binding.SimpleBindItem[map[string]any]{
|
|
|
+ Path: domainPath + "/queryRule/parse",
|
|
|
+ SendResponseFunc: response.SendMapResponse,
|
|
|
+ RequestParams: &QueryRuleParseJsonBody{},
|
|
|
+ ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
|
|
|
+ errResponse := map[string]any{
|
|
|
+ "ruleClause": "",
|
|
|
+ "ruleClauseArgs": make([]any, 0),
|
|
|
+ }
|
|
|
+
|
|
|
+ jsonBody, err := request.ToConcrete[*QueryRuleParseJsonBody](params)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ r := new(rule.Rule)
|
|
|
+ err = json.Unmarshal([]byte(jsonBody.Rule), r)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ ruleConditionClause, err := rule.FormConditionClauseByRule(addParams.Object.DomainCamelName(), *r, nil)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ ruleConditionClauseStr, err := ruleConditionClause.Clause()
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return map[string]any{
|
|
|
+ "ruleClause": ruleConditionClauseStr,
|
|
|
+ "ruleClauseArgs": ruleConditionClause.Args(),
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ return nil
|
|
|
}
|