forked from injoyai/tdx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.go
More file actions
242 lines (202 loc) · 4.21 KB
/
Copy pathmanage.go
File metadata and controls
242 lines (202 loc) · 4.21 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package tdx
import (
"sync"
"github.com/injoyai/tdx/lib/xorms"
"github.com/robfig/cron/v3"
)
const (
DefaultClients = 1
DefaultRetry = 3
DefaultDataDir = "./data"
DefaultDatabaseDir = "./data/database"
DefaultCodesSpec = "0 1 9 * * *"
DefaultWorkdaySpec = "0 3 9 * * *"
DefaultGbbqSpec = "0 5 9 * * *"
)
func NewManageMysql(dsn string, op ...Option) (*Manage, error) {
return NewManage(
WithDialCodes(func(c *Client) (ICodes, error) {
return NewCodesMysql(dsn, WithCodesClient(c))
}),
WithDialWorkday(func(c *Client) (*Workday, error) {
return NewWorkdayMysql(dsn, WithWorkdayClient(c))
}),
WithOption(op...),
)
}
func NewManage(op ...Option) (m *Manage, err error) {
m = &Manage{
poolClients: DefaultClients,
Gbbq: &Gbbq{}, //默认不初始化股本变迁数据
}
for _, v := range op {
if v != nil {
v(m)
}
}
//连接池
if m.IPool == nil {
if m.dialPool == nil {
m.dialPool = func() (IPool, error) {
return NewPool(func() (*Client, error) { return DialDefault() }, m.poolClients)
}
}
m.IPool, err = m.dialPool()
if err != nil {
return nil, err
}
}
//取出一个客户端
c, err := m.IPool.Get()
if err != nil {
return nil, err
}
defer m.IPool.Put(c)
//代码管理
if m.Codes == nil {
if m.dialCodes == nil {
m.dialCodes = func(c *Client) (ICodes, error) { return NewCodes(WithCodesClient(c)) }
}
m.Codes, err = m.dialCodes(c)
if err != nil {
return nil, err
}
}
//工作日管理
if m.Workday == nil {
if m.dialWorkday == nil {
m.dialWorkday = func(c *Client) (*Workday, error) { return NewWorkday(WithWorkdayClient(c)) }
}
m.Workday, err = m.dialWorkday(c)
if err != nil {
return nil, err
}
}
//股本管理
if m.Gbbq == nil {
if m.dialGbbq == nil {
m.dialGbbq = func(c *Client) (IGbbq, error) { return NewGbbq(WithGbbqClient(c)) }
}
m.Gbbq, err = m.dialGbbq(c)
if err != nil {
return nil, err
}
}
return
}
/*
*/
type (
Option func(m *Manage)
DialDBFunc func() (*xorms.Engine, error)
DialClientFunc func() (*Client, error)
)
func WithClients(clients int) Option {
return func(m *Manage) {
m.poolClients = clients
}
}
func WithPool(pool IPool) Option {
return func(m *Manage) {
m.IPool = pool
}
}
func WithDialPool(dial DialPoolFunc) Option {
return func(m *Manage) {
m.IPool = nil
m.dialPool = dial
}
}
func WithCodes(codes ICodes) Option {
return func(m *Manage) {
m.Codes = codes
}
}
func WithDialCodes(dial DialCodesFunc) Option {
return func(m *Manage) {
m.Codes = nil
m.dialCodes = dial
}
}
func WithWorkday(w *Workday) Option {
return func(m *Manage) {
m.Workday = w
}
}
func WithDialWorkday(dial DialWorkdayFunc) Option {
return func(m *Manage) {
m.Workday = nil
m.dialWorkday = dial
}
}
func WithGbbq(gbbq IGbbq) Option {
return func(m *Manage) {
m.Gbbq = gbbq
}
}
func WithDialGbbq(dial DialGbbqFunc) Option {
return func(m *Manage) {
m.Gbbq = nil
m.dialGbbq = dial
}
}
func WithDialGbbqDefault() Option {
return WithDialGbbq(func(c *Client) (IGbbq, error) {
return NewGbbq(WithGbbqClient(c))
})
}
func WithOption(op ...Option) Option {
return func(m *Manage) {
for _, v := range op {
if v != nil {
v(m)
}
}
}
}
type Manage struct {
poolClients int
dialPool DialPoolFunc
dialCodes DialCodesFunc
dialWorkday DialWorkdayFunc
dialGbbq DialGbbqFunc
IPool
Codes ICodes
Workday *Workday
Gbbq IGbbq
/*
*/
cron *cron.Cron
once sync.Once
}
// RangeStocks 遍历所有股票
func (this *Manage) RangeStocks(f func(code string)) {
for _, v := range this.Codes.GetStocks() {
f(v.FullCode())
}
}
// RangeETFs 遍历所有ETF
func (this *Manage) RangeETFs(f func(code string)) {
for _, v := range this.Codes.GetETFs() {
f(v.FullCode())
}
}
// RangeIndexes 遍历所有指数
func (this *Manage) RangeIndexes(f func(code string)) {
for _, v := range this.Codes.GetETFs() {
f(v.FullCode())
}
}
// AddWorkdayTask 添加工作日任务
func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) error {
this.once.Do(func() {
this.cron = cron.New(cron.WithSeconds())
this.cron.Start()
})
_, err := this.cron.AddFunc(spec, func() {
if this.Workday.TodayIs() {
f(this)
}
})
return err
}