蛋蛋星球RabbitMq消费项目
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

184 rindas
5.1 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 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. level++
  125. var tmp = model.UserRelate{
  126. ParentUid: utils2.StrToInt64(msg.ParentUid),
  127. Uid: v.Uid,
  128. Level: level,
  129. InviteTime: v.InviteTime,
  130. }
  131. eg.Insert(&tmp)
  132. for _, v1 := range relateParent1 {
  133. level++
  134. var tmp1 = model.UserRelate{
  135. ParentUid: v1.ParentUid,
  136. Uid: v.Uid,
  137. Level: level,
  138. InviteTime: v.InviteTime,
  139. }
  140. eg.Insert(&tmp1)
  141. }
  142. }
  143. }
  144. eg.Where("parent_uid=?", msg.Uid).Cols("parent_uid").Update(&model.User{ParentUid: utils2.StrToInt64(msg.ParentUid)})
  145. _, err = eg.Where("parent_uid=?", msg.Uid).Delete(&model.UserRelate{})
  146. _, err = eg.Where("uid=?", msg.Uid).Delete(&model.UserRelate{})
  147. //3.公排位置
  148. var publicPlatoonUserRelation model.PublicPlatoonUserRelation
  149. exist, err = eg.Where("uid=?", msg.Uid).Get(&publicPlatoonUserRelation)
  150. if exist {
  151. backEg.Insert(&publicPlatoonUserRelation)
  152. }
  153. eg.Where("uid=?", msg.Uid).Delete(&model.PublicPlatoonUserRelation{})
  154. //4.用户余额
  155. var UserWallet model.UserWallet
  156. exist, err = eg.Where("uid=?", msg.Uid).Get(&UserWallet)
  157. if exist {
  158. backEg.Insert(&UserWallet)
  159. }
  160. eg.Where("uid=?", msg.Uid).Delete(&model.UserWallet{})
  161. //5.用户积分
  162. var UserVirtualAmount []model.UserVirtualAmount
  163. err = eg.Where("uid=?", msg.Uid).Find(&UserVirtualAmount)
  164. if len(UserVirtualAmount) > 0 {
  165. backEg.Insert(&UserVirtualAmount)
  166. }
  167. eg.Where("uid=?", msg.Uid).Delete(&model.UserVirtualAmount{})
  168. //6.用户实名信息
  169. var UserRealNameAuth model.UserRealNameAuth
  170. exist, err = eg.Where("uid=?", msg.Uid).Get(&UserRealNameAuth)
  171. if exist {
  172. backEg.Insert(&UserRealNameAuth)
  173. }
  174. eg.Where("uid=?", msg.Uid).Delete(&model.UserRealNameAuth{})
  175. return nil
  176. }