蛋蛋星球RabbitMq消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

110 lines
3.4 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 EggCanalEnergyExchangeAccountConsume(queue md.MqQueue) {
  22. fmt.Println(">>>>>>>>>>>EggCanalEnergyExchangeAccountConsume>>>>>>>>>>>>>")
  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(">>>>>>>>>>>>>>>>>>EggCanalEnergyExchangeAccountConsume<<<<<<<<<<<<<<<<<<<<<<<<<")
  41. err = handleEggCanalEnergyExchangeAccountConsume(res.Body)
  42. if err != nil {
  43. fmt.Println("EggCanalEnergyExchangeAccountConsume_ERR:::::", err.Error())
  44. utils2.FilePutContents("EggCanalEnergyExchangeAccountConsume_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 handleEggCanalEnergyExchangeAccountConsume(msg []byte) error {
  57. //1.解析canal采集至mq中queue的数据结构体
  58. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  59. var canalMsg *md.CanalEnergyExchangeAccountMessage[md.CanalEnergyExchangeAccount]
  60. err := json.Unmarshal(msg, &canalMsg)
  61. if err != nil {
  62. fmt.Println("EggCanalEnergyExchangeAccountConsumeUnMarshalFailed_ERR:::::", err.Error())
  63. return nil
  64. }
  65. year, week := time.Now().ISOWeek()
  66. yearStr := utils2.IntToStr(year)
  67. weekStr := utils2.IntToStr(week)
  68. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  69. // 2. 监听插入信息
  70. if canalMsg.Type == md2.CanalMsgInsertSqlType {
  71. for _, item := range canalMsg.Data {
  72. uid := item.Uid
  73. id := fmt.Sprintf("%d%d_%s", year, week, uid)
  74. if item.Title != enum.EggEnergyExchangeAccountBalance.String() {
  75. continue
  76. }
  77. // 3. 增加 蛋蛋能量兑换余额 数量
  78. amount := utils2.StrToFloat64(item.Amount)
  79. script := elastic.NewScript("ctx._source.egg_energy_exchange_account_balance += params.inc").Param("inc", amount)
  80. updateDoc, err := es.EsClient.Update().
  81. Index(index).
  82. Id(id).
  83. Script(script).
  84. Do(context.Background())
  85. if err != nil {
  86. if strings.Contains(err.Error(), "elastic: Error 404 (Not Found)") {
  87. // 蛋蛋分数据还不存在,创建蛋蛋分数据
  88. now := time.Now().Format("2006-01-02 15:04:05")
  89. err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, utils2.StrToInt64(uid), enum.EsEggEnergyExchangeAccountBalance, utils2.Float64ToStr(amount), now)
  90. if err1 != nil {
  91. return err1
  92. }
  93. return nil
  94. }
  95. fmt.Println("EggCanalPersonAddActivityValueConsumeUpdateDoc_ERR::::", err.Error())
  96. return err
  97. }
  98. fmt.Println("updateDoc==========>", updateDoc)
  99. }
  100. }
  101. return nil
  102. }