forked from shen100/wemall
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuser.go
129 lines (115 loc) · 4.06 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package model
import (
"strings"
"time"
)
// WeAppUser 微信用户
type WeAppUser struct {
OpenID string `json:"openId"`
Nickname string `json:"nickName"`
Gender int `json:"gender"`
AvatarURL string `json:"avatarUrl"`
}
// User 用户
type User struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `sql:"index" json:"deletedAt"`
ContactID string `json:"contactId"` //默认地址
OpenID string `json:"openId"`
Nickname string `json:"nickname"`
Username string `json:"username"`
Email string `json:"email"`
Phone string `json:"phone"`
Password string `json:"password"`
Token string `json:"token"`
Sex bool `json:"sex"`
Subscribe bool `json:"subscribe"`
Status int `json:"status"`
Lastip string `json:"lastip"`
}
// YesterdayRegisterUser 昨日注册的用户数
func (user User) YesterdayRegisterUser() int {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
todaySec := today.Unix() //秒
yesterdaySec := todaySec - 24 * 60 * 60; //秒
yesterdayTime := time.Unix(yesterdaySec, 0)
todayYMD := today.Format("2006-01-02")
yesterdayYMD := yesterdayTime.Format("2006-01-02")
var count int
var err = DB.Model(&User{}).Where("created_at >= ? AND created_at < ?", yesterdayYMD, todayYMD).Count(&count).Error
if err != nil {
return 0
}
return count
}
// TodayRegisterUser 今日注册的用户数
func (user User) TodayRegisterUser() int {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
todaySec := today.Unix() //秒
tomorrowSec := todaySec + 24 * 60 * 60; //秒
tomorrowTime := time.Unix(tomorrowSec, 0)
todayYMD := today.Format("2006-01-02")
tomorrowYMD := tomorrowTime.Format("2006-01-02")
var count int
var err = DB.Model(&User{}).Where("created_at >= ? AND created_at < ?", todayYMD, tomorrowYMD).Count(&count).Error
if err != nil {
return 0
}
return count
}
// PurchaseUserByDate 指定日期有消费形为的用户数
func (user User) PurchaseUserByDate(date time.Time) int {
startTime := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
startSec := startTime.Unix() //秒
endSec := startSec + 24 * 60 * 60;
endTime := time.Unix(endSec, 0)
startYMD := startTime.Format("2006-01-02")
endYMD := endTime.Format("2006-01-02")
sqlArr := []string{
"SELECT COUNT(DISTINCT user_id) as count, DATE_FORMAT(pay_at,'%Y-%m-%d') as payAt",
"FROM orders",
"WHERE pay_at >= ? and pay_at < ? and status = ?",
"GROUP BY DATE_FORMAT(pay_at,'%Y-%m-%d');",
};
sql := strings.Join(sqlArr, " ")
result := new(struct{
Count int
PayAt string `gorm:"column:payAt"`
})
var err = DB.Raw(sql, startYMD, endYMD, OrderStatusPaid).Scan(&result).Error
if err != nil {
return 0
}
return result.Count;
}
// UserPerDay 每天的用户数
type UserPerDay []struct {
Count int `json:"count"`
CreatedAt string `gorm:"column:createdAt" json:"createdAt"`
}
// Latest30Day 近30天,每天注册的新用户数
func (users UserPerDay) Latest30Day() (UserPerDay) {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
todaySec := today.Unix() //秒
before29 := todaySec - 29 * 24 * 60 * 60;
before29Date := time.Unix(before29, 0)
sqlData := before29Date.Format("2006-01-02")
sqlArr := []string{
"SELECT count(*) as count, DATE_FORMAT(created_at,'%Y-%m-%d') as createdAt",
"FROM users",
"WHERE created_at >= ?",
"GROUP BY DATE_FORMAT(created_at,'%Y-%m-%d');",
};
sql := strings.Join(sqlArr, " ")
var result UserPerDay
var err = DB.Raw(sql, sqlData).Scan(&result).Error
if err != nil {
return nil
}
return result
}