Ver Fonte

fix:活跃用户判断

duxin há 1 ano atrás
pai
commit
1b6a801c2a

+ 3 - 0
doFreeClueSign/config.yaml

@@ -19,6 +19,9 @@ database:
     link: "mysql:root:=PDT49#80Z!RVv52_z@tcp(192.168.3.14:4000)/debris_product"
     debug: true
 
+runCron: "# 0 2 * * *" #每天凌晨2点执行
+payTidb: "jianyu_subjectdb_test.dwd_f_data_equity_info"
+
 cron:
   updatePayUser: "# */30 * * * *" #更新付费用户
   activityUser: "# */30 * * * *" #更新新活跃用户

+ 0 - 3
doFreeClueSign/main.go

@@ -2,10 +2,7 @@ package main
 
 import (
 	"context"
-<<<<<<< HEAD
 	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
-=======
->>>>>>> master
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/gcron"
 	"github.com/gogf/gf/v2/os/gctx"

+ 61 - 2
doFreeClueSign/public/getNewActiveUser.go

@@ -1,6 +1,12 @@
 package public
 
-import "time"
+import (
+	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/util/gconv"
+	"sync"
+	"time"
+)
 
 type NewActiveMsg struct {
 	MgoUserID string
@@ -8,5 +14,58 @@ type NewActiveMsg struct {
 }
 
 func GetNewActiveUser(st, ed time.Time) []*NewActiveMsg {
-	return nil
+	var mgoIds []string
+	stYear, stMonth, stDay := st.Date()
+	edYear, edMonth, edDay := ed.Date()
+	nst := st.AddDate(0, -1, 0)
+	_, nstMonth, nstDay := nst.Date()
+	var sql, nSql string
+	if stYear == edYear && stMonth == edMonth && stDay == edDay {
+		sql = fmt.Sprintf("month = %d and day = %d", int(stMonth), stDay)
+	} else if stMonth == edMonth {
+		sql = fmt.Sprintf("month = %d and day >= %d and day <= %d", int(stMonth), stDay, edDay)
+	} else {
+		sql = fmt.Sprintf("(month = %d and day >= %d) or (month = %d and day <= %d)", int(stMonth), stDay, int(edMonth), edDay)
+	}
+
+	//昨天所有活跃用户
+	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
+	)
+	if nstMonth == stMonth {
+		nSql = fmt.Sprintf("month = %d and day >= %d and day < %d", int(stMonth), nstDay, stDay)
+	} else {
+		nSql = fmt.Sprintf("((month = %d and day >= %d) or (month = %d and day < %d))", int(nstMonth), nstDay, int(stMonth), stDay)
+	}
+	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().GetCount(ctx, fmt.Sprintf("user_mongoid = '%s' and  %s", mId, nSql))
+			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
 }

+ 13 - 28
doFreeClueSign/public/getPayUser.go

@@ -1,39 +1,24 @@
 package public
 
 import (
-	"doFreeClueSign/db"
+	"context"
+	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/util/gconv"
 )
 
-func GetPayUser() map[string]bool {
-	var pay = map[string]bool{}
-
-	//todo 取mongo user付费用户
-	query := gconv.Map(`{"$or":[{"i_vip_status":{"$gt":0}},{"i_member_status":{"$gt":0}}]}`)
-	res, _ := db.MG.DB().Find("user", query, nil, `{"s_phone":1,"s_m_phone":1}`, false, -1, -1)
-	if res != nil && len(*res) > 0 {
-		for _, m := range *res {
-			var (
-				s_phone   = gconv.String(m["s_phone"])
-				s_m_phone = gconv.String(m["s_m_phone"])
-				phone     string
-			)
-
-			if s_phone != "" {
-				phone = s_phone
-			} else if s_m_phone != "" {
-				phone = s_m_phone
-			}
+var (
+	ctx = context.Background()
+)
 
-			if phone == "" {
-				continue
-			}
-			pay[phone] = true
+func GetPayUser() map[string]bool {
+	payTidb := g.Cfg().MustGet(ctx, "payTidb", "Jianyu_subjectdb.dwd_f_data_equity_info").String()
+	pay := make(map[string]bool)
+	res, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT id,user_id FROM %s`, payTidb))
+	if err == nil && !res.IsEmpty() {
+		for _, m := range res.List() {
+			pay[gconv.String(m["user_id"])] = true
 		}
 	}
-	//todo 企业子账号
-
-	//todo 企业身份下购买
-
 	return pay
 }