new_video.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package archive
  2. import (
  3. "context"
  4. "database/sql"
  5. xsql "go-common/library/database/sql"
  6. "go-common/library/log"
  7. "go-common/app/job/main/videoup/model/archive"
  8. farm "github.com/dgryski/go-farm"
  9. )
  10. const (
  11. _upVideoXStateSQL = "UPDATE video SET xcode_state=? WHERE hash64=? AND filename=?"
  12. _upVideoStatusSQL = "UPDATE video SET status=? WHERE hash64=? AND filename=?"
  13. _upVideoPlayurlSQL = "UPDATE video SET playurl=? WHERE hash64=? AND filename=?"
  14. _upVideoDuraSQL = "UPDATE video SET duration=? WHERE hash64=? AND filename=?"
  15. _upVideoFilesizeSQL = "UPDATE video SET filesize=? WHERE hash64=? AND filename=?"
  16. _upVideoResolutionSQL = "UPDATE video SET resolutions=?,dimensions=? WHERE hash64=? AND filename=?"
  17. _upVideoFailCodeSQL = "UPDATE video SET failcode=? WHERE hash64=? AND filename=?"
  18. _upRelationStatusSQL = "UPDATE archive_video_relation SET state=? WHERE cid=?"
  19. _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,
  20. 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
  21. WHERE hash64=? AND filename=?`
  22. _newVideoByAidFnSQL = `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,
  23. 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
  24. WHERE aid=? AND hash64=? AND filename=?`
  25. _newVideosSQL = `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,
  26. 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
  27. WHERE aid=? ORDER BY index_order`
  28. _newVideoCntSQL = `SELECT COUNT(*) FROM archive_video_relation WHERE aid=? AND state!=-100`
  29. _newSumDuraSQL = `SELECT SUM(duration) FROM archive_video_relation avr JOIN video v on avr.cid = v.id WHERE aid=? AND avr.state=0 AND (v.status=0 || v.status=10000)`
  30. _newVdoBvcCntSQL = `SELECT COUNT(*) FROM archive_video_relation avr JOIN video v on avr.cid = v.id WHERE cid=? AND avr.state=0 AND (v.status=0 || v.status=10000) AND v.xcode_state=6`
  31. _validAidByCid = `SELECT DISTINCT aid FROM archive_video_relation avr JOIN video v on avr.cid = v.id WHERE cid=? AND avr.state=0 AND (v.status=0 || v.status=10000) AND v.xcode_state=6`
  32. )
  33. // TxUpVideoXState update video xcodestate.
  34. func (d *Dao) TxUpVideoXState(tx *xsql.Tx, filename string, xState int8) (rows int64, err error) {
  35. hash64 := int64(farm.Hash64([]byte(filename)))
  36. res, err := tx.Exec(_upVideoXStateSQL, xState, hash64, filename)
  37. if err != nil {
  38. log.Error("tx.upVideoXState.Exec(%d, %s) error(%v)", xState, filename, err)
  39. return
  40. }
  41. return res.RowsAffected()
  42. }
  43. // TxUpVideoStatus update video status.
  44. func (d *Dao) TxUpVideoStatus(tx *xsql.Tx, filename string, status int16) (rows int64, err error) {
  45. hash64 := int64(farm.Hash64([]byte(filename)))
  46. res, err := tx.Exec(_upVideoStatusSQL, status, hash64, filename)
  47. if err != nil {
  48. log.Error("tx.upVideoStatus.Exec(%d, %s) error(%v)", status, filename, err)
  49. return
  50. }
  51. return res.RowsAffected()
  52. }
  53. // TxUpVideoPlayurl update video playurl and duration.
  54. func (d *Dao) TxUpVideoPlayurl(tx *xsql.Tx, filename, playurl string) (rows int64, err error) {
  55. hash64 := int64(farm.Hash64([]byte(filename)))
  56. res, err := tx.Exec(_upVideoPlayurlSQL, playurl, hash64, filename)
  57. if err != nil {
  58. log.Error("tx.upVideoPlayurl.Exec(%s, %s) error(%v)", playurl, filename, err)
  59. return
  60. }
  61. return res.RowsAffected()
  62. }
  63. // TxUpVDuration update video playurl and duration.
  64. func (d *Dao) TxUpVDuration(tx *xsql.Tx, filename string, duration int64) (rows int64, err error) {
  65. hash64 := int64(farm.Hash64([]byte(filename)))
  66. res, err := tx.Exec(_upVideoDuraSQL, duration, hash64, filename)
  67. if err != nil {
  68. log.Error("tx.upVideoDura.Exec(%d, %s) error(%v)", duration, filename, err)
  69. return
  70. }
  71. return res.RowsAffected()
  72. }
  73. // TxUpVideoFilesize update video filesize.
  74. func (d *Dao) TxUpVideoFilesize(tx *xsql.Tx, filename string, filesize int64) (rows int64, err error) {
  75. hash64 := int64(farm.Hash64([]byte(filename)))
  76. res, err := tx.Exec(_upVideoFilesizeSQL, filesize, hash64, filename)
  77. if err != nil {
  78. log.Error("tx.upVideoFilesize.Exec(%d, %s) error(%v)", filesize, filename, err)
  79. }
  80. return res.RowsAffected()
  81. }
  82. // TxUpVideoResolutionsAndDimensions update video resolutions and dimensions.
  83. func (d *Dao) TxUpVideoResolutionsAndDimensions(tx *xsql.Tx, filename, resolutions, dimensions string) (rows int64, err error) {
  84. hash64 := int64(farm.Hash64([]byte(filename)))
  85. res, err := tx.Exec(_upVideoResolutionSQL, resolutions, dimensions, hash64, filename)
  86. if err != nil {
  87. log.Error("tx.TxUpVideoResolutionsAndDimensions.Exec(%s,%s, %s) error(%v)", resolutions, dimensions, filename, err)
  88. return
  89. }
  90. return res.RowsAffected()
  91. }
  92. // TxUpVideoFailCode update video fail info.
  93. func (d *Dao) TxUpVideoFailCode(tx *xsql.Tx, filename string, fileCode int8) (rows int64, err error) {
  94. hash64 := int64(farm.Hash64([]byte(filename)))
  95. res, err := tx.Exec(_upVideoFailCodeSQL, fileCode, hash64, filename)
  96. if err != nil {
  97. log.Error("tx.upVideoFailCode.Exec(%s, %d) error(%v)", filename, fileCode, err)
  98. return
  99. }
  100. return res.RowsAffected()
  101. }
  102. // TxUpRelationStatus update video status.
  103. func (d *Dao) TxUpRelationStatus(tx *xsql.Tx, cid int64, status int8) (rows int64, err error) {
  104. res, err := tx.Exec(_upRelationStatusSQL, status, cid)
  105. if err != nil {
  106. log.Error("tx.TxUpRelationStatus.Exec(%d, %d) error(%v)", status, cid, err)
  107. return
  108. }
  109. return res.RowsAffected()
  110. }
  111. // NewVideo get video info by filename.
  112. func (d *Dao) NewVideo(c context.Context, filename string) (v *archive.Video, err error) {
  113. hash64 := int64(farm.Hash64([]byte(filename)))
  114. row := d.db.QueryRow(c, _newVideoByFnSQL, hash64, filename)
  115. v = &archive.Video{}
  116. var avrState, vState int16
  117. if err = row.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  118. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &avrState, &vState, &v.CTime, &v.MTime); err != nil {
  119. if err == sql.ErrNoRows {
  120. v = nil
  121. err = nil
  122. } else {
  123. log.Error("row.Scan error(%v)", err)
  124. }
  125. return
  126. }
  127. // 2 state map to 1
  128. if avrState == archive.VideoStatusDelete {
  129. v.Status = archive.VideoStatusDelete
  130. } else {
  131. v.Status = vState
  132. }
  133. return
  134. }
  135. // NewVideoByAid get video info by filename. and aid
  136. func (d *Dao) NewVideoByAid(c context.Context, filename string, aid int64) (v *archive.Video, err error) {
  137. hash64 := int64(farm.Hash64([]byte(filename)))
  138. row := d.db.QueryRow(c, _newVideoByAidFnSQL, aid, hash64, filename)
  139. v = &archive.Video{}
  140. var avrState, vState int16
  141. if err = row.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  142. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &avrState, &vState, &v.CTime, &v.MTime); err != nil {
  143. if err == sql.ErrNoRows {
  144. v = nil
  145. err = nil
  146. } else {
  147. log.Error("row.Scan error(%v)", err)
  148. }
  149. return
  150. }
  151. // 2 state map to 1
  152. if avrState == archive.VideoStatusDelete {
  153. v.Status = archive.VideoStatusDelete
  154. } else {
  155. v.Status = vState
  156. }
  157. return
  158. }
  159. // NewVideos get videos info by aid.
  160. func (d *Dao) NewVideos(c context.Context, aid int64) (vs []*archive.Video, err error) {
  161. rows, err := d.db.Query(c, _newVideosSQL, aid)
  162. if err != nil {
  163. log.Error("d.db.Query(%d) error(%v)", aid, err)
  164. return
  165. }
  166. defer rows.Close()
  167. for rows.Next() {
  168. v := &archive.Video{}
  169. var avrState, vState int16
  170. if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  171. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &avrState, &vState, &v.CTime, &v.MTime); err != nil {
  172. log.Error("rows.Scan error(%v)", err)
  173. return
  174. }
  175. // 2 state map to 1
  176. if avrState == archive.VideoStatusDelete {
  177. v.Status = archive.VideoStatusDelete
  178. } else {
  179. v.Status = vState
  180. }
  181. vs = append(vs, v)
  182. }
  183. return
  184. }
  185. // NewVideoCount get all video duration by aid.
  186. func (d *Dao) NewVideoCount(c context.Context, aid int64) (count int, err error) {
  187. row := d.db.QueryRow(c, _newVideoCntSQL, aid)
  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. }
  197. // NewSumDuration get all video duration by aid.
  198. func (d *Dao) NewSumDuration(c context.Context, aid int64) (sumDura int64, err error) {
  199. var (
  200. r = &sql.NullInt64{}
  201. row = d.db.QueryRow(c, _newSumDuraSQL, aid)
  202. )
  203. if err = row.Scan(r); err != nil {
  204. if err == sql.ErrNoRows {
  205. err = nil
  206. } else {
  207. log.Error("row.Scan error(%v)", err)
  208. }
  209. return
  210. }
  211. sumDura = r.Int64
  212. return
  213. }
  214. // NewVideoCountCapable get all video duration by aid.
  215. func (d *Dao) NewVideoCountCapable(c context.Context, cid int64) (count int, err error) {
  216. row := d.db.QueryRow(c, _newVdoBvcCntSQL, cid)
  217. if err = row.Scan(&count); err != nil {
  218. if err == sql.ErrNoRows {
  219. err = nil
  220. } else {
  221. log.Error("row.Scan error(%v)", err)
  222. }
  223. }
  224. return
  225. }
  226. // ValidAidByCid get all video duration by aid.
  227. func (d *Dao) ValidAidByCid(c context.Context, cid int64) (aids []int64, err error) {
  228. rows, err := d.db.Query(c, _validAidByCid, cid)
  229. if err != nil {
  230. log.Error("d.db.Query(%d) error(%v)", cid, err)
  231. return
  232. }
  233. defer rows.Close()
  234. for rows.Next() {
  235. var aid int64
  236. if err = rows.Scan(&aid); err != nil {
  237. log.Error("rows.Scan error(%v)", err)
  238. return
  239. }
  240. aids = append(aids, aid)
  241. }
  242. return
  243. }