蛋蛋星球RabbitMq消费项目
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

user_delete_consume.go 4.6 KiB

1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
1 vecka sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. }