蛋蛋星球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.
 
 
 
 

125 lines
3.6 KiB

  1. package consume
  2. import (
  3. "applet/app/db"
  4. utils2 "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "applet/consume/md"
  7. md2 "applet/es/md"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  11. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  13. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  14. "encoding/json"
  15. "errors"
  16. "fmt"
  17. "github.com/streadway/amqp"
  18. "strings"
  19. "time"
  20. )
  21. // EggCanalViolateNumsConsume 更新违规次数
  22. func EggCanalViolateNumsConsume(queue md.MqQueue) {
  23. fmt.Println(">>>>>>>>>>>EggCanalViolateNumsConsume>>>>>>>>>>>>>")
  24. ch, err := rabbit.Cfg.Pool.GetChannel()
  25. if err != nil {
  26. logx.Error(err)
  27. return
  28. }
  29. defer ch.Release()
  30. //1、将自己绑定到交换机上
  31. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  32. //2、取出数据进行消费
  33. ch.Qos(1000)
  34. delivery := ch.Consume(queue.Name, true) //设置自动应答
  35. var res amqp.Delivery
  36. var ok bool
  37. for {
  38. res, ok = <-delivery
  39. if ok == true {
  40. //fmt.Println(string(res.Body))
  41. fmt.Println(">>>>>>>>>>>>>>>>>>EggCanalViolateNumsConsume<<<<<<<<<<<<<<<<<<<<<<<<<")
  42. err = handleEggCanalViolateNumsConsume(res.Body)
  43. if err != nil {
  44. fmt.Println("EggCanalViolateNumsConsume_ERR:::::", err.Error())
  45. utils2.FilePutContents("EggCanalViolateNumsConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  46. "body": res.Body,
  47. "err": err.Error(),
  48. }))
  49. }
  50. //_ = res.Reject(false)
  51. //_ = res.Ack(true)
  52. } else {
  53. panic(errors.New("error getting message"))
  54. }
  55. }
  56. }
  57. func handleEggCanalViolateNumsConsume(msg []byte) error {
  58. //1、解析canal采集至mq中queue的数据结构体
  59. var canalMsg *md.CanalTagRecordsMessage[md.CanalTagRecords]
  60. err := json.Unmarshal(msg, &canalMsg)
  61. if err != nil {
  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 || canalMsg.Type == md2.CanalMsgDeleteSqlType {
  70. tagDb := implement.NewUserTagDb(db.Db)
  71. for _, item := range canalMsg.Data {
  72. tag, err1 := tagDb.UserTagGetOneByParams(map[string]interface{}{
  73. "key": "id",
  74. "value": item.TagId,
  75. })
  76. if err1 != nil {
  77. return err1
  78. }
  79. // 2.1. 判断是否为处罚标签
  80. if tag.IsPunish == 0 {
  81. continue
  82. }
  83. uid := item.Uid
  84. id := fmt.Sprintf("%d%d_%s", year, week, uid)
  85. // 2.2 计算违规次数
  86. sql := "SELECT COUNT(*) AS total " +
  87. "From user_tag_records " +
  88. "left join user_tag on user_tag_records.tag_id = user_tag.id " +
  89. "WHERE user_tag_records.uid = %s " +
  90. "AND user_tag.is_punish = 1"
  91. sql = fmt.Sprintf(sql, uid)
  92. res, err1 := db.QueryNativeString(db.Db, sql)
  93. if err1 != nil {
  94. return err1
  95. }
  96. // 2.3 更新违规记录
  97. updateMap := make(map[string]interface{})
  98. updateMap["violate_nums"] = utils2.StrToInt(res[0]["total"])
  99. updateDocRet, err := es.UpdateDoc(index, id, updateMap)
  100. if err != nil {
  101. if strings.Contains(err.Error(), "elastic: Error 404 (Not Found)") {
  102. // 蛋蛋分数据还不存在,创建蛋蛋分数据
  103. now := time.Now().Format("2006-01-02 15:04:05")
  104. err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, utils2.StrToInt64(uid), enum.ViolateNums, "1", now)
  105. if err1 != nil {
  106. return err1
  107. }
  108. return nil
  109. }
  110. return err
  111. }
  112. fmt.Println("updateDocRet ========>", updateDocRet)
  113. }
  114. }
  115. return nil
  116. }