|
- package consume
-
- import (
- utils2 "applet/app/utils"
- "applet/app/utils/logx"
- "applet/consume/md"
- md2 "applet/es/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"
- "time"
- )
-
- func EggCanalInviteUserNumsConsume(queue md.MqQueue) {
- fmt.Println(">>>>>>>>>>>EggCanalInviteUserNumsConsume>>>>>>>>>>>>>")
- 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(">>>>>>>>>>>>>>>>>>EggCanalInviteUserNumsConsume<<<<<<<<<<<<<<<<<<<<<<<<<")
- err = handleEggCanalInviteUserNumsConsume(res.Body)
- if err != nil {
- fmt.Println("EggCanalInviteUserNumsConsume_ERR:::::", err.Error())
- utils2.FilePutContents("EggCanalInviteUserNumsConsume_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 handleEggCanalInviteUserNumsConsume(msg []byte) error {
- //1、解析canal采集至mq中queue的数据结构体
- var canalMsg *md.CanalUserRelateMessage[md.CanalUserRelate]
- err := json.Unmarshal(msg, &canalMsg)
- if err != nil {
- fmt.Println("EggCanalInviteUserNumsConsumeFaliedUnMarshal_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 {
- parentUid := item.ParentUid
- id := fmt.Sprintf("%d%d_%s", year, week, parentUid)
-
- // 新增拉新人数
- script := elastic.NewScript("ctx._source.invite_user_nums += params.inc").Param("inc", 1)
- service, err := es.EsClient.Update().
- Index(index).
- Id(id).
- Script(script).
- Do(context.Background())
- if err != nil {
- fmt.Println("EggCanalInviteUserNumsConsumeFailedUpdateInviteUserNums_ERR:::::", err.Error())
- return err
- }
- fmt.Println(service)
- }
- }
- fmt.Println("EggCanalInviteUserNumsConsume_Succeed::::")
- return nil
- }
|