search.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/interface/bbq/app-bbq/api/http/v1"
  6. "go-common/app/interface/bbq/app-bbq/model"
  7. "go-common/app/service/bbq/common"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "github.com/json-iterator/go"
  11. )
  12. const (
  13. searchTypeVideo = "bbq_video"
  14. searchTypeUser = "bbq_user"
  15. suggestType = "bbq"
  16. suggestVer = "v4"
  17. searchLen = 20
  18. )
  19. // SearchCursor .
  20. type SearchCursor struct {
  21. Offset int64 `json:"offset"` // 默认为0,数据从1开始
  22. }
  23. func parseSearchCursor(c context.Context, cursorPrev, cursorNext string) (cursor *SearchCursor, directionNext bool, err error) {
  24. cursor = new(SearchCursor)
  25. // 判断是向前还是向后查询
  26. directionNext = true
  27. cursorStr := cursorNext
  28. if len(cursorNext) == 0 && len(cursorPrev) > 0 {
  29. directionNext = false
  30. cursorStr = cursorPrev
  31. }
  32. // 解析
  33. if len(cursorStr) != 0 {
  34. var cursorData = []byte(cursorStr)
  35. err = json.Unmarshal(cursorData, &cursor)
  36. if err != nil {
  37. err = ecode.ReqParamErr
  38. return
  39. }
  40. }
  41. return
  42. }
  43. //HotWord .
  44. func (s *Service) HotWord(c context.Context, req *v1.HotWordRequest) (res *v1.HotWordResponse, err error) {
  45. res = new(v1.HotWordResponse)
  46. res.List = append(res.List, "小姐姐")
  47. res.List = append(res.List, "舞蹈")
  48. res.List = append(res.List, "MMD")
  49. return
  50. }
  51. // VideoSearch 视频搜索
  52. func (s *Service) VideoSearch(c context.Context, arg *v1.BaseSearchReq) (res *v1.VideoSearchRes, err error) {
  53. res = new(v1.VideoSearchRes)
  54. var (
  55. sres []*model.VideoSearchResult
  56. searchRes *model.RawSearchRes
  57. relids []int32
  58. svids []int64
  59. svidMap map[int64]int64
  60. )
  61. // 0. check
  62. // 1. 生成请求,会根据请求参数
  63. // v2接口
  64. reqPage := int64(1)
  65. firstItemOffset := int64(0)
  66. var cursor *SearchCursor
  67. var directionNext bool
  68. if arg.Page == 0 {
  69. cursor, directionNext, err = parseSearchCursor(c, arg.CursorPrev, arg.CursorNext)
  70. if err != nil {
  71. log.Warnw(c, "log", "parse search cursor fail", "prev", arg.CursorPrev, "next", arg.CursorNext)
  72. return
  73. }
  74. firstItemOffset = cursor.Offset - 1
  75. if directionNext {
  76. firstItemOffset = cursor.Offset + 1
  77. }
  78. reqPage = (firstItemOffset-1)/searchLen + 1
  79. log.V(1).Infow(c, "log", "v2 request", "next", arg.CursorNext, "prev", arg.CursorPrev)
  80. } else {
  81. reqPage = arg.Page
  82. }
  83. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  84. sp := &model.SearchBaseReq{
  85. KeyWord: arg.Key,
  86. Type: searchTypeVideo,
  87. Page: reqPage,
  88. PageSize: searchLen,
  89. Highlight: int64(arg.Highlight),
  90. }
  91. // 相信搜索结果返回的就是searchLen
  92. searchRes, err = s.dao.SearchBBQ(c, sp)
  93. if searchRes.Res == nil {
  94. return
  95. }
  96. if err = json.Unmarshal(searchRes.Res, &sres); err != nil {
  97. return
  98. }
  99. for _, v := range sres {
  100. relids = append(relids, v.ID)
  101. }
  102. if len(relids) == 0 {
  103. log.Infov(c, log.KV("log", "search ret empty"))
  104. return
  105. }
  106. ids := s.dao.ParseRel2ID(relids)
  107. svidMap, err = s.dao.ConvID2SVID(c, ids)
  108. if err != nil {
  109. log.Error("ConvID2SVID err [%v]", err)
  110. err = nil
  111. return
  112. }
  113. for _, svid := range svidMap {
  114. svids = append(svids, svid)
  115. }
  116. svMap, _ := s.svInfos(c, svids, 0, false)
  117. for index, sinfo := range sres {
  118. id := s.dao.ParseRel2ID([]int32{sinfo.ID})
  119. if id[0] == 0 {
  120. continue
  121. }
  122. if svid, ok := svidMap[id[0]]; ok {
  123. if sv, ok := svMap[svid]; ok {
  124. if common.IsSearchSvStateAvailable(int64(sv.State)) {
  125. r := new(v1.VideoSearchList)
  126. r.VideoResponse = *sv
  127. r.SVID = svid
  128. r.HitColumns = sinfo.HitColumns
  129. r.TitleHighlight = sinfo.Title
  130. r.Offset = (reqPage-1)*int64(searchLen) + int64(index) + 1
  131. res.List = append(res.List, r)
  132. } else {
  133. log.Warnw(c, "log", "get error svid in search list", "svid", svid, "sv", sv)
  134. }
  135. }
  136. }
  137. }
  138. res.NumPage = searchRes.PageNum
  139. res.Page = searchRes.Page
  140. res.HasMore = false
  141. if arg.Page == 0 {
  142. if (directionNext && searchRes.PageNum > searchRes.Page) || (!directionNext && searchRes.Page > 1) {
  143. res.HasMore = true
  144. }
  145. var list []*v1.VideoSearchList
  146. if directionNext {
  147. for _, item := range res.List {
  148. if item.Offset < firstItemOffset {
  149. continue
  150. }
  151. list = append(list, item)
  152. }
  153. } else {
  154. if firstItemOffset >= int64(len(res.List)) {
  155. firstItemOffset = int64(len(res.List)) - 1
  156. }
  157. for i := firstItemOffset - 1; i >= 0; i-- {
  158. list = append(list, res.List[i])
  159. }
  160. }
  161. res.List = list
  162. }
  163. var cursorValue SearchCursor
  164. for _, item := range res.List {
  165. cursorValue.Offset = item.Offset
  166. data, _ := json.Marshal(cursorValue)
  167. item.CursorValue = string(data)
  168. }
  169. return
  170. }
  171. // UserSearch 用户搜索
  172. func (s *Service) UserSearch(c context.Context, mid int64, arg *v1.BaseSearchReq) (res *v1.UserSearchRes, err error) {
  173. res = new(v1.UserSearchRes)
  174. var (
  175. sres []*model.UserSearchResult
  176. searchRes *model.RawSearchRes
  177. mids []int64
  178. )
  179. // 1. 生成请求,会根据请求参数
  180. // v2接口
  181. reqPage := int64(1)
  182. firstItemOffset := int64(0)
  183. var cursor *SearchCursor
  184. var directionNext bool
  185. if arg.Page == 0 {
  186. cursor, directionNext, err = parseSearchCursor(c, arg.CursorPrev, arg.CursorNext)
  187. if err != nil {
  188. log.Warnw(c, "log", "parse search cursor fail", "prev", arg.CursorPrev, "next", arg.CursorNext)
  189. return
  190. }
  191. firstItemOffset = cursor.Offset - 1
  192. if directionNext {
  193. firstItemOffset = cursor.Offset + 1
  194. }
  195. reqPage = (firstItemOffset-1)/searchLen + 1
  196. log.V(1).Infow(c, "log", "v2 request", "next", arg.CursorNext, "prev", arg.CursorPrev)
  197. } else {
  198. reqPage = arg.Page
  199. }
  200. sp := &model.SearchBaseReq{
  201. KeyWord: arg.Key,
  202. Type: searchTypeUser,
  203. Page: reqPage,
  204. PageSize: searchLen,
  205. Highlight: int64(arg.Highlight),
  206. }
  207. searchRes, err = s.dao.SearchBBQ(c, sp)
  208. if searchRes.Res == nil {
  209. return
  210. }
  211. if err = json.Unmarshal(searchRes.Res, &sres); err != nil {
  212. return
  213. }
  214. for _, v := range sres {
  215. mids = append(mids, v.ID)
  216. }
  217. if len(mids) == 0 {
  218. log.Infov(c, log.KV("log", "search ret empty"))
  219. return
  220. }
  221. uMap, _ := s.dao.BatchUserInfo(c, mid, mids, false, true, true)
  222. for index, sinfo := range sres {
  223. if u, ok := uMap[sinfo.ID]; ok {
  224. r := &v1.UserSearchList{
  225. UserInfo: *u,
  226. }
  227. st := new(v1.UserStatic)
  228. st.Fan = u.Fan
  229. st.Follow = u.Follow
  230. st.Like = u.Like
  231. st.Liked = u.Liked
  232. st.FollowState = u.FollowState
  233. r.UserStatic = st
  234. r.HitColumns = sinfo.HitColumns
  235. r.UnameHighlight = sinfo.Uname
  236. r.Offset = (reqPage-1)*int64(searchLen) + int64(index) + 1
  237. res.List = append(res.List, r)
  238. }
  239. }
  240. res.NumPage = searchRes.PageNum
  241. res.Page = searchRes.Page
  242. res.HasMore = false
  243. if arg.Page == 0 {
  244. if (directionNext && searchRes.PageNum > searchRes.Page) || (!directionNext && searchRes.Page > 1) {
  245. res.HasMore = true
  246. }
  247. var list []*v1.UserSearchList
  248. if directionNext {
  249. for _, item := range res.List {
  250. if item.Offset < firstItemOffset {
  251. continue
  252. }
  253. list = append(list, item)
  254. }
  255. } else {
  256. if firstItemOffset >= int64(len(res.List)) {
  257. firstItemOffset = int64(len(res.List)) - 1
  258. }
  259. for i := firstItemOffset - 1; i >= 0; i-- {
  260. list = append(list, res.List[i])
  261. }
  262. }
  263. res.List = list
  264. }
  265. var cursorValue SearchCursor
  266. for _, item := range res.List {
  267. cursorValue.Offset = item.Offset
  268. data, _ := json.Marshal(cursorValue)
  269. item.CursorValue = string(data)
  270. }
  271. return
  272. }
  273. // BBQSug BBQSUG服务
  274. func (s *Service) BBQSug(c context.Context, arg *v1.SugReq) (res []*v1.SugTag, err error) {
  275. var (
  276. sres []*model.RawSugTag
  277. data json.RawMessage
  278. )
  279. sp := &model.SugBaseReq{
  280. Term: arg.KeyWord,
  281. SuggestType: suggestType,
  282. MainVer: suggestVer,
  283. SugNum: arg.PageSize,
  284. Highlight: int64(arg.Highlight),
  285. }
  286. data, err = s.dao.SugBBQ(c, sp)
  287. if err != nil {
  288. log.Error("s.dao.SugBBQ err[%v] data [%s]", err, string(data))
  289. return
  290. }
  291. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  292. if err = json.Unmarshal(data, &sres); err != nil {
  293. log.Error("json.Unmarshal err[%v] data [%s]", err, string(data))
  294. return
  295. }
  296. for _, sinfo := range sres {
  297. r := new(v1.SugTag)
  298. name := sinfo.Name
  299. r.Name = name
  300. r.Value = sinfo.Value
  301. r.Type = sinfo.Type
  302. r.Ref = sinfo.Ref
  303. res = append(res, r)
  304. }
  305. return
  306. }