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

155 line
4.3 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  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/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. has, err := eg.Where("id=?", msg.Uid).Delete(&model.User{})
  88. if has == 0 || err != nil {
  89. return e.NewErr(400, "注销失败")
  90. }
  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 ids = make([]int64, 0)
  100. for _, v := range relate {
  101. ids = append(ids, v.Uid)
  102. }
  103. //读出所有下级重新处理
  104. var allRelate []model.UserRelate
  105. err = eg.In("uid", ids).Asc("level").Find(&allRelate)
  106. var relateMap = make(map[int64]int)
  107. for _, v := range allRelate {
  108. if v.ParentUid != utils2.StrToInt64(msg.Uid) { //如果相等就删掉
  109. relateMap[v.Uid]++
  110. eg.Where("id=?", v.Id).Cols("level").Update(&model.UserRelate{Level: relateMap[v.Uid]})
  111. }
  112. }
  113. _, err = eg.Where("parent_uid=?", msg.Uid).Delete(&model.UserRelate{})
  114. var relateParent []model.UserRelate
  115. err = eg.Where("uid=?", msg.Uid).Find(&relateParent)
  116. if len(relateParent) > 0 {
  117. backEg.Insert(&relateParent)
  118. }
  119. _, err = eg.Where("uid=?", msg.Uid).Delete(&model.UserRelate{})
  120. //3.公排位置
  121. var publicPlatoonUserRelation model.PublicPlatoonUserRelation
  122. exist, err = eg.Where("uid=?", msg.Uid).Get(&publicPlatoonUserRelation)
  123. if exist {
  124. backEg.Insert(&publicPlatoonUserRelation)
  125. }
  126. eg.Where("uid=?", msg.Uid).Delete(&model.PublicPlatoonUserRelation{})
  127. //4.用户余额
  128. var UserWallet model.UserWallet
  129. exist, err = eg.Where("uid=?", msg.Uid).Get(&UserWallet)
  130. if exist {
  131. backEg.Insert(&UserWallet)
  132. }
  133. eg.Where("uid=?", msg.Uid).Delete(&model.UserWallet{})
  134. //5.用户积分
  135. var UserVirtualAmount []model.UserVirtualAmount
  136. err = eg.Where("uid=?", msg.Uid).Find(&UserVirtualAmount)
  137. if len(UserVirtualAmount) > 0 {
  138. backEg.Insert(&UserVirtualAmount)
  139. }
  140. eg.Where("uid=?", msg.Uid).Delete(&model.UserVirtualAmount{})
  141. //6.用户实名信息
  142. var UserRealNameAuth model.UserRealNameAuth
  143. exist, err = eg.Where("uid=?", msg.Uid).Get(&UserRealNameAuth)
  144. if exist {
  145. backEg.Insert(&UserRealNameAuth)
  146. }
  147. eg.Where("uid=?", msg.Uid).Delete(&model.UserRealNameAuth{})
  148. return nil
  149. }