archive.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package result
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/archive/model/archive"
  6. "go-common/app/job/main/archive/model/result"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _arcSQL = "SELECT aid,mid,typeid,videos,copyright,title,cover,content,duration,attribute,state,access,pubtime,ctime,mission_id,order_id,redirect_url,forward,dynamic FROM archive WHERE aid=?"
  12. _inArchiveSQL = `INSERT IGNORE INTO archive (aid,mid,typeid,videos,title,cover,content,duration,attribute,copyright,access,pubtime,state,mission_id,order_id,redirect_url,forward,dynamic,cid,dimensions)
  13. VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
  14. _upArchiveSQL = "UPDATE archive SET mid=?,typeid=?,videos=?,title=?,cover=?,content=?,duration=?,attribute=?,copyright=?,access=?,pubtime=?,state=?,mission_id=?,order_id=?,redirect_url=?,mtime=?,forward=?,dynamic=?,cid=?,dimensions=? WHERE aid=?"
  15. _delArchiveSQL = "UPDATE archive SET state=? WHERE aid=?"
  16. _upPassedSQL = "SELECT aid FROM archive WHERE mid=? AND state>=0 ORDER BY pubtime DESC"
  17. )
  18. // UpPassed is
  19. func (d *Dao) UpPassed(c context.Context, mid int64) (aids []int64, err error) {
  20. rows, err := d.db.Query(c, _upPassedSQL, mid)
  21. if err != nil {
  22. log.Error("d.db.Query(%s, %d) error(%v)", _upPassedSQL, mid, err)
  23. return
  24. }
  25. defer rows.Close()
  26. for rows.Next() {
  27. var aid int64
  28. if err = rows.Scan(&aid); err != nil {
  29. log.Error("rows.Scan(%d) error(%v)", aid, err)
  30. return
  31. }
  32. aids = append(aids, aid)
  33. }
  34. err = rows.Err()
  35. return
  36. }
  37. // Archive get a archive by aid.
  38. func (d *Dao) Archive(c context.Context, aid int64) (a *result.Archive, err error) {
  39. row := d.db.QueryRow(c, _arcSQL, aid)
  40. a = &result.Archive{}
  41. if err = row.Scan(&a.AID, &a.Mid, &a.TypeID, &a.Videos, &a.Copyright, &a.Title, &a.Cover, &a.Content, &a.Duration,
  42. &a.Attribute, &a.State, &a.Access, &a.PubTime, &a.CTime, &a.MissionID, &a.OrderID, &a.RedirectURL, &a.Forward, &a.Dynamic); err != nil {
  43. if err == sql.ErrNoRows {
  44. a = nil
  45. err = nil
  46. } else {
  47. log.Error("row.Scan error(%v)", err)
  48. }
  49. }
  50. return
  51. }
  52. // TxAddArchive add archive result
  53. func (d *Dao) TxAddArchive(c context.Context, tx *sql.Tx, a *archive.Archive, ad *archive.Addit, videoCnt int, firstCid int64, dimensions string) (rows int64, err error) {
  54. res, err := tx.Exec(_inArchiveSQL, a.ID, a.Mid, a.TypeID, videoCnt, a.Title, a.Cover, a.Content, a.Duration, a.Attribute, a.Copyright, a.Access, a.PubTime, a.State, ad.MissionID, ad.OrderID, ad.RedirectURL, a.Forward, ad.Dynamic, firstCid, dimensions)
  55. if err != nil {
  56. log.Error("tx.Exec(%s) error(%v)", _inArchiveSQL, err)
  57. return
  58. }
  59. return res.RowsAffected()
  60. }
  61. // TxUpArchive update archive result
  62. func (d *Dao) TxUpArchive(c context.Context, tx *sql.Tx, a *archive.Archive, ad *archive.Addit, videoCnt int, firstCid int64, dimensions string) (rows int64, err error) {
  63. res, err := tx.Exec(_upArchiveSQL, a.Mid, a.TypeID, videoCnt, a.Title, a.Cover, a.Content, a.Duration, a.Attribute, a.Copyright, a.Access, a.PubTime, a.State, ad.MissionID, ad.OrderID, ad.RedirectURL, time.Now(), a.Forward, ad.Dynamic, firstCid, dimensions, a.ID)
  64. if err != nil {
  65. log.Error("tx.Exec(%s) error(%v)", _upArchiveSQL, err)
  66. return
  67. }
  68. return res.RowsAffected()
  69. }
  70. // TxDelArchive delete archive
  71. func (d *Dao) TxDelArchive(c context.Context, tx *sql.Tx, aid int64) (rows int64, err error) {
  72. res, err := tx.Exec(_delArchiveSQL, archive.StateForbidUpDelete, aid)
  73. if err != nil {
  74. log.Error("tx.Execerror(%v)", err)
  75. return
  76. }
  77. return res.RowsAffected()
  78. }