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

139 lines
3.9 KiB

  1. package svc
  2. import (
  3. md "applet/app/md/institutional_management/public_platoon"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  5. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_models.git/utils/logx"
  6. "fmt"
  7. "xorm.io/xorm"
  8. )
  9. func GetTrees(engine *xorm.Engine, firstUser *model.PublicPlatoonUserRelation, leveLimit int) (list *md.TreeNode, err error) {
  10. var trees func(list []*md.TreeNode) []*md.TreeNode
  11. trees = func(list []*md.TreeNode) []*md.TreeNode {
  12. for _, item := range list {
  13. userPublicPlatoonRelations := make([]*model.PublicPlatoonUserRelation, 0)
  14. err = engine.Where("father_uid1 = ?", item.Uid).Find(&userPublicPlatoonRelations)
  15. if err != nil || len(userPublicPlatoonRelations) == 0 {
  16. continue
  17. }
  18. userIDs := make([]int64, len(userPublicPlatoonRelations))
  19. for i, relation := range userPublicPlatoonRelations {
  20. userIDs[i] = relation.Uid
  21. }
  22. users := make(map[int64]model.User)
  23. var userRecords []model.User
  24. err = engine.In("id", userIDs).Find(&userRecords)
  25. if err != nil {
  26. users = make(map[int64]model.User)
  27. } else {
  28. for _, user := range userRecords {
  29. users[user.Id] = user
  30. }
  31. }
  32. tempValue := make([]*md.TreeNode, 0)
  33. level := 1
  34. for _, relation := range userPublicPlatoonRelations {
  35. user := users[relation.Uid]
  36. tempValue = append(tempValue, &md.TreeNode{
  37. AvatarURL: user.Avatar,
  38. Phone: user.Phone,
  39. Nickname: user.Nickname,
  40. Uid: relation.Uid,
  41. PID: relation.RecommendUid,
  42. Position: relation.Position1,
  43. SystemID: relation.Id,
  44. Level: relation.LevelTotal,
  45. })
  46. level = relation.LevelTotal
  47. }
  48. item.Son = tempValue
  49. if level <= leveLimit {
  50. trees(item.Son)
  51. }
  52. }
  53. return list
  54. }
  55. if firstUser == nil {
  56. return
  57. }
  58. user := new(model.User)
  59. has, err := engine.ID(firstUser.Uid).Get(user)
  60. if err != nil || !has {
  61. return
  62. }
  63. rootNode := &md.TreeNode{
  64. AvatarURL: user.Avatar,
  65. Phone: user.Phone,
  66. Nickname: user.Nickname,
  67. Uid: firstUser.Uid,
  68. PID: firstUser.RecommendUid,
  69. Level: firstUser.LevelTotal,
  70. Position: firstUser.Position1,
  71. SystemID: firstUser.Id,
  72. }
  73. trees([]*md.TreeNode{rootNode})
  74. return rootNode, nil
  75. }
  76. func ExchangeUserPosition(engine *xorm.Engine, relation1, relation2 *model.PublicPlatoonUserRelation) (err error) {
  77. uid1 := relation1.Uid
  78. uid2 := relation2.Uid
  79. recommendUid1 := relation1.RecommendUid
  80. recommendUid2 := relation2.RecommendUid
  81. relation1.Uid = uid2
  82. relation2.Uid = uid1
  83. relation1.RecommendUid = recommendUid2
  84. relation2.RecommendUid = recommendUid1
  85. forceColums := []string{"recommend_uid", "uid"}
  86. session := engine.NewSession()
  87. defer session.Close()
  88. if err := session.Begin(); err != nil {
  89. return zhios_order_relate_logx.Error(err)
  90. }
  91. _, err = session.Where("id = ?", relation1.Id).Cols(forceColums...).Update(session, &relation1)
  92. if err != nil {
  93. return zhios_order_relate_logx.Error(err)
  94. }
  95. _, err = session.Where("id = ?", relation2.Id).Cols(forceColums...).Update(session, &relation2)
  96. if err != nil {
  97. return zhios_order_relate_logx.Error(err)
  98. }
  99. m := new(model.PublicPlatoonUserRelation)
  100. for i := 1; i < 9; i++ {
  101. str := fmt.Sprintf("father_uid%d", i)
  102. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), uid1).Cols(str).Update(map[string]interface{}{str: -66})
  103. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), uid2).Cols(str).Update(map[string]interface{}{str: -666})
  104. if err != nil {
  105. return zhios_order_relate_logx.Error(err)
  106. }
  107. }
  108. for i := 1; i < 9; i++ {
  109. str := fmt.Sprintf("father_uid%d", i)
  110. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), -66).Cols(str).Update(map[string]interface{}{str: uid2})
  111. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), -666).Cols(str).Update(map[string]interface{}{str: uid1})
  112. if err != nil {
  113. return zhios_order_relate_logx.Error(err)
  114. }
  115. }
  116. if err = session.Commit(); err != nil {
  117. return zhios_order_relate_logx.Error(err)
  118. }
  119. return nil
  120. }