arc_audit.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "go-common/app/admin/main/tv/model"
  4. arcmdl "go-common/app/service/main/archive/api"
  5. "go-common/app/service/main/archive/model/archive"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. )
  10. func arcNormal(state int32) bool {
  11. if state >= 0 || state == -6 { // archive can play
  12. return true
  13. }
  14. return false
  15. }
  16. //AddArcs is used for adding archive
  17. func (s *Service) AddArcs(aids []int64) (res *model.AddResp, err error) {
  18. var (
  19. valid bool
  20. arc *model.SimpleArc
  21. errFmt = "AddArcs %d, Error %v"
  22. )
  23. res = &model.AddResp{
  24. Succ: []int64{},
  25. Invalids: []int64{},
  26. Exist: []int64{},
  27. }
  28. for _, v := range aids {
  29. if valid, err = s.CheckArc(v); err != nil {
  30. log.Error(errFmt, v, err)
  31. return
  32. }
  33. // not valid aids
  34. if !valid {
  35. res.Invalids = append(res.Invalids, v)
  36. continue
  37. }
  38. if arc, err = s.ExistArc(v); err != nil {
  39. log.Error(errFmt, v, err)
  40. return
  41. }
  42. // in our DB, already exist aids
  43. if arc != nil {
  44. res.Exist = append(res.Exist, v)
  45. continue
  46. }
  47. if err = s.dao.NeedImport(v); err != nil {
  48. log.Error(errFmt, v, err)
  49. return
  50. }
  51. // added succesfully aids
  52. res.Succ = append(res.Succ, v)
  53. }
  54. return
  55. }
  56. // CheckArc checks whether the archive is able to play and existing in Archive DB
  57. func (s *Service) CheckArc(aid int64) (ok bool, err error) {
  58. var (
  59. argAid2 = &arcmdl.ArcRequest{Aid: aid}
  60. arcReply *arcmdl.ArcReply
  61. )
  62. if arcReply, err = s.arcClient.Arc(ctx, argAid2); err != nil {
  63. if ecode.NothingFound.Equal(err) { // archive not found at all
  64. err = nil
  65. return
  66. }
  67. log.Error("s.arcRPC.Archive3(%v) error(%v)", argAid2, err)
  68. return
  69. }
  70. arc := arcReply.Arc
  71. if s.Contains(arc.TypeID) { // filter pgc types
  72. ok = false
  73. return
  74. }
  75. if arc.Copyright != 1 {
  76. ok = false
  77. return
  78. }
  79. if arc.Rights.UGCPay == archive.AttrYes {
  80. ok = false
  81. return
  82. }
  83. if arcNormal(arc.State) {
  84. ok = true
  85. }
  86. return
  87. }
  88. // ExistArc checks whether the archive is already in our TV DB, which means no need to import again
  89. func (s *Service) ExistArc(aid int64) (res *model.SimpleArc, err error) {
  90. var arc = model.SimpleArc{}
  91. if err = s.DB.Where("aid = ?", aid).Where("deleted = ?", 0).First(&arc).Error; err != nil {
  92. if err == gorm.ErrRecordNotFound {
  93. err = nil
  94. res = nil
  95. return
  96. }
  97. log.Error("ExistArc DB Error %v", err)
  98. return
  99. }
  100. return &arc, nil
  101. }