蛋蛋星球RabbitMq消费项目
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

208 行
6.0 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. if utils2.StrToInt(msg.ParentUid) > 0 {
  94. //查出所有下级
  95. var relate []model.UserRelate
  96. err = eg.Where("parent_uid=?", msg.Uid).Find(&relate)
  97. if len(relate) > 0 {
  98. backEg.Insert(&relate)
  99. }
  100. var relateParent []model.UserRelate
  101. err = eg.Where("uid=?", msg.Uid).Find(&relateParent)
  102. var relateParent1 []model.UserRelate
  103. err = eg.Where("uid=?", msg.ParentUid).Find(&relateParent1)
  104. var parentIds = make([]int64, 0)
  105. if len(relateParent) > 0 {
  106. backEg.Insert(&relateParent)
  107. for _, v := range relateParent {
  108. parentIds = append(parentIds, v.ParentUid)
  109. }
  110. }
  111. var ids = make([]int64, 0)
  112. for _, v := range relate {
  113. ids = append(ids, v.Uid)
  114. }
  115. //读出所有下级重新处理
  116. var allRelate []model.UserRelate
  117. err = eg.In("uid", ids).Asc("level").Find(&allRelate)
  118. for _, v := range allRelate {
  119. if utils2.InArrInt64(v.ParentUid, parentIds) {
  120. eg.Where("id=?", v.Id).Delete(&model.UserRelate{})
  121. continue
  122. }
  123. if v.ParentUid == utils2.StrToInt64(msg.Uid) { //如果相等
  124. eg.Where("id=?", v.Id).Cols("parent_uid").Update(&model.UserRelate{ParentUid: utils2.StrToInt64(msg.ParentUid)})
  125. level := v.Level
  126. for _, v1 := range relateParent1 {
  127. level++
  128. var tmp = model.UserRelate{
  129. ParentUid: v1.ParentUid,
  130. Uid: v1.Uid,
  131. Level: level,
  132. InviteTime: v.InviteTime,
  133. }
  134. eg.Insert(&tmp)
  135. }
  136. }
  137. }
  138. eg.Where("parent_uid=?", msg.Uid).Cols("parent_uid").Update(&model.User{ParentUid: utils2.StrToInt64(msg.ParentUid)})
  139. } else {
  140. //查出所有下级
  141. var relate []model.UserRelate
  142. err = eg.Where("parent_uid=?", msg.Uid).Find(&relate)
  143. if len(relate) > 0 {
  144. backEg.Insert(&relate)
  145. }
  146. var ids = make([]int64, 0)
  147. for _, v := range relate {
  148. ids = append(ids, v.Uid)
  149. }
  150. var relateParent []model.UserRelate
  151. err = eg.Where("uid=?", msg.Uid).Find(&relateParent)
  152. if len(relateParent) > 0 {
  153. backEg.Insert(&relateParent)
  154. }
  155. //读出所有下级重新处理
  156. var allRelate []model.UserRelate
  157. err = eg.In("uid", ids).Asc("level").Find(&allRelate)
  158. var relateMap = make(map[int64]int)
  159. for _, v := range allRelate {
  160. if v.ParentUid == utils2.StrToInt64(msg.Uid) { //如果相等就删掉
  161. eg.Where("id=?", v.Id).Delete(&model.UserRelate{})
  162. } else {
  163. relateMap[v.Uid]++
  164. eg.Where("id=?", v.Id).Cols("level").Update(&model.UserRelate{Level: relateMap[v.Uid]})
  165. }
  166. }
  167. eg.Where("parent_uid=?", msg.Uid).Cols("parent_uid").Update(&model.User{ParentUid: user.ParentUid})
  168. }
  169. _, err = eg.Where("parent_uid=?", msg.Uid).Delete(&model.UserRelate{})
  170. _, err = eg.Where("uid=?", msg.Uid).Delete(&model.UserRelate{})
  171. //3.公排位置
  172. var publicPlatoonUserRelation model.PublicPlatoonUserRelation
  173. exist, err = eg.Where("uid=?", msg.Uid).Get(&publicPlatoonUserRelation)
  174. if exist {
  175. backEg.Insert(&publicPlatoonUserRelation)
  176. }
  177. eg.Where("uid=?", msg.Uid).Delete(&model.PublicPlatoonUserRelation{})
  178. //4.用户余额
  179. var UserWallet model.UserWallet
  180. exist, err = eg.Where("uid=?", msg.Uid).Get(&UserWallet)
  181. if exist {
  182. backEg.Insert(&UserWallet)
  183. }
  184. eg.Where("uid=?", msg.Uid).Delete(&model.UserWallet{})
  185. //5.用户积分
  186. var UserVirtualAmount []model.UserVirtualAmount
  187. err = eg.Where("uid=?", msg.Uid).Find(&UserVirtualAmount)
  188. if len(UserVirtualAmount) > 0 {
  189. backEg.Insert(&UserVirtualAmount)
  190. }
  191. eg.Where("uid=?", msg.Uid).Delete(&model.UserVirtualAmount{})
  192. //6.用户实名信息
  193. var UserRealNameAuth model.UserRealNameAuth
  194. exist, err = eg.Where("uid=?", msg.Uid).Get(&UserRealNameAuth)
  195. if exist {
  196. backEg.Insert(&UserRealNameAuth)
  197. }
  198. eg.Where("uid=?", msg.Uid).Delete(&model.UserRealNameAuth{})
  199. return nil
  200. }