query_rule_parser.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package rule
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/go-framework/baize/convenient/domain/query_rule/definition"
  5. "git.sxidc.com/go-framework/baize/framework/core/domain"
  6. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  7. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  10. "git.sxidc.com/go-framework/baize/framework/core/tag/rule"
  11. "git.sxidc.com/go-tools/utils/strutils"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. LogicalOperatorAnd = "and"
  16. LogicalOperatorOr = "or"
  17. )
  18. func isSupportedLogicalOperator(logicalOperator string) bool {
  19. return logicalOperator == LogicalOperatorAnd || logicalOperator == LogicalOperatorOr
  20. }
  21. type Rule struct {
  22. LogicalOperator string `json:"logicalOperator"`
  23. Left *Rule `json:"left"`
  24. Right *Rule `json:"right"`
  25. FieldName string `json:"fieldName"`
  26. FieldType string `json:"fieldType"`
  27. Operator string `json:"operator"`
  28. Value any `json:"value"`
  29. }
  30. func (r Rule) check() error {
  31. if strutils.IsStringNotEmpty(r.LogicalOperator) {
  32. return r.checkAsNode()
  33. } else {
  34. return r.checkAsLeaf()
  35. }
  36. }
  37. func (r Rule) checkAsNode() error {
  38. if !isSupportedLogicalOperator(r.LogicalOperator) {
  39. return errors.New("不支持的逻辑操作符")
  40. }
  41. if r.Left == nil {
  42. return errors.New("左结点为空")
  43. }
  44. if r.Right == nil {
  45. return errors.New("右结点为空")
  46. }
  47. if strutils.IsStringNotEmpty(r.Left.LogicalOperator) {
  48. err := r.Left.checkAsNode()
  49. if err != nil {
  50. return err
  51. }
  52. } else {
  53. err := r.Left.checkAsLeaf()
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. if strutils.IsStringNotEmpty(r.Right.LogicalOperator) {
  59. err := r.Right.checkAsNode()
  60. if err != nil {
  61. return err
  62. }
  63. } else {
  64. err := r.Right.checkAsLeaf()
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. func (r Rule) checkAsLeaf() error {
  72. if strutils.IsStringEmpty(r.FieldName) {
  73. return errors.New("字段名为空")
  74. }
  75. if !rule.IsSupportedType(r.FieldType) {
  76. return errors.New("字段类型不支持")
  77. }
  78. if !definition.IsSupportedOperator(r.Operator) {
  79. return errors.New("操作符不支持")
  80. }
  81. return nil
  82. }
  83. // HasRule 检查是否存在规则
  84. // 参数:
  85. // - dbSchema: 数据库schema
  86. // - scope: 范围
  87. // - domainName: 领域名称
  88. // - i: 基础设施
  89. // 返回值:
  90. // - 是否存在规则
  91. // - 错误
  92. func HasRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (bool, error) {
  93. dbExecutor := i.DBExecutor()
  94. return database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  95. TableName: domain.TableName(dbSchema, &Entity{}),
  96. Conditions: sql.NewConditions().
  97. Equal(ColumnScope, scope).
  98. Equal(ColumnDomainName, domainName).
  99. Equal(ColumnEnabled, true),
  100. })
  101. }
  102. // FormConditionClause 获取规则并返回条件语句
  103. // 参数:
  104. // - dbSchema: 数据库schema
  105. // - scope: 范围
  106. // - domainName: 领域名称
  107. // - i: 基础设施
  108. // - ruleParams: 规则参数
  109. // 返回值:
  110. // - 语句接口,当错误为nil时,语句接口也可能是nil,代表该规则并没有被赋值,所以是没有条件的
  111. // - 错误
  112. func FormConditionClause(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure, ruleParams map[string]any) (clause.Clause, error) {
  113. r, err := getEnabledRule(dbSchema, scope, domainName, i)
  114. if err != nil {
  115. return nil, err
  116. }
  117. c, err := formConditionClause(domainName, r, ruleParams)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return c, nil
  122. }
  123. func getEnabledRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (Rule, error) {
  124. dbExecutor := i.DBExecutor()
  125. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  126. TableName: domain.TableName(dbSchema, &Entity{}),
  127. Conditions: sql.NewConditions().
  128. Equal(ColumnScope, scope).
  129. Equal(ColumnDomainName, domainName).
  130. Equal(ColumnEnabled, true),
  131. })
  132. if err != nil {
  133. return Rule{}, err
  134. }
  135. r := new(Rule)
  136. err = json.Unmarshal([]byte(result.ColumnValueString(ColumnRule)), r)
  137. if err != nil {
  138. return Rule{}, err
  139. }
  140. return *r, nil
  141. }
  142. func formConditionClause(domainName string, r Rule, ruleParams map[string]any) (clause.Clause, error) {
  143. if strutils.IsStringEmpty(r.LogicalOperator) {
  144. columnName, err := definition.GetColumnName(domainName, r.FieldName)
  145. if err != nil {
  146. return nil, err
  147. }
  148. ruleValue := r.Value
  149. ruleParamValue, ok := ruleParams[columnName]
  150. if ok {
  151. ruleValue = ruleParamValue
  152. }
  153. if ruleValue == nil {
  154. return nil, nil
  155. }
  156. conditions := clause.NewConditions()
  157. err = definition.AddCondition(conditions, r.Operator, columnName, ruleValue)
  158. if err != nil {
  159. return nil, err
  160. }
  161. return conditions.And(), nil
  162. }
  163. leftClause, err := formConditionClause(domainName, *r.Left, ruleParams)
  164. if err != nil {
  165. return nil, err
  166. }
  167. rightClause, err := formConditionClause(domainName, *r.Right, ruleParams)
  168. if err != nil {
  169. return nil, err
  170. }
  171. switch r.LogicalOperator {
  172. case LogicalOperatorAnd:
  173. return clause.NewConditionJoin(leftClause, rightClause).And(), nil
  174. case LogicalOperatorOr:
  175. return clause.NewConditionJoin(leftClause, rightClause).Or(), nil
  176. default:
  177. return clause.NewConditionJoin(leftClause, rightClause).And(), nil
  178. }
  179. }