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() {
a.NewApiClient()
a.CheckStockBasic()
a.GetStockBasic()
a.GetLastDaily()
a.GetLastIndicator()
}

View File

@@ -4,6 +4,7 @@ import (
"errors"
"log"
"strings"
"time"
"git.apinb.com/bsm-sdk/core/utils"
"git.apinb.com/dataset/stock/internal/impl"
@@ -21,7 +22,7 @@ func NewApiClient() {
TushareClient = tushare.New(TushareToken)
}
func CheckStockBasic() {
func GetStockBasic() {
// 参数
params := map[string]string{
"list_status": "L", // 上市状态 L上市 D退市 P暂停上市默认是L
@@ -33,7 +34,7 @@ func CheckStockBasic() {
// 根据api 请求对应的接口
reply, _ := TushareClient.StockBasic(params, fields)
if reply.Code != 0 {
log.Println("ERROR", "CheckStockBasic", reply.Code, reply.Msg)
log.Println("ERROR", "GetStockBasic", reply.Code, reply.Msg)
return
}
@@ -64,9 +65,6 @@ func CheckStockBasic() {
} else if err == nil {
// check updated.
upd := make(map[string]any)
if record.Symbol != data[1] {
upd["symbol"] = data[1]
}
if record.Name != data[2] {
upd["name"] = data[2]
}
@@ -107,10 +105,98 @@ func CheckStockBasic() {
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 {
var ret []string
for _, item := range any {

View File

@@ -1,43 +1,30 @@
package models
import (
"time"
"gorm.io/gorm"
"git.apinb.com/bsm-sdk/core/database"
)
// LastDaily 股票日线数据
type LastDaily struct {
ID uint `gorm:"primarykey;autoIncrement" json:"id"`
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"`
Open float64 `gorm:"type:decimal(10,4);comment:开盘价" json:"open"`
High float64 `gorm:"type:decimal(10,4);comment:最高价" json:"high"`
Low float64 `gorm:"type:decimal(10,4);comment:最低价" json:"low"`
Close float64 `gorm:"type:decimal(10,4);comment:收盘价" json:"close"`
PreClose float64 `gorm:"type:decimal(10,4);comment:昨收价(除权价)" json:"pre_close"`
Change float64 `gorm:"type:decimal(10,4);comment:涨跌额" json:"change"`
PctChg float64 `gorm:"type:decimal(10,6);comment:涨跌幅(%)" json:"pct_chg"`
Vol float64 `gorm:"type:decimal(15,2);comment:成交量(手)" json:"vol"`
Amount float64 `gorm:"type:decimal(20,2);comment:成交额(千元)" json:"amount"`
ID uint `gorm:"primarykey;autoIncrement" json:"id"`
TsCode string `gorm:"type:varchar(20);not null;index:idx_ts_code;comment:股票代码" json:"ts_code"`
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"`
High float64 `gorm:"type:decimal(10,4);comment:最高价" json:"high"`
Low float64 `gorm:"type:decimal(10,4);comment:最低价" json:"low"`
Close float64 `gorm:"type:decimal(10,4);comment:收盘价" json:"close"`
PreClose float64 `gorm:"type:decimal(10,4);comment:昨收价(除权价)" json:"pre_close"`
Change float64 `gorm:"type:decimal(10,4);comment:涨跌额" json:"change"`
PctChg float64 `gorm:"type:decimal(10,6);comment:涨跌幅(%)" json:"pct_chg"`
Vol float64 `gorm:"type:decimal(15,2);comment:成交量(手)" json:"vol"`
Amount float64 `gorm:"type:decimal(20,2);comment:成交额(千元)" json:"amount"`
}
func init() {
database.AppendMigrate(&LastDaily{})
}
// TableName 指定表名
func (LastDaily) TableName() string {
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"},
},
}
return "last_daily"
}

View File

@@ -1,51 +1,37 @@
package models
import (
"time"
"gorm.io/gorm"
"git.apinb.com/bsm-sdk/core/database"
)
// LastIndicator 股票日线数据模型
type LastIndicator struct {
ID uint `gorm:"primarykey;autoIncrement"`
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"`
Close float64 `gorm:"type:decimal(10,4);comment:当日收盘价"`
TurnoverRate float64 `gorm:"type:decimal(10,6);comment:换手率(%)"`
TurnoverRateF float64 `gorm:"type:decimal(10,6);comment:换手率(自由流通股)"`
VolumeRatio float64 `gorm:"type:decimal(10,4);comment:量比"`
Pe *float64 `gorm:"type:decimal(10,4);comment:市盈率(总市值/净利润)"`
PeTtm *float64 `gorm:"type:decimal(10,4);comment:市盈率(TTM)"`
Pb float64 `gorm:"type:decimal(10,4);comment:市净率"`
Ps float64 `gorm:"type:decimal(10,4);comment:市销率"`
PsTtm float64 `gorm:"type:decimal(10,4);comment:市销率(TTM)"`
DvRatio float64 `gorm:"type:decimal(10,6);comment:股息率(%)"`
DvTtm float64 `gorm:"type:decimal(10,6);comment:股息率(TTM)(%)"`
TotalShare float64 `gorm:"type:decimal(15,2);comment:总股本(万股)"`
FloatShare float64 `gorm:"type:decimal(15,2);comment:流通股本(万股)"`
FreeShare float64 `gorm:"type:decimal(15,2);comment:自由流通股本(万)"`
TotalMv 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"`
ID uint `gorm:"primarykey;autoIncrement"`
TsCode string `gorm:"type:varchar(20);not null;comment:TS股票代码;index:idx_ts_code"`
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:当日收盘价"`
TurnoverRate float64 `gorm:"type:decimal(10,6);comment:换手率(%)"`
TurnoverRateF float64 `gorm:"type:decimal(10,6);comment:换手率(自由流通股)"`
VolumeRatio float64 `gorm:"type:decimal(10,4);comment:量比"`
Pe float64 `gorm:"type:decimal(10,4);comment:市盈率(总市值/净利润)"`
PeTtm float64 `gorm:"type:decimal(10,4);comment:市盈率(TTM)"`
Pb float64 `gorm:"type:decimal(10,4);comment:市净率"`
Ps float64 `gorm:"type:decimal(10,4);comment:市销率"`
PsTtm float64 `gorm:"type:decimal(10,4);comment:市销率(TTM)"`
DvRatio float64 `gorm:"type:decimal(10,6);comment:股息率(%)"`
DvTtm float64 `gorm:"type:decimal(10,6);comment:股息率(TTM)(%)"`
TotalShare float64 `gorm:"type:decimal(15,2);comment:总股本(万股)"`
FloatShare float64 `gorm:"type:decimal(15,2);comment:流通股本(万股)"`
FreeShare float64 `gorm:"type:decimal(15,2);comment:自由流通股本(万)"`
TotalMv float64 `gorm:"type:decimal(15,2);comment:总市值(万元)"`
CircMv float64 `gorm:"type:decimal(15,2);comment:流通市值(万元)"`
}
func init() {
database.AppendMigrate(&LastIndicator{})
}
// TableName 设置表名
func (LastIndicator) TableName() string {
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"},
},
}
return "last_indicator"
}