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

73 lines
1.6 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/utils/logx"
  6. "fmt"
  7. "time"
  8. )
  9. // RoutineInsertUserRelate is 协程 当关联上级用户时,需要查询当前用户的所有关联下级,并新增关联上级与当前用户下级关系
  10. func RoutineInsertUserRelate(puid, uid, addlv int) {
  11. defer Rev()
  12. urs, err := db.UserRelatesByPuid(db.Db, uid, 0, 0)
  13. if err != nil {
  14. logx.Warn(err)
  15. }
  16. // fmt.Println(*urs)
  17. for _, item := range *urs {
  18. _, err = db.UserRelateInsert(db.Db,
  19. &model.UserRelate{
  20. ParentUid: puid,
  21. Uid: item.Uid,
  22. Level: item.Level + addlv,
  23. InviteTime: time.Now(),
  24. })
  25. if err != nil {
  26. continue
  27. }
  28. logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v) ,lv:%v", puid, item.Uid, item.Level+addlv))
  29. }
  30. }
  31. // RoutineMultiRelate is 多级关联
  32. func RoutineMultiRelate(pid int, uid int, lv int) {
  33. userDb := db.UserDb{}
  34. userDb.Set()
  35. defer Rev()
  36. for {
  37. if pid == 0 {
  38. break
  39. }
  40. m, err := userDb.GetUser(pid)
  41. if err != nil || m == nil {
  42. logx.Warn(err)
  43. break
  44. }
  45. if m.ParentUid == 0 {
  46. break
  47. }
  48. lv++
  49. ur := new(model.UserRelate)
  50. ur.ParentUid = m.ParentUid
  51. ur.Uid = uid
  52. ur.Level = lv
  53. ur.InviteTime = time.Now()
  54. _, err = db.UserRelateInsert(db.Db, ur)
  55. if err != nil {
  56. logx.Warn(err)
  57. break
  58. }
  59. // 还要关联当前的用户的所有下级,注意关联等级
  60. RoutineInsertUserRelate(m.ParentUid, uid, lv)
  61. // 下级关联上上级
  62. // 继续查询
  63. logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v),lv:%v", ur.ParentUid, ur.Uid, lv))
  64. logx.Info("继续查询")
  65. pid = m.ParentUid
  66. }
  67. }