query_rule_parser.go 5.9 KB

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