new_video.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. _videoByCid = "SELECT vr.id,vr.aid,vr.title AS eptitle,vr.description,v.filename,v.src_type,vr.cid,v.duration,v.filesize,v.resolutions,vr.index_order,vr.ctime,vr.mtime,v.status,v.playurl,v.attribute,v.failcode AS failinfo,v.xcode_state,v.weblink FROM archive_video_relation AS vr LEFT JOIN video AS v ON vr.cid = v.id WHERE vr.cid = ?"
  12. _inRelationSQL = "INSERT IGNORE INTO archive_video_relation (id,aid,cid,title,description,index_order,ctime,mtime) VALUES (?,?,?,?,?,?,?,?)"
  13. _upRelationSQL = "UPDATE archive_video_relation SET title=?,description=? WHERE id=?"
  14. _upRelationOrderSQL = "UPDATE archive_video_relation SET index_order=? WHERE id=?"
  15. _upRelationStateSQL = "UPDATE archive_video_relation SET state=? WHERE id=?"
  16. _upVideoLinkSQL = "UPDATE video SET weblink=? WHERE id=?"
  17. _upVideoStatusSQL = "UPDATE video SET status=? WHERE id=?"
  18. _upVideoAttrSQL = "UPDATE video SET attribute=attribute&(~(1<<?))|(?<<?) WHERE id=?"
  19. _slPlayurl = "SELECT playurl FROM video WHERE id=? LIMIT 1"
  20. _newVideoIDSQL = `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,
  21. avr.index_order,v.attribute,v.xcode_state,avr.state,avr.ctime,avr.mtime FROM archive_video_relation avr JOIN video v on avr.cid = v.id
  22. WHERE avr.id=? LIMIT 1`
  23. _newVideoIDsSQL = `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,
  24. avr.index_order,v.attribute,v.xcode_state,avr.state,avr.ctime,avr.mtime FROM archive_video_relation avr JOIN video v on avr.cid = v.id
  25. WHERE avr.id in (%s)`
  26. _newVideosAIDSQL = `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,
  27. 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
  28. WHERE aid=? and state != -100 ORDER BY index_order ASC`
  29. _newVideoCntSQL = `SELECT COUNT(*) FROM archive_video_relation WHERE aid=? AND state!=-100`
  30. _slSrcTypeSQL = "SELECT `id`, `src_type` FROM `video` WHERE `id` IN (%s)"
  31. _slVIDSQL = "SELECT ar.id FROM archive_video_relation AS ar, video AS v WHERE ar.cid = v.id AND ar.aid=? AND v.filename=?;"
  32. _videoInfo = `SELECT vr.id, vr.aid, vr.title AS eptitle, vr.description, vr.cid, vr.ctime AS epctime, v.filename, v.xcode_state, v.playurl,
  33. a.ctime, a.author, a.title, a.tag, a.content, a.cover, a.typeid, a.mid, a.copyright,
  34. coalesce(addit.source, '') source, coalesce(addit.dynamic, '') dynamic, coalesce(addit.desc_format_id, 0) desc_format_id, coalesce(addit.description, '') description
  35. FROM archive_video_relation AS vr JOIN archive AS a ON vr.aid = a.id
  36. LEFT OUTER JOIN video AS v ON vr.cid = v.id
  37. LEFT OUTER JOIN archive_addit AS addit ON vr.aid = addit.aid
  38. WHERE vr.aid = ? AND vr.cid=? LIMIT 1`
  39. _videoRelated = `SELECT v.filename,v.status,vr.aid,vr.index_order,a.title,a.ctime FROM archive_video_relation AS vr LEFT JOIN video AS v ON vr.cid = v.id JOIN archive AS a ON vr.aid = a.id WHERE vr.aid = ?`
  40. )
  41. // VideoByCID get video by cid
  42. func (d *Dao) VideoByCID(c context.Context, cid int64) (v *archive.Video, err error) {
  43. row := d.rddb.QueryRow(c, _videoByCid, cid)
  44. v = &archive.Video{}
  45. if err = row.Scan(&v.ID, &v.Aid, &v.Title, &v.Desc, &v.Filename, &v.SrcType, &v.Cid, &v.Duration, &v.Filesize, &v.Resolutions, &v.Index, &v.CTime, &v.MTime, &v.Status, &v.Playurl, &v.Attribute, &v.FailCode, &v.XcodeState, &v.WebLink); err != nil {
  46. if err == sql.ErrNoRows {
  47. v = nil
  48. err = nil
  49. } else {
  50. log.Error("row.Scan error(%v)", err)
  51. }
  52. }
  53. return
  54. }
  55. // TxAddRelation insert archive_video_relation.
  56. func (d *Dao) TxAddRelation(tx *sql.Tx, v *archive.Video) (rows int64, err error) {
  57. res, err := tx.Exec(_inRelationSQL, v.ID, v.Aid, v.Cid, v.Title, v.Desc, v.Index, v.CTime, v.MTime)
  58. if err != nil {
  59. log.Error("d.inRelation.Exec error(%v)", err)
  60. return
  61. }
  62. rows, err = res.RowsAffected()
  63. return
  64. }
  65. // TxUpRelation update title and desc on archive_video_relation by vid.
  66. func (d *Dao) TxUpRelation(tx *sql.Tx, vid int64, title, desc string) (rows int64, err error) {
  67. res, err := tx.Exec(_upRelationSQL, title, desc, vid)
  68. if err != nil {
  69. log.Error("d.upRelation.Exec error(%v)", err)
  70. return
  71. }
  72. rows, err = res.RowsAffected()
  73. return
  74. }
  75. // TxUpRelationOrder update index_order on archive_video_relation by vid.
  76. func (d *Dao) TxUpRelationOrder(tx *sql.Tx, vid int64, index int) (rows int64, err error) {
  77. res, err := tx.Exec(_upRelationOrderSQL, index, vid)
  78. if err != nil {
  79. log.Error("d.upRelationOrder.Exec error(%v)", err)
  80. return
  81. }
  82. rows, err = res.RowsAffected()
  83. return
  84. }
  85. // TxUpRelationState update state on archive_video_relation by vid.
  86. func (d *Dao) TxUpRelationState(tx *sql.Tx, vid int64, state int16) (rows int64, err error) {
  87. res, err := tx.Exec(_upRelationStateSQL, state, vid)
  88. if err != nil {
  89. log.Error("d.upRelationState.Exec error(%v)", err)
  90. return
  91. }
  92. rows, err = res.RowsAffected()
  93. return
  94. }
  95. // TxUpWebLink update weblink on video by cid.
  96. func (d *Dao) TxUpWebLink(tx *sql.Tx, cid int64, weblink string) (rows int64, err error) {
  97. res, err := tx.Exec(_upVideoLinkSQL, weblink, cid)
  98. if err != nil {
  99. log.Error("d.upVideoLink.Exec error(%v)", err)
  100. return
  101. }
  102. rows, err = res.RowsAffected()
  103. return
  104. }
  105. // TxUpStatus update status on video by cid.
  106. func (d *Dao) TxUpStatus(tx *sql.Tx, cid int64, status int16) (rows int64, err error) {
  107. res, err := tx.Exec(_upVideoStatusSQL, status, cid)
  108. if err != nil {
  109. log.Error("d.upVideoStatus.Exec error(%v)", err)
  110. return
  111. }
  112. rows, err = res.RowsAffected()
  113. return
  114. }
  115. // TxUpAttr update attribute on video by cid.
  116. func (d *Dao) TxUpAttr(tx *sql.Tx, cid int64, bit uint, val int32) (rows int64, err error) {
  117. res, err := tx.Exec(_upVideoAttrSQL, bit, val, bit, cid)
  118. if err != nil {
  119. log.Error("d.upVideoAttr.Exec() error(%v)", err)
  120. return
  121. }
  122. rows, err = res.RowsAffected()
  123. return
  124. }
  125. // VideoPlayurl get video play url
  126. func (d *Dao) VideoPlayurl(c context.Context, cid int64) (playurl string, err error) {
  127. row := d.rddb.QueryRow(c, _slPlayurl, cid)
  128. if err = row.Scan(&playurl); err != nil {
  129. if err == sql.ErrNoRows {
  130. err = nil
  131. } else {
  132. log.Error("row.Scan error(%v)", err)
  133. }
  134. }
  135. return
  136. }
  137. // NewVideoByID Video get video info by id.
  138. func (d *Dao) NewVideoByID(c context.Context, id int64) (v *archive.Video, err error) {
  139. row := d.rddb.QueryRow(c, _newVideoIDSQL, id)
  140. v = &archive.Video{}
  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, &v.Status, &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. }
  150. return
  151. }
  152. // NewVideoByIDs Video get video info by ids. NOTE: NOT USED
  153. func (d *Dao) NewVideoByIDs(c context.Context, id []int64) (vs []*archive.Video, err error) {
  154. rows, err := d.rddb.Query(c, fmt.Sprintf(_newVideoIDsSQL, xstr.JoinInts(id)))
  155. if err != nil {
  156. log.Error("db.Query() error(%v)", err)
  157. return
  158. }
  159. defer rows.Close()
  160. for rows.Next() {
  161. v := &archive.Video{}
  162. if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  163. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &v.Status, &v.CTime, &v.MTime); err != nil {
  164. log.Error("rows.Scan error(%v)", err)
  165. return
  166. }
  167. vs = append(vs, v)
  168. }
  169. return
  170. }
  171. // NewVideosByAid Video get video info by aid.
  172. func (d *Dao) NewVideosByAid(c context.Context, aid int64) (vs []*archive.Video, err error) {
  173. rows, err := d.rddb.Query(c, _newVideosAIDSQL, aid)
  174. if err != nil {
  175. log.Error("db.Query() error(%v)", err)
  176. return
  177. }
  178. defer rows.Close()
  179. for rows.Next() {
  180. var avrState, vState int16
  181. v := &archive.Video{}
  182. if err = rows.Scan(&v.ID, &v.Filename, &v.Cid, &v.Aid, &v.Title, &v.Desc, &v.SrcType, &v.Duration, &v.Filesize, &v.Resolutions,
  183. &v.Playurl, &v.FailCode, &v.Index, &v.Attribute, &v.XcodeState, &avrState, &vState, &v.CTime, &v.MTime); err != nil {
  184. log.Error("rows.Scan error(%v)", err)
  185. return
  186. }
  187. // 2 state map to 1
  188. if avrState == archive.VideoStatusDelete {
  189. v.Status = archive.VideoStatusDelete
  190. } else {
  191. v.Status = vState
  192. }
  193. vs = append(vs, v)
  194. }
  195. return
  196. }
  197. // NewVideoCount get all video duration by aid. NOTE: NOT USED
  198. func (d *Dao) NewVideoCount(c context.Context, aid int64) (count int, err error) {
  199. row := d.rddb.QueryRow(c, _newVideoCntSQL, aid)
  200. if err = row.Scan(&count); err != nil {
  201. if err == sql.ErrNoRows {
  202. err = nil
  203. } else {
  204. log.Error("row.Scan error(%v)", err)
  205. }
  206. }
  207. return
  208. }
  209. //VideoSrcTypeByIDs video src_type and id map
  210. func (d *Dao) VideoSrcTypeByIDs(c context.Context, ids []int64) (st map[int64]string, err error) {
  211. st = map[int64]string{}
  212. idStr := xstr.JoinInts(ids)
  213. rows, err := d.db.Query(c, fmt.Sprintf(_slSrcTypeSQL, idStr))
  214. if err != nil {
  215. log.Error("VideoSrcTypeByIDs d.db.Query (ids(%v)) error(%v)", idStr, err)
  216. return
  217. }
  218. defer rows.Close()
  219. for rows.Next() {
  220. var (
  221. id int64
  222. srcType string
  223. )
  224. if err = rows.Scan(&id, &srcType); err != nil {
  225. log.Error("VideoSrcTypeByIDs rows.Scan (ids(%v)) error(%v)", idStr, err)
  226. return
  227. }
  228. st[id] = srcType
  229. }
  230. return
  231. }
  232. //VIDByAIDFilename 根据filename查询视频的vid
  233. func (d *Dao) VIDByAIDFilename(c context.Context, aid int64, filename string) (vid int64, err error) {
  234. row := d.db.QueryRow(c, _slVIDSQL, aid, filename)
  235. if err = row.Scan(&vid); err != nil {
  236. if err == sql.ErrNoRows {
  237. err = nil
  238. } else {
  239. log.Error("VideoRelationIDByFilename row.Scan err(%v) aid(%d) filename(%s)", err, aid, filename)
  240. }
  241. }
  242. return
  243. }
  244. //VideoInfo video info
  245. func (d *Dao) VideoInfo(c context.Context, aid int64, cid int64) (v *archive.VideoInfo, err error) {
  246. var (
  247. descFormatID int64
  248. formatDesc string
  249. )
  250. v = &archive.VideoInfo{}
  251. row := d.rddb.QueryRow(c, _videoInfo, aid, cid)
  252. if err = row.Scan(&v.ID, &v.AID, &v.Eptitle, &v.Description, &v.CID, &v.Epctime, &v.Filename, &v.XcodeState, &v.Playurl,
  253. &v.Ctime, &v.Author, &v.Title, &v.Tag, &v.Content, &v.Cover, &v.Typeid, &v.MID, &v.Copyright,
  254. &v.Source, &v.Dynamic, &descFormatID, &formatDesc); err != nil {
  255. if err == sql.ErrNoRows {
  256. err = nil
  257. v = nil
  258. } else {
  259. log.Error("VideoInfo row.Scan error(%v) aid(%d) cid(%d)", err, aid, cid)
  260. }
  261. return
  262. }
  263. if descFormatID > 0 {
  264. v.Content = formatDesc
  265. }
  266. return
  267. }
  268. //VideoRelated related videos
  269. func (d *Dao) VideoRelated(c context.Context, aid int64) (vs []*archive.RelationVideo, err error) {
  270. var rows *sql.Rows
  271. vs = []*archive.RelationVideo{}
  272. if rows, err = d.rddb.Query(c, _videoRelated, aid); err != nil {
  273. log.Error("VideoRelated d.rddb.Query error(%v) aid(%d)", err, aid)
  274. return
  275. }
  276. defer rows.Close()
  277. for rows.Next() {
  278. v := &archive.RelationVideo{}
  279. if err = rows.Scan(&v.Filename, &v.Status, &v.AID, &v.IndexOrder, &v.Title, &v.Ctime); err != nil {
  280. log.Error("VideoRelated rows.Scan error(%v) aid(%d)", err, aid)
  281. return
  282. }
  283. vs = append(vs, v)
  284. }
  285. return
  286. }