query_rule_parser.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 r.isNode() {
  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. func (r Rule) isNode() bool {
  85. return strutils.IsStringNotEmpty(r.LogicalOperator)
  86. }
  87. // HasRule 检查是否存在规则
  88. // 参数:
  89. // - dbSchema: 数据库schema
  90. // - scope: 范围
  91. // - domainName: 领域名称
  92. // - i: 基础设施
  93. // 返回值:
  94. // - 是否存在规则
  95. // - 错误
  96. func HasRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (bool, error) {
  97. dbExecutor := i.DBExecutor()
  98. return database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  99. TableName: domain.TableName(dbSchema, &Entity{}),
  100. Conditions: sql.NewConditions().
  101. Equal(ColumnScope, scope).
  102. Equal(ColumnDomainName, domainName).
  103. Equal(ColumnEnabled, true),
  104. })
  105. }
  106. // FormConditionClause 获取规则并返回条件语句
  107. // 参数:
  108. // - dbSchema: 数据库schema
  109. // - scope: 范围
  110. // - domainName: 领域名称
  111. // - i: 基础设施
  112. // - ruleParams: 规则参数
  113. // 返回值:
  114. // - 语句接口,当错误为nil时,语句接口也可能是nil,代表该规则并没有被赋值,所以是没有条件的
  115. // - 错误
  116. func FormConditionClause(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure, ruleParams map[string]any) (clause.Clause, error) {
  117. r, err := getEnabledRule(dbSchema, scope, domainName, i)
  118. if err != nil {
  119. return nil, err
  120. }
  121. c, err := formConditionClause(domainName, r, ruleParams)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return c, nil
  126. }
  127. // FormConditionClauseByRule 使用Rule结构构造并返回条件语句
  128. // 参数:
  129. // - domainName: 领域名称
  130. // - rule: Rule的结构
  131. // - ruleParams: 规则参数
  132. // 返回值:
  133. // - 语句接口,当错误为nil时,语句接口也可能是nil,代表该规则并没有被赋值,所以是没有条件的
  134. // - 错误
  135. func FormConditionClauseByRule(domainName string, r Rule, ruleParams map[string]any) (clause.Clause, error) {
  136. c, err := formConditionClause(domainName, r, ruleParams)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return c, nil
  141. }
  142. const (
  143. modifyBranchRoot = "root"
  144. modifyBranchLeft = "left"
  145. modifyBranchRight = "right"
  146. )
  147. func ModifyLeafRuleStr(ruleStr string, leafRuleCallback func(leafRule *Rule) error) (string, error) {
  148. rootRule := new(Rule)
  149. err := json.Unmarshal([]byte(ruleStr), rootRule)
  150. if err != nil {
  151. return "", err
  152. }
  153. err = modifyLeafRule(nil, rootRule, modifyBranchRoot, leafRuleCallback)
  154. if err != nil {
  155. return "", err
  156. }
  157. newRuleStr, err := json.Marshal(rootRule)
  158. if err != nil {
  159. return "", err
  160. }
  161. return string(newRuleStr), nil
  162. }
  163. func modifyLeafRule(parentRule *Rule, r *Rule, branch string, leafRuleCallback func(leafRule *Rule) error) error {
  164. if r == nil {
  165. return nil
  166. }
  167. err := r.check()
  168. if err != nil {
  169. return err
  170. }
  171. if !r.isNode() {
  172. err := leafRuleCallback(r)
  173. if err != nil {
  174. return err
  175. }
  176. if parentRule == nil {
  177. return nil
  178. }
  179. switch branch {
  180. case modifyBranchRoot:
  181. return nil
  182. case modifyBranchLeft:
  183. parentRule.Left = r
  184. case modifyBranchRight:
  185. parentRule.Right = r
  186. default:
  187. return errors.New("不支持的分支类型: " + branch)
  188. }
  189. }
  190. err = modifyLeafRule(r, r.Left, modifyBranchLeft, leafRuleCallback)
  191. if err != nil {
  192. return err
  193. }
  194. err = modifyLeafRule(r, r.Right, modifyBranchRight, leafRuleCallback)
  195. if err != nil {
  196. return err
  197. }
  198. return nil
  199. }
  200. func modifyRule(rootRule *Rule, ruleCallback func(parentRule *Rule, r *Rule, branch string) error) error {
  201. if rootRule == nil {
  202. return nil
  203. }
  204. err := rootRule.check()
  205. if err != nil {
  206. return err
  207. }
  208. if !rootRule.isNode() {
  209. return ruleCallback(nil, rootRule, modifyBranchRoot)
  210. }
  211. err = ruleCallback(rootRule, rootRule.Left, modifyBranchLeft)
  212. if err != nil {
  213. return err
  214. }
  215. if rootRule.Left.isNode() {
  216. err := modifyRule(rootRule.Left, ruleCallback)
  217. if err != nil {
  218. return err
  219. }
  220. }
  221. err = ruleCallback(rootRule, rootRule.Right, modifyBranchRight)
  222. if err != nil {
  223. return err
  224. }
  225. if rootRule.Right.isNode() {
  226. err := modifyRule(rootRule.Right, ruleCallback)
  227. if err != nil {
  228. return err
  229. }
  230. }
  231. return nil
  232. }
  233. func getEnabledRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (Rule, error) {
  234. dbExecutor := i.DBExecutor()
  235. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  236. TableName: domain.TableName(dbSchema, &Entity{}),
  237. Conditions: sql.NewConditions().
  238. Equal(ColumnScope, scope).
  239. Equal(ColumnDomainName, domainName).
  240. Equal(ColumnEnabled, true),
  241. })
  242. if err != nil {
  243. return Rule{}, err
  244. }
  245. r := new(Rule)
  246. err = json.Unmarshal([]byte(result.ColumnValueString(ColumnRule)), r)
  247. if err != nil {
  248. return Rule{}, err
  249. }
  250. return *r, nil
  251. }
  252. func formConditionClause(domainName string, r Rule, ruleParams map[string]any) (clause.Clause, error) {
  253. err := r.check()
  254. if err != nil {
  255. return nil, err
  256. }
  257. if strutils.IsStringEmpty(r.LogicalOperator) {
  258. columnName, err := definition.GetColumnName(domainName, r.FieldName)
  259. if err != nil {
  260. return nil, err
  261. }
  262. ruleValue := r.Value
  263. if ruleParams != nil {
  264. ruleParamValue, ok := ruleParams[columnName]
  265. if ok {
  266. ruleValue = ruleParamValue
  267. }
  268. }
  269. if ruleValue == nil {
  270. return nil, nil
  271. }
  272. conditions := clause.NewConditions()
  273. err = definition.AddCondition(conditions, r.Operator, columnName, ruleValue)
  274. if err != nil {
  275. return nil, err
  276. }
  277. return conditions.And(), nil
  278. }
  279. leftClause, err := formConditionClause(domainName, *r.Left, ruleParams)
  280. if err != nil {
  281. return nil, err
  282. }
  283. rightClause, err := formConditionClause(domainName, *r.Right, ruleParams)
  284. if err != nil {
  285. return nil, err
  286. }
  287. switch r.LogicalOperator {
  288. case LogicalOperatorAnd:
  289. return clause.NewConditionJoin(leftClause, rightClause).And(), nil
  290. case LogicalOperatorOr:
  291. return clause.NewConditionJoin(leftClause, rightClause).Or(), nil
  292. default:
  293. return clause.NewConditionJoin(leftClause, rightClause).And(), nil
  294. }
  295. }