-
Notifications
You must be signed in to change notification settings - Fork 12
/
report.go
180 lines (154 loc) · 4.35 KB
/
report.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package woocommerce
import (
"fmt"
"github.com/araddon/dateparse"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/hiscaler/woocommerce-go/entity"
jsoniter "github.com/json-iterator/go"
)
type reportService service
type ReportsQueryParams struct {
Context string `url:"context,omitempty"`
Period string `url:"period,omitempty"`
DateMin string `url:"date_min,omitempty"`
DateMax string `url:"date_max,omitempty"`
}
func (m ReportsQueryParams) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Period, validation.When(m.Period != "", validation.In("week", "month", "last_month", "year").Error("无效的报表周期"))),
validation.Field(&m.DateMin,
validation.Required.Error("报表开始时间不能为空"),
validation.By(func(value interface{}) error {
dateStr, _ := value.(string)
return IsValidateTime(dateStr)
}),
),
validation.Field(&m.DateMax,
validation.Required.Error("报表结束时间不能为空"),
validation.By(func(value interface{}) (err error) {
dateStr, _ := value.(string)
err = IsValidateTime(dateStr)
if err != nil {
return
}
dateMin, err := dateparse.ParseAny(m.DateMin)
if err != nil {
return
}
dateMax, err := dateparse.ParseAny(m.DateMax)
if err != nil {
return
}
if dateMax.Before(dateMin) {
return fmt.Errorf("结束时间不能小于 %s", m.DateMin)
}
return nil
}),
),
)
}
// All list all reports
func (s reportService) All() (items []entity.Report, err error) {
resp, err := s.httpClient.R().Get("/reports")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// Sales reports
type SalesReportsQueryParams = ReportsQueryParams
// SalesReports list all sales reports
func (s reportService) SalesReports(params SalesReportsQueryParams) (items []entity.SaleReport, err error) {
if err = params.Validate(); err != nil {
return
}
params.DateMin = ToISOTimeString(params.DateMin, true, false)
params.DateMax = ToISOTimeString(params.DateMax, false, true)
resp, err := s.httpClient.R().
SetQueryParamsFromValues(toValues(params)).
Get("/reports/sales")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// TopSellerReports list all sales reports
type TopSellerReportsQueryParams = SalesReportsQueryParams
func (s reportService) TopSellerReports(params SalesReportsQueryParams) (items []entity.TopSellerReport, err error) {
if err = params.Validate(); err != nil {
return
}
params.DateMin = ToISOTimeString(params.DateMin, true, false)
params.DateMax = ToISOTimeString(params.DateMax, false, true)
resp, err := s.httpClient.R().
SetQueryParamsFromValues(toValues(params)).
Get("/reports/top_sellers")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// CouponTotals retrieve coupons totals
func (s reportService) CouponTotals() (items []entity.CouponTotal, err error) {
resp, err := s.httpClient.R().Get("/reports/coupons/totals")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// CustomerTotals retrieve customer totals
func (s reportService) CustomerTotals() (items []entity.CustomerTotal, err error) {
resp, err := s.httpClient.R().Get("/reports/customers/totals")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// OrderTotals retrieve customer totals
func (s reportService) OrderTotals() (items []entity.OrderTotal, err error) {
resp, err := s.httpClient.R().Get("/reports/orders/totals")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// ProductTotals retrieve product totals
func (s reportService) ProductTotals() (items []entity.OrderTotal, err error) {
resp, err := s.httpClient.R().Get("/reports/products/totals")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// ReviewTotals retrieve review totals
func (s reportService) ReviewTotals() (items []entity.OrderTotal, err error) {
resp, err := s.httpClient.R().Get("/reports/reviews/totals")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}