query.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package http
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. "go-common/app/admin/main/search/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func checkParams(c *bm.Context) (sp *model.QueryParams, err error) {
  12. sp = &model.QueryParams{
  13. QueryBody: &model.QueryBody{},
  14. }
  15. if err = c.Bind(sp); err != nil {
  16. return
  17. }
  18. decoder := json.NewDecoder(strings.NewReader(sp.QueryBodyStr))
  19. decoder.UseNumber()
  20. if err = decoder.Decode(&sp.QueryBody); err != nil {
  21. log.Error("s.http.upsert(%v) json error(%v)", err, sp.QueryBodyStr)
  22. }
  23. if sp.QueryBody == nil {
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. // eq
  28. if sp.QueryBody.Where != nil {
  29. for k, v := range sp.QueryBody.Where.EQ {
  30. if integer, ok := v.(json.Number); ok {
  31. if sp.QueryBody.Where.EQ[k], err = integer.Int64(); err != nil {
  32. log.Error("sp.QueryBody.Where.EQ.numberToInt64(%v)(%v)", integer, err)
  33. }
  34. }
  35. }
  36. // or
  37. for k, v := range sp.QueryBody.Where.Or {
  38. if integer, ok := v.(json.Number); ok {
  39. if sp.QueryBody.Where.Or[k], err = integer.Int64(); err != nil {
  40. log.Error("sp.QueryBody.Where.EQ.numberToInt64(%v)(%v)", integer, err)
  41. }
  42. }
  43. }
  44. // in
  45. for k, v := range sp.QueryBody.Where.In {
  46. vs := make([]interface{}, 0)
  47. for _, v2 := range v {
  48. if integer, ok := v2.(json.Number); ok {
  49. vs = append(vs, integer)
  50. } else {
  51. vs = append(vs, v2)
  52. }
  53. }
  54. sp.QueryBody.Where.In[k] = vs
  55. }
  56. }
  57. if sp.QueryBody.From == "" {
  58. err = errors.New("query from is empty")
  59. return
  60. }
  61. // deny *
  62. if b, ok := model.PermConf["star"][sp.Business]; !(ok && b == "true") {
  63. if strings.Contains(sp.QueryBody.From, "*") {
  64. err = errors.New("query from * is denied")
  65. return
  66. }
  67. }
  68. if err = svr.CheckQueryConf(c, sp); err != nil {
  69. log.Error("svr.QueryConf(%+v) error(%v)", sp, err)
  70. return
  71. }
  72. if sp.QueryBody.Pn == 0 {
  73. sp.QueryBody.Pn = 1
  74. }
  75. if sp.QueryBody.Ps == 0 {
  76. sp.QueryBody.Ps = 10
  77. }
  78. return
  79. }
  80. func querySearch(c *bm.Context) {
  81. var (
  82. err error
  83. sp *model.QueryParams
  84. res *model.QueryResult
  85. )
  86. res = &model.QueryResult{}
  87. if sp, err = checkParams(c); err != nil {
  88. log.Error("checkParams(%v) error(%v)", sp, err)
  89. c.JSON(res, err)
  90. return
  91. }
  92. //query mode
  93. switch sp.AppIDConf.QueryMode {
  94. case model.QueryModeBasic: //完全通用查询,支持nested方式
  95. res, _, err = svr.QueryBasic(c, sp)
  96. case model.QueryModeExtra: //半自定义,在查询体基础上加额外条件
  97. res, _, err = svr.QueryExtra(c, sp)
  98. case 4: //完整自定义查询
  99. // todo
  100. default:
  101. res, _, err = svr.QueryBasic(c, sp)
  102. }
  103. if err != nil {
  104. log.Error("srv.QueryBasic(%v) error(%v)", sp, err)
  105. c.JSON(nil, err)
  106. return
  107. }
  108. c.JSON(res, err)
  109. }
  110. // queryDebug 1. queryBody is right or not 2. return dsl body 3. return dsl profile
  111. func queryDebug(c *bm.Context) {
  112. var (
  113. err error
  114. sp *model.QueryParams
  115. resDebug = &model.QueryDebugResult{}
  116. )
  117. if sp, err = checkParams(c); err != nil {
  118. log.Error("checkParams(%v) error(%v)", c, err)
  119. resDebug.AddErrMsg(err.Error())
  120. resDebug.QueryBody = sp.QueryBodyStr
  121. c.JSON(resDebug, ecode.RequestErr)
  122. return
  123. }
  124. // 默认高级debug
  125. if sp.DebugLevel == 0 {
  126. sp.DebugLevel = 2
  127. }
  128. switch sp.AppIDConf.QueryMode {
  129. case 0, model.QueryModeBasic: //完全通用查询
  130. if _, resDebug, err = svr.QueryBasic(c, sp); err != nil {
  131. log.Error("QueryDebug(%v) error(%v)", sp, err)
  132. resDebug.AddErrMsg(err.Error())
  133. c.JSON(resDebug, ecode.RequestErr)
  134. return
  135. }
  136. case model.QueryModeExtra: //半自定义,根据查询体基础再做额外处理
  137. if _, resDebug, err = svr.QueryExtra(c, sp); err != nil {
  138. log.Error("QueryDebug(%v) error(%v)", sp, err)
  139. resDebug.AddErrMsg(err.Error())
  140. c.JSON(resDebug, ecode.RequestErr)
  141. return
  142. }
  143. case 3: //完全自定义
  144. //todo
  145. default:
  146. }
  147. resDebug.QueryBody = sp.QueryBodyStr
  148. c.JSON(resDebug, err)
  149. }