shelve.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package cms
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. "go-common/library/xstr"
  8. )
  9. const (
  10. _validSns = "SELECT DISTINCT a.id FROM tv_ep_season a LEFT JOIN tv_content b ON a.id = b.season_id " +
  11. "WHERE a.is_deleted = 0 AND a.`check` = ? AND b.is_deleted = 0 AND b.valid = ? AND b.state = ?"
  12. _allPassedSns = "SELECT id, valid FROM tv_ep_season WHERE is_deleted = 0 AND `check` = 1"
  13. _actSns = "UPDATE tv_ep_season SET valid = ? WHERE id IN (%s)"
  14. _offArcs = "SELECT aid FROM ugc_archive WHERE aid IN (%s) AND valid = 0 AND deleted = 0 AND result = 1 "
  15. _reshelfArcs = "UPDATE ugc_archive SET valid = 1 WHERE aid IN (%s)"
  16. _cmsOnline = 1
  17. _cmsOffline = 0
  18. _epPassed = 3
  19. )
  20. // ValidSns gets all the seasons that should be on the shelves, which includes free and audited episodes.
  21. func (d *Dao) ValidSns(ctx context.Context, onlyfree bool) (res map[int64]int, err error) {
  22. var (
  23. rows *sql.Rows
  24. validSql = _validSns
  25. )
  26. res = make(map[int64]int)
  27. if onlyfree {
  28. validSql = validSql + " AND b.pay_status = 2" // free episode
  29. }
  30. if rows, err = d.DB.Query(ctx, validSql, _cmsOnline, _cmsOnline, _epPassed); err != nil {
  31. log.Error("d.ValidSns.Query: %s error(%v)", validSql, err)
  32. return
  33. }
  34. defer rows.Close()
  35. for rows.Next() {
  36. var sid int64
  37. if err = rows.Scan(&sid); err != nil {
  38. log.Error("ValidSns row.Scan() error(%v)", err)
  39. return
  40. }
  41. res[sid] = 1
  42. }
  43. if err = rows.Err(); err != nil {
  44. log.Error("d.PgcCont.Query error(%v)", err)
  45. }
  46. return
  47. }
  48. // ShelveOp gets the status of all audited seasons on and off shelves, and compare the results with the "ValidSns" method above to determine which episodes need to be on or off shelves.
  49. func (d *Dao) ShelveOp(ctx context.Context, validSns map[int64]int) (onIDs, offIDs []int64, err error) {
  50. var rows *sql.Rows
  51. if rows, err = d.DB.Query(ctx, _allPassedSns); err != nil {
  52. log.Error("d.ShelveOp.Query: %s error(%v)", _allPassedSns, err)
  53. return
  54. }
  55. defer rows.Close()
  56. for rows.Next() {
  57. var sid, valid int64
  58. if err = rows.Scan(&sid, &valid); err != nil {
  59. log.Error("ValidSns row.Scan() error(%v)", err)
  60. return
  61. }
  62. _, ok := validSns[sid]
  63. if ok && valid == 0 {
  64. onIDs = append(onIDs, sid)
  65. }
  66. if !ok && valid == 1 {
  67. offIDs = append(offIDs, sid)
  68. }
  69. }
  70. if err = rows.Err(); err != nil {
  71. log.Error("d.PgcCont.Query error(%v)", err)
  72. }
  73. return
  74. }
  75. // ActOps carries out the action on the season which need to be on/off shelves
  76. func (d *Dao) ActOps(ctx context.Context, ids []int64, on bool) (err error) {
  77. var action int
  78. if on {
  79. action = _cmsOnline
  80. } else {
  81. action = _cmsOffline
  82. }
  83. if _, err = d.DB.Exec(ctx, fmt.Sprintf(_actSns, xstr.JoinInts(ids)), action); err != nil {
  84. log.Error("ActOps, Ids %v, Err %v", ids, err)
  85. }
  86. return
  87. }
  88. // OffArcs takes the archives that passed but cms invalid archives
  89. func (d *Dao) OffArcs(ctx context.Context, aids []int64) (offAids []int64, err error) {
  90. var rows *sql.Rows
  91. if rows, err = d.DB.Query(ctx, fmt.Sprintf(_offArcs, xstr.JoinInts(aids))); err != nil {
  92. return
  93. }
  94. defer rows.Close()
  95. for rows.Next() {
  96. var aid int64
  97. if err = rows.Scan(&aid); err != nil {
  98. return
  99. }
  100. offAids = append(offAids, aid)
  101. }
  102. err = rows.Err()
  103. return
  104. }
  105. // ReshelfArcs re-put the arcs onshelf ( CMS valid )
  106. func (d *Dao) ReshelfArcs(ctx context.Context, aids []int64) (err error) {
  107. _, err = d.DB.Exec(ctx, fmt.Sprintf(_reshelfArcs, xstr.JoinInts(aids)))
  108. return
  109. }