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

190 lines
5.7 KiB

  1. package svc
  2. import (
  3. md "applet/app/md/institutional_management/public_platoon"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  6. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_models.git/utils/logx"
  7. "fmt"
  8. "xorm.io/xorm"
  9. )
  10. func GetTrees(engine *xorm.Engine, firstUser *model.PublicPlatoonUserRelation, leveLimit int) (list *md.TreeNode, err error) {
  11. var trees func(list []*md.TreeNode) []*md.TreeNode
  12. trees = func(list []*md.TreeNode) []*md.TreeNode {
  13. for _, item := range list {
  14. userPublicPlatoonRelations := make([]*model.PublicPlatoonUserRelation, 0)
  15. err = engine.Where("father_uid1 = ?", item.Uid).Find(&userPublicPlatoonRelations)
  16. if err != nil || len(userPublicPlatoonRelations) == 0 {
  17. continue
  18. }
  19. userIDs := make([]int64, len(userPublicPlatoonRelations))
  20. for i, relation := range userPublicPlatoonRelations {
  21. userIDs[i] = relation.Uid
  22. }
  23. users := make(map[int64]model.User)
  24. var userRecords []model.User
  25. err = engine.In("id", userIDs).Find(&userRecords)
  26. if err != nil {
  27. users = make(map[int64]model.User)
  28. } else {
  29. for _, user := range userRecords {
  30. users[user.Id] = user
  31. }
  32. }
  33. tempValue := make([]*md.TreeNode, 0)
  34. level := 1
  35. for _, relation := range userPublicPlatoonRelations {
  36. user := users[relation.Uid]
  37. tempValue = append(tempValue, &md.TreeNode{
  38. AvatarURL: user.Avatar,
  39. Phone: user.Phone,
  40. Nickname: user.Nickname,
  41. Uid: relation.Uid,
  42. PID: relation.RecommendUid,
  43. Position: relation.Position1,
  44. SystemID: relation.Id,
  45. Level: relation.LevelTotal,
  46. })
  47. level = relation.LevelTotal
  48. }
  49. item.Son = tempValue
  50. if level <= leveLimit {
  51. trees(item.Son)
  52. }
  53. }
  54. return list
  55. }
  56. if firstUser == nil {
  57. return
  58. }
  59. user := new(model.User)
  60. has, err := engine.ID(firstUser.Uid).Get(user)
  61. if err != nil || !has {
  62. return
  63. }
  64. rootNode := &md.TreeNode{
  65. AvatarURL: user.Avatar,
  66. Phone: user.Phone,
  67. Nickname: user.Nickname,
  68. Uid: firstUser.Uid,
  69. PID: firstUser.RecommendUid,
  70. Level: firstUser.LevelTotal,
  71. Position: firstUser.Position1,
  72. SystemID: firstUser.Id,
  73. }
  74. trees([]*md.TreeNode{rootNode})
  75. return rootNode, nil
  76. }
  77. func ExchangeUserPosition(engine *xorm.Engine, relation1, relation2 *model.PublicPlatoonUserRelation) (err error) {
  78. uid1 := relation1.Uid
  79. uid2 := relation2.Uid
  80. recommendUid1 := relation1.RecommendUid
  81. recommendUid2 := relation2.RecommendUid
  82. relation1.Uid = uid2
  83. relation2.Uid = uid1
  84. relation1.RecommendUid = recommendUid2
  85. relation2.RecommendUid = recommendUid1
  86. forceColumns := []string{"recommend_uid", "uid"}
  87. session := engine.NewSession()
  88. defer session.Close()
  89. if err := session.Begin(); err != nil {
  90. return zhios_order_relate_logx.Error(err)
  91. }
  92. _, err = session.Table(new(model.PublicPlatoonUserRelation)).ID(relation1.Id).Update(map[string]interface{}{"uid": -99})
  93. if err != nil {
  94. return zhios_order_relate_logx.Error(err)
  95. }
  96. _, err = session.Table(new(model.PublicPlatoonUserRelation)).ID(relation2.Id).Update(map[string]interface{}{"uid": -999})
  97. if err != nil {
  98. return zhios_order_relate_logx.Error(err)
  99. }
  100. _, err = session.Where("id = ?", relation1.Id).Cols(forceColumns...).Update(session, &relation1)
  101. if err != nil {
  102. return zhios_order_relate_logx.Error(err)
  103. }
  104. _, err = session.Where("id = ?", relation2.Id).Cols(forceColumns...).Update(session, &relation2)
  105. if err != nil {
  106. return zhios_order_relate_logx.Error(err)
  107. }
  108. m := new(model.PublicPlatoonUserRelation)
  109. for i := 1; i < 9; i++ {
  110. str := fmt.Sprintf("father_uid%d", i)
  111. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), uid1).Cols(str).Update(map[string]interface{}{str: -66})
  112. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), uid2).Cols(str).Update(map[string]interface{}{str: -666})
  113. if err != nil {
  114. return zhios_order_relate_logx.Error(err)
  115. }
  116. }
  117. for i := 1; i < 9; i++ {
  118. str := fmt.Sprintf("father_uid%d", i)
  119. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), -66).Cols(str).Update(map[string]interface{}{str: uid2})
  120. _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), -666).Cols(str).Update(map[string]interface{}{str: uid1})
  121. if err != nil {
  122. return zhios_order_relate_logx.Error(err)
  123. }
  124. }
  125. if err = session.Commit(); err != nil {
  126. return zhios_order_relate_logx.Error(err)
  127. }
  128. return nil
  129. }
  130. func GetSonUserDailyActivity(engine *xorm.Engine, fatherUID int64, startDate, endDate string) (activityUserCount int64, activityCount int64, sons []md.SonUserDailyActivityAnalysisNode, err error) {
  131. relationDb := implement.NewUserRelateDb(engine)
  132. sonRelations, err := relationDb.FindUserRelateByParentUid(fatherUID, 1)
  133. if err != nil {
  134. return 0, 0, nil, err
  135. }
  136. if sonRelations == nil {
  137. return 0, 0, nil, nil
  138. }
  139. userIDs := make([]int64, 0)
  140. for _, relation := range *sonRelations {
  141. userIDs = append(userIDs, relation.Uid)
  142. }
  143. activityDb := implement.NewEggEnergyUserActivityDb(engine)
  144. // 先判断这些用户中是否有活跃用户,如果有再逐个统计
  145. activityCount, err = activityDb.UserDailyActivityAnalysisFindByUidAndTime(startDate, endDate, userIDs)
  146. if err != nil {
  147. return 0, 0, nil, zhios_order_relate_logx.Error(err)
  148. }
  149. if activityCount == 0 {
  150. return 0, 0, nil, nil
  151. }
  152. sons = make([]md.SonUserDailyActivityAnalysisNode, 0)
  153. for _, userID := range userIDs {
  154. count, err := activityDb.UserDailyActivityAnalysisCountByUidAndTime(startDate, endDate, userID)
  155. if err != nil {
  156. return 0, 0, nil, err
  157. }
  158. if count > 0 {
  159. sons = append(sons, md.SonUserDailyActivityAnalysisNode{
  160. Uid: userID,
  161. Nickname: "",
  162. Phone: "",
  163. ActivityDayNums: count,
  164. })
  165. activityUserCount++
  166. }
  167. }
  168. return activityUserCount, activityCount, sons, nil
  169. }