jobOrderRange.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package userAnalysis
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/util/gconv"
  6. "time"
  7. )
  8. // TiDBOrderRange 订单分析
  9. func (ua *UserAnalysis) TiDBOrderRange(ctx context.Context) ([]*AnalysisRes, error) {
  10. if len(ua.UserMapping) == 0 {
  11. return nil, nil
  12. }
  13. order, err := g.DB("order").Query(ctx, "SELECT user_id,order_status,product_type,create_time,pay_money,is_backstage_order FROM dataexport_order")
  14. if err != nil {
  15. g.Log().Errorf(ctx, "读取订单异常")
  16. return nil, err
  17. }
  18. var (
  19. now = time.Now()
  20. hasPay = map[BaseUserId]bool{}
  21. unBuyVipMonth, unBuyMemberMonth = map[BaseUserId]bool{}, map[BaseUserId]bool{}
  22. vip, member = map[BaseUserId]int{}, map[BaseUserId]int{}
  23. )
  24. for index, m := range order.List() {
  25. if index%10e4 == 0 {
  26. g.Log().Infof(context.TODO(), "TiDBOrderRange %d/%d", index, order.Size())
  27. }
  28. var (
  29. userId = gconv.String(m["user_id"])
  30. baseUserId BaseUserId
  31. product = gconv.String(m["product_type"])
  32. orderStatus = gconv.Int(m["order_status"])
  33. )
  34. baseUserId, ok := ua.UserMapping[userId]
  35. if !ok {
  36. continue
  37. }
  38. if orderStatus == 1 {
  39. hasPay[baseUserId] = true
  40. }
  41. //统计超级订阅、大会员当月加购未购
  42. if product == "VIP订阅" || product == "大会员" {
  43. var (
  44. pay_money = gconv.Int(m["pay_money"])
  45. is_backstage_order = gconv.Int(m["is_backstage_order"])
  46. )
  47. t, err := time.ParseInLocation(time.DateTime, gconv.String(m["create_time"]), time.Local)
  48. if err != nil {
  49. continue
  50. }
  51. if now.Month() == t.Month() && now.Year() == t.Year() {
  52. if product == "VIP订阅" {
  53. if orderStatus <= 0 && pay_money == 0 && is_backstage_order == 0 {
  54. vip[baseUserId] += 0
  55. } else {
  56. vip[baseUserId] += 1
  57. }
  58. }
  59. if product == "大会员" {
  60. if orderStatus <= 0 && pay_money == 0 && is_backstage_order == 0 {
  61. member[baseUserId] += 0
  62. } else {
  63. member[baseUserId] += 1
  64. }
  65. }
  66. }
  67. }
  68. }
  69. for id, i := range vip {
  70. if i == 0 {
  71. unBuyVipMonth[id] = true
  72. }
  73. }
  74. for id, i := range member {
  75. if i == 0 {
  76. unBuyMemberMonth[id] = true
  77. }
  78. }
  79. return []*AnalysisRes{
  80. {Name: "付费用户", Code: "hasPay", Data: hasPay},
  81. {Name: "本月加购未购超级订阅", Code: "unBuyVipMonth", Data: unBuyVipMonth},
  82. {Name: "本月加购未购大会员", Code: "unBuyMemberMonth", Data: unBuyMemberMonth}},
  83. nil
  84. }