video.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package archive
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. "time"
  6. "database/sql"
  7. "go-common/app/job/main/videoup-report/model/archive"
  8. farm "github.com/dgryski/go-farm"
  9. )
  10. const (
  11. _updatedFilenamesByTime = "SELECT filename FROM video WHERE mtime >= ? AND mtime < ?"
  12. _videos2SQL = `SELECT vr.id,v.filename,vr.cid,vr.aid,vr.title,vr.description,v.src_type,v.duration,v.filesize,v.resolutions,
  13. v.playurl,v.failcode,vr.index_order,v.attribute,v.xcode_state,v.status,vr.state,vr.ctime,vr.mtime FROM archive_video_relation AS vr JOIN video AS v ON vr.cid=v.id WHERE vr.aid=? ORDER BY vr.index_order`
  14. _newVideoByFnSQL = `SELECT avr.id,v.filename,avr.cid,avr.aid,avr.title,avr.description,v.src_type,v.duration,v.filesize,v.resolutions,v.playurl,v.failcode,
  15. avr.index_order,v.attribute,v.xcode_state,avr.state,v.status,avr.ctime,avr.mtime FROM archive_video_relation avr JOIN video v on avr.cid = v.id
  16. WHERE hash64=? AND filename=?`
  17. )
  18. // UpdatedFilenames Get updated video's filename between stime and etime.
  19. func (d *Dao) UpdatedFilenames(c context.Context, stime, etime time.Time) (fns []string, err error) {
  20. rows, err := d.db.Query(c, _updatedFilenamesByTime, stime, etime)
  21. if err != nil {
  22. log.Error("d.UpdatedFilenames.Query(%v,%v) error(%v)", stime, etime, err)
  23. return
  24. }
  25. defer rows.Close()
  26. for rows.Next() {
  27. fn := ""
  28. if err = rows.Scan(&fn); err != nil {
  29. log.Error("rows.Scan error(%v)", err)
  30. return
  31. }
  32. fns = append(fns, fn)
  33. }
  34. return
  35. }
  36. // Videos2 get videos by 2 table em.......
  37. func (d *Dao) Videos2(c context.Context, aid int64) (vs []*archive.Video, err error) {
  38. rows, err := d.db.Query(c, _videos2SQL, aid)
  39. if err != nil {
  40. log.Error("d.db.Query(%s, %d) error(%v)", _videos2SQL, aid, err)
  41. return
  42. }
  43. defer rows.Close()
  44. for rows.Next() {
  45. v := &archive.Video{}
  46. if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  47. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.State, &v.CTime, &v.MTime); err != nil {
  48. log.Error("rows.Scan error(%v)", err)
  49. return
  50. }
  51. vs = append(vs, v)
  52. }
  53. return
  54. }
  55. // NewVideo get video info by filename.
  56. func (d *Dao) NewVideo(c context.Context, filename string) (v *archive.Video, err error) {
  57. hash64 := int64(farm.Hash64([]byte(filename)))
  58. row := d.db.QueryRow(c, _newVideoByFnSQL, hash64, filename)
  59. v = &archive.Video{}
  60. var avrState, vState int16
  61. if err = row.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  62. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &avrState, &vState, &v.CTime, &v.MTime); err != nil {
  63. if err == sql.ErrNoRows {
  64. v = nil
  65. err = nil
  66. } else {
  67. log.Error("row.Scan error(%v)", err)
  68. }
  69. return
  70. }
  71. // 2 state map to 1
  72. if avrState == archive.VideoStatusDelete {
  73. v.Status = archive.VideoStatusDelete
  74. } else {
  75. v.Status = vState
  76. }
  77. return
  78. }