|
- package consume
-
- import (
- "applet/app/cfg"
- "applet/app/db"
- "applet/app/utils"
- md2 "applet/es/md"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
-
- "applet/app/utils/logx"
- "applet/consume/md"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/streadway/amqp"
- "time"
- )
-
- func EggEnergyUserActivityConsume(queue md.MqQueue) {
- fmt.Println(">>>>>>>>>>>>EggEnergyUserActivityConsume>>>>>>>>>>>>")
- 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 = handleEggEnergyUserActivityConsume(res.Body, ch)
- err = res.Ack(true)
- fmt.Println("err ::: ", err)
- } else {
- panic(errors.New("error getting message"))
- }
- }
- fmt.Println("get msg done")
- }
-
- func handleEggEnergyUserActivityConsume(msg []byte, ch *rabbit.Channel) error {
- time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
- var canalMsg *md.CanalEggEnergyUserActivityMessage[md.CanalEggEnergyUserActivity]
- err := json.Unmarshal(msg, &canalMsg)
- if err != nil {
- fmt.Println("EggCanalInviteUserNumsConsumeFaliedUnMarshal_ERR:::::", err.Error())
- return nil
- }
- if canalMsg.Type == md2.CanalMsgInsertSqlType {
- for _, item := range canalMsg.Data {
- userDb := implement.NewUserDb(db.Db)
- user, _ := userDb.GetUser(utils.StrToInt64(item.Uid))
- if user == nil {
- continue
- }
- if user.ParentUid == 0 {
- continue
- }
- count := rule.ExtendUserCount(db.Db, int(user.ParentUid))
- if count > 1000 {
- msg1 := md.CommUserId{
- Uid: utils.Int64ToStr(user.ParentUid),
- }
- ch.Publish("egg.app", msg1, "egg_slow_auto_up_lv")
- continue
- }
- rule.UserUpgradeInsert(db.Db, int(user.ParentUid))
- }
- }
-
- return nil
- }
|