search.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package search
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/tv/model"
  6. searchMdl "go-common/app/interface/main/tv/model/search"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _searchAll = "all_tv"
  13. _searchPGC = "tv_pgc"
  14. _searchUGC = "tv_ugc"
  15. _typePGC = "pgc"
  16. )
  17. // SearchSug returns the result of search sug
  18. func (s *Service) SearchSug(ctx context.Context, req *searchMdl.ReqSug) (result searchMdl.SugResponse, err error) {
  19. if result, err = s.dao.SearchSug(ctx, req); err != nil {
  20. return
  21. }
  22. build, _ := strconv.Atoi(req.Build)
  23. if build != 0 && build <= s.conf.Search.SugPGCBuild && len(result.Result.Tag) > 0 {
  24. var filtered = []*searchMdl.STag{}
  25. for _, v := range result.Result.Tag {
  26. if v.Type == _typePGC {
  27. filtered = append(filtered, v)
  28. }
  29. }
  30. result.Result.Tag = filtered
  31. }
  32. return
  33. }
  34. func (s *Service) batchToCommonPgc(ctx context.Context, input []*searchMdl.PgcResult) (output []*searchMdl.CommonResult) {
  35. var (
  36. err error
  37. cids []int64
  38. cmsRes map[int64]*model.SeasonCMS
  39. )
  40. for _, v := range input {
  41. output = append(output, v.ToCommon())
  42. cids = append(cids, int64(v.ID))
  43. }
  44. if cmsRes, err = s.cmsDao.LoadSnsCMSMap(ctx, cids); err != nil {
  45. log.Error("[search.cornerMark] cids(%s) error(%v)", xstr.JoinInts(cids), err)
  46. return
  47. }
  48. for idx, v := range output {
  49. if r, ok := cmsRes[int64(v.ID)]; ok && r.NeedVip() {
  50. output[idx].CornerMark = &(*s.conf.Cfg.SnVipCorner)
  51. }
  52. }
  53. return
  54. }
  55. func batchToCommonUgc(input []*searchMdl.UgcResult) (output []*searchMdl.CommonResult) {
  56. for _, v := range input {
  57. output = append(output, v.ToCommon())
  58. }
  59. return
  60. }
  61. // SearchRes distinguishes the search type and pick the result
  62. func (s *Service) SearchRes(ctx context.Context, req *searchMdl.ReqSearch) (data *searchMdl.RespForClient, err error) {
  63. var resCommon *searchMdl.ResultResponse
  64. data = &searchMdl.RespForClient{
  65. SearchType: req.SearchType,
  66. }
  67. switch req.SearchType {
  68. case _searchAll:
  69. var resAll searchMdl.RespAll
  70. if resAll, resCommon, err = s.dao.SearchAll(ctx, req); err != nil {
  71. return
  72. }
  73. if resAll.PageInfo != nil {
  74. data.PageInfo = resAll.PageInfo
  75. }
  76. if resAll.Result != nil {
  77. data.ResultAll = &searchMdl.AllForClient{
  78. Pgc: s.batchToCommonPgc(ctx, resAll.Result.Pgc),
  79. Ugc: batchToCommonUgc(resAll.Result.Ugc),
  80. }
  81. }
  82. case _searchPGC:
  83. var resPgc searchMdl.RespPgc
  84. if resPgc, resCommon, err = s.dao.SearchPgc(ctx, req); err != nil {
  85. return
  86. }
  87. data.PGC = s.batchToCommonPgc(ctx, resPgc.Result)
  88. case _searchUGC:
  89. if req.Category == 0 { // in case of ugc, must have category
  90. err = ecode.RequestErr
  91. return
  92. }
  93. var resUgc searchMdl.RespUgc
  94. if resUgc, resCommon, err = s.dao.SearchUgc(ctx, req); err != nil {
  95. return
  96. }
  97. data.UGC = batchToCommonUgc(resUgc.Result)
  98. default:
  99. data = nil
  100. err = ecode.TvDangbeiWrongType
  101. return
  102. }
  103. data.ResultResponse = resCommon
  104. return
  105. }