video.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package dao
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/job/bbq/recall/internal/model"
  9. "go-common/app/job/bbq/recall/proto"
  10. "go-common/app/job/bbq/recall/proto/quality"
  11. "go-common/library/log"
  12. "github.com/golang/snappy"
  13. )
  14. const (
  15. // _fetchVideo = "select `id`, `title`, `content`, `mid`, `avid`, `cid`, `pubtime`, `ctime`, `mtime`, `duration`, `state`, `tid`, `sub_tid` from video where pubtime > ? limit ?, ?;"
  16. _fetchVideo = "select `svid`, `title`, `content`, `mid`, `avid`, `cid`, `pubtime`, `ctime`, `mtime`, `duration`, `state`, `tid`, `sub_tid` from video limit ?, ?;"
  17. _fetchVideoTag = "select `id`, `name`, `type` from `tag` where `id` = ? and `status` = 1;"
  18. _fetchVideoTagAll = "select `id`, `name`, `type` from `tag` where `status` = 1;"
  19. _fetchVideoTextTag = "select `tag` from `video_repository` where `svid` = ? limit 1;"
  20. _queryVideoQuality = "select `stat_info` from `video_forward_index_stat_info` where `svid` = ?;"
  21. _fetchNewIncomeVideo = "select `svid` from `video` where ctime > ? and state in (%s);"
  22. )
  23. // FetchVideoInfo .
  24. func (d *Dao) FetchVideoInfo(c context.Context, offset, size int) (result []*model.Video, err error) {
  25. // rows, err := d.db.Query(c, _fetchVideo, ptime.Format("2006-01-02 15:04:05"), offset, size)
  26. rows, err := d.db.Query(c, _fetchVideo, offset, size)
  27. if err != nil {
  28. return nil, err
  29. }
  30. for rows.Next() {
  31. tmp := &model.Video{}
  32. if err = rows.Scan(&tmp.SVID, &tmp.Title, &tmp.Content, &tmp.MID, &tmp.AVID, &tmp.CID, &tmp.PubTime, &tmp.CTime, &tmp.MTime, &tmp.Duration, &tmp.State, &tmp.TID, &tmp.SubTID); err != nil {
  33. log.Error("FetchVideoInfo: %v", err)
  34. return nil, err
  35. }
  36. result = append(result, tmp)
  37. }
  38. return result, nil
  39. }
  40. // FetchVideoTagAll .
  41. func (d *Dao) FetchVideoTagAll(c context.Context) (result []*proto.Tag, err error) {
  42. result = make([]*proto.Tag, 0)
  43. rows, err := d.db.Query(c, _fetchVideoTagAll)
  44. if err != nil {
  45. return
  46. }
  47. for rows.Next() {
  48. tmp := new(proto.Tag)
  49. if err = rows.Scan(&tmp.TagID, &tmp.TagName, &tmp.TagType); err != nil {
  50. log.Error("FetchVideoTag: %v", err)
  51. continue
  52. }
  53. result = append(result, tmp)
  54. }
  55. return
  56. }
  57. // FetchVideoTag .
  58. func (d *Dao) FetchVideoTag(c context.Context, tid int32) (result *proto.Tag, err error) {
  59. row := d.db.QueryRow(c, _fetchVideoTag, tid)
  60. result = new(proto.Tag)
  61. if err = row.Scan(&result.TagID, &result.TagName, &result.TagType); err != nil {
  62. log.Error("FetchVideoTag: %v", err)
  63. return
  64. }
  65. return
  66. }
  67. // FetchVideoTextTag .
  68. func (d *Dao) FetchVideoTextTag(c context.Context, svid int64) (result []string, err error) {
  69. row := d.dbCms.QueryRow(c, _fetchVideoTextTag, svid)
  70. var tags string
  71. if err = row.Scan(&tags); err != nil {
  72. log.Errorv(c, log.KV("log", "_fetchVideoTextTag failed"), log.KV("error", err), log.KV("svid", svid))
  73. return
  74. }
  75. result = strings.Split(tags, ",")
  76. return
  77. }
  78. // FetchVideoQuality .
  79. func (d *Dao) FetchVideoQuality(c context.Context, svid uint64) (result *quality.VideoQuality, err error) {
  80. var raw string
  81. row := d.dbOffline.QueryRow(c, _queryVideoQuality, svid)
  82. row.Scan(&raw)
  83. if raw == "" {
  84. return
  85. }
  86. trimed := strings.Trim(raw, "\n")
  87. hexDst, err := hex.DecodeString(trimed)
  88. if err != nil {
  89. log.Error("FetchVideoQuality: %v src[%s] raw[%s]", err, trimed, trimed)
  90. return
  91. }
  92. snappyDst, err := snappy.Decode(nil, hexDst)
  93. if err != nil {
  94. log.Error("FetchVideoQuality: %v src[%s] raw[%s]", err, string(hexDst), trimed)
  95. return
  96. }
  97. result = &quality.VideoQuality{}
  98. result.Unmarshal(snappyDst)
  99. if err != nil {
  100. log.Error("FetchVideoQuality: %v src[%s] raw[%s]", err, snappyDst, trimed)
  101. }
  102. return
  103. }
  104. // FetchNewincomeVideo .
  105. func (d *Dao) FetchNewincomeVideo() (res []int64, err error) {
  106. duration, _ := time.ParseDuration("-24h")
  107. today := time.Now().Add(duration).Format("2006-01-02")
  108. _query := fmt.Sprintf(_fetchNewIncomeVideo, strings.Join(model.RecommendVideoState, ","))
  109. row, err := d.db.Query(context.Background(), _query, today)
  110. if err != nil {
  111. return
  112. }
  113. res = make([]int64, 0)
  114. for row.Next() {
  115. var tmp int64
  116. if err = row.Scan(&tmp); err != nil {
  117. return
  118. }
  119. res = append(res, tmp)
  120. }
  121. return
  122. }