forked from shen100/wemall
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorder.go
163 lines (140 loc) · 4.45 KB
/
order.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package model
import (
"strings"
"time"
)
// Order 订单
type Order 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"`
UserID uint `json:"userId"`
TotalPrice float64 `json:"totalPrice"`
Payment float64 `json:"payment"`
Freight float64 `json:"freight"`
Remark string `json:"remark"`
Discount int `json:"discount"`
DeliverStart time.Time `json:"deliverStart"`
DeliverEnd time.Time `json:"deliverEnd"`
Status int `json:"status"`
PayAt time.Time `json:"payAt"`
}
// Total 总的订单数
func (order Order) Total() int {
count := 0
if DB.Model(&Order{}).Count(&count).Error != nil {
count = 0
}
return count
}
// TotalSale 总的销售额
func (order Order) TotalSale() float64 {
result := new(struct{
TotalSale float64 `gorm:"column:totalPay"`
})
var err = DB.Table("orders").Select("sum(payment) as totalPay").Where("status = ?",
OrderStatusPaid).Scan(&result).Error
if err != nil {
return 0
}
return result.TotalSale
}
// CountByDate 指定日期的订单数
func (order Order) CountByDate(date time.Time) int {
startTime := date
startSec := startTime.Unix();
tomorrowSec := startSec + 24 * 60 * 60;
tomorrowTime := time.Unix(tomorrowSec, 0)
startYMD := startTime.Format("2006-01-02")
tomorrowYMD := tomorrowTime.Format("2006-01-02")
var count int
var err = DB.Model(&Order{}).Where("created_at >= ? AND created_at < ?",
startYMD, tomorrowYMD).Count(&count).Error
if err != nil {
return 0
}
return count
}
// TotalSaleByDate 指定日期的销售额
func (order Order) TotalSaleByDate(date time.Time) float64 {
startTime := date
startSec := startTime.Unix();
tomorrowSec := startSec + 24 * 60 * 60;
tomorrowTime := time.Unix(tomorrowSec, 0)
startStr := startTime.Format("2006-01-02")
tomorrowStr := tomorrowTime.Format("2006-01-02")
result := new(struct{
TotalPay float64 `gorm:"column:totalPay"`
})
var err = DB.Table("orders").Select("sum(payment) as totalPay").Where("pay_at >= ? AND pay_at < ? AND status = ?",
startStr, tomorrowStr, OrderStatusPaid).Scan(&result).Error
if err != nil {
return 0
}
return result.TotalPay
}
const (
// OrderStatusPending 未支付
OrderStatusPending = 0
// OrderStatusPaid 已支付
OrderStatusPaid = 1
)
// OrderPerDay 每天的订单数
type OrderPerDay []struct {
Count int `json:"count"`
CreatedAt string `gorm:"column:createdAt" json:"createdAt"`
}
// Latest30Day 近30天,每天的订单数
func (orders OrderPerDay) Latest30Day() (OrderPerDay) {
now := time.Now()
year := now.Year()
month := now.Month()
date := now.Day()
today := time.Date(year, month, date, 0, 0, 0, 0, time.Local)
before29 := today.Unix() - 29 * 24 * 60 * 60; //29天前(秒)
before29Date := time.Unix(before29, 0)
sqlData := before29Date.Format("2006-01-02")
sqlArr := []string{
"SELECT count(id) as count, DATE_FORMAT(created_at,'%Y-%m-%d') as createdAt",
"FROM `orders`",
"WHERE created_at > ?",
"GROUP BY DATE_FORMAT(created_at,'%Y-%m-%d');",
}
sql := strings.Join(sqlArr, " ")
var result OrderPerDay
var err = DB.Raw(sql, sqlData).Scan(&result).Error
if err != nil {
return nil
}
return result
}
// AmountPerDay 每天的销售额
type AmountPerDay []struct {
Amount float64 `json:"amount"`
PayAt string `gorm:"column:payAt" json:"payAt"`
}
// AmountLatest30Day 近30天,每天的销售额
func (amount AmountPerDay) AmountLatest30Day() (AmountPerDay) {
now := time.Now()
year := now.Year()
month := now.Month()
date := now.Day()
today := time.Date(year, month, date, 0, 0, 0, 0, time.Local)
before29 := today.Unix() - 29 * 24 * 60 * 60; //29天前(秒)
before29Date := time.Unix(before29, 0)
sqlData := before29Date.Format("2006-01-02")
sqlArr := []string{
"SELECT sum(payment) as amount, DATE_FORMAT(pay_at,'%Y-%m-%d') as payAt",
"FROM `orders`",
"WHERE pay_at > ? and status = ?",
"GROUP BY DATE_FORMAT(pay_at,'%Y-%m-%d');",
};
sql := strings.Join(sqlArr, " ")
var result AmountPerDay
var err = DB.Raw(sql, sqlData, OrderStatusPaid).Scan(&result).Error
if err != nil {
return nil
}
return result
}