广告涉及的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.
 
 
 
 
 
 

117 lines
3.3 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. if err != nil {
  42. fmt.Println("AdOriginalDataApplication_ERR:::::", err.Error())
  43. _ = res.Reject(true) //TODO::拒绝 Ack
  44. //_ = res.Reject(false)
  45. var msg interface{}
  46. json.Unmarshal(res.Body, &msg)
  47. if err != nil {
  48. //TODO::重新推回队列末尾,避免造成队列堵塞
  49. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  50. }
  51. } else {
  52. err = res.Ack(true)
  53. }
  54. } else {
  55. panic(errors.New("error getting message"))
  56. }
  57. }
  58. fmt.Println("get msg done")
  59. }
  60. func handleAdOriginalDataApplication(msgData []byte) error {
  61. //1、解析mq中queue的数据结构体
  62. var msg md2.ZhiosAdOriginalDataApplication
  63. err := json.Unmarshal(msgData, &msg)
  64. if err != nil {
  65. return err
  66. }
  67. time.Sleep(time.Microsecond * 100) // 等待100毫秒
  68. fmt.Println("handleAdOriginalDataApplication:::::::::::>>>>>>>>>")
  69. fmt.Println(msg)
  70. if db.DBs[msg.Mid] == nil {
  71. return nil
  72. }
  73. eg := db.DBs[msg.Mid]
  74. wxApiService, err := wechat.NewWxApiService(msg.Mid, msg.ComponentAppid, msg.ComponentAppsecret)
  75. if err != nil {
  76. return err
  77. }
  78. err, args := wxApiService.GetAdposDetail(msg.AppId, 1, 1, msg.Date, msg.Date, msg.AdId)
  79. if err != nil {
  80. return err
  81. }
  82. for _, v := range args.List {
  83. count, _ := db.Db.Where("uuid=? and date=? and app_id=? and slot_id=?", msg.Mid, msg.Date, v.AppId, v.AdUnitId).Count(&model.OriginalWxAdData{})
  84. if count > 0 {
  85. continue
  86. }
  87. tmp := model.OriginalWxAdData{
  88. Uuid: utils.StrToInt(msg.Mid),
  89. MediumId: 0,
  90. AppId: v.AppId,
  91. SlotId: v.AdUnitId,
  92. AdSlot: v.StatItem.AdSlot,
  93. Date: msg.Date,
  94. ReqSuccCount: int(v.StatItem.ReqSuccCount),
  95. ExposureCount: int(v.StatItem.ExposureCount),
  96. ExposureRate: utils.Float64ToStr(v.StatItem.ExposureRate),
  97. ClickCount: int(v.StatItem.ClickCount),
  98. ClickRate: utils.Float64ToStr(v.StatItem.ClickRate),
  99. PublisherIncome: int(v.StatItem.PublisherIncome),
  100. Ecpm: utils.Float64ToStr(v.StatItem.Ecpm),
  101. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  102. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  103. Platform: "wx_applet",
  104. }
  105. eg.Insert(&tmp)
  106. }
  107. if msg.IsEnd == "1" {
  108. cache.SetEx(msg.Mid+":original.wx.ad.data", "0", 5)
  109. }
  110. return nil
  111. }