video.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package archive
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/job/main/videoup/model/archive"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. // insert // NOTE: will delete???
  11. // _inFilenameSQL = `INSERT INTO archive_video (filename,filesize,xcode_state) VALUES(?,?,?)
  12. // ON DUPLICATE KEY UPDATE filesize=?,xcode_state=?`
  13. // update
  14. _upXStateSQL = "UPDATE archive_video SET xcode_state=? WHERE filename=?"
  15. _upStatusSQL = "UPDATE archive_video SET status=? WHERE filename=?"
  16. _upPlayurlSQL = "UPDATE archive_video SET playurl=? WHERE filename=?"
  17. _upVDuraSQL = "UPDATE archive_video SET duration=? WHERE filename=?"
  18. _upResolutionSQL = "UPDATE archive_video SET resolutions=? WHERE filename=?"
  19. _upFilesizeSQL = "UPDATE archive_video SET filesize=? WHERE filename=?"
  20. _upFailCodeSQL = "UPDATE archive_video SET failinfo=? WHERE filename=?"
  21. // select
  22. _videoSQL = `SELECT id,filename,cid,aid,eptitle,description,src_type,duration,filesize,resolutions,playurl,failinfo,
  23. index_order,attribute,xcode_state,status,ctime,mtime FROM archive_video WHERE filename=?`
  24. _videoByAidSQL = `SELECT id,filename,cid,aid,eptitle,description,src_type,duration,filesize,resolutions,playurl,failinfo,
  25. index_order,attribute,xcode_state,status,ctime,mtime FROM archive_video WHERE filename=? AND aid=?`
  26. _videosSQL = `SELECT id,filename,cid,aid,eptitle,description,src_type,duration,filesize,resolutions,playurl,failinfo,
  27. index_order,attribute,xcode_state,status,ctime,mtime FROM archive_video WHERE aid=? ORDER BY index_order`
  28. _videoCntSQL = `SELECT COUNT(*) FROM archive_video WHERE aid=? AND status!=-100`
  29. _sumDuraSQL = `SELECT SUM(duration) FROM archive_video WHERE aid=? AND (status=0 || status=10000)`
  30. _vdoBvcCntSQL = `SELECT COUNT(*) FROM archive_video WHERE cid=? AND (status=0 || status=10000) AND xcode_state=6`
  31. _vdoAidBvcCntSQL = `SELECT COUNT(*) FROM archive_video av LEFT JOIN archive a ON av.aid=a.id WHERE
  32. av.cid=? AND (av.status=0 || av.status=10000) AND av.xcode_state=6 AND (a.state>=0 || a.state=-6)`
  33. )
  34. // TxUpXcodeState update video state.
  35. func (d *Dao) TxUpXcodeState(tx *xsql.Tx, filename string, xState int8) (rows int64, err error) {
  36. res, err := tx.Exec(_upXStateSQL, xState, filename)
  37. if err != nil {
  38. log.Error("tx.Exec(%d, %s) error(%v)", xState, filename, err)
  39. return
  40. }
  41. return res.RowsAffected()
  42. }
  43. // TxUpStatus update video status.
  44. func (d *Dao) TxUpStatus(tx *xsql.Tx, filename string, status int16) (rows int64, err error) {
  45. res, err := tx.Exec(_upStatusSQL, status, filename)
  46. if err != nil {
  47. log.Error("tx.Exec(%d, %s) error(%v)", status, filename, err)
  48. return
  49. }
  50. return res.RowsAffected()
  51. }
  52. // TxUpPlayurl update video playurl and duration.
  53. func (d *Dao) TxUpPlayurl(tx *xsql.Tx, filename, playurl string) (rows int64, err error) {
  54. res, err := tx.Exec(_upPlayurlSQL, playurl, filename)
  55. if err != nil {
  56. log.Error("tx.Exec(%s, %s) error(%v)", playurl, filename, err)
  57. return
  58. }
  59. return res.RowsAffected()
  60. }
  61. // TxUpVideoDuration update video playurl and duration.
  62. func (d *Dao) TxUpVideoDuration(tx *xsql.Tx, filename string, duration int64) (rows int64, err error) {
  63. res, err := tx.Exec(_upVDuraSQL, duration, filename)
  64. if err != nil {
  65. log.Error("tx.Exec(%d, %s) error(%v)", duration, filename, err)
  66. return
  67. }
  68. return res.RowsAffected()
  69. }
  70. // TxUpFilesize update video filesize.
  71. func (d *Dao) TxUpFilesize(tx *xsql.Tx, filename string, filesize int64) (rows int64, err error) {
  72. res, err := tx.Exec(_upFilesizeSQL, filesize, filename)
  73. if err != nil {
  74. log.Error("tx.Exec(%d, %s) error(%v)", filesize, filename, err)
  75. }
  76. return res.RowsAffected()
  77. }
  78. // TxUpResolutions update video resolutions.
  79. func (d *Dao) TxUpResolutions(tx *xsql.Tx, filename, resolutions string) (rows int64, err error) {
  80. res, err := tx.Exec(_upResolutionSQL, resolutions, filename)
  81. if err != nil {
  82. log.Error("tx.Exec(%s, %s) error(%v)", resolutions, filename, err)
  83. return
  84. }
  85. return res.RowsAffected()
  86. }
  87. // TxUpFailCode update video fail info.
  88. func (d *Dao) TxUpFailCode(tx *xsql.Tx, filename string, fileCode int8) (rows int64, err error) {
  89. res, err := tx.Exec(_upFailCodeSQL, fileCode, filename)
  90. if err != nil {
  91. log.Error("tx.Exec(%s, %d) error(%v)", filename, fileCode, err)
  92. return
  93. }
  94. return res.RowsAffected()
  95. }
  96. // Video get video info by filename. NOTE Deprecated
  97. func (d *Dao) Video(c context.Context, filename string) (v *archive.Video, err error) {
  98. row := d.db.QueryRow(c, _videoSQL, filename)
  99. v = &archive.Video{}
  100. if err = row.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  101. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
  102. if err == sql.ErrNoRows {
  103. v = nil
  104. err = nil
  105. } else {
  106. log.Error("row.Scan error(%v)", err)
  107. }
  108. }
  109. return
  110. }
  111. // VideoByAid get video info by filename. and aid. NOTE Deprecated
  112. func (d *Dao) VideoByAid(c context.Context, filename string, aid int64) (v *archive.Video, err error) {
  113. row := d.db.QueryRow(c, _videoByAidSQL, filename, aid)
  114. v = &archive.Video{}
  115. if err = row.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  116. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
  117. if err == sql.ErrNoRows {
  118. v = nil
  119. err = nil
  120. } else {
  121. log.Error("row.Scan error(%v)", err)
  122. }
  123. }
  124. return
  125. }
  126. // Videos get videos info by aid. NOTE Deprecated
  127. func (d *Dao) Videos(c context.Context, aid int64) (vs []*archive.Video, err error) {
  128. rows, err := d.db.Query(c, _videosSQL, aid)
  129. if err != nil {
  130. log.Error("d.db.Query(%d) error(%v)", aid, err)
  131. return
  132. }
  133. defer rows.Close()
  134. for rows.Next() {
  135. v := &archive.Video{}
  136. if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  137. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
  138. log.Error("rows.Scan error(%v)", err)
  139. return
  140. }
  141. vs = append(vs, v)
  142. }
  143. return
  144. }
  145. // VideoCount get all video duration by aid. NOTE Deprecated
  146. func (d *Dao) VideoCount(c context.Context, aid int64) (count int, err error) {
  147. row := d.db.QueryRow(c, _videoCntSQL, aid)
  148. if err = row.Scan(&count); err != nil {
  149. if err == sql.ErrNoRows {
  150. err = nil
  151. } else {
  152. log.Error("row.Scan error(%v)", err)
  153. }
  154. }
  155. return
  156. }
  157. // SumDuration get all video duration by aid. NOTE Deprecated
  158. func (d *Dao) SumDuration(c context.Context, aid int64) (sumDura int64, err error) {
  159. var (
  160. r = &sql.NullInt64{}
  161. row = d.db.QueryRow(c, _sumDuraSQL, aid)
  162. )
  163. if err = row.Scan(r); err != nil {
  164. if err == sql.ErrNoRows {
  165. err = nil
  166. } else {
  167. log.Error("row.Scan error(%v)", err)
  168. }
  169. }
  170. sumDura = r.Int64
  171. return
  172. }
  173. // VideoCountCapable get all video duration by aid. NOTE Deprecated
  174. func (d *Dao) VideoCountCapable(c context.Context, cid int64) (count int, err error) {
  175. row := d.db.QueryRow(c, _vdoBvcCntSQL, cid)
  176. if err = row.Scan(&count); err != nil {
  177. if err == sql.ErrNoRows {
  178. err = nil
  179. } else {
  180. log.Error("row.Scan error(%v)", err)
  181. }
  182. }
  183. return
  184. }
  185. // VdoWithArcCntCapable get all video duration by aid. NOTE Deprecated
  186. func (d *Dao) VdoWithArcCntCapable(c context.Context, cid int64) (count int, err error) {
  187. row := d.db.QueryRow(c, _vdoAidBvcCntSQL, cid)
  188. if err = row.Scan(&count); err != nil {
  189. if err == sql.ErrNoRows {
  190. err = nil
  191. } else {
  192. log.Error("row.Scan error(%v)", err)
  193. }
  194. }
  195. return
  196. }