jobMgoUserRange.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package userAnalysis
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "strings"
  8. "time"
  9. "workTasks/common"
  10. )
  11. func (ua *UserAnalysis) GetMgoUserRes(ctx context.Context) ([]*AnalysisRes, error) {
  12. ua.mgoUserRange(ctx)
  13. ua.mgoEntUserRange(ctx)
  14. rData := []*AnalysisRes{
  15. {Name: "已绑定手机号用户", Code: "binPhone", Data: ua.BinPhone},
  16. {Name: "已绑定邮箱用户", Code: "bindMail", Data: ua.BindMail},
  17. {Name: "超级订阅用户", Code: "vip", Data: ua.Vip},
  18. {Name: "超级订阅15天到期", Code: "vipExpire_15", Data: ua.Vip15},
  19. {Name: "超级订阅30天到期", Code: "vipExpire_30", Data: ua.Vip30},
  20. {Name: "超级订阅已经到期30天", Code: "vipExpired_30", Data: ua.VipExpired30},
  21. {Name: "超级订阅赠送7天到期", Code: "vipGift7DayExpired ", Data: ua.VipExpired30},
  22. {Name: "大会员用户", Code: "member", Data: ua.Member},
  23. {Name: "测试用户", Code: "testGroup", Data: ua.TestUser},
  24. {Name: "曾购大会员用户", Code: "memberExpired", Data: ua.MemberExpire}}
  25. for i := 1; i <= 7; i++ {
  26. rData = append(rData, &AnalysisRes{
  27. Name: fmt.Sprintf("近第%d天注册用户", i),
  28. Code: fmt.Sprintf("newUser_%d", i),
  29. Data: ua.NewUser[i],
  30. })
  31. //g.Dump(fmt.Sprintf("近第%d天注册用户", i), ua.NewUser[i])
  32. }
  33. rData = append(rData, &AnalysisRes{
  34. Name: "近第90天注册用户",
  35. Code: "newUser_90",
  36. Data: ua.NewUser[90],
  37. })
  38. return rData, nil
  39. }
  40. func (ua *UserAnalysis) mgoUserRange(ctx context.Context) {
  41. sess := common.MG.DB().GetMgoConn()
  42. defer common.MG.DB().DestoryMongoConn(sess)
  43. //个人身份
  44. 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()
  45. var (
  46. index int64
  47. now = time.Now()
  48. newUserLimit = now.AddDate(0, 0, -8).Unix()
  49. newUser90Limit = now.AddDate(0, 0, -90).Unix()
  50. )
  51. for m := make(map[string]interface{}); it.Next(&m); {
  52. index++
  53. if index%10e4 == 0 {
  54. g.Log().Infof(context.TODO(), "MgoUserRange %d", index)
  55. }
  56. var (
  57. base_user_id = BaseUserId(gconv.Int64(m["base_user_id"]))
  58. s_phone = gconv.String(m["s_phone"])
  59. s_m_phone = gconv.String(m["s_m_phone"])
  60. s_email = gconv.String(m["s_email"])
  61. registedate = gconv.Int64(m["l_registedate"])
  62. openid = gconv.String(m["s_m_openid"])
  63. isSub = gconv.Int(m["i_ispush"])
  64. )
  65. if base_user_id == 0 {
  66. continue
  67. }
  68. //绑定手机号用户
  69. if len(s_phone) == 11 || len(s_m_phone) == 11 {
  70. ua.BinPhone[base_user_id] = true
  71. }
  72. //近8天注册用户
  73. if registedate > newUserLimit {
  74. //过滤取关
  75. if (openid != "" && isSub == 1 && !ua.UnSubUser[openid]) || openid == "" {
  76. //if !ua.UnSubUser[openid] {
  77. day := 8 - gconv.Int((registedate-newUserLimit)/(60*60*24))
  78. if _, ok := ua.NewUser[day]; ok {
  79. ua.NewUser[day][base_user_id] = true
  80. } else {
  81. ua.NewUser[day] = map[BaseUserId]bool{base_user_id: true}
  82. }
  83. }
  84. }
  85. if registedate > newUser90Limit {
  86. if _, ok := ua.NewUser[90]; ok {
  87. ua.NewUser[90][base_user_id] = true
  88. } else {
  89. ua.NewUser[90] = map[BaseUserId]bool{base_user_id: true}
  90. }
  91. }
  92. for _, p := range g.Cfg().MustGet(ctx, "testUserPhone").Strings() {
  93. if (p == s_phone || p == s_m_phone) && p != "" {
  94. ua.TestUser[base_user_id] = true
  95. }
  96. }
  97. //绑定邮箱用户
  98. if s_email != "" && strings.Contains(s_email, "@") {
  99. ua.BindMail[base_user_id] = true
  100. }
  101. //超级订阅用户处理
  102. vip_status := gconv.Int(m["i_vip_status"])
  103. if vip_status > 0 {
  104. ua.Vip[base_user_id] = true
  105. if timeStamp := gconv.Int64(m["l_vip_endtime"]); timeStamp > 0 {
  106. var vip_endtime = time.Unix(timeStamp, 0)
  107. if vip_endtime.AddDate(0, 0, -15).Before(now) {
  108. ua.Vip15[base_user_id] = true
  109. }
  110. if vip_endtime.AddDate(0, 0, -30).Before(now) {
  111. ua.Vip30[base_user_id] = true
  112. }
  113. }
  114. } else if vip_status < 0 {
  115. if vip_status == -2 { //赠送7天超级订阅到期
  116. ua.VipGift7DayExpired[base_user_id] = true
  117. }
  118. if timeStamp := gconv.Int64(m["l_vip_endtime"]); timeStamp > 0 {
  119. var vip_endtime = time.Unix(timeStamp, 0)
  120. if vip_endtime.AddDate(0, 0, 30).Before(now) {
  121. ua.VipExpired30[base_user_id] = true //超级订阅已到期30天
  122. }
  123. }
  124. }
  125. //大会员处理
  126. if member_status := gconv.Int(m["i_member_status"]); member_status > 0 {
  127. ua.Member[base_user_id] = true
  128. } else if member_status != 0 {
  129. ua.MemberExpire[base_user_id] = true
  130. }
  131. }
  132. g.Log().Infof(ctx, "MgoUserRange 加载%d挑数据完成", index)
  133. }
  134. func (ua *UserAnalysis) mgoEntUserRange(ctx context.Context) {
  135. if len(ua.UserMapping) == 0 || len(ua.EntUserIdMapping) == 0 {
  136. return
  137. }
  138. sess := common.MG.DB().GetMgoConn()
  139. defer common.MG.DB().DestoryMongoConn(sess)
  140. //企业身份
  141. 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()
  142. var (
  143. index int64
  144. now = time.Now()
  145. )
  146. for m := make(map[string]interface{}); it.Next(&m); {
  147. index++
  148. if index%10e4 == 0 {
  149. g.Log().Infof(context.TODO(), "MgoUserRange %d", index)
  150. }
  151. var (
  152. entUserId = gconv.Int64(m["i_userid"])
  153. )
  154. baseUserId, ok := ua.EntUserIdMapping[entUserId]
  155. if !ok {
  156. continue
  157. }
  158. //超级订阅用户处理
  159. if vip_status := gconv.Int(m["i_vip_status"]); vip_status > 0 {
  160. ua.Vip[baseUserId] = true
  161. if timeStamp := gconv.Int64(m["l_vip_endtime"]); timeStamp > 0 {
  162. var vip_endtime = time.Unix(timeStamp, 0)
  163. if vip_endtime.AddDate(0, 0, -15).Before(now) {
  164. ua.Vip15[baseUserId] = true
  165. }
  166. if vip_endtime.AddDate(0, 0, -30).Before(now) {
  167. ua.Vip30[baseUserId] = true
  168. }
  169. }
  170. }
  171. //大会员处理
  172. if member_status := gconv.Int(m["i_member_status"]); member_status > 0 {
  173. ua.Member[baseUserId] = true
  174. } else if member_status != 0 {
  175. ua.MemberExpire[baseUserId] = true
  176. }
  177. }
  178. g.Log().Infof(ctx, "MgoUserRange 加载%d挑数据完成", index)
  179. }
  180. // GetMgoUnSubUser 加载num天 取关的用户
  181. func (ua *UserAnalysis) GetMgoUnSubUser(ctx context.Context, num int) {
  182. sess := common.MG.DB().GetMgoConn()
  183. defer common.MG.DB().DestoryMongoConn(sess)
  184. it := sess.DB("qfw").C("jy_subscribe").Find(map[string]interface{}{
  185. "s_event": "unsubscribe",
  186. "l_date": map[string]interface{}{
  187. "$gt": time.Now().AddDate(0, 0, num).Unix(),
  188. },
  189. }).Select(g.Map{"s_m_openid": 1}).Iter()
  190. for m := make(map[string]interface{}); it.Next(&m); {
  191. if openid := gconv.String(m["s_m_openid"]); openid != "" {
  192. ua.UnSubUser[openid] = true
  193. }
  194. }
  195. g.Log().Infof(ctx, "GetMgoUnSubUser 加载%d天%d个取关用户", num, len(ua.UnSubUser))
  196. }