兼容北交所代码到GetCodeAll,方便原先的逻辑不用改动

This commit is contained in:
injoyai
2025-09-30 11:43:37 +08:00
parent aec2cf1518
commit 110eaddc4d
2 changed files with 68 additions and 47 deletions

View File

@@ -220,6 +220,26 @@ func (this *Client) GetCode(exchange protocol.Exchange, start uint16) (*protocol
// GetCodeAll 通过多次请求的方式获取全部证券代码
func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp, error) {
resp := &protocol.CodeResp{}
//通达信没有北交所代码列表,通过爬虫的方式从北交所官网获取,放在这里是为了方便业务逻辑
//不放在extend包时防止循环引用
//todo 这是临时方案,等通达信有北交所代码列表时再改
if exchange == protocol.ExchangeBJ {
codes, err := GetBjCodes()
if err != nil {
return nil, err
}
resp.Count = uint16(len(codes))
for _, v := range codes {
resp.List = append(resp.List, &protocol.Code{
Code: v.Code,
Name: v.Name,
LastPrice: v.Last,
})
}
return resp, nil
}
size := uint16(1000)
for start := uint16(0); ; start += size {
r, err := this.GetCode(exchange, start)
@@ -238,7 +258,7 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
// GetStockAll 获取所有股票代码
func (this *Client) GetStockAll() ([]string, error) {
ls := []string(nil)
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} {
resp, err := this.GetCodeAll(ex)
if err != nil {
return nil, err

View File

@@ -155,52 +155,53 @@ func (this *Codes) Get(code string) *CodeModel {
return this.Map[code]
}
// GetExchange 获取股票交易所,这里的参数不需要带前缀
func (this *Codes) GetExchange(code string) protocol.Exchange {
if len(code) == 6 {
switch {
case code[:1] == "6":
return protocol.ExchangeSH
case code[:1] == "0":
return protocol.ExchangeSZ
case code[:2] == "30":
return protocol.ExchangeSZ
}
}
var exchange string
exchanges := this.exchanges[code]
if len(exchanges) >= 1 {
exchange = exchanges[0]
}
if len(code) == 8 {
exchange = code[0:2]
}
switch exchange {
case protocol.ExchangeSH.String():
return protocol.ExchangeSH
case protocol.ExchangeSZ.String():
return protocol.ExchangeSZ
default:
return protocol.ExchangeSH
}
}
//// GetExchange 获取股票交易所,这里的参数不需要带前缀
//func (this *Codes) GetExchange(code string) protocol.Exchange {
// if len(code) == 6 {
// switch {
// case code[:1] == "6":
// return protocol.ExchangeSH
// case code[:1] == "0":
// return protocol.ExchangeSZ
// case code[:2] == "30":
// return protocol.ExchangeSZ
// }
// }
// var exchange string
// exchanges := this.exchanges[code]
// if len(exchanges) >= 1 {
// exchange = exchanges[0]
// }
// if len(code) == 8 {
// exchange = code[0:2]
// }
// switch exchange {
// case protocol.ExchangeSH.String():
// return protocol.ExchangeSH
// case protocol.ExchangeSZ.String():
// return protocol.ExchangeSZ
// default:
// return protocol.ExchangeSH
// }
//}
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
return protocol.AddPrefix(code)
//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 更新数据,从服务器或者数据库
@@ -249,7 +250,7 @@ func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
//3. 从服务器获取所有股票代码
insert := []*CodeModel(nil)
update := []*CodeModel(nil)
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} {
resp, err := this.Client.GetCodeAll(exchange)
if err != nil {
return nil, err
@@ -334,7 +335,7 @@ func (this *CodeModel) FullCode() string {
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
return protocol.Price(float64(p) * math.Pow10(int(2-this.Decimal)))
return p * protocol.Price(math.Pow10(int(2-this.Decimal)))
//return p * protocol.Price(math.Pow10(int(2-this.Decimal)))
}
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {