search.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/interface/bbq/app-bbq/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "time"
  15. jsoniter "github.com/json-iterator/go"
  16. )
  17. const (
  18. httpProto = "http://"
  19. searchBaseURI = "/bbq/search"
  20. sugBaseURI = "/main/suggest"
  21. getSVIDbyRelID = "select id,svid from video where id in (%s)"
  22. )
  23. // SearchBBQ 搜索视频
  24. func (d *Dao) SearchBBQ(c context.Context, sreq *model.SearchBaseReq) (ret *model.RawSearchRes, err error) {
  25. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  26. ret = new(model.RawSearchRes)
  27. path := httpProto + d.c.Search.Host + searchBaseURI
  28. params := url.Values{}
  29. d.preSetSearchParam(c, &params)
  30. params.Set("keyword", sreq.KeyWord)
  31. params.Set("page", strconv.FormatInt(sreq.Page, 10))
  32. params.Set("pagesize", strconv.FormatInt(sreq.PageSize, 10))
  33. params.Set("highlight", strconv.FormatInt(sreq.Highlight, 10))
  34. params.Set("search_type", sreq.Type)
  35. log.Infov(c, log.KV("log", fmt.Sprintf("search url(%s)", path+"?"+params.Encode())))
  36. req, err := d.httpClient.NewRequest("GET", path, params.Get("ip"), params)
  37. if err != nil {
  38. log.Error("search url(%s) error(%v)", path+"?"+params.Encode(), err)
  39. return
  40. }
  41. if err = d.httpClient.Do(c, req, &ret); err != nil {
  42. log.Errorv(c, log.KV("log", fmt.Sprintf("search url(%s) error(%v)", path+"?"+params.Encode(), err)))
  43. return
  44. }
  45. if ret.Code != 0 {
  46. err = ecode.Int(ret.Code)
  47. log.Errorv(c, log.KV("log", fmt.Sprintf("search url(%s) error(%v)", path+"?"+params.Encode(), err)))
  48. return
  49. }
  50. _str, _ := json.Marshal(ret)
  51. log.Infov(c,
  52. log.KV("log", fmt.Sprintf("Search req[%s] ret[%s]", path+"?"+params.Encode(), _str)))
  53. return
  54. }
  55. // SugBBQ 搜索视频
  56. func (d *Dao) SugBBQ(c context.Context, sreq *model.SugBaseReq) (ret json.RawMessage, err error) {
  57. path := httpProto + d.c.Search.Host + sugBaseURI
  58. params := url.Values{}
  59. d.preSetSearchParam(c, &params)
  60. params.Set("term", sreq.Term)
  61. params.Set("suggest_type", sreq.SuggestType)
  62. params.Set("highlight", strconv.FormatInt(sreq.Highlight, 10))
  63. params.Set("main_ver", sreq.MainVer)
  64. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  65. log.Infov(c, log.KV("log", fmt.Sprintf("sug url(%s)", path+"?"+params.Encode())))
  66. req, err := d.httpClient.NewRequest("GET", path, params.Get("ip"), params)
  67. if err != nil {
  68. log.Error("sug url(%s) error(%v)", path+"?"+params.Encode(), err)
  69. return
  70. }
  71. var res struct {
  72. Code int `json:"code"`
  73. Stoken string `json:"stoken"`
  74. Res struct {
  75. Tag json.RawMessage `json:"tag"`
  76. } `json:"result"`
  77. }
  78. if err = d.httpClient.Do(c, req, &res); err != nil {
  79. log.Errorv(c, log.KV("log", fmt.Sprintf("sug url(%s) error(%v)", path+"?"+params.Encode(), err)))
  80. return
  81. }
  82. if res.Code != 0 {
  83. err = ecode.Int(res.Code)
  84. log.Errorv(c, log.KV("log", fmt.Sprintf("sug url(%s) error(%v)", path+"?"+params.Encode(), err)))
  85. return
  86. }
  87. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  88. _str, _ := json.Marshal(ret)
  89. log.Infov(c, log.KV("log", fmt.Sprintf("Sug req[%s] ret[%s]", path+"?"+params.Encode(), _str)))
  90. ret = res.Res.Tag
  91. return
  92. }
  93. func (d *Dao) preSetSearchParam(c context.Context, params *url.Values) {
  94. device := c.Value("device")
  95. if device != nil {
  96. dev := device.(*bm.Device)
  97. if dev.RawPlatform != "" {
  98. params.Set("platform", dev.RawPlatform)
  99. }
  100. if dev.RawMobiApp != "" {
  101. params.Set("mobi_app", dev.RawMobiApp)
  102. }
  103. if dev.Device != "" {
  104. params.Set("device", dev.Device)
  105. }
  106. if dev.Build > 0 {
  107. params.Set("device", strconv.FormatInt(dev.Build, 10))
  108. }
  109. }
  110. ip := metadata.String(c, metadata.RemoteIP)
  111. if ip != "" {
  112. params.Set("clientip", ip)
  113. }
  114. }
  115. // ConvID2SVID 转换搜索相对id到svid
  116. func (d *Dao) ConvID2SVID(c context.Context, ids []int64) (res map[int64]int64, err error) {
  117. var idList []string
  118. res = make(map[int64]int64)
  119. if len(ids) == 0 {
  120. return
  121. }
  122. for _, id := range ids {
  123. idList = append(idList, strconv.FormatInt(id, 10))
  124. }
  125. if len(idList) == 0 {
  126. log.Warn("empty query list relID [%v]", ids)
  127. return
  128. }
  129. idStr := strings.Join(idList, ",")
  130. sql := fmt.Sprintf(getSVIDbyRelID, idStr)
  131. rows, err := d.db.Query(c, sql)
  132. if err != nil {
  133. log.Warn("Query Err [%v]", err)
  134. return
  135. }
  136. defer rows.Close()
  137. for rows.Next() {
  138. var svid int64
  139. var id int64
  140. err = rows.Scan(&id, &svid)
  141. if err != nil {
  142. log.Warn("Scan Err [%v]", err)
  143. return
  144. }
  145. res[id] = svid
  146. }
  147. return
  148. }
  149. // ParseRel2ID 转换搜索相对id到自增id
  150. func (d *Dao) ParseRel2ID(relID []int32) (idList []int64) {
  151. for _, id := range relID {
  152. idList = append(idList, int64(id/100))
  153. }
  154. return
  155. }