query_rule.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package query_rule
  2. import (
  3. "git.sxidc.com/go-framework/baize/convenient/domain/query_rule/definition"
  4. "git.sxidc.com/go-framework/baize/convenient/domain/query_rule/rule"
  5. "git.sxidc.com/go-framework/baize/framework/binding"
  6. "git.sxidc.com/go-framework/baize/framework/core/api"
  7. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  8. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  9. "git.sxidc.com/go-framework/baize/framework/core/application"
  10. "git.sxidc.com/go-framework/baize/framework/core/domain"
  11. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  12. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  13. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  14. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
  15. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  16. "git.sxidc.com/go-tools/utils/strutils"
  17. "github.com/goccy/go-json"
  18. "github.com/pkg/errors"
  19. )
  20. // Simple Bind参数
  21. type Simple struct {
  22. // schema
  23. Schema string
  24. // 关闭记录查询规则
  25. DisableRecordRule bool
  26. }
  27. func (simple *Simple) bind(binder *binding.Binder) {
  28. (&definition.Simple{Schema: simple.Schema}).Bind(binder)
  29. if !simple.DisableRecordRule {
  30. (&rule.Simple{Schema: simple.Schema}).Bind(binder)
  31. }
  32. }
  33. func Bind(app *application.App, simple *Simple) {
  34. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  35. simple.bind(binder)
  36. }
  37. type AdvanceQueryParams interface {
  38. GetRule() string
  39. GetPageNo() int
  40. GetPageSize() int
  41. }
  42. type BaseAdvanceQueryParams struct {
  43. Rule string `json:"rule" assign:"-"`
  44. PageNo int `json:"pageNo" assign:"-"`
  45. PageSize int `json:"pageSize" assign:"-"`
  46. }
  47. func (queryParams *BaseAdvanceQueryParams) GetRule() string {
  48. return queryParams.Rule
  49. }
  50. func (queryParams *BaseAdvanceQueryParams) GetPageNo() int {
  51. return queryParams.PageNo
  52. }
  53. func (queryParams *BaseAdvanceQueryParams) GetPageSize() int {
  54. return queryParams.PageSize
  55. }
  56. type FormAdditionalSelectClausesFunc func(queryParams AdvanceQueryParams) ([]string, error)
  57. type FormAdditionalFromFunc func(queryParams AdvanceQueryParams) ([]clause.Clause, error)
  58. type FormAdditionalConditionsFunc func(queryParams AdvanceQueryParams) (clause.Clause, error)
  59. type FormOrderByFunc func(queryParams AdvanceQueryParams) (clause.Clause, error)
  60. type AddUseQueryRuleQueryRouteParams struct {
  61. DBSchema string
  62. QueryParams AdvanceQueryParams
  63. FormAdditionalSelectClausesFunc FormAdditionalSelectClausesFunc
  64. FormAdditionalFromFunc FormAdditionalFromFunc
  65. FormAdditionalConditionsFunc FormAdditionalConditionsFunc
  66. FormOrderByFunc FormOrderByFunc
  67. Object domain.Object
  68. QueryMiddlewares []binding.Middleware
  69. }
  70. func AddUseQueryRuleQueryRoute[O any](binder *binding.Binder, addParams *AddUseQueryRuleQueryRouteParams) {
  71. domainPath := domain.RelativeDomainPath(addParams.Object)
  72. var queryParams AdvanceQueryParams
  73. queryParams = new(BaseAdvanceQueryParams)
  74. if addParams.QueryParams != nil {
  75. queryParams = addParams.QueryParams
  76. }
  77. binding.PostBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
  78. Path: domainPath + "/advancedQuery",
  79. SendResponseFunc: response.SendInfosResponse[O],
  80. RequestParams: queryParams,
  81. Objects: []domain.Object{addParams.Object},
  82. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
  83. errResponse := response.InfosData[O]{
  84. Infos: make([]O, 0),
  85. }
  86. if params == nil {
  87. return errResponse, errors.New("请求参数为空")
  88. }
  89. object := objects[0]
  90. if object == nil {
  91. return errResponse, errors.New("领域实体为空")
  92. }
  93. advanceQueryParams, ok := params.(AdvanceQueryParams)
  94. if !ok {
  95. return errResponse, errors.New("请求参数不是Query接口")
  96. }
  97. e, ok := objects[0].(entity.Entity)
  98. if !ok {
  99. return errResponse, errors.New("需要传递领域对象应该为实体")
  100. }
  101. // 构造select语句块
  102. selectClauses := make([]string, 0)
  103. if addParams.FormAdditionalSelectClausesFunc != nil {
  104. additionalSelectClauses, err := addParams.FormAdditionalSelectClausesFunc(advanceQueryParams)
  105. if err != nil {
  106. return errResponse, err
  107. }
  108. selectClauses = additionalSelectClauses
  109. }
  110. // 构造from语句块
  111. fromClauses := []clause.Clause{clause.TableName(domain.TableName(addParams.DBSchema, e))}
  112. if addParams.FormAdditionalFromFunc != nil {
  113. additionalFromClauses, err := addParams.FormAdditionalFromFunc(advanceQueryParams)
  114. if err != nil {
  115. return errResponse, err
  116. }
  117. fromClauses = append(fromClauses, additionalFromClauses...)
  118. }
  119. // 构造where语句块
  120. var conditionsClause clause.Clause
  121. if strutils.IsStringNotEmpty(advanceQueryParams.GetRule()) {
  122. queryRule := new(rule.Rule)
  123. err := json.Unmarshal([]byte(advanceQueryParams.GetRule()), queryRule)
  124. if err != nil {
  125. return errResponse, err
  126. }
  127. ruleConditionClause, err := rule.FormConditionClauseByRule(e.DomainCamelName(), *queryRule, nil)
  128. if err != nil {
  129. return errResponse, err
  130. }
  131. conditionsClause = ruleConditionClause
  132. }
  133. if addParams.FormAdditionalConditionsFunc != nil {
  134. additionalConditionsClause, err := addParams.FormAdditionalConditionsFunc(advanceQueryParams)
  135. if err != nil {
  136. return errResponse, err
  137. }
  138. conditionsClause = clause.NewConditionJoin(conditionsClause, additionalConditionsClause).And()
  139. }
  140. // 构造order by语句块
  141. var orderByClause clause.Clause
  142. if addParams.FormOrderByFunc != nil {
  143. innerOrderByClause, err := addParams.FormOrderByFunc(advanceQueryParams)
  144. if err != nil {
  145. return errResponse, err
  146. }
  147. orderByClause = innerOrderByClause
  148. }
  149. // 构造limit语句块
  150. limitClause := clause.NewLimit(advanceQueryParams.GetPageNo(), advanceQueryParams.GetPageSize())
  151. fromClause := clause.NewFrom(fromClauses)
  152. selectClause := clause.NewSelect(selectClauses, fromClause, clause.NewWhere(conditionsClause), orderByClause, limitClause)
  153. countClause := clause.NewSelect([]string{"COUNT(*) AS total"}, fromClause, clause.NewWhere(conditionsClause))
  154. selectClauseStr, err := selectClause.Clause()
  155. if err != nil {
  156. return errResponse, err
  157. }
  158. results, err := database.ExecuteRawSql(i.DBExecutor(), selectClauseStr, selectClause.Args()...)
  159. if err != nil {
  160. return errResponse, err
  161. }
  162. countClauseStr, err := countClause.Clause()
  163. if err != nil {
  164. return errResponse, err
  165. }
  166. countResults, err := database.ExecuteRawSql(i.DBExecutor(), countClauseStr, countClause.Args()...)
  167. if err != nil {
  168. return errResponse, err
  169. }
  170. infos := make([]O, 0)
  171. err = sql.ParseSqlResult(results, &infos)
  172. if err != nil {
  173. return errResponse, err
  174. }
  175. return response.InfosData[O]{
  176. Infos: infos,
  177. TotalCount: countResults[0].ColumnValueInt64("total"),
  178. PageNo: advanceQueryParams.GetPageNo(),
  179. }, nil
  180. },
  181. }, addParams.QueryMiddlewares...)
  182. }