123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package userAnalysis
- import (
- "context"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "strings"
- "time"
- "workTasks/common"
- )
- func (ua *UserAnalysis) GetMgoUserRes(ctx context.Context) ([]*AnalysisRes, error) {
- ua.mgoUserRange(ctx)
- ua.mgoEntUserRange(ctx)
- rData := []*AnalysisRes{
- {Name: "已绑定手机号用户", Code: "binPhone", Data: ua.BinPhone},
- {Name: "已绑定邮箱用户", Code: "bindMail", Data: ua.BindMail},
- {Name: "超级订阅用户", Code: "vip", Data: ua.Vip},
- {Name: "超级订阅15天到期", Code: "vipExpire_15", Data: ua.Vip15},
- {Name: "超级订阅30天到期", Code: "vipExpire_30", Data: ua.Vip30},
- {Name: "超级订阅已经到期30天", Code: "vipExpired_30", Data: ua.VipExpired30},
- {Name: "超级订阅赠送7天到期", Code: "vipGift7DayExpired ", Data: ua.VipExpired30},
- {Name: "大会员用户", Code: "member", Data: ua.Member},
- {Name: "测试用户", Code: "testGroup", Data: ua.TestUser},
- {Name: "曾购大会员用户", Code: "memberExpired", Data: ua.MemberExpire}}
- for i := 1; i <= 7; i++ {
- rData = append(rData, &AnalysisRes{
- Name: fmt.Sprintf("近第%d天注册用户", i),
- Code: fmt.Sprintf("newUser_%d", i),
- Data: ua.NewUser[i],
- })
- //g.Dump(fmt.Sprintf("近第%d天注册用户", i), ua.NewUser[i])
- }
- rData = append(rData, &AnalysisRes{
- Name: "近第90天注册用户",
- Code: "newUser_90",
- Data: ua.NewUser[90],
- })
- return rData, nil
- }
- func (ua *UserAnalysis) mgoUserRange(ctx context.Context) {
- sess := common.MG.DB().GetMgoConn()
- defer common.MG.DB().DestoryMongoConn(sess)
- //个人身份
- it := sess.DB("qfw").C("user").Find(nil).Select(g.Map{"base_user_id": 1, "s_phone": 1, "s_m_phone": 1, "s_email": 1, "l_registedate": 1, "i_vip_status": 1, "l_vip_endtime": 1, "i_member_status": 1, "s_m_openid": 1, "i_ispush": 1}).Iter()
- var (
- index int64
- now = time.Now()
- newUserLimit = now.AddDate(0, 0, -8).Unix()
- newUser90Limit = now.AddDate(0, 0, -90).Unix()
- )
- for m := make(map[string]interface{}); it.Next(&m); {
- index++
- if index%10e4 == 0 {
- g.Log().Infof(context.TODO(), "MgoUserRange %d", index)
- }
- var (
- base_user_id = BaseUserId(gconv.Int64(m["base_user_id"]))
- s_phone = gconv.String(m["s_phone"])
- s_m_phone = gconv.String(m["s_m_phone"])
- s_email = gconv.String(m["s_email"])
- registedate = gconv.Int64(m["l_registedate"])
- openid = gconv.String(m["s_m_openid"])
- isSub = gconv.Int(m["i_ispush"])
- )
- if base_user_id == 0 {
- continue
- }
- //绑定手机号用户
- if len(s_phone) == 11 || len(s_m_phone) == 11 {
- ua.BinPhone[base_user_id] = true
- }
- //近8天注册用户
- if registedate > newUserLimit {
- //过滤取关
- if (openid != "" && isSub == 1 && !ua.UnSubUser[openid]) || openid == "" {
- //if !ua.UnSubUser[openid] {
- day := 8 - gconv.Int((registedate-newUserLimit)/(60*60*24))
- if _, ok := ua.NewUser[day]; ok {
- ua.NewUser[day][base_user_id] = true
- } else {
- ua.NewUser[day] = map[BaseUserId]bool{base_user_id: true}
- }
- }
- }
- if registedate > newUser90Limit {
- if _, ok := ua.NewUser[90]; ok {
- ua.NewUser[90][base_user_id] = true
- } else {
- ua.NewUser[90] = map[BaseUserId]bool{base_user_id: true}
- }
- }
- for _, p := range g.Cfg().MustGet(ctx, "testUserPhone").Strings() {
- if (p == s_phone || p == s_m_phone) && p != "" {
- ua.TestUser[base_user_id] = true
- }
- }
- //绑定邮箱用户
- if s_email != "" && strings.Contains(s_email, "@") {
- ua.BindMail[base_user_id] = true
- }
- //超级订阅用户处理
- vip_status := gconv.Int(m["i_vip_status"])
- if vip_status > 0 {
- ua.Vip[base_user_id] = true
- if timeStamp := gconv.Int64(m["l_vip_endtime"]); timeStamp > 0 {
- var vip_endtime = time.Unix(timeStamp, 0)
- if vip_endtime.AddDate(0, 0, -15).Before(now) {
- ua.Vip15[base_user_id] = true
- }
- if vip_endtime.AddDate(0, 0, -30).Before(now) {
- ua.Vip30[base_user_id] = true
- }
- }
- } else if vip_status < 0 {
- if vip_status == -2 { //赠送7天超级订阅到期
- ua.VipGift7DayExpired[base_user_id] = true
- }
- if timeStamp := gconv.Int64(m["l_vip_endtime"]); timeStamp > 0 {
- var vip_endtime = time.Unix(timeStamp, 0)
- if vip_endtime.AddDate(0, 0, 30).Before(now) {
- ua.VipExpired30[base_user_id] = true //超级订阅已到期30天
- }
- }
- }
- //大会员处理
- if member_status := gconv.Int(m["i_member_status"]); member_status > 0 {
- ua.Member[base_user_id] = true
- } else if member_status != 0 {
- ua.MemberExpire[base_user_id] = true
- }
- }
- g.Log().Infof(ctx, "MgoUserRange 加载%d挑数据完成", index)
- }
- func (ua *UserAnalysis) mgoEntUserRange(ctx context.Context) {
- if len(ua.UserMapping) == 0 || len(ua.EntUserIdMapping) == 0 {
- return
- }
- sess := common.MG.DB().GetMgoConn()
- defer common.MG.DB().DestoryMongoConn(sess)
- //企业身份
- it := sess.DB("qfw").C("ent_user").Find(nil).Select(g.Map{"i_userid": 1, "l_registedate": 1, "i_vip_status": 1, "l_vip_endtime": 1, "i_member_status": 1}).Iter()
- var (
- index int64
- now = time.Now()
- )
- for m := make(map[string]interface{}); it.Next(&m); {
- index++
- if index%10e4 == 0 {
- g.Log().Infof(context.TODO(), "MgoUserRange %d", index)
- }
- var (
- entUserId = gconv.Int64(m["i_userid"])
- )
- baseUserId, ok := ua.EntUserIdMapping[entUserId]
- if !ok {
- continue
- }
- //超级订阅用户处理
- if vip_status := gconv.Int(m["i_vip_status"]); vip_status > 0 {
- ua.Vip[baseUserId] = true
- if timeStamp := gconv.Int64(m["l_vip_endtime"]); timeStamp > 0 {
- var vip_endtime = time.Unix(timeStamp, 0)
- if vip_endtime.AddDate(0, 0, -15).Before(now) {
- ua.Vip15[baseUserId] = true
- }
- if vip_endtime.AddDate(0, 0, -30).Before(now) {
- ua.Vip30[baseUserId] = true
- }
- }
- }
- //大会员处理
- if member_status := gconv.Int(m["i_member_status"]); member_status > 0 {
- ua.Member[baseUserId] = true
- } else if member_status != 0 {
- ua.MemberExpire[baseUserId] = true
- }
- }
- g.Log().Infof(ctx, "MgoUserRange 加载%d挑数据完成", index)
- }
- // GetMgoUnSubUser 加载num天 取关的用户
- func (ua *UserAnalysis) GetMgoUnSubUser(ctx context.Context, num int) {
- sess := common.MG.DB().GetMgoConn()
- defer common.MG.DB().DestoryMongoConn(sess)
- it := sess.DB("qfw").C("jy_subscribe").Find(map[string]interface{}{
- "s_event": "unsubscribe",
- "l_date": map[string]interface{}{
- "$gt": time.Now().AddDate(0, 0, num).Unix(),
- },
- }).Select(g.Map{"s_m_openid": 1}).Iter()
- for m := make(map[string]interface{}); it.Next(&m); {
- if openid := gconv.String(m["s_m_openid"]); openid != "" {
- ua.UnSubUser[openid] = true
- }
- }
- g.Log().Infof(ctx, "GetMgoUnSubUser 加载%d天%d个取关用户", num, len(ua.UnSubUser))
- }
|