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

170 lines
4.7 KiB

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