package consume import ( "applet/app/cfg" "applet/app/lib/wechat" md2 "applet/app/md" "applet/app/utils" "applet/app/utils/cache" "applet/app/utils/logx" "applet/consume/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles" db "code.fnuoos.com/zhimeng/model.git/src" "code.fnuoos.com/zhimeng/model.git/src/super/model" "encoding/json" "errors" "fmt" "github.com/streadway/amqp" "time" ) func AdOriginalDataApplication(queue md.MqQueue) { fmt.Println(">>>>>>>>>>>>AdOriginalDataApplication>>>>>>>>>>>>") ch, err := rabbit.Cfg.Pool.GetChannel() if err != nil { logx.Error(err) return } defer ch.Release() //1、将自己绑定到交换机上 ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) //2、取出数据进行消费 ch.Qos(1) delivery := ch.Consume(queue.Name, false) one_circles.Init(cfg.RedisAddr) var res amqp.Delivery var ok bool for { res, ok = <-delivery if ok == true { err = handleAdOriginalDataApplication(res.Body) fmt.Println("err ::: ", err) err = res.Ack(true) } else { panic(errors.New("error getting message")) } } fmt.Println("get msg done") } func handleAdOriginalDataApplication(msgData []byte) error { //1、解析mq中queue的数据结构体 var msg md2.ZhiosAdOriginalDataApplication var tmpString string err := json.Unmarshal(msgData, &tmpString) if err != nil { fmt.Println(err.Error()) return err } fmt.Println(tmpString) err = json.Unmarshal([]byte(tmpString), &msg) if err != nil { return err } if msg.IsEnd == "1" { cache.SetEx(msg.Mid+":original.wx.ad.data", "0", 5) } time.Sleep(time.Microsecond * 100) // 等待100毫秒 fmt.Println("handleAdOriginalDataApplication:::::::::::>>>>>>>>>") fmt.Println(msg) if db.DBs[msg.Mid] == nil { return nil } eg := db.DBs[msg.Mid] wxApiService, err := wechat.NewWxApiService(msg.Mid, msg.ComponentAppid, msg.ComponentAppsecret) if err != nil { return err } err, args := wxApiService.GetAdposDetail(msg.AppId, 1, 1, msg.Date, msg.Date, msg.AdId) if err != nil { return err } for _, v := range args.List { count, _ := db.Db.Where("uuid=? and date=? and app_id=? and slot_id=?", msg.Mid, msg.Date, v.AppId, v.AdUnitId).Count(&model.OriginalWxAdData{}) if count > 0 { continue } tmp := model.OriginalWxAdData{ Uuid: utils.StrToInt(msg.Mid), MediumId: 0, AppId: v.AppId, SlotId: v.AdUnitId, AdSlot: v.StatItem.AdSlot, Date: msg.Date, ReqSuccCount: int(v.StatItem.ReqSuccCount), ExposureCount: int(v.StatItem.ExposureCount), ExposureRate: utils.Float64ToStr(v.StatItem.ExposureRate), ClickCount: int(v.StatItem.ClickCount), ClickRate: utils.Float64ToStr(v.StatItem.ClickRate), PublisherIncome: int(v.StatItem.PublisherIncome), Ecpm: utils.Float64ToStr(v.StatItem.Ecpm), CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), Platform: "wx_applet", } eg.Insert(&tmp) } return nil }