123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package userAnalysis
- import (
- "context"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "time"
- )
- // TiDBOrderRange 订单分析
- func (ua *UserAnalysis) TiDBOrderRange(ctx context.Context) ([]*AnalysisRes, error) {
- if len(ua.UserMapping) == 0 {
- return nil, nil
- }
- order, err := g.DB("order").Query(ctx, "SELECT user_id,order_status,product_type,create_time,pay_money,is_backstage_order FROM dataexport_order")
- if err != nil {
- g.Log().Errorf(ctx, "读取订单异常")
- return nil, err
- }
- var (
- now = time.Now()
- hasPay = map[BaseUserId]bool{}
- unBuyVipMonth, unBuyMemberMonth = map[BaseUserId]bool{}, map[BaseUserId]bool{}
- vip, member = map[BaseUserId]int{}, map[BaseUserId]int{}
- )
- for index, m := range order.List() {
- if index%10e4 == 0 {
- g.Log().Infof(context.TODO(), "TiDBOrderRange %d/%d", index, order.Size())
- }
- var (
- userId = gconv.String(m["user_id"])
- baseUserId BaseUserId
- product = gconv.String(m["product_type"])
- orderStatus = gconv.Int(m["order_status"])
- )
- baseUserId, ok := ua.UserMapping[userId]
- if !ok {
- continue
- }
- if orderStatus == 1 {
- hasPay[baseUserId] = true
- }
- //统计超级订阅、大会员当月加购未购
- if product == "VIP订阅" || product == "大会员" {
- var (
- pay_money = gconv.Int(m["pay_money"])
- is_backstage_order = gconv.Int(m["is_backstage_order"])
- )
- t, err := time.ParseInLocation(time.DateTime, gconv.String(m["create_time"]), time.Local)
- if err != nil {
- continue
- }
- if now.Month() == t.Month() && now.Year() == t.Year() {
- if product == "VIP订阅" {
- if orderStatus <= 0 && pay_money == 0 && is_backstage_order == 0 {
- vip[baseUserId] += 0
- } else {
- vip[baseUserId] += 1
- }
- }
- if product == "大会员" {
- if orderStatus <= 0 && pay_money == 0 && is_backstage_order == 0 {
- member[baseUserId] += 0
- } else {
- member[baseUserId] += 1
- }
- }
- }
- }
- }
- for id, i := range vip {
- if i == 0 {
- unBuyVipMonth[id] = true
- }
- }
- for id, i := range member {
- if i == 0 {
- unBuyMemberMonth[id] = true
- }
- }
- return []*AnalysisRes{
- {Name: "付费用户", Code: "hasPay", Data: hasPay},
- {Name: "本月加购未购超级订阅", Code: "unBuyVipMonth", Data: unBuyVipMonth},
- {Name: "本月加购未购大会员", Code: "unBuyMemberMonth", Data: unBuyMemberMonth}},
- nil
- }
|