|
@@ -35,25 +35,53 @@ func Bind(app *application.App, simple *Simple) {
|
|
|
simple.bind(binder)
|
|
|
}
|
|
|
|
|
|
+type AdvanceQueryParams interface {
|
|
|
+ GetRule() string
|
|
|
+ GetPageNo() int
|
|
|
+ GetPageSize() int
|
|
|
+}
|
|
|
+
|
|
|
type BaseAdvanceQueryParams struct {
|
|
|
Rule string `json:"rule" assign:"-"`
|
|
|
PageNo int `json:"pageNo" assign:"-"`
|
|
|
PageSize int `json:"pageSize" assign:"-"`
|
|
|
}
|
|
|
|
|
|
+func (queryParams *BaseAdvanceQueryParams) GetRule() string {
|
|
|
+ return queryParams.Rule
|
|
|
+}
|
|
|
+
|
|
|
+func (queryParams *BaseAdvanceQueryParams) GetPageNo() int {
|
|
|
+ return queryParams.PageNo
|
|
|
+}
|
|
|
+
|
|
|
+func (queryParams *BaseAdvanceQueryParams) GetPageSize() int {
|
|
|
+ return queryParams.PageSize
|
|
|
+}
|
|
|
+
|
|
|
+type FormAdditionalConditionsFunc func(queryParams AdvanceQueryParams) (clause.Clause, error)
|
|
|
+
|
|
|
type AddUseQueryRuleQueryRouteParams struct {
|
|
|
- DBSchema string
|
|
|
- Object domain.Object
|
|
|
- QueryMiddlewares []binding.Middleware
|
|
|
+ DBSchema string
|
|
|
+ QueryParams AdvanceQueryParams
|
|
|
+ FormAdditionalConditionsFunc FormAdditionalConditionsFunc
|
|
|
+ Object domain.Object
|
|
|
+ QueryMiddlewares []binding.Middleware
|
|
|
}
|
|
|
|
|
|
func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQueryRuleQueryRouteParams) {
|
|
|
domainPath := domain.RelativeDomainPath(addParams.Object)
|
|
|
|
|
|
+ var queryParams AdvanceQueryParams
|
|
|
+ queryParams = new(BaseAdvanceQueryParams)
|
|
|
+ if addParams.QueryParams != nil {
|
|
|
+ queryParams = addParams.QueryParams
|
|
|
+ }
|
|
|
+
|
|
|
binding.PostBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
|
|
|
Path: domainPath + "/advancedQuery",
|
|
|
SendResponseFunc: response.SendInfosResponse[O],
|
|
|
- RequestParams: &BaseAdvanceQueryParams{},
|
|
|
+ RequestParams: queryParams,
|
|
|
Objects: []domain.Object{addParams.Object},
|
|
|
ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
|
|
|
errResponse := response.InfosData[O]{
|
|
@@ -69,7 +97,7 @@ func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQ
|
|
|
return errResponse, errors.New("领域实体为空")
|
|
|
}
|
|
|
|
|
|
- queryParams, ok := params.(*BaseAdvanceQueryParams)
|
|
|
+ advanceQueryParams, ok := params.(AdvanceQueryParams)
|
|
|
if !ok {
|
|
|
return errResponse, errors.New("请求参数不是Query接口")
|
|
|
}
|
|
@@ -81,16 +109,31 @@ func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQ
|
|
|
|
|
|
var selectClause *clause.Select
|
|
|
var countClause *clause.Select
|
|
|
+ var additionalConditions clause.Clause
|
|
|
|
|
|
fromClause := clause.NewFrom([]clause.Clause{clause.TableName(domain.TableName(addParams.DBSchema, e))})
|
|
|
- limitClause := clause.NewLimit(queryParams.PageNo, queryParams.PageSize)
|
|
|
+ limitClause := clause.NewLimit(advanceQueryParams.GetPageNo(), advanceQueryParams.GetPageSize())
|
|
|
|
|
|
- if strutils.IsStringEmpty(queryParams.Rule) {
|
|
|
- selectClause = clause.NewSelect(nil, fromClause, limitClause)
|
|
|
- countClause = clause.NewSelect([]string{"COUNT(*) AS total"}, fromClause)
|
|
|
+ if addParams.FormAdditionalConditionsFunc != nil {
|
|
|
+ conditions, err := addParams.FormAdditionalConditionsFunc(advanceQueryParams)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ additionalConditions = conditions
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringEmpty(advanceQueryParams.GetRule()) {
|
|
|
+ if additionalConditions != nil {
|
|
|
+ selectClause = clause.NewSelect(nil, fromClause, clause.NewWhere(additionalConditions), limitClause)
|
|
|
+ countClause = clause.NewSelect([]string{"COUNT(*) AS total"}, fromClause, clause.NewWhere(additionalConditions))
|
|
|
+ } else {
|
|
|
+ selectClause = clause.NewSelect(nil, fromClause, limitClause)
|
|
|
+ countClause = clause.NewSelect([]string{"COUNT(*) AS total"}, fromClause)
|
|
|
+ }
|
|
|
} else {
|
|
|
queryRule := new(rule.Rule)
|
|
|
- err := json.Unmarshal([]byte(queryParams.Rule), queryRule)
|
|
|
+ err := json.Unmarshal([]byte(advanceQueryParams.GetRule()), queryRule)
|
|
|
if err != nil {
|
|
|
return errResponse, err
|
|
|
}
|
|
@@ -100,6 +143,10 @@ func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQ
|
|
|
return errResponse, err
|
|
|
}
|
|
|
|
|
|
+ if additionalConditions != nil {
|
|
|
+ conditionClause = clause.NewConditionJoin(conditionClause, additionalConditions).And()
|
|
|
+ }
|
|
|
+
|
|
|
selectClause = clause.NewSelect(nil, fromClause, clause.NewWhere(conditionClause), limitClause)
|
|
|
countClause = clause.NewSelect([]string{"COUNT(*) AS total"}, fromClause, clause.NewWhere(conditionClause))
|
|
|
}
|
|
@@ -133,7 +180,7 @@ func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQ
|
|
|
return response.InfosData[O]{
|
|
|
Infos: infos,
|
|
|
TotalCount: countResults[0].ColumnValueInt64("total"),
|
|
|
- PageNo: queryParams.PageNo,
|
|
|
+ PageNo: advanceQueryParams.GetPageNo(),
|
|
|
}, nil
|
|
|
},
|
|
|
}, addParams.QueryMiddlewares...)
|