蛋蛋星球 后台端
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

197 lines
5.9 KiB

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