remainData.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package service
  2. import (
  3. "designs/global"
  4. "designs/model"
  5. "designs/utils"
  6. "github.com/pkg/errors"
  7. "math"
  8. "time"
  9. )
  10. func RemainDataBydDay(types int, pf string, gid string, startTime string, endTime string) (map[string]map[string]interface{}, error) {
  11. var err error
  12. //对于传过来的endTime ,做一个处理
  13. t, _ := time.Parse("2006-01-02", endTime)
  14. endTimeData := t.AddDate(0, 0, 1).Format("2006-01-02")
  15. UsersBydDay := utils.GetTimeDayDate(startTime, endTime) //用户分别是在哪天注册的
  16. UserLoginBydDay := utils.GetTimeDayDate(startTime, endTime) //用户分别是在哪天注册的
  17. var UsersId []int
  18. userIdMap := make(map[int]bool)
  19. if types == 1 {
  20. //先计算出这个时间内 ,每天的注册用户数量
  21. var users []model.User
  22. err = global.App.DB.Table("user").
  23. Where("pf", pf).Where("gid", gid).
  24. Where("createdAt", ">=", startTime).
  25. Where("createdAt", "<=", endTimeData).
  26. Scan(&users).Error
  27. if err != nil {
  28. return nil, err
  29. }
  30. for _, user := range users {
  31. userIdMap[user.UserId] = true
  32. UsersBydDay[user.CreatedAt.Format("2006-01-02")] = append(UsersBydDay[user.CreatedAt.Format("2006-01-02")], user.UserId)
  33. }
  34. for user := range userIdMap {
  35. UsersId = append(UsersId, user)
  36. }
  37. users = nil //用完后清空内存
  38. } else if types == 2 {
  39. var users []model.UserLogin
  40. err = global.App.DB.Table("user_login").
  41. Where("pf", pf).Where("gid", gid).
  42. Where("loginTime", ">=", startTime).
  43. Where("loginTime", "<=", endTimeData).
  44. Group("userId").
  45. Scan(&users).Error
  46. if err != nil {
  47. return nil, err
  48. }
  49. for _, user := range users {
  50. UsersId = append(UsersId, user.UserId)
  51. UsersBydDay[user.LoginTime.Format("2006-01-02")] = append(UsersBydDay[user.LoginTime.Format("2006-01-02")], user.UserId)
  52. }
  53. } else {
  54. return nil, errors.New("type 参数错误")
  55. }
  56. //把每天的注册用户进行集合,查出后面所有天数的活跃情况
  57. var UserLogin []model.UserOnline
  58. err = global.App.DB.Table("user_online").
  59. Where("pf", pf).Where("gid", gid).
  60. WhereIn("userId", UsersId).
  61. Where("logTime", ">=", startTime).
  62. Select("logTime", "userId").
  63. Group("date,userId").
  64. Scan(&UserLogin).Error
  65. if err != nil {
  66. return nil, err
  67. }
  68. //对这些数据进行整理
  69. for _, v := range UserLogin {
  70. //根据天进行分组,得出总共有多少
  71. times := v.LogTime.Format("2006-01-02")
  72. UserLoginBydDay[times] = append(UserLoginBydDay[times], v.UserId)
  73. }
  74. res := make(map[string]map[string]interface{})
  75. //逐天比较注册的数据和留存的数据,得出每天的活跃比例和人数
  76. for day, v := range UsersBydDay {
  77. remain := make(map[string]interface{})
  78. remain["count"] = len(v)
  79. remain["date"] = day
  80. //分别比较每一天的数据
  81. for dayDate, loginDay := range UserLoginBydDay {
  82. ok, remainDays := utils.CompareDates(dayDate, day)
  83. if !ok {
  84. continue
  85. }
  86. remain[remainDays] = math.Round(utils.IntersectionRate(v, loginDay)*100*100) / 100
  87. }
  88. res[day] = remain
  89. }
  90. return res, nil
  91. }