mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
重新定义接口
This commit is contained in:
53
codes.go
53
codes.go
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"iter"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -16,10 +17,13 @@ import (
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// DefaultCodes 增加单例,部分数据需要通过Codes里面的信息计算
|
||||
@@ -138,6 +142,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 +165,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 +180,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 +200,9 @@ 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()
|
||||
}
|
||||
|
||||
func (this *Codes) AddExchange(code string) string {
|
||||
@@ -367,3 +390,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
|
||||
}
|
||||
|
||||
41
codes_v2.go
41
codes_v2.go
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/injoyai/tdx/internal/xorms"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"iter"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -150,8 +151,9 @@ type Codes2 struct {
|
||||
|
||||
c *Client //
|
||||
db *xorms.Engine //
|
||||
stocks types.List[string] //缓存
|
||||
etfs types.List[string] //缓存
|
||||
stocks types.List[*CodeModel] //缓存
|
||||
etfs types.List[*CodeModel] //缓存
|
||||
all types.List[*CodeModel] //缓存
|
||||
m *maps.Generic[string, *CodeModel] //缓存
|
||||
}
|
||||
|
||||
@@ -160,6 +162,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 +180,22 @@ 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) updated() (bool, error) {
|
||||
@@ -218,21 +238,22 @@ func (this *Codes2) Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
stocks := []string(nil)
|
||||
etfs := []string(nil)
|
||||
stocks := []*CodeModel(nil)
|
||||
etfs := []*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)
|
||||
}
|
||||
}
|
||||
|
||||
this.stocks = stocks
|
||||
this.etfs = etfs
|
||||
this.all = codes
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user