operator.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package definition
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
  4. "git.sxidc.com/go-framework/baize/framework/core/tag/rule"
  5. "git.sxidc.com/go-tools/utils/reflectutils"
  6. "github.com/pkg/errors"
  7. "reflect"
  8. )
  9. type Operator struct {
  10. Name string `json:"name"`
  11. CNName string `json:"cnName"`
  12. }
  13. const (
  14. opEqual = "eq"
  15. opNotEqual = "ne"
  16. opLessThan = "lt"
  17. opLessThanOrEqual = "lte"
  18. opGreaterThan = "gt"
  19. opGreaterThanOrEqual = "gte"
  20. opIn = "in"
  21. opNotIn = "not-in"
  22. opPrefix = "prefix"
  23. opSuffix = "suffix"
  24. opContains = "contains"
  25. )
  26. func IsSupportedOperator(op string) bool {
  27. return op == opEqual || op == opNotEqual ||
  28. op == opLessThan || op == opLessThanOrEqual ||
  29. op == opGreaterThan || op == opGreaterThanOrEqual ||
  30. op == opIn || op == opNotIn ||
  31. op == opPrefix || op == opSuffix || op == opContains
  32. }
  33. var opTranslationMap = map[string]string{
  34. opEqual: "等于",
  35. opNotEqual: "不等于",
  36. opLessThan: "小于",
  37. opLessThanOrEqual: "小于等于",
  38. opGreaterThan: "大于",
  39. opGreaterThanOrEqual: "大于等于",
  40. opIn: "包含在列表",
  41. opNotIn: "不包含在列表",
  42. opPrefix: "包含前缀",
  43. opSuffix: "包含后缀",
  44. opContains: "包含",
  45. }
  46. var (
  47. operatorEqual = Operator{
  48. Name: opEqual,
  49. CNName: opTranslationMap[opEqual],
  50. }
  51. operatorNotEqual = Operator{
  52. Name: opNotEqual,
  53. CNName: opTranslationMap[opNotEqual],
  54. }
  55. operatorLessThan = Operator{
  56. Name: opLessThan,
  57. CNName: opTranslationMap[opLessThan],
  58. }
  59. operatorLessThanOrEqual = Operator{
  60. Name: opLessThanOrEqual,
  61. CNName: opTranslationMap[opLessThanOrEqual],
  62. }
  63. operatorGreaterThan = Operator{
  64. Name: opGreaterThan,
  65. CNName: opTranslationMap[opGreaterThan],
  66. }
  67. operatorGreaterThanOrEqual = Operator{
  68. Name: opGreaterThanOrEqual,
  69. CNName: opTranslationMap[opGreaterThanOrEqual],
  70. }
  71. operatorIn = Operator{
  72. Name: opIn,
  73. CNName: opTranslationMap[opIn],
  74. }
  75. operatorNotIn = Operator{
  76. Name: opNotIn,
  77. CNName: opTranslationMap[opNotIn],
  78. }
  79. operatorPrefix = Operator{
  80. Name: opPrefix,
  81. CNName: opTranslationMap[opPrefix],
  82. }
  83. operatorSuffix = Operator{
  84. Name: opSuffix,
  85. CNName: opTranslationMap[opSuffix],
  86. }
  87. operatorContains = Operator{
  88. Name: opContains,
  89. CNName: opTranslationMap[opContains],
  90. }
  91. )
  92. var operatorOfTypeMap = map[string][]Operator{
  93. rule.TypeString: {
  94. operatorEqual, operatorNotEqual, operatorIn, operatorNotIn, operatorPrefix, operatorSuffix, operatorContains,
  95. },
  96. rule.TypeTime: {
  97. operatorEqual, operatorNotEqual, operatorLessThan, operatorLessThanOrEqual, operatorGreaterThan, operatorGreaterThanOrEqual, operatorIn, operatorNotIn,
  98. },
  99. rule.TypeNumber: {
  100. operatorEqual, operatorNotEqual, operatorLessThan, operatorLessThanOrEqual, operatorGreaterThan, operatorGreaterThanOrEqual, operatorIn, operatorNotIn,
  101. },
  102. rule.TypeBool: {
  103. operatorEqual, operatorNotEqual,
  104. },
  105. }
  106. func AddCondition(conditions *clause.Conditions, op string, columnName string, value any) error {
  107. switch op {
  108. case opEqual:
  109. conditions.Equal(columnName, value)
  110. case opNotEqual:
  111. conditions.Not(columnName, value)
  112. case opLessThan:
  113. conditions.LessThan(columnName, value)
  114. case opLessThanOrEqual:
  115. conditions.LessThanAndEqual(columnName, value)
  116. case opGreaterThan:
  117. conditions.GreaterThan(columnName, value)
  118. case opGreaterThanOrEqual:
  119. conditions.GreaterThanAndEqual(columnName, value)
  120. case opIn:
  121. if reflect.TypeOf(value).Kind() != reflect.Slice {
  122. return errors.New("使用\"包含在列表\"操作符必须使用列表")
  123. }
  124. conditions.In(columnName, value)
  125. case opNotIn:
  126. if reflect.TypeOf(value).Kind() != reflect.Slice {
  127. return errors.New("使用\"不包含在列表\"操作符必须使用列表")
  128. }
  129. conditions.NotIn(columnName, value)
  130. case opPrefix:
  131. strValue, err := reflectutils.ToString(value)
  132. if err != nil {
  133. return err
  134. }
  135. conditions.Like(columnName, strValue+"%")
  136. case opSuffix:
  137. strValue, err := reflectutils.ToString(value)
  138. if err != nil {
  139. return err
  140. }
  141. conditions.Like(columnName, "%"+strValue)
  142. case opContains:
  143. strValue, err := reflectutils.ToString(value)
  144. if err != nil {
  145. return err
  146. }
  147. conditions.Like(columnName, "%"+strValue+"%")
  148. default:
  149. return errors.Errorf("不支持的操作符%v", op)
  150. }
  151. return nil
  152. }