package consume import ( "applet/app/cfg" utils2 "applet/app/utils" "applet/app/utils/logx" "applet/consume/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum" md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "context" "encoding/json" "errors" "fmt" "github.com/olivere/elastic/v7" "github.com/streadway/amqp" "strings" "time" ) func EggEnergyDealUserECPMConsume(queue md.MqQueue) { fmt.Println(">>>>>>>>>>>>EggEnergyDealUserECPMConsume>>>>>>>>>>>>") 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) egg_system_rules.Init(cfg.RedisAddr) var res amqp.Delivery var ok bool for { res, ok = <-delivery if ok == true { err = handleEggEnergyDealUserECPMConsume(res.Body, ch) if err != nil { fmt.Println("EggEnergyDealUserECPMConsume_ERR:::::", err.Error()) utils2.FilePutContents("EggEnergyDealUserECPMConsume_ERR", utils2.SerializeStr(map[string]interface{}{ "body": res.Body, "err": err.Error(), })) } //_ = res.Reject(false) err = res.Ack(true) if err != nil { fmt.Println("err ::: ", err) } } else { panic(errors.New("error getting message")) } } } func handleEggEnergyDealUserECPMConsume(msgData []byte, ch *rabbit.Channel) error { //1、解析mq中queue的数据结构体 var msg *md2.DealUserEcpmReq err := json.Unmarshal(msgData, &msg) if err != nil { return err } //2、更新用户信息 year, week := time.Now().ISOWeek() yearStr := utils2.IntToStr(year) weekStr := utils2.IntToStr(week) index := es2.GetAppointIndexFromAlias(yearStr, weekStr) id := fmt.Sprintf("%d%d_%d", year, week, msg.Uid) doc, err := es.FirstDoc(index, id) if err != nil { if !elastic.IsNotFound(err) { return err } else { now := time.Now().Format("2006-01-02 15:04:05") err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, msg.Uid, enum.Ecpm, msg.Ecpm, now) if err1 != nil { return err1 } return nil } } else { var score md2.EggEnergyUserEggScoreEs err = json.Unmarshal(doc.Source, &score) if err != nil { fmt.Println("handleTemporaryEggEnergyDealUserECPMConsume failed to unmarshal", err.Error()) return err } ecpm := score.Ecpm ecpmNums := score.EcpmNums ecpmNums++ ecpm += utils2.StrToFloat64(msg.Ecpm) ecpmAvg := ecpm / float64(ecpmNums) _, err = es.EsClient.Update(). Index(index). Id(id). IfSeqNo(*doc.SeqNo). IfPrimaryTerm(*doc.PrimaryTerm). Doc(map[string]interface{}{ "ecpm": ecpm, "ecpm_avg": ecpmAvg, "ecpm_nums": ecpmNums, }).Do(context.Background()) if err != nil { fmt.Println("update ecpm err ==========>", err.Error()) if strings.Contains(err.Error(), "elastic: Error 409") { ch.Publish(md3.EggEnergyExchange, msg, md3.EggEnergyRoutKeyForDealUserEcpm) fmt.Println("Successfully pushed to the end of the queue:", md3.EggEnergyRoutKeyForDealUserEcpm, msg) } return err } } return err }