蛋蛋星球RabbitMq消费项目
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

168 lignes
4.6 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. utils2 "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "applet/consume/md"
  8. db2 "code.fnuoos.com/EggPlanet/egg_models.git/src"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  13. "encoding/json"
  14. "errors"
  15. "fmt"
  16. "github.com/jinzhu/copier"
  17. "github.com/streadway/amqp"
  18. "time"
  19. "xorm.io/xorm"
  20. )
  21. func UserDeleteConsume(queue md.MqQueue) {
  22. var backCfg db2.BackUpDBCfg
  23. copier.Copy(&backCfg, &cfg.BackUpDb)
  24. backUpDb, err := db2.InitBackUpDB(&backCfg)
  25. if err != nil {
  26. logx.Error(err)
  27. return
  28. }
  29. fmt.Println(">>>>>>>>>>>>UserDeleteConsume>>>>>>>>>>>>")
  30. ch, err := rabbit.Cfg.Pool.GetChannel()
  31. if err != nil {
  32. logx.Error(err)
  33. return
  34. }
  35. defer ch.Release()
  36. //1、将自己绑定到交换机上
  37. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  38. //2、取出数据进行消费
  39. ch.Qos(100)
  40. delivery := ch.Consume(queue.Name, false)
  41. egg_system_rules.Init(cfg.RedisAddr)
  42. var res amqp.Delivery
  43. var ok bool
  44. for {
  45. res, ok = <-delivery
  46. if ok == true {
  47. err = handleUserDeleteConsume(backUpDb, ch, res.Body)
  48. if err != nil {
  49. fmt.Println("UserDeleteConsume_ERR:::::", err.Error())
  50. utils2.FilePutContents("UserDeleteConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  51. "body": res.Body,
  52. "err": err.Error(),
  53. }))
  54. _ = res.Reject(false)
  55. //TODO::重新推回队列末尾,避免造成队列堵塞
  56. var msg *md.CommUserId
  57. json.Unmarshal(res.Body, &msg)
  58. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  59. } else {
  60. //_ = res.Reject(false)
  61. err = res.Ack(true)
  62. }
  63. fmt.Println("err ::: ", err)
  64. } else {
  65. panic(errors.New("error getting message"))
  66. }
  67. }
  68. fmt.Println("get msg done")
  69. }
  70. func handleUserDeleteConsume(backEg *xorm.Engine, ch *rabbit.Channel, msgData []byte) error {
  71. if backEg == nil {
  72. return nil
  73. }
  74. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  75. // 1.解析mq中queue的数据结构体
  76. var msg *md.CommUserId
  77. err := json.Unmarshal(msgData, &msg)
  78. if err != nil {
  79. return err
  80. }
  81. eg := db.Db
  82. //1.用户信息
  83. var user model.User
  84. exist, err := eg.Where("id=?", msg.Uid).Get(&user)
  85. if exist {
  86. backEg.Insert(&user)
  87. user.Phone = ""
  88. user.State = 2
  89. user.Nickname = "注销用户"
  90. db.Db.Where("id=?", user.Id).Cols("state,phone,nickname").Update(&user)
  91. }
  92. //2.用户关系链
  93. //查出所有下级
  94. var relate []model.UserRelate
  95. err = eg.Where("parent_uid=?", msg.Uid).Find(&relate)
  96. if len(relate) > 0 {
  97. backEg.Insert(&relate)
  98. }
  99. var relateParent []model.UserRelate
  100. err = eg.Where("uid=?", msg.Uid).Find(&relateParent)
  101. var relateParent1 []model.UserRelate
  102. err = eg.Where("uid=?", msg.ParentUid).Find(&relateParent1)
  103. var parentIds = make([]int64, 0)
  104. if len(relateParent) > 0 {
  105. backEg.Insert(&relateParent)
  106. for _, v := range relateParent {
  107. parentIds = append(parentIds, v.ParentUid)
  108. }
  109. }
  110. var ids = make([]int64, 0)
  111. for _, v := range relate {
  112. ids = append(ids, v.Uid)
  113. }
  114. //读出所有下级重新处理
  115. var allRelate []model.UserRelate
  116. err = eg.In("uid", ids).Desc("level").Find(&allRelate)
  117. for _, v := range allRelate {
  118. if utils2.InArrInt64(v.ParentUid, parentIds) {
  119. eg.Where("id=?", v.Id).Delete(&model.UserRelate{})
  120. continue
  121. }
  122. if v.ParentUid == utils2.StrToInt64(msg.Uid) && utils2.StrToInt64(msg.ParentUid) > 0 { //如果相等
  123. level := v.Level
  124. var tmp = model.UserRelate{
  125. ParentUid: utils2.StrToInt64(msg.ParentUid),
  126. Uid: v.Uid,
  127. Level: level,
  128. InviteTime: v.InviteTime,
  129. }
  130. eg.Insert(&tmp)
  131. for _, v1 := range relateParent1 {
  132. level++
  133. var tmp1 = model.UserRelate{
  134. ParentUid: v1.ParentUid,
  135. Uid: v.Uid,
  136. Level: level,
  137. InviteTime: v.InviteTime,
  138. }
  139. eg.Insert(&tmp1)
  140. }
  141. }
  142. }
  143. eg.Where("parent_uid=?", msg.Uid).Cols("parent_uid").Update(&model.User{ParentUid: utils2.StrToInt64(msg.ParentUid)})
  144. _, err = eg.Where("parent_uid=?", msg.Uid).Delete(&model.UserRelate{})
  145. _, err = eg.Where("uid=?", msg.Uid).Delete(&model.UserRelate{})
  146. var UserRealNameAuth model.UserRealNameAuth
  147. exist, err = eg.Where("uid=?", msg.Uid).Get(&UserRealNameAuth)
  148. if exist {
  149. backEg.Insert(&UserRealNameAuth)
  150. }
  151. eg.Where("uid=?", msg.Uid).Delete(&model.UserRealNameAuth{})
  152. var publicPlatoonUserRelation model.PublicPlatoonUserRelation
  153. exist, err = eg.Where("uid=?", msg.Uid).Get(&publicPlatoonUserRelation)
  154. if exist {
  155. publicPlatoonUserRelation.Id = 0
  156. backEg.Insert(&publicPlatoonUserRelation)
  157. }
  158. rule.DealPublicPlatoon(eg, utils2.StrToInt64(msg.Uid), "注销")
  159. return nil
  160. }