season_sync.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package app
  2. import (
  3. "context"
  4. dsql "database/sql"
  5. "fmt"
  6. "time"
  7. model "go-common/app/job/main/tv/model/pgc"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. // season `check` meaning
  12. const (
  13. SeasonRejected = 0
  14. SeasonPassed = 1
  15. SeasonToReAudit = 2
  16. SeasonAuditing = 3
  17. SeasonAlreadyPassed = 7
  18. EPToAudit = 1
  19. EPAuditing = 2
  20. EPPassed = 3
  21. EPNotPass = 4
  22. _CMSValid = 1
  23. _NotDeleted = 0
  24. )
  25. const (
  26. _modifiedSeason = "SELECT id,origin_name,title,alias,category,`desc`,style,area,play_time,info,state,total_num,upinfo,staff,role,copyright,`check`,is_deleted,cover,producer,version,status" +
  27. " FROM tv_ep_season WHERE `check` = ? AND audit_time < UNIX_TIMESTAMP(now()) AND is_deleted = 0 LIMIT 0,"
  28. _snEmpty = "SELECT id FROM tv_content WHERE season_id = ? AND is_deleted = 0 LIMIT 1"
  29. _auditingSeason = "UPDATE tv_ep_season SET `check` = ? WHERE is_deleted = 0 AND id = ?"
  30. _delaySeason = "UPDATE tv_ep_season SET audit_time = ? WHERE id = ?"
  31. )
  32. // SnEmpty determines whether the
  33. func (d *Dao) SnEmpty(c context.Context, sid int64) (res bool, err error) {
  34. var epid int
  35. if err = d.DB.QueryRow(c, _snEmpty, sid).Scan(&epid); err != nil {
  36. if err == sql.ErrNoRows {
  37. err = nil
  38. res = true
  39. return
  40. }
  41. log.Error("d.SnEmpty Error (%v)", err)
  42. return
  43. }
  44. res = false
  45. return
  46. }
  47. // ModSeason picks the modified season data to sync
  48. func (d *Dao) ModSeason(c context.Context) (res []*model.TVEpSeason, err error) {
  49. var (
  50. rows *sql.Rows
  51. isEmpty bool
  52. )
  53. if rows, err = d.DB.Query(c, _modifiedSeason+fmt.Sprintf("%d", d.conf.Sync.LConf.NbSeason), SeasonToReAudit); err != nil {
  54. log.Error("d._modifiedSeason.Query error(%v)", err)
  55. return
  56. }
  57. defer rows.Close()
  58. for rows.Next() {
  59. var r = &model.TVEpSeason{}
  60. if err = rows.Scan(&r.ID, &r.OriginName, &r.Title, &r.Alias, &r.Category, &r.Desc, &r.Style, &r.Area, &r.PlayTime, &r.Info,
  61. &r.State, &r.TotalNum, &r.Upinfo, &r.Staff, &r.Role, &r.Copyright, &r.Check, &r.IsDeleted, &r.Cover,
  62. &r.Producer, &r.Version, &r.Status); err != nil {
  63. log.Error("modSeason row.Scan() error(%v)", err)
  64. return
  65. }
  66. if isEmpty, err = d.SnEmpty(c, r.ID); err != nil {
  67. log.Error("modSeason SnEmpty Error (%v)", err)
  68. return
  69. }
  70. if !isEmpty { // we don't submit empty season to audit
  71. res = append(res, r)
  72. } else {
  73. d.DelaySeason(c, r.ID)
  74. }
  75. }
  76. if err = rows.Err(); err != nil {
  77. log.Error("d.ModSeason.Query error(%v)", err)
  78. }
  79. return
  80. }
  81. // AuditSeason updates the indicated season to Auditing Status
  82. func (d *Dao) AuditSeason(c context.Context, sid int) (nbRows int64, err error) {
  83. var res dsql.Result
  84. if res, err = d.DB.Exec(c, _auditingSeason, SeasonAuditing, sid); err != nil {
  85. log.Error("_auditingSeason, failed to update to auditing: (%v), Error: %v", sid, err)
  86. return
  87. }
  88. return res.RowsAffected()
  89. }
  90. // DelaySeason postpones the season to sync in 30 minutes
  91. func (d *Dao) DelaySeason(c context.Context, sid int64) (nbRows int64, err error) {
  92. var (
  93. res dsql.Result
  94. delay = time.Now().Unix() + d.conf.Sync.Frequency.AuditDelay
  95. )
  96. if res, err = d.DB.Exec(c, _delaySeason, delay, sid); err != nil {
  97. log.Error("_delaySeason, failed to delay: (%v,%v), Error: %v", err)
  98. return
  99. }
  100. return res.RowsAffected()
  101. }