cpm.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package model
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "go-common/library/log"
  6. )
  7. // ADRequest cpm type
  8. type ADRequest struct {
  9. RequestID string `json:"request_id"`
  10. ADIndexs map[string]map[string]*ADIndex `json:"ads_info"`
  11. }
  12. // ADIndex cpm type
  13. type ADIndex struct {
  14. Index int `json:"index"`
  15. Info *ADInfo `json:"ad_info"`
  16. IsAd bool `json:"is_ad"`
  17. CmMark int `json:"cm_mark"`
  18. }
  19. // ADInfo cpm type
  20. type ADInfo struct {
  21. CreativeID int `json:"creative_id"`
  22. CreativeType int `json:"creative_type"`
  23. CreativeContent struct {
  24. Title string `json:"title"`
  25. Desc string `json:"description"`
  26. VideoID int64 `json:"video_id"`
  27. UserName string `json:"username"`
  28. ImageURL string `json:"image_url"`
  29. ImageMD5 string `json:"image_md5"`
  30. LogURL string `json:"log_url"`
  31. LogMD5 string `json:"log_md5"`
  32. URL string `json:"url"`
  33. ClickURL string `json:"click_url"`
  34. ShowURL string `json:"show_url"`
  35. } `json:"creative_content"`
  36. AdCb string `json:"ad_cb"`
  37. Extra json.RawMessage `json:"extra"`
  38. }
  39. // ConvertBanner expair cpm type
  40. func (adr *ADRequest) ConvertBanner(ip, mobiApp string, build int) (banners map[int]map[int]*Banner) {
  41. banners = map[int]map[int]*Banner{}
  42. for resIDStr, sAdis := range adr.ADIndexs {
  43. resID, _ := strconv.Atoi(resIDStr)
  44. if len(sAdis) == 0 {
  45. log.Info("mobi_app:%v-build:%v-resource:%v-is_ad_loc:%v", mobiApp, build, resID, false)
  46. continue
  47. }
  48. for sidStr, adi := range sAdis {
  49. sid, _ := strconv.Atoi(sidStr)
  50. var bnnr = &Banner{
  51. IsAdLoc: true,
  52. IsAd: adi.IsAd,
  53. IsAdReplace: false,
  54. CmMark: adi.CmMark,
  55. Rank: adi.Index,
  56. SrcId: sid,
  57. RequestId: adr.RequestID,
  58. ClientIp: ip,
  59. }
  60. if adInfo := adi.Info; adInfo != nil {
  61. bnnr.IsAdReplace = true
  62. bnnr.CreativeId = adInfo.CreativeID
  63. bnnr.AdCb = adInfo.AdCb
  64. bnnr.ShowUrl = adInfo.CreativeContent.ShowURL
  65. bnnr.ClickUrl = adInfo.CreativeContent.ClickURL
  66. bnnr.Title = adInfo.CreativeContent.Title
  67. bnnr.Image = adInfo.CreativeContent.ImageURL
  68. bnnr.Hash = adInfo.CreativeContent.ImageMD5
  69. bnnr.URI = adInfo.CreativeContent.URL
  70. bnnr.Channel = "*"
  71. bnnr.Extra = adInfo.Extra
  72. bnnr.ServerType = 1
  73. }
  74. if _, ok := banners[resID]; ok {
  75. banners[resID][bnnr.Rank] = bnnr
  76. } else {
  77. banners[resID] = map[int]*Banner{
  78. bnnr.Rank: bnnr,
  79. }
  80. }
  81. log.Info("mobi_app:%v-build:%v-source:%v-resource:%v-is_ad_loc:%v", mobiApp, build, sid, resID, true)
  82. }
  83. }
  84. return
  85. }