video.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package archive
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/videoup/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _inVdoSQL = `INSERT INTO archive_video(filename,cid,aid,eptitle,description,src_type,duration,filesize,resolutions,playurl,failinfo,index_order,
  12. attribute,xcode_state,status,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
  13. _upVdoSQL = "UPDATE archive_video SET eptitle=?,description=? WHERE id=?"
  14. _upVdoIndexSQL = "UPDATE archive_video SET index_order=? WHERE id=?"
  15. _upVdoLinkSQL = "UPDATE archive_video SET weblink=? WHERE id=?"
  16. _upVdoStatusSQL = "UPDATE archive_video SET status=? WHERE id=?"
  17. _upVdoAttrSQL = "UPDATE archive_video SET attribute=attribute&(~(1<<?))|(?<<?) WHERE id=?"
  18. _videoIDSQL = `SELECT id,filename,cid,aid,eptitle,description,src_type,duration,filesize,resolutions,playurl,failinfo,
  19. index_order,attribute,xcode_state,status,ctime,mtime FROM archive_video WHERE id=? LIMIT 1`
  20. _videoIDsSQL = `SELECT id,filename,cid,aid,eptitle,description,src_type,duration,filesize,resolutions,playurl,failinfo,
  21. index_order,attribute,xcode_state,status,ctime,mtime FROM archive_video WHERE id in (%s)`
  22. _videoAidSQL = `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 aid=? and status != -100 ORDER BY index_order ASC`
  24. _videoStatesSQL = "SELECT vr.id,vr.state AS vr_state,v.status AS v_status FROM archive_video_relation AS vr LEFT JOIN video AS v on vr.cid = v.id WHERE vr.id IN (%s)"
  25. _aidByVidsSQL = "SELECT id,aid FROM archive_video_relation WHERE id IN (%s)"
  26. )
  27. // TxAddVideo insert video.
  28. func (d *Dao) TxAddVideo(tx *sql.Tx, v *archive.Video) (vid int64, err error) {
  29. res, err := tx.Exec(_inVdoSQL, v.Filename, v.Cid, v.Aid, v.Title, v.Desc, v.SrcType, v.Duration, v.Filesize, v.Resolutions,
  30. v.Playurl, v.FailCode, v.Index, v.Attribute, v.XcodeState, v.Status, v.CTime, v.MTime)
  31. if err != nil {
  32. log.Error("d.inVideo.Exec error(%v)", err)
  33. return
  34. }
  35. vid, err = res.LastInsertId()
  36. return
  37. }
  38. // TxUpVideo update video by id.
  39. func (d *Dao) TxUpVideo(tx *sql.Tx, vid int64, title, desc string) (rows int64, err error) {
  40. res, err := tx.Exec(_upVdoSQL, title, desc, vid)
  41. if err != nil {
  42. log.Error("d.upVideo.Exec error(%v)", err)
  43. return
  44. }
  45. rows, err = res.RowsAffected()
  46. return
  47. }
  48. // TxUpVideoIndex update video index by id.
  49. func (d *Dao) TxUpVideoIndex(tx *sql.Tx, vid int64, index int) (rows int64, err error) {
  50. res, err := tx.Exec(_upVdoIndexSQL, index, vid)
  51. if err != nil {
  52. log.Error("d.upVideoIndex.Exec error(%v)", err)
  53. return
  54. }
  55. rows, err = res.RowsAffected()
  56. return
  57. }
  58. // TxUpVideoLink update weblink.
  59. func (d *Dao) TxUpVideoLink(tx *sql.Tx, id int64, weblink string) (rows int64, err error) {
  60. res, err := tx.Exec(_upVdoLinkSQL, weblink, id)
  61. if err != nil {
  62. log.Error("d.upVideoLink.Exec error(%v)", err)
  63. return
  64. }
  65. rows, err = res.RowsAffected()
  66. return
  67. }
  68. // TxUpVideoStatus update video status by id.
  69. func (d *Dao) TxUpVideoStatus(tx *sql.Tx, id int64, status int16) (rows int64, err error) {
  70. res, err := tx.Exec(_upVdoStatusSQL, status, id)
  71. if err != nil {
  72. log.Error("d.upVideoStatus.Exec error(%v)", err)
  73. return
  74. }
  75. rows, err = res.RowsAffected()
  76. return
  77. }
  78. // TxUpVideoAttr update video attribute by id.
  79. func (d *Dao) TxUpVideoAttr(tx *sql.Tx, id int64, bit uint, val int32) (rows int64, err error) {
  80. res, err := tx.Exec(_upVdoAttrSQL, bit, val, bit, id)
  81. if err != nil {
  82. log.Error("d.upVideoAttr.Exec() error(%v)", err)
  83. return
  84. }
  85. rows, err = res.RowsAffected()
  86. return
  87. }
  88. // VideoByID Video get video info by id. TODO Depreciated
  89. func (d *Dao) VideoByID(c context.Context, id int64) (v *archive.Video, err error) {
  90. row := d.rddb.QueryRow(c, _videoIDSQL, id)
  91. v = &archive.Video{}
  92. if err = row.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  93. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
  94. if err == sql.ErrNoRows {
  95. v = nil
  96. err = nil
  97. } else {
  98. log.Error("row.Scan error(%v)", err)
  99. }
  100. }
  101. return
  102. }
  103. // VideoByIDs Video get video info by ids. TODO Depreciated
  104. func (d *Dao) VideoByIDs(c context.Context, id []int64) (vs []*archive.Video, err error) {
  105. rows, err := d.rddb.Query(c, fmt.Sprintf(_videoIDsSQL, xstr.JoinInts(id)))
  106. if err != nil {
  107. log.Error("db.Query() error(%v)", err)
  108. return
  109. }
  110. defer rows.Close()
  111. for rows.Next() {
  112. v := &archive.Video{}
  113. if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  114. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
  115. log.Error("rows.Scan error(%v)", err)
  116. return
  117. }
  118. vs = append(vs, v)
  119. }
  120. return
  121. }
  122. // VideosByAid Video get video info by aid. TODO Depreciated
  123. func (d *Dao) VideosByAid(c context.Context, aid int64) (vs []*archive.Video, err error) {
  124. rows, err := d.rddb.Query(c, _videoAidSQL, aid)
  125. if err != nil {
  126. log.Error("db.Query() error(%v)", err)
  127. return
  128. }
  129. defer rows.Close()
  130. for rows.Next() {
  131. v := &archive.Video{}
  132. if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  133. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
  134. log.Error("rows.Scan error(%v)", err)
  135. return
  136. }
  137. vs = append(vs, v)
  138. }
  139. return
  140. }
  141. // VideoStateMap get archive id and state map
  142. func (d *Dao) VideoStateMap(c context.Context, vids []int64) (sMap map[int64]int, err error) {
  143. sMap = make(map[int64]int)
  144. if len(vids) == 0 {
  145. return
  146. }
  147. rows, err := d.rddb.Query(c, fmt.Sprintf(_videoStatesSQL, xstr.JoinInts(vids)))
  148. if err != nil {
  149. log.Error("db.Query() error(%v)", err)
  150. return
  151. }
  152. defer rows.Close()
  153. for rows.Next() {
  154. a := struct {
  155. ID int64
  156. State int
  157. Status int
  158. }{}
  159. if err = rows.Scan(&a.ID, &a.State, &a.Status); err != nil {
  160. log.Error("rows.Scan error(%v)", err)
  161. return
  162. }
  163. if a.State == -100 {
  164. sMap[a.ID] = -100
  165. } else {
  166. sMap[a.ID] = a.Status
  167. }
  168. }
  169. return
  170. }
  171. // VideoAidMap 批量通过视频id获取稿件id
  172. func (d *Dao) VideoAidMap(c context.Context, vids []int64) (vMap map[int64]int64, err error) {
  173. var (
  174. aid, vid int64
  175. )
  176. vMap = make(map[int64]int64)
  177. if len(vids) == 0 {
  178. return
  179. }
  180. rows, err := d.rddb.Query(c, fmt.Sprintf(_aidByVidsSQL, xstr.JoinInts(vids)))
  181. defer rows.Close()
  182. if err != nil {
  183. log.Error("db.Query() error(%v)", err)
  184. return
  185. }
  186. for rows.Next() {
  187. if err = rows.Scan(&vid, &aid); err != nil {
  188. log.Error("rows.Scan error(%v)", err)
  189. return
  190. }
  191. vMap[vid] = aid
  192. }
  193. return
  194. }