video.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package service
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "encoding/csv"
  6. "fmt"
  7. "time"
  8. "go-common/app/admin/main/tv/model"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "github.com/siddontang/go-mysql/mysql"
  12. )
  13. //VideoList is used for getting PGC video from DB
  14. func (s *Service) VideoList(c *bm.Context, param *model.VideoListParam) (pager *model.VideoListPager, err error) {
  15. var (
  16. order string
  17. total int
  18. videos []*model.VideoListQuery
  19. )
  20. selectStr := []string{
  21. "ugc_video.id",
  22. "ugc_video.aid",
  23. "ugc_video.cid",
  24. "ugc_video.eptitle",
  25. "ugc_video.valid",
  26. "ugc_video.mtime",
  27. "ugc_video.index_order",
  28. "ugc_archive.title",
  29. "ugc_archive.typeid",
  30. }
  31. //只筛选出未删除 且审核过的视频
  32. w := map[string]interface{}{"deleted": 0, "result": 1}
  33. db := s.DB.Model(&model.VideoListQuery{}).Where(w)
  34. db = db.Select(selectStr).
  35. Where("ugc_archive.deleted = ?", 0).
  36. Where("ugc_archive.result = ?", 1).
  37. Joins("LEFT JOIN ugc_archive ON ugc_archive.aid = ugc_video.aid")
  38. if param.CID != "" {
  39. db = db.Where("ugc_video.aid = ?", param.CID)
  40. }
  41. if param.VID != "" {
  42. db = db.Where("`cid` = ?", param.VID)
  43. }
  44. if param.Typeid > 0 {
  45. db = db.Where("typeid = ?", param.Typeid)
  46. }
  47. if param.Pid > 0 {
  48. db = db.Where("typeid in (?)", s.arcPTids[param.Pid])
  49. }
  50. if param.Valid != "" {
  51. db = db.Where("ugc_video.valid = ?", param.Valid)
  52. }
  53. if param.Order == 2 {
  54. order = "ugc_video.mtime DESC"
  55. } else {
  56. order = "ugc_video.mtime ASC"
  57. }
  58. if err = db.Order(order).Offset((param.Pn - 1) * param.Ps).Limit(param.Ps).Find(&videos).Error; err != nil {
  59. return
  60. }
  61. if err = db.Count(&total).Error; err != nil {
  62. return
  63. }
  64. //get parent id
  65. for k, v := range videos {
  66. videos[k].PTypeID = s.GetArchivePid(v.TypeID)
  67. }
  68. pager = &model.VideoListPager{
  69. Items: videos,
  70. Page: &model.Page{
  71. Num: param.Pn,
  72. Size: param.Ps,
  73. Total: int(total),
  74. },
  75. }
  76. return
  77. }
  78. //VideoOnline is used for online PGC video
  79. func (s *Service) VideoOnline(ids []int64) (err error) {
  80. w := map[string]interface{}{"deleted": 0, "result": 1}
  81. tx := s.DB.Begin()
  82. for _, v := range ids {
  83. video := model.Video{}
  84. if errDB := tx.Model(&model.Video{}).Where(w).Where("id=?", v).First(&video).Error; errDB != nil {
  85. err = fmt.Errorf("找不到id为%v的数据", v)
  86. tx.Rollback()
  87. return
  88. }
  89. if errDB := tx.Model(&model.Video{}).Where("id=?", v).
  90. Update("valid", 1).Error; errDB != nil {
  91. err = errDB
  92. tx.Rollback()
  93. return
  94. }
  95. }
  96. tx.Commit()
  97. return
  98. }
  99. //VideoHidden is used for hidden UGC video
  100. func (s *Service) VideoHidden(ids []int64) (err error) {
  101. w := map[string]interface{}{"deleted": 0, "result": 1}
  102. tx := s.DB.Begin()
  103. for _, v := range ids {
  104. video := model.Video{}
  105. if errDB := tx.Model(&model.Video{}).Where(w).Where("id=?", v).First(&video).Error; errDB != nil {
  106. err = fmt.Errorf("找不到id为%v的数据", v)
  107. tx.Rollback()
  108. return
  109. }
  110. if errDB := tx.Model(&model.Video{}).Where("id=?", v).
  111. Update("valid", 0).Error; errDB != nil {
  112. err = errDB
  113. tx.Rollback()
  114. return
  115. }
  116. }
  117. tx.Commit()
  118. return
  119. }
  120. //VideoUpdate is used for hidden UGC video
  121. func (s *Service) VideoUpdate(id int, title string) (err error) {
  122. w := map[string]interface{}{"id": id}
  123. if err = s.DB.Model(&model.Video{}).Where(w).Update("eptitle", title).Error; err != nil {
  124. return
  125. }
  126. return
  127. }
  128. func (s *Service) getArc(aid int64) (res *model.Archive, err error) {
  129. var data = model.Archive{}
  130. if err = s.DB.Where("aid = ?", aid).Where("deleted =0").First(&data).Error; err != nil {
  131. log.Error("getArc Aid %d, Err %v", aid, err)
  132. return
  133. }
  134. res = &data
  135. return
  136. }
  137. func (s *Service) loadAbnCids() {
  138. var (
  139. rows *sql.Rows
  140. err error
  141. abnCids []*model.AbnorCids
  142. cfg = s.c.Cfg.Abnormal
  143. )
  144. // select cid from ugc_video where cid > 12780000 and deleted =0 and mark = 1 and submit = 1 and ctime < '2018-10-17 18:00:00' and transcoded = 0
  145. if rows, err = s.DB.Model(&model.Video{}).Where("cid > ?", cfg.CriticalCid).
  146. Where("deleted = 0").Where("mark = 1").Where("submit = 1").
  147. Where(fmt.Sprintf("ctime < DATE_SUB(NOW(), INTERVAL %d HOUR)", cfg.AbnormHours)).
  148. Where("transcoded = 0").Select("cid,aid,ctime,eptitle").Rows(); err != nil {
  149. log.Error("loadAbnCids Err %v", err)
  150. return
  151. }
  152. defer rows.Close()
  153. for rows.Next() {
  154. var (
  155. video = &model.AbnorVideo{}
  156. arc *model.Archive
  157. )
  158. if err = rows.Scan(&video.CID, &video.AID, &video.CTime, &video.VideoTitle); err != nil {
  159. log.Error("rows.Scan error(%v)", err)
  160. return
  161. }
  162. if arc, err = s.getArc(video.AID); err != nil {
  163. log.Error("getArc Aid %d, Err %v", video.AID, err)
  164. continue
  165. }
  166. abnCids = append(abnCids, video.ToCids(arc))
  167. }
  168. if err = rows.Err(); err != nil {
  169. log.Error("loadAbnCids rows Err %v", err)
  170. return
  171. }
  172. s.abnCids = abnCids
  173. log.Info("loadAbnCids Update Memory, Length %d", len(abnCids))
  174. }
  175. func (s *Service) loadAbnCidsproc() {
  176. for {
  177. time.Sleep(time.Duration(s.c.Cfg.Abnormal.ReloadFre))
  178. s.loadAbnCids()
  179. }
  180. }
  181. // AbnDebug returns the memory abnormal cids
  182. func (s *Service) AbnDebug() (data []*model.AbnorCids) {
  183. return s.abnCids
  184. }
  185. // AbnormExport exports the abnormal cids in CSV format
  186. func (s *Service) AbnormExport() (data *bytes.Buffer, fileName string) {
  187. var cfg = s.c.Cfg.Abnormal
  188. data = &bytes.Buffer{}
  189. csvWriter := csv.NewWriter(data)
  190. fileName = fmt.Sprintf("attachment;filename=\"Abnormal_%dh_%s.csv\"", cfg.AbnormHours, time.Now().Format(mysql.TimeFormat))
  191. csvWriter.Write(cfg.ExportTitles)
  192. for _, v := range s.abnCids {
  193. csvWriter.Write(v.Export())
  194. }
  195. csvWriter.Flush()
  196. return
  197. }