advanced_query.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package managesdk
  2. type QueryOperator string
  3. const (
  4. OperatorEqual QueryOperator = "="
  5. OperatorNotEqual QueryOperator = "!="
  6. OperatorGreaterThan QueryOperator = ">"
  7. OperatorGreaterEqual QueryOperator = ">="
  8. OperatorLessThan QueryOperator = "<"
  9. OperatorLessEqual QueryOperator = "<="
  10. OperatorLike QueryOperator = "like"
  11. OperatorNotLike QueryOperator = "not_like"
  12. OperatorStartsWith QueryOperator = "starts_with"
  13. OperatorEndsWith QueryOperator = "ends_with"
  14. OperatorIsNull QueryOperator = "is_null"
  15. OperatorIsNotNull QueryOperator = "is_not_null"
  16. OperatorIn QueryOperator = "in"
  17. OperatorNotIn QueryOperator = "not_in"
  18. )
  19. type QueryOperatorInfo struct {
  20. Name string `json:"name"`
  21. Value QueryOperator `json:"value"`
  22. }
  23. func GetQueryOperators() []QueryOperatorInfo {
  24. return []QueryOperatorInfo{
  25. {Name: "等于", Value: OperatorEqual},
  26. {Name: "不等于", Value: OperatorNotEqual},
  27. {Name: "大于", Value: OperatorGreaterThan},
  28. {Name: "大于等于", Value: OperatorGreaterEqual},
  29. {Name: "小于", Value: OperatorLessThan},
  30. {Name: "小于等于", Value: OperatorLessEqual},
  31. {Name: "模糊匹配", Value: OperatorLike},
  32. {Name: "不模糊匹配", Value: OperatorNotLike},
  33. {Name: "开头是", Value: OperatorStartsWith},
  34. {Name: "结尾是", Value: OperatorEndsWith},
  35. {Name: "为空", Value: OperatorIsNull},
  36. {Name: "不为空", Value: OperatorIsNotNull},
  37. {Name: "在列表中", Value: OperatorIn},
  38. {Name: "不在列表中", Value: OperatorNotIn},
  39. }
  40. }
  41. func StringToQueryOperator(op string) QueryOperator {
  42. switch op {
  43. case "eq", "=", "等于":
  44. return OperatorEqual
  45. case "ne", "!=", "不等于":
  46. return OperatorNotEqual
  47. case "gt", ">", "大于":
  48. return OperatorGreaterThan
  49. case "gte", ">=", "大于等于":
  50. return OperatorGreaterEqual
  51. case "lt", "<", "小于":
  52. return OperatorLessThan
  53. case "lte", "<=", "小于等于":
  54. return OperatorLessEqual
  55. case "like", "模糊匹配":
  56. return OperatorLike
  57. case "not_like", "不模糊匹配":
  58. return OperatorNotLike
  59. case "starts_with", "开头是":
  60. return OperatorStartsWith
  61. case "ends_with", "结尾是":
  62. return OperatorEndsWith
  63. case "is_null", "为空":
  64. return OperatorIsNull
  65. case "is_not_null", "不为空":
  66. return OperatorIsNotNull
  67. case "in", "在列表中":
  68. return OperatorIn
  69. case "not_in", "不在列表中":
  70. return OperatorNotIn
  71. default:
  72. return OperatorEqual
  73. }
  74. }
  75. type LogicOperator string
  76. const (
  77. LogicAnd LogicOperator = "AND"
  78. LogicOr LogicOperator = "OR"
  79. )
  80. type QueryCondition struct {
  81. Field string `json:"field"`
  82. Operator QueryOperator `json:"operator"`
  83. Value any `json:"value,omitempty"`
  84. }
  85. type ConditionGroup struct {
  86. Conditions []ConditionItem `json:"conditions"`
  87. Logic LogicOperator `json:"logic"`
  88. }
  89. type ConditionItem struct {
  90. Condition *ConditionGroup `json:"condition,omitempty"`
  91. SingleCondition *QueryCondition `json:"singleCondition,omitempty"`
  92. }
  93. type AdvancedQueryParams struct {
  94. ConditionGroups []ConditionGroup `json:"conditionGroups"`
  95. GroupLogic LogicOperator `json:"groupLogic"`
  96. }
  97. type AdvancedQueryRequest struct {
  98. AdvancedQuery *AdvancedQueryParams `json:"advancedQuery,omitempty"`
  99. BaseQueryParams
  100. }
  101. func NewConditionGroup(logic LogicOperator, conditions ...ConditionItem) ConditionGroup {
  102. return ConditionGroup{
  103. Logic: logic,
  104. Conditions: conditions,
  105. }
  106. }
  107. func NewSingleCondition(field string, operator QueryOperator, value any) ConditionItem {
  108. return ConditionItem{
  109. SingleCondition: &QueryCondition{
  110. Field: field,
  111. Operator: operator,
  112. Value: value,
  113. },
  114. }
  115. }
  116. func NewNestedConditionGroup(group ConditionGroup) ConditionItem {
  117. return ConditionItem{
  118. Condition: &group,
  119. }
  120. }
  121. func NewAdvancedQueryParams(groupLogic LogicOperator, groups ...ConditionGroup) *AdvancedQueryParams {
  122. return &AdvancedQueryParams{
  123. GroupLogic: groupLogic,
  124. ConditionGroups: groups,
  125. }
  126. }
  127. func Equal(field string, value any) ConditionItem {
  128. return NewSingleCondition(field, OperatorEqual, value)
  129. }
  130. func NotEqual(field string, value any) ConditionItem {
  131. return NewSingleCondition(field, OperatorNotEqual, value)
  132. }
  133. func GreaterThan(field string, value any) ConditionItem {
  134. return NewSingleCondition(field, OperatorGreaterThan, value)
  135. }
  136. func GreaterEqual(field string, value any) ConditionItem {
  137. return NewSingleCondition(field, OperatorGreaterEqual, value)
  138. }
  139. func LessThan(field string, value any) ConditionItem {
  140. return NewSingleCondition(field, OperatorLessThan, value)
  141. }
  142. func LessEqual(field string, value any) ConditionItem {
  143. return NewSingleCondition(field, OperatorLessEqual, value)
  144. }
  145. func Like(field string, value any) ConditionItem {
  146. return NewSingleCondition(field, OperatorLike, value)
  147. }
  148. func NotLike(field string, value any) ConditionItem {
  149. return NewSingleCondition(field, OperatorNotLike, value)
  150. }
  151. func StartsWith(field string, value any) ConditionItem {
  152. return NewSingleCondition(field, OperatorStartsWith, value)
  153. }
  154. func EndsWith(field string, value any) ConditionItem {
  155. return NewSingleCondition(field, OperatorEndsWith, value)
  156. }
  157. func IsNull(field string) ConditionItem {
  158. return NewSingleCondition(field, OperatorIsNull, nil)
  159. }
  160. func IsNotNull(field string) ConditionItem {
  161. return NewSingleCondition(field, OperatorIsNotNull, nil)
  162. }
  163. func In(field string, value any) ConditionItem {
  164. return NewSingleCondition(field, OperatorIn, value)
  165. }
  166. func NotIn(field string, value any) ConditionItem {
  167. return NewSingleCondition(field, OperatorNotIn, value)
  168. }
  169. func AndGroup(conditions ...ConditionItem) ConditionGroup {
  170. return NewConditionGroup(LogicAnd, conditions...)
  171. }
  172. func OrGroup(conditions ...ConditionItem) ConditionGroup {
  173. return NewConditionGroup(LogicOr, conditions...)
  174. }
  175. func AndQuery(groups ...ConditionGroup) *AdvancedQueryParams {
  176. return NewAdvancedQueryParams(LogicAnd, groups...)
  177. }
  178. func OrQuery(groups ...ConditionGroup) *AdvancedQueryParams {
  179. return NewAdvancedQueryParams(LogicOr, groups...)
  180. }