广告涉及的mq都放这里
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
3.1 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/lib/wechat"
  5. md2 "applet/app/md"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. "applet/app/utils/logx"
  9. "applet/consume/md"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  11. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles"
  12. db "code.fnuoos.com/zhimeng/model.git/src"
  13. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  14. "encoding/json"
  15. "errors"
  16. "fmt"
  17. "github.com/streadway/amqp"
  18. "time"
  19. )
  20. func AdOriginalDataApplication(queue md.MqQueue) {
  21. fmt.Println(">>>>>>>>>>>>AdOriginalDataApplication>>>>>>>>>>>>")
  22. ch, err := rabbit.Cfg.Pool.GetChannel()
  23. if err != nil {
  24. logx.Error(err)
  25. return
  26. }
  27. defer ch.Release()
  28. //1、将自己绑定到交换机上
  29. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  30. //2、取出数据进行消费
  31. ch.Qos(1)
  32. delivery := ch.Consume(queue.Name, false)
  33. one_circles.Init(cfg.RedisAddr)
  34. var res amqp.Delivery
  35. var ok bool
  36. for {
  37. res, ok = <-delivery
  38. if ok == true {
  39. err = handleAdOriginalDataApplication(res.Body)
  40. fmt.Println("err ::: ", err)
  41. err = res.Ack(true)
  42. } else {
  43. panic(errors.New("error getting message"))
  44. }
  45. }
  46. fmt.Println("get msg done")
  47. }
  48. func handleAdOriginalDataApplication(msgData []byte) error {
  49. //1、解析mq中queue的数据结构体
  50. var msg md2.ZhiosAdOriginalDataApplication
  51. var tmpString string
  52. err := json.Unmarshal(msgData, &tmpString)
  53. if err != nil {
  54. fmt.Println(err.Error())
  55. return err
  56. }
  57. fmt.Println(tmpString)
  58. err = json.Unmarshal([]byte(tmpString), &msg)
  59. if err != nil {
  60. return err
  61. }
  62. if msg.IsEnd == "1" {
  63. cache.SetEx(msg.Mid+":original.wx.ad.data", "0", 5)
  64. }
  65. time.Sleep(time.Microsecond * 100) // 等待100毫秒
  66. fmt.Println("handleAdOriginalDataApplication:::::::::::>>>>>>>>>")
  67. fmt.Println(msg)
  68. if db.DBs[msg.Mid] == nil {
  69. return nil
  70. }
  71. eg := db.DBs[msg.Mid]
  72. wxApiService, err := wechat.NewWxApiService(msg.Mid, msg.ComponentAppid, msg.ComponentAppsecret)
  73. if err != nil {
  74. return err
  75. }
  76. err, args := wxApiService.GetAdposDetail(msg.AppId, 1, 1, msg.Date, msg.Date, msg.AdId)
  77. if err != nil {
  78. return err
  79. }
  80. for _, v := range args.List {
  81. count, _ := db.Db.Where("uuid=? and date=? and app_id=? and slot_id=?", msg.Mid, msg.Date, v.AppId, v.AdUnitId).Count(&model.OriginalWxAdData{})
  82. if count > 0 {
  83. continue
  84. }
  85. tmp := model.OriginalWxAdData{
  86. Uuid: utils.StrToInt(msg.Mid),
  87. MediumId: 0,
  88. AppId: v.AppId,
  89. SlotId: v.AdUnitId,
  90. AdSlot: v.StatItem.AdSlot,
  91. Date: msg.Date,
  92. ReqSuccCount: int(v.StatItem.ReqSuccCount),
  93. ExposureCount: int(v.StatItem.ExposureCount),
  94. ExposureRate: utils.Float64ToStr(v.StatItem.ExposureRate),
  95. ClickCount: int(v.StatItem.ClickCount),
  96. ClickRate: utils.Float64ToStr(v.StatItem.ClickRate),
  97. PublisherIncome: int(v.StatItem.PublisherIncome),
  98. Ecpm: utils.Float64ToStr(v.StatItem.Ecpm),
  99. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  100. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  101. Platform: "wx_applet",
  102. }
  103. eg.Insert(&tmp)
  104. }
  105. return nil
  106. }