service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package resource
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. "go-common/app/interface/main/creative/dao/resource"
  6. "go-common/app/interface/main/creative/dao/tool"
  7. model "go-common/app/interface/main/creative/model/resource"
  8. "go-common/app/interface/main/creative/service"
  9. resmdl "go-common/app/service/main/resource/model"
  10. "go-common/library/log"
  11. "sort"
  12. "strconv"
  13. "time"
  14. )
  15. // Android iPhone
  16. const (
  17. BanneriPhone = 2417
  18. BannerAndroid = 2431
  19. AcademyiPhone = 2873
  20. AcademyAndroid = 2877
  21. BannerCooperate = 2893
  22. )
  23. //Service struct
  24. type Service struct {
  25. c *conf.Config
  26. resDao *resource.Dao
  27. Seed int64
  28. }
  29. //New get service
  30. func New(c *conf.Config, rpcdaos *service.RPCDaos) *Service {
  31. s := &Service{
  32. c: c,
  33. resDao: resource.New(c),
  34. }
  35. return s
  36. }
  37. // TopBanner fn
  38. func (s *Service) TopBanner(c context.Context, mobiApp, device, network, ipAddr, buvid, adExtra string, build, resID int, plat int8, mid int64, isAd bool) (res []*model.Banner, err error) {
  39. var bnsm map[int][]*resmdl.Banner
  40. if resID == 0 {
  41. if model.IsAndroid(plat) {
  42. resID = BannerAndroid
  43. } else if model.IsIPhone(plat) || model.IsIPad(plat) {
  44. resID = BanneriPhone
  45. mobiApp = "iphone"
  46. device = "phone"
  47. plat = resmdl.PlatIPhone
  48. }
  49. }
  50. if bnsm, err = s.resDao.Banner(c, mobiApp, device, network, "", ipAddr, buvid, adExtra, strconv.Itoa(resID), build, plat, mid, isAd); err != nil {
  51. log.Error("s.resDao.Banner err(%v)", err)
  52. return
  53. }
  54. for _, rb := range bnsm[resID] {
  55. b := &model.Banner{}
  56. b.ChangeBanner(rb)
  57. if b.ClientIp == "" {
  58. b.ClientIp = ipAddr
  59. }
  60. res = append(res, b)
  61. }
  62. topLen := 5
  63. if len(res) > topLen {
  64. res = res[:5]
  65. }
  66. sort.Slice(res, func(i, j int) bool { return res[i].Rank < res[j].Rank })
  67. return
  68. }
  69. // AcademyBanner fn
  70. func (s *Service) AcademyBanner(c context.Context, mobiApp, device, network, ipAddr, buvid, adExtra string, build, resID int, plat int8, mid int64, isAd bool) (randomRes []*model.Banner, err error) {
  71. var (
  72. bnsm map[int][]*resmdl.Banner
  73. res = make([]*model.Banner, 0)
  74. keys []int
  75. )
  76. randomRes = make([]*model.Banner, 0)
  77. if model.IsAndroid(plat) {
  78. resID = AcademyAndroid
  79. } else if model.IsIPhone(plat) {
  80. resID = AcademyiPhone
  81. } else if model.IsIPad(plat) {
  82. return
  83. }
  84. if bnsm, err = s.resDao.Banner(c, mobiApp, device, network, "", ipAddr, buvid, adExtra, strconv.Itoa(resID), build, plat, mid, isAd); err != nil {
  85. log.Error("s.resDao.Banner err(%v)", err)
  86. return
  87. }
  88. for _, rb := range bnsm[resID] {
  89. b := &model.Banner{}
  90. b.ChangeBanner(rb)
  91. if b.ClientIp == "" {
  92. b.ClientIp = ipAddr
  93. }
  94. res = append(res, b)
  95. }
  96. sort.Slice(res, func(i, j int) bool { return res[i].Rank < res[j].Rank })
  97. randLength := 3
  98. if len(res) > randLength {
  99. keys = tool.RandomSliceKeys(0, len(res), randLength, time.Now().Unix())
  100. } else {
  101. keys = tool.RandomSliceKeys(0, len(res), len(res), time.Now().Unix())
  102. }
  103. for _, k := range keys {
  104. randomRes = append(randomRes, res[k])
  105. }
  106. return
  107. }
  108. // CooperateBanner fn
  109. func (s *Service) CooperateBanner(c context.Context, mobiApp, device, network, buvid, adExtra string, build int, plat int8, mid int64, isAd bool) (ass []*resmdl.Assignment, err error) {
  110. var res *resmdl.Resource
  111. if res, err = s.resDao.SimpleResource(c, BannerCooperate); err != nil {
  112. log.Error("Resource SimpleResource (%d) error(%v)", BannerCooperate, err)
  113. return
  114. }
  115. if res != nil {
  116. ass = res.Assignments
  117. return
  118. }
  119. return
  120. }