company_user.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package logic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_open_api/models/tables"
  6. "hongze/hongze_open_api/models/tables/user_view_statistics"
  7. "hongze/hongze_open_api/utils"
  8. "sort"
  9. "time"
  10. )
  11. // UserView 用户阅读
  12. type UserView struct {
  13. Phone string `json:"phone" description:"手机号"`
  14. Count int `json:"count" description:"阅读数"`
  15. }
  16. // GetUserViewTotal 获取用户阅读报告数量统计
  17. func GetUserViewTotal() (list []UserView, err error) {
  18. dayStr := time.Now().AddDate(0, 0, -30).Format(utils.FormatDate)
  19. key := "openapi:user_view_total:" + dayStr
  20. redisJsonData, redisErr := utils.Rc.RedisString(key)
  21. if redisErr == nil {
  22. err = json.Unmarshal([]byte(redisJsonData), &list)
  23. if err != nil {
  24. fmt.Println("开放api获取用户阅读报告数量统计,json转换失败")
  25. }
  26. return
  27. }
  28. userViewMap := make(map[string]UserView)
  29. mobileList := make([]string, 0)
  30. historyUserViewList, err := user_view_statistics.GetUserViewListByDate(dayStr)
  31. if err != nil {
  32. return
  33. }
  34. for _, historyUserView := range historyUserViewList {
  35. userViewMap[historyUserView.Mobile] = UserView{
  36. Phone: historyUserView.Mobile,
  37. Count: historyUserView.Total,
  38. }
  39. mobileList = append(mobileList, historyUserView.Mobile)
  40. }
  41. todayStr := time.Now().Format(utils.FormatDate)
  42. tmpList, err := tables.GetCountUserViewHistoryByMobile(todayStr)
  43. if err != nil {
  44. return
  45. }
  46. for _, tmpUserView := range tmpList {
  47. if userView, ok := userViewMap[tmpUserView.Mobile]; ok {
  48. userView.Count += tmpUserView.Total
  49. userViewMap[tmpUserView.Mobile] = userView
  50. } else {
  51. userViewMap[tmpUserView.Mobile] = UserView{
  52. Phone: tmpUserView.Mobile,
  53. Count: tmpUserView.Total,
  54. }
  55. mobileList = append(mobileList, tmpUserView.Mobile)
  56. }
  57. }
  58. tmpList, err = tables.GetReportViewMaxTimeByMobile(todayStr)
  59. if err != nil {
  60. return
  61. }
  62. for _, tmpUserView := range tmpList {
  63. if userView, ok := userViewMap[tmpUserView.Mobile]; ok {
  64. userView.Count += tmpUserView.Total
  65. userViewMap[tmpUserView.Mobile] = userView
  66. } else {
  67. userViewMap[tmpUserView.Mobile] = UserView{
  68. Phone: tmpUserView.Mobile,
  69. Count: tmpUserView.Total,
  70. }
  71. mobileList = append(mobileList, tmpUserView.Mobile)
  72. }
  73. }
  74. tmpList, err = tables.GetAdvisoryCountUserViewHistoryByMobile(todayStr)
  75. if err != nil {
  76. return
  77. }
  78. for _, tmpUserView := range tmpList {
  79. if userView, ok := userViewMap[tmpUserView.Mobile]; ok {
  80. userView.Count += tmpUserView.Total
  81. userViewMap[tmpUserView.Mobile] = userView
  82. } else {
  83. userViewMap[tmpUserView.Mobile] = UserView{
  84. Phone: tmpUserView.Mobile,
  85. Count: tmpUserView.Total,
  86. }
  87. mobileList = append(mobileList, tmpUserView.Mobile)
  88. }
  89. }
  90. sort.Strings(mobileList)
  91. for _, mobile := range mobileList {
  92. list = append(list, userViewMap[mobile])
  93. delete(userViewMap, mobile)
  94. }
  95. redisJsonByteData, err := json.Marshal(list)
  96. if err == nil {
  97. _ = utils.Rc.Put(key, string(redisJsonByteData), time.Minute*30)
  98. }
  99. return
  100. }