manual.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package ugc
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. ugcmdl "go-common/app/job/main/tv/model/ugc"
  7. arccli "go-common/app/service/main/archive/api"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _manual = "SELECT id,aid FROM ugc_archive WHERE manual = 1 AND retry < UNIX_TIMESTAMP(now()) AND deleted = 0 LIMIT "
  13. _postpone = "UPDATE ugc_archive SET retry = ? WHERE aid = ? AND deleted = 0"
  14. _importFinish = "UPDATE ugc_archive SET manual = 0 WHERE aid = ? AND deleted = 0"
  15. _manualArc = "UPDATE ugc_archive SET videos = ?, mid = ?, typeid = ?, title = ?, cover = ?, content = ?, duration = ?, " +
  16. "copyright = ?, pubtime = ?, state = ?, submit = ? WHERE aid = ? AND deleted = 0"
  17. _autoArc = "REPLACE INTO ugc_archive (videos, mid, typeid, title, cover, content, duration, copyright, pubtime, state, submit, aid) VALUES " +
  18. "(?,?,?,?,?,?,?,?,?,?,?,?)"
  19. _importVideo = "REPLACE INTO ugc_video (aid,cid,eptitle,index_order,duration,description) VALUES (?,?,?,?,?,?)"
  20. )
  21. // TxMnlArc updates the db with data from API
  22. func (d *Dao) TxMnlArc(tx *sql.Tx, arc *ugcmdl.Archive) (err error) {
  23. if _, err = tx.Exec(_manualArc,
  24. arc.Videos, arc.MID, arc.TypeID, arc.Title, arc.Cover, arc.Content, arc.Duration,
  25. arc.Copyright, arc.Pubtime, arc.State, _needSubmit, arc.AID); err != nil {
  26. log.Error("_importArc, failed to update: (%v), Error: %v", arc, err)
  27. }
  28. return
  29. }
  30. // TxAutoArc imports the db an arc
  31. func (d *Dao) TxAutoArc(tx *sql.Tx, arc *ugcmdl.Archive) (err error) {
  32. if _, err = tx.Exec(_autoArc,
  33. arc.Videos, arc.MID, arc.TypeID, arc.Title, arc.Cover, arc.Content, arc.Duration,
  34. arc.Copyright, arc.Pubtime, arc.State, _needSubmit, arc.AID); err != nil {
  35. log.Error("TxAutoArc, failed to update: (%v), Error: %v", arc, err)
  36. }
  37. return
  38. }
  39. // TxMnlVideos updates the db with data from API, if the
  40. func (d *Dao) TxMnlVideos(tx *sql.Tx, view *arccli.ViewReply) (err error) {
  41. for _, v := range view.Pages {
  42. if _, err = tx.Exec(_importVideo, view.Arc.Aid, v.Cid, v.Part, v.Page, v.Duration, v.Desc); err != nil {
  43. log.Error("_importArc, failed to insert: (%v), Error: %v", v, err)
  44. return
  45. }
  46. }
  47. return
  48. }
  49. // TxMnlStatus updates the aid's manual status to 0
  50. func (d *Dao) TxMnlStatus(tx *sql.Tx, aid int64) (err error) {
  51. if _, err = tx.Exec(_importFinish, aid); err != nil {
  52. log.Error("_importFinish, failed to update: (%v), Error: %v", aid, err)
  53. }
  54. return
  55. }
  56. // Manual picks the archives that added manually
  57. func (d *Dao) Manual(c context.Context) (res []*ugcmdl.Archive, err error) {
  58. var rows *sql.Rows
  59. if rows, err = d.DB.Query(c, _manual+fmt.Sprintf("%d", d.conf.UgcSync.Batch.ManualNum)); err != nil {
  60. log.Error("d.Import.Query error(%v)", err)
  61. return
  62. }
  63. defer rows.Close()
  64. for rows.Next() {
  65. var r = &ugcmdl.Archive{}
  66. if err = rows.Scan(&r.ID, &r.AID); err != nil {
  67. log.Error("Manual row.Scan() error(%v)", err)
  68. return
  69. }
  70. res = append(res, r)
  71. }
  72. if err = rows.Err(); err != nil {
  73. log.Error("d.Manual.Query error(%v)", err)
  74. }
  75. return
  76. }
  77. // Ppmnl means postpone manual operation due to some error happened
  78. func (d *Dao) Ppmnl(c context.Context, aid int64) (err error) {
  79. var delay = time.Now().Unix() + int64(d.conf.UgcSync.Frequency.ErrorWait)
  80. if _, err = d.DB.Exec(c, _postpone, delay, aid); err != nil {
  81. log.Error("Ppmnl, failed to delay: (%v,%v), Error: %v", delay, aid, err)
  82. }
  83. return
  84. }