package public import ( "context" "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "sync" "time" ) type NewActiveMsg struct { MgoUserID string TimeStamp int64 //活跃时间 } func GetNewActiveUser(st time.Time) ([]*NewActiveMsg, time.Time) { var ( mgoIds []string ctx = context.Background() ) //统计昨天的所有注册用户 过滤掉 end := st.AddDate(0, 0, 1) ed := time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, st.Location()) stYear, stMonth, stDay := st.Date() nst := st.AddDate(0, -1, 0) _, nstMonth, nstDay := nst.Date() //昨天所有活跃用户 res, err := g.DB("useranaly").Query(ctx, fmt.Sprintf(`SELECT DISTINCT user_mongoid FROM user_countbyhour WHERE CONCAT(month, '-', day) = '%s'`, fmt.Sprintf("%d-%d", int(stMonth), stDay))) //res, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT DISTINCT user_mongoid FROM user_countbyhour WHERE %s `, sql)) if err == nil && !res.IsEmpty() { for _, m := range res.List() { mongoid := gconv.String(m["user_mongoid"]) mgoIds = append(mgoIds, mongoid) } } var ( lock sync.Mutex data []*NewActiveMsg ) pool := make(chan bool, 5) wait := &sync.WaitGroup{} for _, id := range mgoIds { pool <- true wait.Add(1) go func(mId string) { defer func() { wait.Done() <-pool }() count, _ := g.DB("useranaly").GetCount(ctx, fmt.Sprintf(`SELECT count(1) FROM user_countbyhour WHERE user_mongoid = '%s' and CONCAT(month, '-', day) >= '%s' AND CONCAT(month, '-', day) < '%s';`, mId, fmt.Sprintf("%d-%d", int(nstMonth), nstDay), fmt.Sprintf("%d-%d", int(stMonth), stDay))) if count == 0 { //统计昨天之前30天不活跃用户 lock.Lock() data = append(data, &NewActiveMsg{ MgoUserID: mId, TimeStamp: time.Date(stYear, stMonth, stDay, 0, 0, 0, 0, time.Local).Unix(), }) lock.Unlock() } }(id) } wait.Wait() return data, ed }