This commit is contained in:
2026-01-15 12:52:12 +08:00
parent db1d5397c0
commit 2249667d8b
4 changed files with 130 additions and 69 deletions

View File

@@ -4,5 +4,7 @@ import "git.apinb.com/dataset/stock/internal/logic/a"
func Boot() { func Boot() {
a.NewApiClient() a.NewApiClient()
a.CheckStockBasic() a.GetStockBasic()
a.GetLastDaily()
a.GetLastIndicator()
} }

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"log" "log"
"strings" "strings"
"time"
"git.apinb.com/bsm-sdk/core/utils" "git.apinb.com/bsm-sdk/core/utils"
"git.apinb.com/dataset/stock/internal/impl" "git.apinb.com/dataset/stock/internal/impl"
@@ -21,7 +22,7 @@ func NewApiClient() {
TushareClient = tushare.New(TushareToken) TushareClient = tushare.New(TushareToken)
} }
func CheckStockBasic() { func GetStockBasic() {
// 参数 // 参数
params := map[string]string{ params := map[string]string{
"list_status": "L", // 上市状态 L上市 D退市 P暂停上市默认是L "list_status": "L", // 上市状态 L上市 D退市 P暂停上市默认是L
@@ -33,7 +34,7 @@ func CheckStockBasic() {
// 根据api 请求对应的接口 // 根据api 请求对应的接口
reply, _ := TushareClient.StockBasic(params, fields) reply, _ := TushareClient.StockBasic(params, fields)
if reply.Code != 0 { if reply.Code != 0 {
log.Println("ERROR", "CheckStockBasic", reply.Code, reply.Msg) log.Println("ERROR", "GetStockBasic", reply.Code, reply.Msg)
return return
} }
@@ -64,9 +65,6 @@ func CheckStockBasic() {
} else if err == nil { } else if err == nil {
// check updated. // check updated.
upd := make(map[string]any) upd := make(map[string]any)
if record.Symbol != data[1] {
upd["symbol"] = data[1]
}
if record.Name != data[2] { if record.Name != data[2] {
upd["name"] = data[2] upd["name"] = data[2]
} }
@@ -107,10 +105,98 @@ func CheckStockBasic() {
impl.DBService.Model(record).Updates(upd) impl.DBService.Model(record).Updates(upd)
} }
} }
} }
} }
func GetLastDaily() {
s, e := ReturnLastDay()
params := map[string]string{
"start_date": s,
"end_date": e,
}
reply, _ := TushareClient.Daily(params, []string{})
if reply.Code != 0 {
log.Println("ERROR", "GetLastDaily", reply.Code, reply.Msg)
return
}
for _, item := range reply.Data.Items {
var cnt int64
impl.DBService.Model(&models.LastDaily{}).Where("ts_code=? and trade_date=?", item[0].(string), item[1].(string)).Count(&cnt)
if cnt == 0 {
impl.DBService.Create(&models.LastDaily{
TsCode: item[0].(string),
TradeDate: item[1].(string),
Open: item[2].(float64),
High: item[3].(float64),
Low: item[4].(float64),
Close: item[5].(float64),
PreClose: item[6].(float64),
Change: item[7].(float64),
PctChg: item[8].(float64),
Vol: item[9].(float64),
Amount: item[10].(float64),
})
}
}
}
func GetLastIndicator() {
var stocks []models.StockBasic
impl.DBService.Find(&stocks)
s, e := ReturnLastDay()
for _, row := range stocks {
params := map[string]string{
"ts_code": row.TsCode,
"start_date": s,
"end_date": e,
}
reply, _ := TushareClient.Daily(params, []string{})
if reply.Code != 0 {
log.Println("ERROR", "GetLastIndicator", reply.Code, reply.Msg)
return
}
for _, item := range reply.Data.Items {
var cnt int64
impl.DBService.Model(&models.LastIndicator{}).Where("ts_code=? and trade_date=?", item[0].(string), item[1].(string)).Count(&cnt)
if cnt == 0 {
impl.DBService.Create(&models.LastIndicator{
TsCode: item[0].(string),
TradeDate: item[1].(string),
Close: item[2].(float64),
TurnoverRate: item[3].(float64),
TurnoverRateF: item[4].(float64),
VolumeRatio: item[5].(float64),
Pe: item[6].(float64),
PeTtm: item[7].(float64),
Pb: item[8].(float64),
Ps: item[9].(float64),
PsTtm: item[10].(float64),
DvRatio: item[11].(float64),
DvTtm: item[12].(float64),
TotalShare: item[13].(float64),
FloatShare: item[14].(float64),
FreeShare: item[15].(float64),
TotalMv: item[16].(float64),
CircMv: item[17].(float64),
})
}
}
}
}
func ReturnLastDay() (string, string) {
now := time.Now()
start := now.Format("20060102")
end := now.AddDate(0, -1, 0).Format("20060102")
return start, end
}
func Anys2Strings(any []any) []string { func Anys2Strings(any []any) []string {
var ret []string var ret []string
for _, item := range any { for _, item := range any {

View File

@@ -1,16 +1,14 @@
package models package models
import ( import (
"time" "git.apinb.com/bsm-sdk/core/database"
"gorm.io/gorm"
) )
// LastDaily 股票日线数据 // LastDaily 股票日线数据
type LastDaily struct { type LastDaily struct {
ID uint `gorm:"primarykey;autoIncrement" json:"id"` ID uint `gorm:"primarykey;autoIncrement" json:"id"`
TsCode string `gorm:"type:varchar(20);not null;index:idx_ts_code;comment:股票代码" json:"ts_code"` TsCode string `gorm:"type:varchar(20);not null;index:idx_ts_code;comment:股票代码" json:"ts_code"`
TradeDate time.Time `gorm:"type:date;not null;index:idx_trade_date;comment:交易日期" json:"trade_date"` TradeDate string `gorm:"type:date;not null;index:idx_trade_date;comment:交易日期" json:"trade_date"`
Open float64 `gorm:"type:decimal(10,4);comment:开盘价" json:"open"` Open float64 `gorm:"type:decimal(10,4);comment:开盘价" json:"open"`
High float64 `gorm:"type:decimal(10,4);comment:最高价" json:"high"` High float64 `gorm:"type:decimal(10,4);comment:最高价" json:"high"`
Low float64 `gorm:"type:decimal(10,4);comment:最低价" json:"low"` Low float64 `gorm:"type:decimal(10,4);comment:最低价" json:"low"`
@@ -22,7 +20,6 @@ type LastDaily struct {
Amount float64 `gorm:"type:decimal(20,2);comment:成交额(千元)" json:"amount"` Amount float64 `gorm:"type:decimal(20,2);comment:成交额(千元)" json:"amount"`
} }
func init() { func init() {
database.AppendMigrate(&LastDaily{}) database.AppendMigrate(&LastDaily{})
} }
@@ -31,13 +28,3 @@ func init() {
func (LastDaily) TableName() string { func (LastDaily) TableName() string {
return "last_daily" return "last_daily"
} }
// Indexes 定义复合索引可选也可以在migration中创建
func (LastDaily) Indexes() []gorm.Index {
return []gorm.Index{
&gorm.IndexModel{
Name: "idx_ts_code_trade_date",
Fields: []string{"ts_code", "trade_date"},
},
}
}

View File

@@ -1,21 +1,20 @@
package models package models
import ( import (
"time" "git.apinb.com/bsm-sdk/core/database"
"gorm.io/gorm"
) )
// LastIndicator 股票日线数据模型 // LastIndicator 股票日线数据模型
type LastIndicator struct { type LastIndicator struct {
ID uint `gorm:"primarykey;autoIncrement"` ID uint `gorm:"primarykey;autoIncrement"`
TsCode string `gorm:"type:varchar(20);not null;comment:TS股票代码;index:idx_ts_code"` TsCode string `gorm:"type:varchar(20);not null;comment:TS股票代码;index:idx_ts_code"`
TradeDate time.Time `gorm:"type:date;not null;comment:交易日期;index:idx_trade_date;index:idx_ts_trade,priority:2"` TradeDate string `gorm:"type:date;not null;comment:交易日期;index:idx_trade_date;index:idx_ts_trade,priority:2"`
Close float64 `gorm:"type:decimal(10,4);comment:当日收盘价"` Close float64 `gorm:"type:decimal(10,4);comment:当日收盘价"`
TurnoverRate float64 `gorm:"type:decimal(10,6);comment:换手率(%)"` TurnoverRate float64 `gorm:"type:decimal(10,6);comment:换手率(%)"`
TurnoverRateF float64 `gorm:"type:decimal(10,6);comment:换手率(自由流通股)"` TurnoverRateF float64 `gorm:"type:decimal(10,6);comment:换手率(自由流通股)"`
VolumeRatio float64 `gorm:"type:decimal(10,4);comment:量比"` VolumeRatio float64 `gorm:"type:decimal(10,4);comment:量比"`
Pe *float64 `gorm:"type:decimal(10,4);comment:市盈率(总市值/净利润)"` Pe float64 `gorm:"type:decimal(10,4);comment:市盈率(总市值/净利润)"`
PeTtm *float64 `gorm:"type:decimal(10,4);comment:市盈率(TTM)"` PeTtm float64 `gorm:"type:decimal(10,4);comment:市盈率(TTM)"`
Pb float64 `gorm:"type:decimal(10,4);comment:市净率"` Pb float64 `gorm:"type:decimal(10,4);comment:市净率"`
Ps float64 `gorm:"type:decimal(10,4);comment:市销率"` Ps float64 `gorm:"type:decimal(10,4);comment:市销率"`
PsTtm float64 `gorm:"type:decimal(10,4);comment:市销率(TTM)"` PsTtm float64 `gorm:"type:decimal(10,4);comment:市销率(TTM)"`
@@ -26,11 +25,8 @@ type LastIndicator struct {
FreeShare float64 `gorm:"type:decimal(15,2);comment:自由流通股本(万)"` FreeShare float64 `gorm:"type:decimal(15,2);comment:自由流通股本(万)"`
TotalMv float64 `gorm:"type:decimal(15,2);comment:总市值(万元)"` TotalMv float64 `gorm:"type:decimal(15,2);comment:总市值(万元)"`
CircMv float64 `gorm:"type:decimal(15,2);comment:流通市值(万元)"` CircMv float64 `gorm:"type:decimal(15,2);comment:流通市值(万元)"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
} }
func init() { func init() {
database.AppendMigrate(&LastIndicator{}) database.AppendMigrate(&LastIndicator{})
} }
@@ -39,13 +35,3 @@ func init() {
func (LastIndicator) TableName() string { func (LastIndicator) TableName() string {
return "last_indicator" return "last_indicator"
} }
// 索引定义也可以在migration中创建
func (LastIndicator) Indexes() []string {
return []gorm.Index{
&gorm.IndexModel{
Name: "idx_ts_code_trade_date",
Fields: []string{"ts_code", "trade_date"},
},
}
}