main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "context"
  4. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gcron"
  7. "github.com/gogf/gf/v2/os/gctx"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "time"
  10. "workTasks/common"
  11. "workTasks/wxSign/wxSignGroup"
  12. )
  13. type (
  14. UserSubAction struct {
  15. S_m_openid string `bson:"s_m_openid"`
  16. LastData int64 `bson:"latest_l_date"`
  17. }
  18. )
  19. func main() {
  20. ctx := gctx.New()
  21. _, err := gcron.Add(gctx.New(), g.Cfg().MustGet(ctx, "runCron", "# 0 2 * * *").String(), func(ctx context.Context) {
  22. //ctx := context.Background()
  23. nwsm, err := wxSignGroup.NewWxSignManager()
  24. if err != nil {
  25. panic(err)
  26. }
  27. nwsm.LoadGroupUser(ctx)
  28. nwsm.ClearGroupUser(ctx)
  29. //同步最新标签
  30. for i, arr := range logNewSign() {
  31. if err := nwsm.AddNewUsers(i, arr...); err != nil {
  32. panic(err)
  33. }
  34. }
  35. nwsm.LoadGroupUser(ctx)
  36. }, "userWxSign")
  37. if err != nil {
  38. panic(err)
  39. }
  40. select {}
  41. }
  42. func logNewSign() map[int][]string {
  43. var (
  44. dayMaps = map[int]bool{}
  45. signList = map[int][]string{}
  46. now = time.Now()
  47. signData = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
  48. maxDay int = 0
  49. )
  50. for _, dayNum := range g.Cfg().MustGet(context.Background(), "subDaySign").Ints() {
  51. if dayNum > maxDay {
  52. maxDay = dayNum
  53. }
  54. dayMaps[dayNum] = true
  55. }
  56. g.Dump(dayMaps)
  57. subActionList, err := GetUserSubList(-maxDay)
  58. if err != nil {
  59. panic(err)
  60. }
  61. for _, action := range subActionList {
  62. sign := int((signData-action.LastData)/(60*60*24)) + 1
  63. if dayMaps[sign] {
  64. signList[sign] = append(signList[sign], action.S_m_openid)
  65. }
  66. }
  67. //加载分组完成,开始更新
  68. g.Dump(signList)
  69. return signList
  70. }
  71. func GetUserSubList(day int) ([]*UserSubAction, error) {
  72. //获取30天时间戳
  73. now := time.Now()
  74. endTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  75. startTime := endTime.AddDate(0, 0, day) // 30 天前
  76. g.Log().Infof(context.Background(), "开始加载 %d-%d关注数据", startTime.Unix(), endTime.Unix())
  77. client := common.MG.DB().C
  78. collection := client.Database("qfw").Collection("jy_subscribe")
  79. // 创建聚合管道
  80. pipeline := []bson.M{
  81. {
  82. // 第一个阶段:筛选符合条件的文档
  83. "$match": bson.M{"l_date": bson.M{"$gt": startTime.Unix(), "$lt": endTime.Unix()}, "s_event": "subscribe"},
  84. },
  85. {
  86. "$group": bson.M{
  87. "_id": "$s_m_openid", // 按 s_m_openid 分组
  88. "latest_l_date": bson.M{"$max": "$l_date"}, // 计算每个 s_m_openid 出现的次数
  89. },
  90. },
  91. {
  92. "$project": bson.M{
  93. "_id": 0,
  94. "s_m_openid": "$_id", // 显示 s_m_openid
  95. "latest_l_date": "$latest_l_date", // 显示出现次数
  96. },
  97. },
  98. {
  99. "$sort": bson.M{
  100. "latest_l_date": -1, // 按 count 降序排序
  101. },
  102. },
  103. }
  104. // 执行聚合查询
  105. cursor, err := collection.Aggregate(context.Background(), pipeline)
  106. if err != nil {
  107. g.Log().Fatalf(context.Background(), "Failed to execute aggregation: %v", err)
  108. return nil, err
  109. }
  110. defer cursor.Close(context.Background())
  111. var list []*UserSubAction
  112. // 遍历结果并打印
  113. for cursor.Next(context.Background()) {
  114. var result UserSubAction
  115. if err := cursor.Decode(&result); err != nil {
  116. g.Log().Fatalf(context.Background(), "Failed to decode result: %v", err)
  117. }
  118. list = append(list, &result)
  119. }
  120. // 检查遍历过程中是否出错
  121. if err := cursor.Err(); err != nil {
  122. g.Log().Fatalf(context.Background(), "Cursor error: %v", err)
  123. return nil, err
  124. }
  125. return list, nil
  126. }