|
- package consume
-
- import (
- utils2 "applet/app/utils"
- "applet/app/utils/logx"
- "applet/consume/md"
- md2 "applet/es/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
- 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 EggCanalEnergyExchangeAccountConsume(queue md.MqQueue) {
- fmt.Println(">>>>>>>>>>>EggCanalEnergyExchangeAccountConsume>>>>>>>>>>>>>")
- 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(1000)
- delivery := ch.Consume(queue.Name, true) //设置自动应答
-
- var res amqp.Delivery
- var ok bool
- for {
- res, ok = <-delivery
- if ok == true {
- //fmt.Println(string(res.Body))
- fmt.Println(">>>>>>>>>>>>>>>>>>EggCanalEnergyExchangeAccountConsume<<<<<<<<<<<<<<<<<<<<<<<<<")
- err = handleEggCanalEnergyExchangeAccountConsume(res.Body)
- if err != nil {
- fmt.Println("EggCanalEnergyExchangeAccountConsume_ERR:::::", err.Error())
- utils2.FilePutContents("EggCanalEnergyExchangeAccountConsume_ERR", utils2.SerializeStr(map[string]interface{}{
- "body": res.Body,
- "err": err.Error(),
- }))
- }
- //_ = res.Reject(false)
- //_ = res.Ack(true)
- } else {
- panic(errors.New("error getting message"))
- }
- }
- }
-
- func handleEggCanalEnergyExchangeAccountConsume(msg []byte) error {
- //1.解析canal采集至mq中queue的数据结构体
- time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
- var canalMsg *md.CanalEnergyExchangeAccountMessage[md.CanalEnergyExchangeAccount]
- err := json.Unmarshal(msg, &canalMsg)
- if err != nil {
- fmt.Println("EggCanalEnergyExchangeAccountConsumeUnMarshalFailed_ERR:::::", err.Error())
- return nil
- }
-
- year, week := time.Now().ISOWeek()
- yearStr := utils2.IntToStr(year)
- weekStr := utils2.IntToStr(week)
- index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
- // 2. 监听插入信息
- if canalMsg.Type == md2.CanalMsgInsertSqlType {
- for _, item := range canalMsg.Data {
- uid := item.Uid
- id := fmt.Sprintf("%d%d_%s", year, week, uid)
-
- if item.Title != enum.EggEnergyExchangeAccountBalance.String() {
- continue
- }
-
- // 3. 增加 蛋蛋能量兑换余额 数量
- amount := utils2.StrToFloat64(item.Amount)
- script := elastic.NewScript("ctx._source.egg_energy_exchange_account_balance += params.inc").Param("inc", amount)
- updateDoc, err := es.EsClient.Update().
- Index(index).
- Id(id).
- Script(script).
- Do(context.Background())
- if err != nil {
- if strings.Contains(err.Error(), "elastic: Error 404 (Not Found)") {
- // 蛋蛋分数据还不存在,创建蛋蛋分数据
- now := time.Now().Format("2006-01-02 15:04:05")
- err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, utils2.StrToInt64(uid), enum.EsEggEnergyExchangeAccountBalance, utils2.Float64ToStr(amount), now)
- if err1 != nil {
- return err1
- }
- return nil
- }
- fmt.Println("EggCanalPersonAddActivityValueConsumeUpdateDoc_ERR::::", err.Error())
- return err
- }
- fmt.Println("updateDoc==========>", updateDoc)
- }
- }
- return nil
- }
|