Merge remote-tracking branch 'origin/master'

This commit is contained in:
injoyai
2025-03-20 13:19:05 +08:00
3 changed files with 30 additions and 5 deletions

View File

@@ -246,6 +246,9 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
if DefaultCodes == nil {
return nil, errors.New("DefaultCodes未初始化")
}
for i := range codes {
codes[i] = DefaultCodes.AddExchange(codes[i])
}
f, err := protocol.MQuote.Frame(codes...)
if err != nil {
return nil, err

View File

@@ -97,7 +97,7 @@ type Codes struct {
db *xorm.Engine //数据库实例
Map map[string]*CodeModel //股票缓存
list []*CodeModel //列表方式缓存
exchanges map[string]string //交易所缓存
exchanges map[string][]string //交易所缓存
}
// GetName 获取股票名称
@@ -140,7 +140,11 @@ func (this *Codes) GetExchange(code string) protocol.Exchange {
return protocol.ExchangeSZ
}
}
exchange := this.exchanges[code]
var exchange string
exchanges := this.exchanges[code]
if len(exchanges) >= 1 {
exchange = exchanges[0]
}
if len(code) == 8 {
exchange = code[0:2]
}
@@ -154,6 +158,24 @@ func (this *Codes) GetExchange(code string) protocol.Exchange {
}
}
func (this *Codes) AddExchange(code string) string {
if exchanges := this.exchanges[code]; len(exchanges) == 1 {
return exchanges[0] + code
}
if len(code) == 6 {
switch {
case code[:1] == "6":
return protocol.ExchangeSH.String() + code
case code[:1] == "0":
return protocol.ExchangeSZ.String() + code
case code[:2] == "30":
return protocol.ExchangeSZ.String() + code
}
return this.GetExchange(code).String() + code
}
return code
}
// Update 更新数据,从服务器或者数据库
func (this *Codes) Update(byDB ...bool) error {
codes, err := this.GetCodes(len(byDB) > 0 && byDB[0])
@@ -161,10 +183,10 @@ func (this *Codes) Update(byDB ...bool) error {
return err
}
codeMap := make(map[string]*CodeModel)
exchanges := make(map[string]string)
exchanges := make(map[string][]string)
for _, code := range codes {
codeMap[code.Exchange+code.Code] = code
exchanges[code.Code] = code.Exchange
exchanges[code.Code] = append(exchanges[code.Code], code.Exchange)
}
this.Map = codeMap
this.list = codes

View File

@@ -21,7 +21,7 @@ func main() {
b1cb74001c00000000000d005100bd00789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13
b1cb74000c02000000003e05ac00ac000102020000303030303031601294121a1c2d4eadabcf0ed412aae5fc01afb0024561124fbcc08301afa47900b2e3174100bf68871a4201b741b6144302bb09af334403972e96354504ac09b619560e00000000f8ff601201363030303038b60fba04060607429788a70efa04ada37ab2531c12974d91e7449dbc354184b6010001844bad324102b5679ea1014203a65abd8d0143048a6ba4dd01440587e101b3d2029613000000000000b60f
*/
resp, err := c.GetQuote("sz000001", "sh600008")
resp, err := c.GetQuote("000001", "600000", "159558", "010504")
logs.PanicErr(err)
for _, v := range resp {