sync_pgc.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package service
  2. import (
  3. "database/sql"
  4. "go-common/app/admin/main/tv/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "github.com/jinzhu/gorm"
  9. )
  10. const (
  11. _seasonPassed = 1
  12. _seasonToAudit = 2
  13. _epPassed = 3
  14. _epToAudit = 1
  15. _seasonDefault = 4
  16. )
  17. // SeasonCheck manages the season's check status
  18. func (s *Service) SeasonCheck(sn *model.TVEpSeason) (err error) {
  19. var status int
  20. if sn.Check != _seasonPassed {
  21. status = _seasonToAudit
  22. } else {
  23. return
  24. }
  25. if err = s.DB.Model(&model.TVEpSeason{}).Where("id = ?", sn.ID).Update(map[string]int{"check": status}).Error; err != nil {
  26. log.Error("tvSrv.SeasonCheck error(%v)", err)
  27. }
  28. return
  29. }
  30. // EpCheck manages the ep's check status
  31. func (s *Service) EpCheck(ep *model.Content) (err error) {
  32. var status int
  33. if ep.State != _epPassed {
  34. status = _epToAudit
  35. } else {
  36. return // if passed, no need to update the status
  37. }
  38. if err = s.DB.Model(&model.Content{}).Where("id = ?", ep.ID).Update(map[string]int{"state": status}).Error; err != nil {
  39. log.Error("tvSrv.EpCheck error(%v)", err)
  40. }
  41. return
  42. }
  43. // EpDel controls the ep's deletion status, 0 or 1
  44. func (s *Service) EpDel(epid int64, action int) (err error) {
  45. if err = s.DB.Model(&model.TVEpContent{}).Where("id=?", epid).Update(map[string]int{"is_deleted": action}).Error; err != nil {
  46. log.Error("tvSrv.EpDel error(%v)\n", err)
  47. return
  48. }
  49. if err = s.DB.Model(&model.Content{}).Where("epid=?", epid).Update(map[string]int{"is_deleted": action}).Error; err != nil {
  50. log.Error("tvSrv.EpDel error(%v)\n", err)
  51. }
  52. return
  53. }
  54. // SeasonRemove removes the season and its eps
  55. func (s *Service) SeasonRemove(season *model.TVEpSeason) (err error) {
  56. var rows *sql.Rows
  57. // remove season
  58. if err = s.DB.Model(&model.TVEpSeason{}).Where("id = ?", season.ID).Update(map[string]int{"is_deleted": 1}).Error; err != nil {
  59. log.Error("tvSrv.removeSeason error(%v)", err)
  60. return
  61. }
  62. // manage season's check status
  63. if err = s.SeasonCheck(season); err != nil {
  64. return
  65. }
  66. // manage season's eps check status and deletion status
  67. if rows, err = s.DB.Model(&model.Content{}).Where("season_id = ?", season.ID).Select("id, epid, state").Rows(); err != nil {
  68. log.Error("tvSrv.removeSeason error(%v)", err)
  69. return
  70. }
  71. defer rows.Close()
  72. for rows.Next() {
  73. cont := &model.Content{}
  74. if err = rows.Scan(&cont.ID, &cont.EPID, &cont.State); err != nil {
  75. log.Error("rows.Scan error(%v)", err)
  76. continue
  77. }
  78. if err = s.EpDel(int64(cont.EPID), 1); err != nil {
  79. continue
  80. }
  81. if err = s.EpCheck(cont); err != nil {
  82. continue
  83. }
  84. }
  85. return
  86. }
  87. // SnUpdate receives the season update/create info from PGC side and save them in DB
  88. func (s *Service) SnUpdate(c *bm.Context, req *model.TVEpSeason) (err error) {
  89. var (
  90. exist = model.TVEpSeason{}
  91. updateMap = make(map[string]interface{})
  92. reqForm = c.Request.PostForm
  93. )
  94. if err = s.DB.Where("id=?", req.ID).First(&exist).Error; err != nil && err != gorm.ErrRecordNotFound {
  95. log.Error("SnUpdate Sid %d Err %v", req.ID, err)
  96. return
  97. }
  98. if exist.ID <= 0 { // if data not exist, it's brand new data
  99. req.Check = _seasonDefault
  100. if err = s.DB.Create(req).Error; err != nil {
  101. log.Error("tvSrv.createSeason error(%v)", err)
  102. }
  103. return
  104. }
  105. if exist.IsDeleted == 1 { // exist but was deleted
  106. if err = s.DB.Model(&model.TVEpSeason{}).Where("id = ?", req.ID).Update(map[string]int{"is_deleted": 0}).Error; err != nil {
  107. log.Error("tvSrv.removeSeason error(%v)", err)
  108. return
  109. }
  110. }
  111. if updateMap = exist.Updated(reqForm); len(updateMap) == 0 {
  112. log.Warn("SnUpdate Sid %d No change", req.ID)
  113. return
  114. }
  115. if err = s.DB.Model(&model.TVEpSeason{}).Where("id = ?", req.ID).Update(updateMap).Error; err != nil {
  116. log.Error("SnUpdate Sid %d, Update Err %v", req.ID, err)
  117. return
  118. }
  119. log.Info("SnUpdate Sid %d, Update Fields %v Succ", req.ID, updateMap)
  120. return s.SeasonCheck(&exist)
  121. }
  122. // EpAct acts on ep item
  123. func (s *Service) EpAct(c *bm.Context, cid int64, act int) (err error) {
  124. var (
  125. item = &model.Content{}
  126. db = s.DB.Model(&model.Content{})
  127. )
  128. if err = db.Where("state=?", _epPassed).Where("is_deleted=?", 0).First(item, cid).Error; err != nil {
  129. log.Error("query fail(%v)\n", err)
  130. return ecode.RequestErr
  131. }
  132. if err = db.Where("id=?", cid).Update(map[string]int{"valid": act}).Error; err != nil {
  133. log.Error("online error(%v)\n", err)
  134. return ecode.RequestErr
  135. }
  136. return
  137. }