package consume import ( "applet/app/cfg" "applet/app/db" utils2 "applet/app/utils" "applet/app/utils/logx" "applet/consume/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/enum" md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "encoding/json" "errors" "fmt" "github.com/streadway/amqp" "time" ) func EggEnergyStartLevelDividendConsume(queue md.MqQueue) { fmt.Println(">>>>>>>>>>>>EggEnergyStartLevelDividendConsume>>>>>>>>>>>>") 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 = handleEggEnergyStartLevelDividendConsume(res.Body) if err != nil { fmt.Println("EggEnergyStartLevelDividendConsume_ERR:::::", err.Error()) utils2.FilePutContents("EggEnergyStartLevelDividendConsume_ERR", utils2.SerializeStr(map[string]interface{}{ "body": res.Body, "err": err.Error(), })) } //_ = res.Reject(false) err = res.Ack(true) fmt.Println("err ::: ", err) } else { panic(errors.New("error getting message")) } } fmt.Println("get msg done") } func handleEggEnergyStartLevelDividendConsume(msgData []byte) error { time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒 //1、解析mq中queue的数据结构体 var msg *md2.EggEnergyStructForStarLevelDividends err := json.Unmarshal(msgData, &msg) if err != nil { return err } engine := db.Db //2、查找 `egg_energy_basic_setting` 基础设置 eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine) eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{ "key": "is_open", "value": 1, }) if err != nil { return err } if eggEnergyBasicSetting == nil { return errors.New("蛋蛋能量设置未开启!") } //3、给相应的用户加上个人的团队绿色能量 session := engine.NewSession() defer func() { session.Close() if err := recover(); err != nil { _ = logx.Error(err) } }() session.Begin() err = rule.DealUserVirtualCoin(session, md3.DealUserVirtualCoinReq{ Kind: "add", Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyCommunityDividends), TransferType: int(enum.EggEnergyCommunityDividends), CoinId: eggEnergyBasicSetting.TeamEggEnergyCoinId, Uid: msg.Uid, Amount: msg.SignDividend, }) if err != nil { session.Rollback() return err } return session.Begin() }