mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26e2479e2f | ||
|
|
6d0125afef | ||
|
|
2d77d769fd | ||
|
|
33627c3d6c | ||
|
|
1ff1ceb8d7 | ||
|
|
233d1b689e | ||
|
|
2a27eea873 | ||
|
|
fcfb329712 | ||
|
|
ed2c814fab | ||
|
|
fc04b5042a | ||
|
|
b2a4c00253 | ||
|
|
c68c7582bc | ||
|
|
4e62ee1c5e |
@@ -1,9 +1,8 @@
|
||||
### 说明
|
||||
|
||||
* 参考golang库 [`https://github.com/bensema/gotdx`](https://github.com/bensema/gotdx)
|
||||
* 参考python库 [`https://github.com/mootdx/mootdx`](https://github.com/mootdx/mootdx)
|
||||
|
||||
* 数据入库示例(开发中) [`https://github.com/injoyai/stock`](https://github.com/injoyai/stock)
|
||||
* 参考 [`https://github.com/bensema/gotdx`](https://github.com/bensema/gotdx)
|
||||
* 参考 [`https://github.com/mootdx/mootdx`](https://github.com/mootdx/mootdx)
|
||||
* 参考 [`https://github.com/jing2uo/tdx2db`](https://github.com/jing2uo/tdx2db)
|
||||
|
||||
### 如何使用
|
||||
|
||||
|
||||
29
client.go
29
client.go
@@ -3,6 +3,10 @@ package tdx
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/injoyai/base/maps"
|
||||
"github.com/injoyai/base/maps/wait"
|
||||
"github.com/injoyai/conv"
|
||||
@@ -10,11 +14,8 @@ import (
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/ios/module/common"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/internal/bse"
|
||||
"github.com/injoyai/tdx/lib/bse"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"runtime/debug"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -560,6 +561,26 @@ func (this *Client) GetIndexAll(Type uint8, code string) (*protocol.KlineResp, e
|
||||
return this.GetIndexUntil(Type, code, func(k *protocol.Kline) bool { return false })
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexMinute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetIndex(protocol.TypeKlineMinute, code, start, count)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndex5Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetIndex(protocol.TypeKline5Minute, code, start, count)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndex15Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetIndex(protocol.TypeKline15Minute, code, start, count)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndex30Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetIndex(protocol.TypeKline30Minute, code, start, count)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndex60Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetIndex(protocol.TypeKline60Minute, code, start, count)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexDay(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetIndex(protocol.TypeKlineDay, code, start, count)
|
||||
}
|
||||
|
||||
86
codes.go
86
codes.go
@@ -2,24 +2,31 @@ package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"iter"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type ICodes interface {
|
||||
Iter() iter.Seq2[string, *CodeModel]
|
||||
Get(code string) *CodeModel
|
||||
GetName(code string) string
|
||||
GetStocks(limit ...int) []string
|
||||
GetETFs(limit ...int) []string
|
||||
GetStocks(limit ...int) CodeModels
|
||||
GetStockCodes(limit ...int) []string
|
||||
GetETFs(limit ...int) CodeModels
|
||||
GetETFCodes(limit ...int) []string
|
||||
GetIndexes(limits ...int) CodeModels
|
||||
GetIndexCodes(limits ...int) []string
|
||||
}
|
||||
|
||||
// DefaultCodes 增加单例,部分数据需要通过Codes里面的信息计算
|
||||
@@ -130,6 +137,8 @@ func NewCodes(c *Client, db *xorm.Engine) (*Codes, error) {
|
||||
return cc, cc.Update(true)
|
||||
}
|
||||
|
||||
var _ ICodes = &Codes{}
|
||||
|
||||
type Codes struct {
|
||||
*Client //客户端
|
||||
db *xorm.Engine //数据库实例
|
||||
@@ -138,6 +147,20 @@ type Codes struct {
|
||||
exchanges map[string][]string //交易所缓存
|
||||
}
|
||||
|
||||
func (this *Codes) Get(code string) *CodeModel {
|
||||
return this.Map[code]
|
||||
}
|
||||
|
||||
func (this *Codes) Iter() iter.Seq2[string, *CodeModel] {
|
||||
return func(yield func(string, *CodeModel) bool) {
|
||||
for _, code := range this.list {
|
||||
if !yield(code.FullCode(), code) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取股票名称
|
||||
func (this *Codes) GetName(code string) string {
|
||||
if v, ok := this.Map[code]; ok {
|
||||
@@ -147,13 +170,13 @@ func (this *Codes) GetName(code string) string {
|
||||
}
|
||||
|
||||
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
||||
func (this *Codes) GetStocks(limits ...int) []string {
|
||||
func (this *Codes) GetStocks(limits ...int) CodeModels {
|
||||
limit := conv.Default(-1, limits...)
|
||||
ls := []string(nil)
|
||||
ls := []*CodeModel(nil)
|
||||
for _, m := range this.list {
|
||||
code := m.FullCode()
|
||||
if protocol.IsStock(code) {
|
||||
ls = append(ls, code)
|
||||
ls = append(ls, m)
|
||||
}
|
||||
if limit > 0 && len(ls) >= limit {
|
||||
break
|
||||
@@ -162,14 +185,18 @@ func (this *Codes) GetStocks(limits ...int) []string {
|
||||
return ls
|
||||
}
|
||||
|
||||
func (this *Codes) GetStockCodes(limits ...int) []string {
|
||||
return this.GetStocks(limits...).Codes()
|
||||
}
|
||||
|
||||
// GetETFs 获取基金代码,sz159xxx,sh510xxx,sh511xxx
|
||||
func (this *Codes) GetETFs(limits ...int) []string {
|
||||
func (this *Codes) GetETFs(limits ...int) CodeModels {
|
||||
limit := conv.Default(-1, limits...)
|
||||
ls := []string(nil)
|
||||
ls := []*CodeModel(nil)
|
||||
for _, m := range this.list {
|
||||
code := m.FullCode()
|
||||
if protocol.IsETF(code) {
|
||||
ls = append(ls, code)
|
||||
ls = append(ls, m)
|
||||
}
|
||||
if limit > 0 && len(ls) >= limit {
|
||||
break
|
||||
@@ -178,8 +205,29 @@ func (this *Codes) GetETFs(limits ...int) []string {
|
||||
return ls
|
||||
}
|
||||
|
||||
func (this *Codes) Get(code string) *CodeModel {
|
||||
return this.Map[code]
|
||||
// GetETFCodes 获取基金代码,sz159xxx,sh510xxx,sh511xxx
|
||||
func (this *Codes) GetETFCodes(limits ...int) []string {
|
||||
return this.GetETFs(limits...).Codes()
|
||||
}
|
||||
|
||||
// GetIndexes 获取基金代码,sz159xxx,sh510xxx,sh511xxx
|
||||
func (this *Codes) GetIndexes(limits ...int) CodeModels {
|
||||
limit := conv.Default(-1, limits...)
|
||||
ls := []*CodeModel(nil)
|
||||
for _, m := range this.list {
|
||||
code := m.FullCode()
|
||||
if protocol.IsIndex(code) {
|
||||
ls = append(ls, m)
|
||||
}
|
||||
if limit > 0 && len(ls) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return ls
|
||||
}
|
||||
|
||||
func (this *Codes) GetIndexCodes(limits ...int) []string {
|
||||
return this.GetIndexes(limits...).Codes()
|
||||
}
|
||||
|
||||
func (this *Codes) AddExchange(code string) string {
|
||||
@@ -367,3 +415,13 @@ func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type CodeModels []*CodeModel
|
||||
|
||||
func (this CodeModels) Codes() []string {
|
||||
codes := make([]string, len(this))
|
||||
for i, v := range this {
|
||||
codes[i] = v.FullCode()
|
||||
}
|
||||
return codes
|
||||
}
|
||||
|
||||
92
codes_v2.go
92
codes_v2.go
@@ -2,68 +2,70 @@ package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"iter"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/injoyai/base/maps"
|
||||
"github.com/injoyai/base/types"
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/ios"
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/internal/gbbq"
|
||||
"github.com/injoyai/tdx/internal/xorms"
|
||||
"github.com/injoyai/tdx/lib/gbbq"
|
||||
"github.com/injoyai/tdx/lib/xorms"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type Codes2Option func(*Codes2)
|
||||
|
||||
func WithDBFilename(filename string) Codes2Option {
|
||||
func WithCodes2Database(filename string) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.dbFilename = filename
|
||||
}
|
||||
}
|
||||
|
||||
func WithTempDir(dir string) Codes2Option {
|
||||
func WithCodes2TempDir(dir string) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.tempDir = dir
|
||||
}
|
||||
}
|
||||
|
||||
func WithSpec(spec string) Codes2Option {
|
||||
func WithCodes2Spec(spec string) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.spec = spec
|
||||
}
|
||||
}
|
||||
|
||||
func WithUpdateKey(key string) Codes2Option {
|
||||
func WithCodes2UpdateKey(key string) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.updateKey = key
|
||||
}
|
||||
}
|
||||
|
||||
func WithRetry(retry int) Codes2Option {
|
||||
func WithCodes2Retry(retry int) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.retry = retry
|
||||
}
|
||||
}
|
||||
|
||||
func WithClient(c *Client) Codes2Option {
|
||||
func WithCodes2Client(c *Client) Codes2Option {
|
||||
return func(cs *Codes2) {
|
||||
cs.c = c
|
||||
}
|
||||
}
|
||||
|
||||
func WithDial(dial ios.DialFunc, op ...client.Option) Codes2Option {
|
||||
func WithCodes2Dial(dial ios.DialFunc, op ...client.Option) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.dial = dial
|
||||
c.dialOption = op
|
||||
}
|
||||
}
|
||||
|
||||
func WithDialOption(op ...client.Option) Codes2Option {
|
||||
func WithCodes2DialOption(op ...client.Option) Codes2Option {
|
||||
return func(c *Codes2) {
|
||||
c.dialOption = op
|
||||
}
|
||||
@@ -75,7 +77,7 @@ func NewCodes2(op ...Codes2Option) (*Codes2, error) {
|
||||
tempDir: filepath.Join(DefaultDataDir, "temp"),
|
||||
spec: "10 0 9 * * *",
|
||||
updateKey: "codes",
|
||||
retry: 3,
|
||||
retry: DefaultRetry,
|
||||
dial: NewRangeDial(Hosts),
|
||||
dialOption: nil,
|
||||
m: maps.NewGeneric[string, *CodeModel](),
|
||||
@@ -115,7 +117,7 @@ func NewCodes2(op ...Codes2Option) (*Codes2, error) {
|
||||
// 定时更新
|
||||
cr := cron.New(cron.WithSeconds())
|
||||
_, err = cr.AddFunc(cs.spec, func() {
|
||||
for i := 0; i < 3; i++ {
|
||||
for i := 0; i == 0 || i < cs.retry; i++ {
|
||||
if err := cs.Update(); err != nil {
|
||||
logs.Err(err)
|
||||
<-time.After(time.Minute * 5)
|
||||
@@ -148,11 +150,13 @@ type Codes2 struct {
|
||||
内部字段
|
||||
*/
|
||||
|
||||
c *Client //
|
||||
db *xorms.Engine //
|
||||
stocks types.List[string] //缓存
|
||||
etfs types.List[string] //缓存
|
||||
m *maps.Generic[string, *CodeModel] //缓存
|
||||
c *Client //
|
||||
db *xorms.Engine //
|
||||
stocks types.List[*CodeModel] //股票缓存
|
||||
etfs types.List[*CodeModel] //etf缓存
|
||||
indexes types.List[*CodeModel] //指数缓存
|
||||
all types.List[*CodeModel] //全部缓存
|
||||
m *maps.Generic[string, *CodeModel] //缓存
|
||||
}
|
||||
|
||||
func (this *Codes2) Get(code string) *CodeModel {
|
||||
@@ -160,6 +164,16 @@ func (this *Codes2) Get(code string) *CodeModel {
|
||||
return v
|
||||
}
|
||||
|
||||
func (this *Codes2) Iter() iter.Seq2[string, *CodeModel] {
|
||||
return func(yield func(string, *CodeModel) bool) {
|
||||
for _, code := range this.all {
|
||||
if !yield(code.FullCode(), code) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Codes2) GetName(code string) string {
|
||||
v, _ := this.m.Get(code)
|
||||
if v == nil {
|
||||
@@ -168,14 +182,31 @@ func (this *Codes2) GetName(code string) string {
|
||||
return v.Name
|
||||
}
|
||||
|
||||
func (this *Codes2) GetStocks(limit ...int) []string {
|
||||
func (this *Codes2) GetStocks(limit ...int) CodeModels {
|
||||
size := conv.Default(this.stocks.Len(), limit...)
|
||||
return this.stocks.Limit(size)
|
||||
return CodeModels(this.stocks.Limit(size))
|
||||
}
|
||||
|
||||
func (this *Codes2) GetETFs(limit ...int) []string {
|
||||
func (this *Codes2) GetStockCodes(limit ...int) []string {
|
||||
return this.GetStocks(limit...).Codes()
|
||||
}
|
||||
|
||||
func (this *Codes2) GetETFs(limit ...int) CodeModels {
|
||||
size := conv.Default(this.etfs.Len(), limit...)
|
||||
return this.etfs.Limit(size)
|
||||
return CodeModels(this.etfs.Limit(size))
|
||||
}
|
||||
|
||||
func (this *Codes2) GetETFCodes(limit ...int) []string {
|
||||
return this.GetETFs(limit...).Codes()
|
||||
}
|
||||
|
||||
func (this *Codes2) GetIndexes(limit ...int) CodeModels {
|
||||
size := conv.Default(this.etfs.Len(), limit...)
|
||||
return CodeModels(this.indexes.Limit(size))
|
||||
}
|
||||
|
||||
func (this *Codes2) GetIndexCodes(limit ...int) []string {
|
||||
return this.GetIndexes(limit...).Codes()
|
||||
}
|
||||
|
||||
func (this *Codes2) updated() (bool, error) {
|
||||
@@ -218,21 +249,26 @@ func (this *Codes2) Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
stocks := []string(nil)
|
||||
etfs := []string(nil)
|
||||
stocks := []*CodeModel(nil)
|
||||
etfs := []*CodeModel(nil)
|
||||
indexes := []*CodeModel(nil)
|
||||
for _, v := range codes {
|
||||
fullCode := v.FullCode()
|
||||
this.m.Set(fullCode, v)
|
||||
switch {
|
||||
case protocol.IsStock(fullCode):
|
||||
stocks = append(stocks, fullCode)
|
||||
stocks = append(stocks, v)
|
||||
case protocol.IsETF(fullCode):
|
||||
etfs = append(etfs, fullCode)
|
||||
etfs = append(etfs, v)
|
||||
case protocol.IsIndex(fullCode):
|
||||
indexes = append(indexes, v)
|
||||
}
|
||||
}
|
||||
|
||||
this.stocks = stocks
|
||||
this.etfs = etfs
|
||||
this.indexes = indexes
|
||||
this.all = codes
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m, err := tdx.NewManage(nil)
|
||||
m, err := tdx.NewManage()
|
||||
logs.PanicErr(err)
|
||||
|
||||
codes := m.Codes.GetStocks()
|
||||
codes := m.Codes.GetStocks().Codes()
|
||||
//codes = []string{
|
||||
// "sz000001",
|
||||
// "sz000002",
|
||||
|
||||
29
example/GetETFCodeNumber/main.go
Normal file
29
example/GetETFCodeNumber/main.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cs, err := tdx.NewCodes2()
|
||||
logs.PanicErr(err)
|
||||
|
||||
ls := cs.GetETFCodes()
|
||||
|
||||
shNumber := 0
|
||||
szNumber := 0
|
||||
for _, v := range ls {
|
||||
switch {
|
||||
case strings.HasPrefix(v, "sh"):
|
||||
shNumber++
|
||||
case strings.HasPrefix(v, "sz"):
|
||||
szNumber++
|
||||
}
|
||||
}
|
||||
|
||||
logs.Debug("sh:", shNumber)
|
||||
logs.Debug("sz:", szNumber)
|
||||
}
|
||||
23
example/Manage/main.go
Normal file
23
example/Manage/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m, err := tdx.NewManage()
|
||||
logs.PanicErr(err)
|
||||
|
||||
err = m.Do(func(c *tdx.Client) error {
|
||||
resp, err := c.GetIndexDayAll("sh000001")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range resp.List {
|
||||
logs.Debug(v)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
logs.PanicErr(err)
|
||||
}
|
||||
@@ -2,16 +2,17 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/extend"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
m, err := tdx.NewManage(nil)
|
||||
m, err := tdx.NewManage()
|
||||
logs.PanicErr(err)
|
||||
|
||||
err = extend.NewPullKline(extend.PullKlineConfig{
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/extend"
|
||||
@@ -11,7 +12,7 @@ func main() {
|
||||
|
||||
pt := extend.NewPullTrade("./data/trade")
|
||||
|
||||
m, err := tdx.NewManage(nil)
|
||||
m, err := tdx.NewManage()
|
||||
logs.PanicErr(err)
|
||||
|
||||
err = pt.PullYear(context.Background(), m, 2025, "sz000001")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package extend
|
||||
|
||||
import (
|
||||
"github.com/injoyai/tdx/internal/bse"
|
||||
"github.com/injoyai/tdx/lib/bse"
|
||||
)
|
||||
|
||||
func GetBjCodes() ([]string, error) {
|
||||
|
||||
@@ -48,7 +48,7 @@ func (this *PullKlineMysql) Run(ctx context.Context, m *tdx.Manage) error {
|
||||
//1. 获取所有股票代码
|
||||
codes := this.Config.Codes
|
||||
if len(codes) == 0 {
|
||||
codes = m.Codes.GetStocks()
|
||||
codes = m.Codes.GetStockCodes()
|
||||
}
|
||||
|
||||
for _, v := range codes {
|
||||
|
||||
@@ -110,7 +110,7 @@ func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
|
||||
//1. 获取所有股票代码
|
||||
codes := this.Config.Codes
|
||||
if len(codes) == 0 {
|
||||
codes = m.Codes.GetStocks()
|
||||
codes = m.Codes.GetStockCodes()
|
||||
}
|
||||
|
||||
for _, v := range codes {
|
||||
|
||||
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
||||
module github.com/injoyai/tdx
|
||||
|
||||
go 1.20
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/glebarez/go-sqlite v1.22.0
|
||||
|
||||
16
go.sum
16
go.sum
@@ -22,12 +22,11 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
|
||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/injoyai/base v1.2.15 h1:K/ysPqZl7vgNUAz/jpG1IdDpzdSMWvUfoJL+1gPdM9g=
|
||||
github.com/injoyai/base v1.2.15/go.mod h1:NfCQjml3z2pCvQ3J3YcOXtecqXD0xVPKjo4YTsMLhr8=
|
||||
github.com/injoyai/base v1.2.17 h1:+qYeCSeEMWgmTla+LBC0Ozan9ysS4mV0ne5nfMt9opU=
|
||||
github.com/injoyai/base v1.2.17/go.mod h1:NfCQjml3z2pCvQ3J3YcOXtecqXD0xVPKjo4YTsMLhr8=
|
||||
github.com/injoyai/conv v1.2.5 h1:G4OCyF0NTZul5W1u9IgXDOhW4/zmIigdPKXFHQGmv1M=
|
||||
@@ -39,6 +38,7 @@ github.com/injoyai/logs v1.0.12/go.mod h1:+dKEL6GvaFqqVRatqUBiCicJbZnAgtj7hVs824
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
@@ -50,6 +50,7 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
@@ -84,11 +85,14 @@ github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8
|
||||
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
|
||||
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
||||
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@@ -98,9 +102,11 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||
google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
@@ -114,8 +120,11 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
lukechampine.com/uint128 v1.3.0 h1:cDdUVfRwDUDovz610ABgFD17nXD4/uDgVHl2sC3+sbo=
|
||||
lukechampine.com/uint128 v1.3.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
|
||||
modernc.org/cc/v3 v3.41.0 h1:QoR1Sn3YWlmA1T4vLaKZfawdVtSiGx8H+cEojbC7v1Q=
|
||||
modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y=
|
||||
modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0=
|
||||
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
|
||||
modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw=
|
||||
modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
|
||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||
@@ -123,10 +132,13 @@ modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWP
|
||||
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
|
||||
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
|
||||
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
|
||||
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
|
||||
modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ=
|
||||
modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
|
||||
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
|
||||
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 h1:bvLlAPW1ZMTWA32LuZMBEGHAUOcATZjzHcotf3SWweM=
|
||||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
|
||||
xorm.io/core v0.7.3 h1:W8ws1PlrnkS1CZU1YWaYLMQcQilwAmQXU0BJDJon+H0=
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/injoyai/base/types"
|
||||
"github.com/injoyai/tdx/internal/zip"
|
||||
"github.com/injoyai/tdx/lib/zip"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
333
manage.go
333
manage.go
@@ -2,170 +2,224 @@ package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/robfig/cron/v3"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultClients = 1
|
||||
DefaultRetry = 3
|
||||
DefaultDataDir = "./data"
|
||||
DefaultDatabaseDir = "./data/database"
|
||||
)
|
||||
|
||||
func NewManageMysql(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
||||
//初始化配置
|
||||
if cfg == nil {
|
||||
cfg = &ManageConfig{}
|
||||
}
|
||||
if cfg.CodesFilename == "" {
|
||||
return nil, errors.New("未配置Codes的数据库")
|
||||
}
|
||||
if cfg.WorkdayFileName == "" {
|
||||
return nil, errors.New("未配置Workday的数据库")
|
||||
}
|
||||
if cfg.Dial == nil {
|
||||
cfg.Dial = DialDefault
|
||||
}
|
||||
|
||||
//通用客户端
|
||||
commonClient, err := cfg.Dial(op...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commonClient.Wait.SetTimeout(time.Second * 5)
|
||||
|
||||
//代码管理
|
||||
codes, err := NewCodesMysql(commonClient, cfg.CodesFilename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//工作日管理
|
||||
workday, err := NewWorkdayMysql(commonClient, cfg.WorkdayFileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//连接池
|
||||
p, err := NewPool(func() (*Client, error) {
|
||||
return cfg.Dial(op...)
|
||||
}, cfg.Number)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Manage{
|
||||
Pool: p,
|
||||
Config: cfg,
|
||||
Codes: codes,
|
||||
Workday: workday,
|
||||
}, nil
|
||||
func NewManageMysql(op ...Option) (*Manage, error) {
|
||||
return NewManage(
|
||||
WithOptions(op...),
|
||||
WithDialCodes(func(c *Client, database string) (ICodes, error) {
|
||||
if database == "" {
|
||||
return nil, errors.New("未配置Codes的数据库")
|
||||
}
|
||||
return NewCodesMysql(c, database)
|
||||
}),
|
||||
WithDialWorkday(func(c *Client, database string) (*Workday, error) {
|
||||
if database == "" {
|
||||
return nil, errors.New("未配置Workday的数据库")
|
||||
}
|
||||
return NewWorkdayMysql(c, database)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
||||
//初始化配置
|
||||
if cfg == nil {
|
||||
cfg = &ManageConfig{}
|
||||
}
|
||||
if cfg.CodesFilename == "" {
|
||||
cfg.CodesFilename = DefaultDatabaseDir + "/codes.db"
|
||||
}
|
||||
if cfg.WorkdayFileName == "" {
|
||||
cfg.WorkdayFileName = DefaultDatabaseDir + "/workday.db"
|
||||
}
|
||||
if cfg.Dial == nil {
|
||||
cfg.Dial = DialDefault
|
||||
}
|
||||
|
||||
//通用客户端
|
||||
commonClient, err := cfg.Dial(op...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commonClient.Wait.SetTimeout(time.Second * 5)
|
||||
|
||||
//代码管理
|
||||
codes, err := NewCodesSqlite(commonClient, cfg.CodesFilename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//工作日管理
|
||||
workday, err := NewWorkdaySqlite(commonClient, cfg.WorkdayFileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//连接池
|
||||
p, err := NewPool(func() (*Client, error) {
|
||||
return cfg.Dial(op...)
|
||||
}, cfg.Number)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Manage{
|
||||
Pool: p,
|
||||
Config: cfg,
|
||||
Codes: codes,
|
||||
Workday: workday,
|
||||
}, nil
|
||||
func NewManageSqlite(op ...Option) (*Manage, error) {
|
||||
return NewManage(
|
||||
WithCodesDatabase(DefaultDatabaseDir+"/codes.db"),
|
||||
WithWorkdayDatabase(DefaultDatabaseDir+"/workday.db"),
|
||||
WithOptions(op...),
|
||||
WithDialCodes(func(c *Client, database string) (ICodes, error) {
|
||||
return NewCodesSqlite(c, database)
|
||||
}),
|
||||
WithDialWorkday(func(c *Client, database string) (*Workday, error) {
|
||||
return NewWorkdaySqlite(c, database)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
func NewManage2(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
||||
//初始化配置
|
||||
if cfg == nil {
|
||||
cfg = &ManageConfig{}
|
||||
}
|
||||
if cfg.CodesFilename == "" {
|
||||
cfg.CodesFilename = DefaultDatabaseDir + "/codes2.db"
|
||||
}
|
||||
if cfg.WorkdayFileName == "" {
|
||||
cfg.WorkdayFileName = DefaultDatabaseDir + "/workday.db"
|
||||
}
|
||||
if cfg.Dial == nil {
|
||||
cfg.Dial = DialDefault
|
||||
func NewManageSqlite2(op ...Option) (*Manage, error) {
|
||||
return NewManage(
|
||||
WithCodesDatabase(DefaultDatabaseDir+"/codes2.db"),
|
||||
WithWorkdayDatabase(DefaultDatabaseDir+"/workday.db"),
|
||||
WithOptions(op...),
|
||||
WithDialCodes(func(c *Client, database string) (ICodes, error) {
|
||||
return NewCodes2(
|
||||
WithCodes2Client(c),
|
||||
WithCodes2Database(database),
|
||||
)
|
||||
}),
|
||||
WithDialWorkday(func(c *Client, database string) (*Workday, error) {
|
||||
return NewWorkdaySqlite(c, database)
|
||||
}),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
func NewManage(op ...Option) (m *Manage, err error) {
|
||||
|
||||
m = &Manage{
|
||||
clients: DefaultClients,
|
||||
dial: DialDefault,
|
||||
dialOptions: nil,
|
||||
dialCodes: nil,
|
||||
codesDatabase: DefaultDatabaseDir + "/codes2.db",
|
||||
dialWorkday: nil,
|
||||
workdayDatabase: DefaultDatabaseDir + "/workday.db",
|
||||
Pool: nil,
|
||||
Codes: nil,
|
||||
Workday: nil,
|
||||
cron: nil,
|
||||
once: sync.Once{},
|
||||
}
|
||||
|
||||
//通用客户端
|
||||
commonClient, err := cfg.Dial(op...)
|
||||
for _, v := range op {
|
||||
if v != nil {
|
||||
v(m)
|
||||
}
|
||||
}
|
||||
|
||||
m.clients = conv.Select(m.clients <= 0, 1, m.clients)
|
||||
m.dial = conv.Select(m.dial == nil, DialDefault, m.dial)
|
||||
|
||||
//连接池
|
||||
m.Pool, err = NewPool(func() (*Client, error) { return m.dial(m.dialOptions...) }, m.clients)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commonClient.Wait.SetTimeout(time.Second * 5)
|
||||
|
||||
//代码管理
|
||||
codes, err := NewCodes2(WithClient(commonClient), WithDBFilename(cfg.CodesFilename))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if m.Codes == nil {
|
||||
if m.dialCodes == nil {
|
||||
m.dialCodes = func(c *Client, database string) (ICodes, error) {
|
||||
return NewCodes2(WithCodes2Client(c), WithCodes2Database(database))
|
||||
}
|
||||
}
|
||||
err = m.Pool.Do(func(c *Client) error {
|
||||
m.Codes, err = m.dialCodes(c, m.codesDatabase)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
//工作日管理
|
||||
workday, err := NewWorkdaySqlite(commonClient, cfg.WorkdayFileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if m.Workday == nil {
|
||||
if m.dialWorkday == nil {
|
||||
m.dialWorkday = func(c *Client, database string) (*Workday, error) {
|
||||
return NewWorkdaySqlite(c, database)
|
||||
}
|
||||
}
|
||||
err = m.Pool.Do(func(c *Client) error {
|
||||
m.Workday, err = m.dialWorkday(c, m.workdayDatabase)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
//连接池
|
||||
p, err := NewPool(func() (*Client, error) {
|
||||
return cfg.Dial(op...)
|
||||
}, cfg.Number)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
return &Manage{
|
||||
Pool: p,
|
||||
Config: cfg,
|
||||
Codes: codes,
|
||||
Workday: workday,
|
||||
}, nil
|
||||
/*
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
type Option func(m *Manage)
|
||||
type DialWorkdayFunc func(c *Client, database string) (*Workday, error)
|
||||
type DialCodesFunc func(c *Client, database string) (ICodes, error)
|
||||
|
||||
func WithClients(clients int) Option {
|
||||
return func(m *Manage) {
|
||||
m.clients = clients
|
||||
}
|
||||
}
|
||||
|
||||
func WithDial(dial func(op ...client.Option) (*Client, error), op ...client.Option) Option {
|
||||
return func(m *Manage) {
|
||||
m.dial = dial
|
||||
m.dialOptions = op
|
||||
}
|
||||
}
|
||||
|
||||
func WithDialOptions(op ...client.Option) Option {
|
||||
return func(m *Manage) {
|
||||
m.dialOptions = op
|
||||
}
|
||||
}
|
||||
|
||||
func WithCodes(codes ICodes) Option {
|
||||
return func(m *Manage) {
|
||||
m.Codes = codes
|
||||
}
|
||||
}
|
||||
|
||||
func WithDialCodes(dial DialCodesFunc) Option {
|
||||
return func(m *Manage) {
|
||||
m.dialCodes = dial
|
||||
}
|
||||
}
|
||||
|
||||
func WithCodesDatabase(database string) Option {
|
||||
return func(m *Manage) {
|
||||
m.codesDatabase = database
|
||||
}
|
||||
}
|
||||
|
||||
func WithWorkday(w *Workday) Option {
|
||||
return func(m *Manage) {
|
||||
m.Workday = w
|
||||
}
|
||||
}
|
||||
|
||||
func WithDialWorkday(dial DialWorkdayFunc) Option {
|
||||
return func(m *Manage) {
|
||||
m.dialWorkday = dial
|
||||
}
|
||||
}
|
||||
|
||||
func WithWorkdayDatabase(database string) Option {
|
||||
return func(m *Manage) {
|
||||
m.workdayDatabase = database
|
||||
}
|
||||
}
|
||||
|
||||
func WithOptions(op ...Option) Option {
|
||||
return func(m *Manage) {
|
||||
for _, v := range op {
|
||||
v(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Manage struct {
|
||||
clients int
|
||||
dial func(op ...client.Option) (cli *Client, err error)
|
||||
dialOptions []client.Option
|
||||
dialCodes func(c *Client, database string) (ICodes, error)
|
||||
codesDatabase string
|
||||
dialWorkday DialWorkdayFunc
|
||||
workdayDatabase string
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
*Pool
|
||||
Config *ManageConfig
|
||||
Codes ICodes
|
||||
Workday *Workday
|
||||
cron *cron.Cron
|
||||
@@ -175,14 +229,21 @@ type Manage struct {
|
||||
// RangeStocks 遍历所有股票
|
||||
func (this *Manage) RangeStocks(f func(code string)) {
|
||||
for _, v := range this.Codes.GetStocks() {
|
||||
f(v)
|
||||
f(v.FullCode())
|
||||
}
|
||||
}
|
||||
|
||||
// RangeETFs 遍历所有ETF
|
||||
func (this *Manage) RangeETFs(f func(code string)) {
|
||||
for _, v := range this.Codes.GetETFs() {
|
||||
f(v)
|
||||
f(v.FullCode())
|
||||
}
|
||||
}
|
||||
|
||||
// RangeIndexes 遍历所有指数
|
||||
func (this *Manage) RangeIndexes(f func(code string)) {
|
||||
for _, v := range this.Codes.GetETFs() {
|
||||
f(v.FullCode())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,13 +3,14 @@ package protocol
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/injoyai/conv"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"golang.org/x/text/transform"
|
||||
"io"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/injoyai/conv"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
// String 字节先转小端,再转字符
|
||||
@@ -285,12 +286,27 @@ func IsETF(code string) bool {
|
||||
return true
|
||||
|
||||
case code[0:2] == ExchangeSZ.String() &&
|
||||
(code[2:4] == "15" || code[2:4] == "16"):
|
||||
(code[2:4] == "15"):
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsIndex 是否是指数,sh000001,sz399001,bj899100
|
||||
func IsIndex(code string) bool {
|
||||
if len(code) != 8 {
|
||||
return false
|
||||
}
|
||||
code = strings.ToLower(code)
|
||||
switch {
|
||||
case code[0:2] == ExchangeSH.String() && code[2:5] == "000":
|
||||
return true
|
||||
case code[0:2] == ExchangeSZ.String() && code[2:5] == "399":
|
||||
case code[0:2] == ExchangeBJ.String() && code[2:5] == "899":
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddPrefix 添加股票/基金代码前缀,针对股票/基金生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数)
|
||||
func AddPrefix(code string) string {
|
||||
if len(code) == 6 {
|
||||
|
||||
49
workday.go
49
workday.go
@@ -2,6 +2,11 @@ package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"iter"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/injoyai/base/maps"
|
||||
@@ -10,9 +15,6 @@ import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
@@ -154,7 +156,7 @@ func (this *Workday) TodayIs() bool {
|
||||
func (this *Workday) RangeYear(year int, f func(t time.Time) bool) {
|
||||
this.Range(
|
||||
time.Date(year, 1, 1, 0, 0, 0, 0, time.Local),
|
||||
time.Date(year, 12, 31, 0, 0, 0, 0, time.Local),
|
||||
time.Date(year, 12, 31, 0, 0, 0, 1, time.Local),
|
||||
f,
|
||||
)
|
||||
}
|
||||
@@ -162,8 +164,6 @@ func (this *Workday) RangeYear(year int, f func(t time.Time) bool) {
|
||||
// Range 遍历指定范围的工作日,推荐start带上时间15:00,这样当天小于15点不会触发
|
||||
func (this *Workday) Range(start, end time.Time, f func(t time.Time) bool) {
|
||||
start = conv.Select(start.Before(protocol.ExchangeEstablish), protocol.ExchangeEstablish, start)
|
||||
//now := IntegerDay(time.Now())
|
||||
//end = conv.Select(end.After(now), now, end).Add(1)
|
||||
for ; start.Before(end); start = start.Add(time.Hour * 24) {
|
||||
if this.Is(start) {
|
||||
if !f(start) {
|
||||
@@ -173,13 +173,36 @@ func (this *Workday) Range(start, end time.Time, f func(t time.Time) bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// RangeDesc 倒序遍历工作日,从今天-1990年12月19日(上海交易所成立时间)
|
||||
func (this *Workday) RangeDesc(f func(t time.Time) bool) {
|
||||
t := IntegerDay(time.Now())
|
||||
for ; t.After(time.Date(1990, 12, 18, 0, 0, 0, 0, time.Local)); t = t.Add(-time.Hour * 24) {
|
||||
if this.Is(t) {
|
||||
if !f(t) {
|
||||
return
|
||||
func (this *Workday) IterYear(year int, desc ...bool) iter.Seq[time.Time] {
|
||||
return this.Iter(
|
||||
time.Date(year, 1, 1, 0, 0, 0, 0, time.Local),
|
||||
time.Date(year, 12, 31, 0, 0, 0, 1, time.Local),
|
||||
desc...,
|
||||
)
|
||||
}
|
||||
|
||||
// Iter 遍历指定范围的工作日,推荐start带上时间15:00,这样当天小于15点不会触发
|
||||
func (this *Workday) Iter(start, end time.Time, desc ...bool) iter.Seq[time.Time] {
|
||||
start = conv.Select(start.Before(protocol.ExchangeEstablish), protocol.ExchangeEstablish, start)
|
||||
if len(desc) > 0 && desc[0] {
|
||||
//倒序遍历
|
||||
return func(yield func(time.Time) bool) {
|
||||
for ; end.After(start); end = end.Add(-time.Hour * 24) {
|
||||
if this.Is(end) {
|
||||
if !yield(end) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//正序遍历
|
||||
return func(yield func(time.Time) bool) {
|
||||
for ; start.Before(end); start = start.Add(time.Hour * 24) {
|
||||
if this.Is(start) {
|
||||
if !yield(start) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user