蛋蛋星球RabbitMq消费项目
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

108 行
3.2 KiB

  1. package consume
  2. import (
  3. utils2 "applet/app/utils"
  4. "applet/app/utils/logx"
  5. "applet/consume/md"
  6. md2 "applet/es/md"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  9. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  11. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  12. "context"
  13. "encoding/json"
  14. "errors"
  15. "fmt"
  16. "github.com/olivere/elastic/v7"
  17. "github.com/streadway/amqp"
  18. "strings"
  19. "time"
  20. )
  21. func EggCanalInviteUserNumsConsume(queue md.MqQueue) {
  22. fmt.Println(">>>>>>>>>>>EggCanalInviteUserNumsConsume>>>>>>>>>>>>>")
  23. ch, err := rabbit.Cfg.Pool.GetChannel()
  24. if err != nil {
  25. logx.Error(err)
  26. return
  27. }
  28. defer ch.Release()
  29. //1、将自己绑定到交换机上
  30. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  31. //2、取出数据进行消费
  32. ch.Qos(1000)
  33. delivery := ch.Consume(queue.Name, true) //设置自动应答
  34. var res amqp.Delivery
  35. var ok bool
  36. for {
  37. res, ok = <-delivery
  38. if ok == true {
  39. //fmt.Println(string(res.Body))
  40. fmt.Println(">>>>>>>>>>>>>>>>>>EggCanalInviteUserNumsConsume<<<<<<<<<<<<<<<<<<<<<<<<<")
  41. err = handleEggCanalInviteUserNumsConsume(res.Body)
  42. if err != nil {
  43. fmt.Println("EggCanalInviteUserNumsConsume_ERR:::::", err.Error())
  44. utils2.FilePutContents("EggCanalInviteUserNumsConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  45. "body": res.Body,
  46. "err": err.Error(),
  47. }))
  48. }
  49. //_ = res.Reject(false)
  50. //_ = res.Ack(true)
  51. } else {
  52. panic(errors.New("error getting message"))
  53. }
  54. }
  55. }
  56. func handleEggCanalInviteUserNumsConsume(msg []byte) error {
  57. //1、解析canal采集至mq中queue的数据结构体
  58. var canalMsg *md.CanalUserRelateMessage[md.CanalUserRelate]
  59. err := json.Unmarshal(msg, &canalMsg)
  60. if err != nil {
  61. fmt.Println("EggCanalInviteUserNumsConsumeFaliedUnMarshal_ERR:::::", err.Error())
  62. return nil
  63. }
  64. year, week := time.Now().ISOWeek()
  65. yearStr := utils2.IntToStr(year)
  66. weekStr := utils2.IntToStr(week)
  67. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  68. // 2. 监听插入信息
  69. if canalMsg.Type == md2.CanalMsgInsertSqlType {
  70. for _, item := range canalMsg.Data {
  71. if item.Level != "1" {
  72. return nil
  73. }
  74. parentUid := item.ParentUid
  75. id := fmt.Sprintf("%d%d_%s", year, week, parentUid)
  76. // 新增拉新人数
  77. script := elastic.NewScript("ctx._source.invite_user_nums += params.inc").Param("inc", 1)
  78. service, err := es.EsClient.Update().
  79. Index(index).
  80. Id(id).
  81. Script(script).
  82. Do(context.Background())
  83. if err != nil {
  84. if strings.Contains(err.Error(), "elastic: Error 404 (Not Found)") {
  85. // 蛋蛋分数据还不存在,创建蛋蛋分数据
  86. now := time.Now().Format("2006-01-02 15:04:05")
  87. err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, utils2.StrToInt64(parentUid), enum.InviteUserNums, "1", now)
  88. if err1 != nil {
  89. return err1
  90. }
  91. return nil
  92. }
  93. fmt.Println("EggCanalInviteUserNumsConsumeFailedUpdateInviteUserNums_ERR:::::", err.Error())
  94. return err
  95. }
  96. fmt.Println(service)
  97. }
  98. }
  99. fmt.Println("EggCanalInviteUserNumsConsume_Succeed::::")
  100. return nil
  101. }