main.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. . "activeStartTip/config"
  4. . "activeStartTip/rpc"
  5. "log"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  10. "app.yhyue.com/moapp/jybase/common"
  11. . "app.yhyue.com/moapp/jybase/mongodb"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. )
  14. func main() {
  15. allUser := loadAllUser()
  16. pool := make(chan bool, Config.SendMsgPool)
  17. wait := &sync.WaitGroup{}
  18. sess := Mgo.GetMgoConn()
  19. defer Mgo.DestoryMongoConn(sess)
  20. query := map[string]interface{}{}
  21. if len(Config.TestIds) > 0 {
  22. query["_id"] = map[string]interface{}{
  23. "$in": ToObjectIds(Config.TestIds),
  24. }
  25. } else {
  26. query["i_appid"] = 2
  27. }
  28. log.Println("start...", query)
  29. it := sess.DB("qfw").C("user").Find(query).Select(map[string]interface{}{
  30. "_id": 1,
  31. "base_user_id": 1,
  32. }).Sort("_id").Iter()
  33. var index int64
  34. for m := make(map[string]interface{}); it.Next(&m); {
  35. pool <- true
  36. wait.Add(1)
  37. go func(u map[string]interface{}) {
  38. defer func() {
  39. <-pool
  40. wait.Done()
  41. }()
  42. if !allUser[common.Int64All(u["base_user_id"])] {
  43. return
  44. }
  45. rs := atomic.AddInt64(&index, 1)
  46. rs++
  47. if rs%5000 == 0 {
  48. log.Println("index", rs)
  49. }
  50. _id := BsonIdToSId(u["_id"])
  51. SendMsg(&message.MultipleSaveMsgReq{
  52. UserIds: _id,
  53. Title: "做任务赚好礼",
  54. Content: "7天内完成限时任务,额外赠送超级订阅7天体验。获取更多采购项目信息,快速对接项目联系人!",
  55. MsgType: 1,
  56. Link: "/page_workDesktop/work-bench/app/points/earn,/jy_mobile/points/earn,/jy_mobile/points/earn,/jy_mobile/points/earn",
  57. Appid: "10000",
  58. AppPushUrl: "/jy_mobile/points/earn",
  59. IosPushUrl: "/jy_mobile/points/earn",
  60. })
  61. }(m)
  62. }
  63. wait.Wait()
  64. log.Println("over...", index)
  65. }
  66. func loadAllUser() map[int64]bool {
  67. _id := primitive.NewObjectIDFromTimestamp(time.Now().AddDate(-1, 0, 0))
  68. log.Println("开始加载最近一年活跃用户。。。", _id)
  69. allUser := map[int64]bool{}
  70. sess := Mgo_Log.GetMgoConn()
  71. defer Mgo_Log.DestoryMongoConn(sess)
  72. it := sess.DB("qfw").C("jy_gateway_logs").Find(map[string]interface{}{
  73. "_id": map[string]interface{}{
  74. "$gt": _id,
  75. },
  76. }).Select(map[string]interface{}{
  77. "_id": 0,
  78. "userid_new": 1,
  79. }).Iter()
  80. var index int64
  81. for m := make(map[string]interface{}); it.Next(&m); {
  82. index++
  83. if index%50000 == 0 {
  84. log.Println("加载最近一年活跃用户", index)
  85. }
  86. userid_new := common.Int64All(m["userid_new"])
  87. if userid_new <= 0 {
  88. continue
  89. }
  90. allUser[userid_new] = true
  91. }
  92. log.Println("加载最近一年活跃用户结束。。。", _id, index)
  93. return allUser
  94. }