dynamic.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/web/conf"
  5. "go-common/app/service/main/archive/api"
  6. dymdl "go-common/app/service/main/dynamic/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. )
  11. // DynamicRegion get dynamic region.
  12. func (s *Service) DynamicRegion(c context.Context, rid int32, pn, ps int) (rs *dymdl.DynamicArcs3, err error) {
  13. ip := metadata.String(c, metadata.RemoteIP)
  14. if rs, err = s.dy.RegionArcs3(c, &dymdl.ArgRegion3{RegionID: rid, Pn: pn, Ps: ps, RealIP: ip}); err != nil {
  15. log.Error("s.dy.RegionArcs3(%d,%d,%d) error(%v)", rid, pn, ps, err)
  16. err = nil
  17. } else if rs != nil && len(rs.Archives) > 0 {
  18. fmtArcs3(rs.Archives)
  19. s.cache.Do(c, func(c context.Context) {
  20. s.dao.SetRegionBakCache(c, rid, pn, ps, rs)
  21. })
  22. return
  23. }
  24. if rs, err = s.dao.RegionBakCache(c, rid, pn, ps); err != nil {
  25. return
  26. }
  27. if rs == nil {
  28. err = ecode.NothingFound
  29. }
  30. return
  31. }
  32. func fmtArcs3(arcs []*api.Arc) {
  33. for _, v := range arcs {
  34. if v.Access >= 10000 {
  35. v.Stat.View = -1
  36. }
  37. }
  38. }
  39. // DynamicRegionTag get dynamic region tag.
  40. func (s *Service) DynamicRegionTag(c context.Context, tagID int64, rid int32, pn, ps int) (rs *dymdl.DynamicArcs3, err error) {
  41. ip := metadata.String(c, metadata.RemoteIP)
  42. if rs, err = s.dy.RegionTagArcs3(c, &dymdl.ArgRegionTag3{TagID: tagID, RegionID: rid, Pn: pn, Ps: ps, RealIP: ip}); err != nil {
  43. log.Error("s.dy.RegionTagArcs3(%d,%d,%d,%d) error(%v)", tagID, rid, pn, ps, err)
  44. err = nil
  45. } else if rs != nil && len(rs.Archives) > 0 {
  46. fmtArcs3(rs.Archives)
  47. s.cache.Do(c, func(c context.Context) {
  48. s.dao.SetRegionTagBakCache(c, tagID, rid, pn, ps, rs)
  49. })
  50. return
  51. }
  52. if rs, err = s.dao.RegionTagBakCache(c, tagID, rid, pn, ps); err != nil {
  53. return
  54. }
  55. if rs == nil {
  56. err = ecode.NothingFound
  57. }
  58. return
  59. }
  60. // DynamicRegionTotal get dynamic region total.
  61. func (s *Service) DynamicRegionTotal(c context.Context) (map[string]int, error) {
  62. rs, err := s.dy.RegionTotal(c, &dymdl.ArgRegionTotal{RealIP: metadata.String(c, metadata.RemoteIP)})
  63. if err != nil {
  64. log.Error("s.dy.RegionTotal error(%v)", err)
  65. return nil, err
  66. }
  67. return rs, nil
  68. }
  69. // DynamicRegions get dynamic regions.
  70. func (s *Service) DynamicRegions(c context.Context) (rs map[int32][]*api.Arc, err error) {
  71. var (
  72. rids []int32
  73. common, bg map[int32][]*api.Arc
  74. bgid = int32(13)
  75. ip = metadata.String(c, metadata.RemoteIP)
  76. )
  77. // get first type id
  78. for rid := range s.rids {
  79. if rid == bgid { //bangumi ignore.
  80. continue
  81. } else if rid == 167 { //guochuang use second rid 168.
  82. rid = 168
  83. }
  84. rids = append(rids, rid)
  85. }
  86. rs = make(map[int32][]*api.Arc, len(rids)+1)
  87. if common, err = s.dy.RegionsArcs3(c, &dymdl.ArgRegions3{RegionIDs: rids, Count: 10, RealIP: ip}); err != nil {
  88. log.Error("s.dy.RegionsArcs3(%v) error(%v)", rids, err)
  89. err = nil
  90. }
  91. for _, rid := range rids {
  92. rs[rid] = common[rid]
  93. }
  94. // bangumi type id 13 find 200,condition mid == 928123.
  95. if bg, err = s.dy.RegionsArcs3(c, &dymdl.ArgRegions3{RegionIDs: []int32{bgid}, Count: conf.Conf.Rule.BangumiCount, RealIP: ip}); err != nil {
  96. log.Error("s.dy.RegionsArcs3 error(%v)", err)
  97. err = nil
  98. } else {
  99. n := 1
  100. count := 1
  101. for _, arc := range bg[bgid] {
  102. count++
  103. if arc.Author.Mid == 928123 {
  104. rs[bgid] = append(rs[bgid], arc)
  105. } else {
  106. continue
  107. }
  108. n++
  109. if n > conf.Conf.Rule.RegionsCount {
  110. log.Info("s.dy.RegionsArcs bangumi count(%d)", count)
  111. break
  112. }
  113. }
  114. // not enough add other.
  115. if n <= conf.Conf.Rule.RegionsCount {
  116. for _, arc := range bg[bgid] {
  117. count++
  118. if arc.Author.Mid == 928123 {
  119. continue
  120. } else {
  121. rs[bgid] = append(rs[bgid], arc)
  122. }
  123. n++
  124. if n > conf.Conf.Rule.RegionsCount {
  125. log.Info("s.dy.RegionsArcs bangumi count(%d)", count)
  126. break
  127. }
  128. }
  129. }
  130. }
  131. if len(rs) > 0 {
  132. countCheck := true
  133. for rid, region := range rs {
  134. if len(region) < conf.Conf.Rule.MinDyCount {
  135. countCheck = false
  136. log.Info("countCheck rid(%d) len(%d) false", rid, len(region))
  137. break
  138. }
  139. }
  140. if countCheck {
  141. s.cache.Do(c, func(c context.Context) {
  142. s.dao.SetRegionsBakCache(c, rs)
  143. })
  144. return
  145. }
  146. }
  147. rs, err = s.dao.RegionsBakCache(c)
  148. return
  149. }