intervs.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package service
  2. import (
  3. "go-common/app/admin/main/tv/model"
  4. "go-common/library/database/sql"
  5. "go-common/library/log"
  6. )
  7. // 0=not found, 1=pgc, 2=cms, 3=license
  8. const (
  9. ErrNotFound = 0
  10. _TypeDefault = 0
  11. _TypePGC = 1
  12. _TypeUGC = 2
  13. )
  14. func (s *Service) getSeason(sid int64) (res *model.TVEpSeason, err error) {
  15. sn := model.TVEpSeason{}
  16. if err = s.DB.Where("id = ?", sid).First(&sn).Error; err != nil {
  17. if err == sql.ErrNoRows {
  18. err = nil
  19. return
  20. }
  21. log.Error("GetSeason error(%v)\n", err)
  22. return
  23. }
  24. return &sn, nil
  25. }
  26. // RemoveInvalids removes invalid interventions
  27. func (s *Service) RemoveInvalids(invalids []*model.RankError) (err error) {
  28. tx := s.DB.Begin()
  29. for _, v := range invalids {
  30. if err = tx.Model(&model.Rank{}).Where("id=?", v.ID).Update(map[string]int{"is_deleted": 1}).Error; err != nil {
  31. log.Error("tvSrv.RemoveInvalids error(%v)", err)
  32. tx.Rollback()
  33. return
  34. }
  35. }
  36. tx.Commit()
  37. log.Info("Remove Invalid Interventions: %d", len(invalids))
  38. return
  39. }
  40. // Intervs pick the intervention and combine the season data
  41. func (s *Service) Intervs(req *model.IntervListReq) (res *model.RankList, err error) {
  42. var (
  43. intervs []*model.Rank
  44. items []*model.SimpleRank
  45. invalids []*model.RankError
  46. db = req.BuildDB(s.DB).Order("position asc")
  47. )
  48. if err = db.Find(&intervs).Error; err != nil {
  49. log.Error("[Intervs] DB query fail(%v)", err)
  50. return
  51. }
  52. items, invalids = s.intervsValid(intervs)
  53. err = s.RemoveInvalids(invalids)
  54. res = &model.RankList{
  55. List: items,
  56. }
  57. return
  58. }
  59. func (s *Service) intervsValid(intervs []*model.Rank) (items []*model.SimpleRank, invalids []*model.RankError) {
  60. // check Its Season Status, pick invalid ones
  61. for _, v := range intervs {
  62. switch v.ContType {
  63. case _TypePGC, _TypeDefault:
  64. isValid, sn := s.snValid(v.ContID)
  65. if !isValid {
  66. invalids = append(invalids, v.BeError())
  67. continue
  68. }
  69. items = append(items, v.BeSimpleSn(sn, s.pgcCatToName))
  70. case _TypeUGC:
  71. isValid, arc := s.arcValid(v.ContID)
  72. if !isValid {
  73. invalids = append(invalids, v.BeError())
  74. continue
  75. }
  76. items = append(items, v.BeSimpleArc(arc, s.arcPName))
  77. default:
  78. log.Error("[Intervs] Rank Error Cont_Type %d, RankID:%d", v.ContType, v.ID)
  79. continue
  80. }
  81. }
  82. return
  83. }
  84. // snValid Distinguish whether the Season is existing and valid
  85. func (s *Service) snValid(sid int64) (res bool, season *model.TVEpSeason) {
  86. var err error
  87. if season, err = s.getSeason(sid); err != nil || season == nil {
  88. return
  89. }
  90. res = errTyping(int(season.Check), int(season.Valid), int(season.IsDeleted))
  91. return
  92. }
  93. // arcValid Distinguish whether the archive is existing and valid
  94. func (s *Service) arcValid(aid int64) (res bool, arc *model.SimpleArc) {
  95. var err error
  96. if arc, err = s.ExistArc(aid); err != nil || arc == nil {
  97. return
  98. }
  99. res = errTyping(arc.Result, arc.Valid, arc.Deleted)
  100. return
  101. }
  102. func errTyping(check, valid, isDeleted int) (res bool) {
  103. if check == 1 && valid == 1 && isDeleted == 0 {
  104. return true
  105. }
  106. return false
  107. }
  108. // RefreshIntervs is used to delete the previous interventions
  109. func (s *Service) RefreshIntervs(req *model.IntervPubReq) (invalid *model.RankError, err error) {
  110. var (
  111. tx = s.DB.Begin()
  112. txDel = req.BuildDB(tx)
  113. position = 1
  114. title string
  115. )
  116. if err = txDel.Delete(&model.Rank{}).Error; err != nil { // delete old intervs
  117. log.Error("Del Previsou Intervs error(%v)\n", err)
  118. tx.Rollback()
  119. return
  120. }
  121. for _, v := range req.Items {
  122. if invalid, title = s.checkInterv(req, v); invalid != nil {
  123. tx.Rollback()
  124. return
  125. }
  126. if err = tx.Create(v.BeComplete(req, title, position)).Error; err != nil { // create new ones
  127. log.Error("Create New Intervs %v ,Error(%v)\n", v, err)
  128. tx.Rollback()
  129. return
  130. }
  131. position = position + 1
  132. }
  133. tx.Commit()
  134. log.Info("RefreshIntervs Success")
  135. return
  136. }
  137. // checkInterv checks whether the to-publish intervention is valid
  138. func (s *Service) checkInterv(req *model.IntervPubReq, v *model.SimpleRank) (invalid *model.RankError, title string) {
  139. var (
  140. isValid bool
  141. sn *model.TVEpSeason
  142. arc *model.SimpleArc
  143. rankErr = &model.RankError{
  144. ID: int(v.ID),
  145. SeasonID: int(v.ContID),
  146. }
  147. )
  148. if req.IsIdx() {
  149. if int(req.Category) != v.ContType+model.RankIdxBase { // if ugc, we can't accept pgc data
  150. isValid = false
  151. return rankErr, ""
  152. }
  153. }
  154. switch v.ContType {
  155. case _TypePGC, _TypeDefault:
  156. isValid, sn = s.snValid(v.ContID)
  157. if isValid && req.IsIdx() { // if index, check it's the pgc category's season
  158. isValid = (sn.Category == int(req.Rank))
  159. }
  160. case _TypeUGC:
  161. isValid, arc = s.arcValid(v.ContID)
  162. if isValid && req.IsIdx() { // if index, check it's the first level type's archive
  163. pid := s.GetArchivePid(arc.TypeID)
  164. isValid = pid == int32(req.Rank)
  165. }
  166. default:
  167. log.Error("[Intervs] Rank Error Cont_Type %d, RankID:%d", v.ContType, v.ID)
  168. return rankErr, ""
  169. }
  170. if !isValid {
  171. log.Error("snValid (%d) Not passed", v.ContID)
  172. return rankErr, ""
  173. }
  174. if v.ContType == _TypeUGC && arc != nil {
  175. title = arc.Title
  176. }
  177. if (v.ContType == _TypePGC || v.ContType == _TypeDefault) && sn != nil {
  178. title = sn.Title
  179. }
  180. return
  181. }
  182. func (s *Service) pgcCatToName(cat int) (res string) {
  183. if res, ok := s.pgcCatName[cat]; ok {
  184. return res
  185. }
  186. return ""
  187. }