community_video_play_log.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package yb
  2. import "github.com/beego/beego/v2/client/orm"
  3. type CommunityVideoPlayLog struct {
  4. Id int `gorm:"primaryKey;column:id;type:int(10) unsigned;not null" `
  5. CommunityVideoID int `gorm:"column:community_video_id;type:int(10) unsigned;not null"` //视频ID
  6. UserID int `gorm:"index:idx_user_id;column:user_id;type:int(10) unsigned;not null;default:0" ` // 点击视频的用户ID
  7. SourceAgent int `gorm:"column:source_agent;type:tinyint(4);default:1" ` // 操作来源,1:小程序,2:小程序 pc 3:弘则研究公众号,4:web pc
  8. CreateTime string `gorm:"column:create_time;type:datetime;default:CURRENT_TIMESTAMP"` // 创建日志时间
  9. Type string `description:"统计类型(1 视频社区,2 路演视频)"`
  10. }
  11. // TableName get sql table name.获取数据库表名
  12. func (l *CommunityVideoPlayLog) TableName() string {
  13. return "yb_community_video_play_log"
  14. }
  15. type VideoPlayLogItem struct {
  16. UserId int
  17. NewSourceAgent int
  18. CreateTime string
  19. StopSeconds int
  20. //LastCreateTime string
  21. //ClickNum int `description:"点击量"`
  22. }
  23. // GetVideoPlayLogListByVideoId 查询视频点击量列表
  24. func GetVideoPlayLogListByVideoId(videoId, videoType, startSize, pageSize int) (list []*VideoPlayLogItem, err error) {
  25. o := orm.NewOrm()
  26. sql := `SELECT
  27. user_id,
  28. IF(source_agent=4,2,source_agent) as new_source_agent,
  29. count(*) as click_num,
  30. MAX( create_time ) as last_create_time
  31. FROM
  32. yb_community_video_play_log
  33. WHERE community_video_id=? and type=?
  34. GROUP BY
  35. user_id,
  36. new_source_agent
  37. ORDER BY MAX( create_time ) DESC, id DESC
  38. LIMIT ?,?`
  39. _, err = o.Raw(sql, videoId, videoType, startSize, pageSize).QueryRows(&list)
  40. return
  41. }
  42. // GetVideoPlayListTotalByVideoId 查询视频点击量列表总数
  43. func GetVideoPlayListTotalByVideoId(videoId, videoType int) (total int64, err error) {
  44. o := orm.NewOrm()
  45. sql := `select count(*) from (SELECT
  46. user_id,
  47. IF(source_agent=4,2,source_agent) as new_source_agent
  48. FROM
  49. yb_community_video_play_log
  50. WHERE community_video_id=? and type=?
  51. GROUP BY
  52. user_id,
  53. new_source_agent) as b
  54. `
  55. err = o.Raw(sql, videoId, videoType).QueryRow(&total)
  56. return
  57. }
  58. // GetVideoPlayTotalByVideoId 查询
  59. func GetVideoPlayTotalByVideoId(videoId, videoType int) (total int64, err error) {
  60. o := orm.NewOrm()
  61. sql := `select count(*)
  62. FROM
  63. yb_community_video_play_log
  64. WHERE community_video_id=? and type=?
  65. `
  66. err = o.Raw(sql, videoId, videoType).QueryRow(&total)
  67. return
  68. }
  69. // GetVideoPlayLogListByVideoIdV2 查询视频点击量列表
  70. func GetVideoPlayLogListByVideoIdV2(videoId, videoType, startSize, pageSize int, orderRule string) (list []*VideoPlayLogItem, err error) {
  71. o := orm.NewOrm()
  72. sql := `SELECT
  73. user_id,
  74. IF(source_agent=4,2,source_agent) as new_source_agent,
  75. create_time,
  76. stop_seconds
  77. FROM
  78. yb_community_video_play_log
  79. WHERE
  80. community_video_id = ? and type = ? `
  81. order := ` ORDER BY stop_seconds DESC, create_time DESC`
  82. if orderRule != `` {
  83. order = orderRule
  84. }
  85. sql += order
  86. sql += ` LIMIT ?,?`
  87. _, err = o.Raw(sql, videoId, videoType, startSize, pageSize).QueryRows(&list)
  88. return
  89. }
  90. // GetVideoPlayTotalByVideoId 查询
  91. func GetVideoPlayTotalByVideoIdV2(videoId, videoType int) (total int64, err error) {
  92. o := orm.NewOrm()
  93. sql := `SELECT
  94. count(*)
  95. FROM
  96. yb_community_video_play_log
  97. WHERE
  98. community_video_id = ? AND type = ?`
  99. err = o.Raw(sql, videoId, videoType).QueryRow(&total)
  100. return
  101. }
  102. // UserCommunityVideoVisitCount 用户视频点击量
  103. type UserCommunityVideoVisitCount struct {
  104. VisitCount int `json:"visit_count"`
  105. CommunityVideoId int `json:"community_video_id"`
  106. NewSource int `json:"new_source"`
  107. RecentTime string `json:"recent_time"`
  108. }
  109. // GetCommunityVideoVisitCountByUserId 视频社区点击量统计-根据用户
  110. func GetCommunityVideoVisitCountByUserId(userId, videoType, startSize, pageSize int, orderRule string) (total int, list []*UserCommunityVideoVisitCount, err error) {
  111. o := orm.NewOrm()
  112. sql := `SELECT COUNT(1) AS visit_count, community_video_id, IF(source_agent=4,2,source_agent) as new_source, MAX(create_time) AS recent_time
  113. FROM yb_community_video_play_log WHERE user_id = ? and type=?
  114. GROUP BY community_video_id, new_source`
  115. if orderRule != `` {
  116. sql += ` ORDER BY ` + orderRule
  117. } else {
  118. sql += ` ORDER BY recent_time DESC`
  119. }
  120. totalSQL := `SELECT COUNT(1) total FROM (` + sql + `) z `
  121. err = o.Raw(totalSQL, userId, videoType).QueryRow(&total)
  122. if err != nil {
  123. return
  124. }
  125. sql += ` LIMIT ?,?`
  126. _, err = o.Raw(sql, userId, videoType, startSize, pageSize).QueryRows(&list)
  127. return
  128. }