-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade.go
211 lines (190 loc) · 5.77 KB
/
trade.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package model
import (
"fmt"
"strconv"
"time"
"go-common/app/admin/main/growup/util"
)
// GoodsInfo .
type GoodsInfo struct {
// internal
ID int64 `json:"id"`
ProductID string `json:"product_id"`
ResourceID int64 `json:"-"`
GoodsType GoodsType `json:"-"`
Discount int `json:"discount"`
IsDisplay DisplayStatus `json:"is_display"`
DisplayOnTime time.Time `json:"-"`
// derived
GoodsTypeDesc string `json:"goods_type"` // 商品类型描述
// external
ProductName string `json:"product_name"` // 商品名称
OriginPrice int64 `json:"origin_price"` // 实时成本, 单位分
CurrentPrice int64 `json:"current_price"` // 实时售价, 单位分
Month int32 `json:"month"` //有效期
}
// MergeExternal information from src to target
func MergeExternal(target *GoodsInfo, src *GoodsInfo) error {
switch target.GoodsType {
case GoodsVIP:
target.OriginPrice = src.OriginPrice
target.ProductName = src.ProductName
target.CurrentPrice = int64(util.DivWithRound(float64(target.OriginPrice*int64(target.Discount)), 100, 0))
target.Month = src.Month
return nil
default:
return fmt.Errorf("illegal type of goods(%v)", target)
}
}
// OrderInfo .
type OrderInfo struct {
// internal
ID int64 `json:"-"`
MID int64 `json:"mid"`
OrderNo string `json:"order_no"`
OrderTime time.Time `json:"-"`
GoodsType GoodsType `json:"-"`
GoodsID string `json:"goods_id"`
GoodsName string `json:"goods_name"`
GoodsPrice int64 `json:"goods_price"`
GoodsCost int64 `json:"goods_cost"`
// desc for front end
GoodsTypeDesc string `json:"goods_type"` // 商品类型描述
OrderTimeDesc string `json:"order_time"` // 订单时间
// derived
TotalPrice int64 `json:"total_price"`
TotalCost int64 `json:"total_cost"`
GoodsNum int64 `json:"goods_num"`
// external
Nickname string `json:"nickname"`
}
// OrderExportFields .
func OrderExportFields() []string {
return []string{"订单ID", "时间", "商品ID", "商品名称", "售价", "成本", "数量", "总实收", "总成本", "UID", "昵称"}
}
// ExportStrings .
func (v *OrderInfo) ExportStrings() []string {
return []string{
v.OrderNo,
v.OrderTimeDesc,
v.GoodsID,
v.GoodsName,
strconv.FormatFloat(util.Div(float64(v.GoodsPrice), float64(100)), 'f', 2, 64),
strconv.FormatFloat(util.Div(float64(v.GoodsCost), float64(100)), 'f', 2, 64),
strconv.FormatInt(v.GoodsNum, 10),
strconv.FormatFloat(util.Div(float64(v.TotalPrice), float64(100)), 'f', 2, 64),
strconv.FormatFloat(util.Div(float64(v.TotalCost), float64(100)), 'f', 2, 64),
strconv.FormatInt(v.MID, 10),
v.Nickname,
}
}
// GenDerived generates derived information
func (v *OrderInfo) GenDerived() *OrderInfo {
v.GoodsNum = 1
v.TotalPrice = v.GoodsPrice
v.TotalCost = v.GoodsCost
return v
}
// GenDesc generates descriptions
func (v *OrderInfo) GenDesc() *OrderInfo {
v.GoodsTypeDesc = v.GoodsType.Desc()
v.OrderTimeDesc = v.OrderTime.Format("2006-01-02 15:04:05")
return v
}
// DisplayStatus .
type DisplayStatus int
// DisplayStatuses enum
const (
DisplayOff DisplayStatus = 1
DisplayOn DisplayStatus = 2
)
// GoodsType .
type GoodsType int
// GoodsTypes enum
const (
GoodsVIP GoodsType = 1
)
// Desc of GoodsType
func (t GoodsType) Desc() string {
switch t {
case GoodsVIP:
return "大会员"
default:
return "未定义商品类型 " + string(t)
}
}
// TimeType .
type TimeType int
// TimeTypes enum
const (
Daily TimeType = 1 + iota
Weekly
Monthly
)
// RangeStart returns the included startTime
func (t TimeType) RangeStart(date time.Time) time.Time {
if t == Weekly {
n := int(date.Weekday() - time.Monday)
if n < 0 {
n += 7
}
return time.Date(date.Year(), date.Month(), date.Day()-n, 0, 0, 0, 0, time.Local)
} else if t == Monthly {
return time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, time.Local)
}
return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
}
// RangeEnd returns the excluded endTime
func (t TimeType) RangeEnd(date time.Time) time.Time {
if t == Weekly {
n := int(time.Monday - date.Weekday())
if n <= 0 {
n += 7
}
return time.Date(date.Year(), date.Month(), date.Day()+n, 0, 0, 0, 0, time.Local)
} else if t == Monthly {
return time.Date(date.Year(), date.Month()+1, 1, 0, 0, 0, 0, time.Local)
} else if t == Daily {
return time.Date(date.Year(), date.Month(), date.Day()+1, 0, 0, 0, 0, time.Local)
}
return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
}
// RangeDesc .
func (t TimeType) RangeDesc(start time.Time, end time.Time) string {
if t == Daily {
return start.Format("2006-01-02")
}
return start.Format("2006-01-02") + "~" + end.AddDate(0, 0, -1).Format("2006-01-02")
}
// Next returns time on next range
func (t TimeType) Next() func(time.Time) time.Time {
return func(start time.Time) time.Time {
switch t {
case Daily:
return start.AddDate(0, 0, 1)
case Weekly:
return start.AddDate(0, 0, 7)
case Monthly:
return start.AddDate(0, 1, 0)
default:
return start.AddDate(0, 0, 1)
}
}
}
// OrderQueryArg .
type OrderQueryArg struct {
TimeType TimeType `form:"time_type" default:"1"`
FromTime int64 `form:"from_time" validate:"required,min=1"`
ToTime int64 `form:"to_time" validate:"required,min=1"`
GoodsType int `form:"goods_type"`
GoodsID string `form:"goods_id"`
GoodsName string `form:"goods_name"`
OrderNO string `form:"order_no"`
MID int64 `form:"mid"`
Nickname string `form:"nickname"`
From int `form:"from" validate:"min=0" default:"0"`
Limit int `form:"limit" validate:"min=1" default:"20"`
// fromTime + toTime + timeType => (included) startTime & (excluded) endTime
StartTime time.Time `form:"-"`
EndTime time.Time `form:"-"`
}