video.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package video
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type Video struct {
  10. VideoId int `orm:"column(video_id);pk" description:"视频id"`
  11. Title string `description:"视频标题"`
  12. VideoUrl string `description:"视频地址"`
  13. VideoCoverUrl string `description:"视频封面地址"`
  14. Status int `description:"视频状态:0:未发布,1:已发布"`
  15. PublishTime time.Time `description:"视频发布时间"`
  16. PlaySeconds uint32 `description:"视频播放时长"`
  17. CreateTime time.Time `description:"视频创建时间"`
  18. ModifyTime time.Time `description:"视频修改时间"`
  19. }
  20. //视频保存请求参数
  21. type VideoSaveReq struct {
  22. Title string `description:"视频标题"`
  23. VideoUrl string `description:"视频地址"`
  24. VideoCoverUrl string `description:"视频封面地址"`
  25. TagsIdStr string `description:"标签id,多个用英文逗号隔开"`
  26. PlaySeconds uint32 `description:"视频播放时长"`
  27. }
  28. //视频保存返回参数
  29. type VideoSaveResp struct {
  30. VideoId int `description:"视频id"`
  31. }
  32. //新增视频
  33. func AddVideo(item *Video) (newId int64, err error) {
  34. o := orm.NewOrmUsingDB("rddp")
  35. newId, err = o.Insert(item)
  36. return
  37. }
  38. //申请转正请求参数
  39. type VideoDeleteReq struct {
  40. VideoId int `description:"视频id"`
  41. }
  42. func DeleteVideo(videoId int) (err error) {
  43. o := orm.NewOrmUsingDB("rddp")
  44. to, err := o.Begin()
  45. if err != nil {
  46. return
  47. }
  48. defer func() {
  49. if err != nil {
  50. _ = to.Rollback()
  51. } else {
  52. _ = to.Commit()
  53. }
  54. }()
  55. sql := `DELETE FROM video WHERE video_id=? `
  56. _, err = to.Raw(sql, videoId).Exec()
  57. if err != nil {
  58. return
  59. }
  60. tagSql := ` DELETE FROM video_tags WHERE video_id=? `
  61. _, err = to.Raw(tagSql, videoId).Exec()
  62. if err != nil {
  63. return
  64. }
  65. return
  66. }
  67. func GetVideoIdByKeyWord(keyWord string) (video_id string, err error) {
  68. o := orm.NewOrmUsingDB("rddp")
  69. sql := ` SELECT GROUP_CONCAT(DISTINCT a.video_id) AS video_id FROM video_tags AS a
  70. INNER JOIN tags AS b ON a.tags_id=b.tags_id
  71. WHERE b.tags_name LIKE '%` + keyWord + `%'`
  72. err = o.Raw(sql).QueryRow(&video_id)
  73. return
  74. }
  75. func GetVideoListCount(condition string, pars []interface{}) (count int, err error) {
  76. o := orm.NewOrmUsingDB("rddp")
  77. sql := `SELECT COUNT(1) AS count
  78. FROM video AS a
  79. WHERE 1=1 `
  80. if condition != "" {
  81. sql += condition
  82. }
  83. err = o.Raw(sql, pars).QueryRow(&count)
  84. return
  85. }
  86. type VideoList struct {
  87. VideoId int `description:"视频id"`
  88. Title string `description:"视频标题"`
  89. VideoUrl string `description:"视频地址"`
  90. VideoCoverUrl string `description:"视频封面地址"`
  91. Status int `description:"视频状态:0:未发布,1:已发布"`
  92. PublishTime time.Time `description:"视频发布时间"`
  93. PlaySeconds uint32 `description:"视频播放时长"`
  94. CreateTime time.Time `description:"视频创建时间"`
  95. ModifyTime time.Time `description:"视频修改时间"`
  96. TagsName string `description:"标签名称"`
  97. TagsId string `description:"标签ID"`
  98. }
  99. func GetVideoList(condition string, pars []interface{}, startSize, pageSize int) (items []*VideoList, err error) {
  100. o := orm.NewOrmUsingDB("rddp")
  101. sql := `SELECT a.*,GROUP_CONCAT(c.tags_name SEPARATOR ',') AS tags_name
  102. ,GROUP_CONCAT(c.tags_id SEPARATOR ',') AS tags_id
  103. FROM video AS a
  104. INNER JOIN video_tags AS b ON a.video_id=b.video_id
  105. INNER JOIN tags AS c ON b.tags_id=c.tags_id `
  106. if condition != "" {
  107. sql += condition
  108. }
  109. sql += ` GROUP BY a.video_id ORDER BY a.modify_time DESC LIMIT ?,? `
  110. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  111. return
  112. }
  113. type VideoListResp struct {
  114. Paging *paging.PagingItem
  115. List []*VideoList
  116. }
  117. //申请转正请求参数
  118. type VideoPublishReq struct {
  119. VideoIds string `description:"视频id,多个用英文逗号隔开"`
  120. }
  121. func PublishVideo(videoIds string) (err error) {
  122. o := orm.NewOrmUsingDB("rddp")
  123. sql := `UPDATE video SET status=1,publish_time=NOW(),modify_time=NOW() WHERE video_id IN(` + videoIds + `)`
  124. _, err = o.Raw(sql).Exec()
  125. return
  126. }
  127. //视频保存请求参数
  128. type VideoEditReq struct {
  129. VideoId int `description:"视频id"`
  130. Title string `description:"视频标题"`
  131. VideoUrl string `description:"视频地址"`
  132. VideoCoverUrl string `description:"视频封面地址"`
  133. TagsIdStr string `description:"标签id,多个用英文逗号隔开"`
  134. PlaySeconds uint32 `description:"视频播放时长"`
  135. }
  136. //新增视频
  137. func EditVideo(item *Video, tagsIds string) (err error) {
  138. o := orm.NewOrmUsingDB("rddp")
  139. to, err := o.Begin()
  140. if err != nil {
  141. return
  142. }
  143. defer func() {
  144. if err != nil {
  145. _ = to.Rollback()
  146. } else {
  147. _ = to.Commit()
  148. }
  149. }()
  150. sql := ` UPDATE video SET title = ?, video_url = ?,video_cover_url = ?, modify_time = NOW(), play_seconds = ? WHERE video_id = ? `
  151. _, err = to.Raw(sql, item.Title, item.VideoUrl, item.VideoCoverUrl, item.PlaySeconds, item.VideoId).Exec()
  152. if err != nil {
  153. return
  154. }
  155. sql = ` DELETE FROM video_tags WHERE video_id = ? `
  156. _, err = to.Raw(sql, item.VideoId).Exec()
  157. if err != nil {
  158. return
  159. }
  160. if tagsIds != "" {
  161. tagsArr := strings.Split(tagsIds, ",")
  162. for _, v := range tagsArr {
  163. tagId, _ := strconv.Atoi(v)
  164. tagItem := new(VideoTags)
  165. tagItem.VideoId = item.VideoId
  166. tagItem.TagsId = tagId
  167. err = AddVideoTags(tagItem)
  168. if err != nil {
  169. return
  170. }
  171. }
  172. }
  173. return
  174. }
  175. func PublishVideoCancel(videoIds string) (err error) {
  176. o := orm.NewOrmUsingDB("rddp")
  177. sql := `UPDATE video SET status=0,publish_time=null,modify_time=NOW() WHERE video_id IN(` + videoIds + `)`
  178. _, err = o.Raw(sql).Exec()
  179. return
  180. }
  181. func GetVideoByTitle(title string) (count int, err error) {
  182. o := orm.NewOrmUsingDB("rddp")
  183. sql := ` SELECT COUNT(1) AS count FROM video WHERE title =? `
  184. err = o.Raw(sql, title).QueryRow(&count)
  185. return
  186. }