refactor(data):更新内置股票基础数据

- 更新内置股票基础数据
This commit is contained in:
ArvinLovegood 2025-06-18 09:54:15 +08:00
parent 700572567e
commit 68e7b6a68c
4 changed files with 55 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -26,6 +26,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/plugin/soft_delete" "gorm.io/plugin/soft_delete"
"io" "io"
"io/ioutil"
"strings" "strings"
"time" "time"
) )
@ -247,7 +248,7 @@ func (receiver StockDataApi) GetIndexBasic() {
func (receiver StockDataApi) GetStockBaseInfo() { func (receiver StockDataApi) GetStockBaseInfo() {
res := &TushareStockBasicResponse{} res := &TushareStockBasicResponse{}
fields := "ts_code,symbol,name,area,industry,cnspell,market,list_date,act_name,act_ent_type,fullname,exchange,list_status,curr_type,enname,delist_date,is_hs" fields := "ts_code,symbol,name,area,industry,cnspell,market,list_date,act_name,act_ent_type,fullname,exchange,list_status,curr_type,enname,delist_date,is_hs"
_, err := receiver.client.R(). resp, err := receiver.client.R().
SetHeader("content-type", "application/json"). SetHeader("content-type", "application/json").
SetBody(&TushareRequest{ SetBody(&TushareRequest{
ApiName: "stock_basic", ApiName: "stock_basic",
@ -258,8 +259,7 @@ func (receiver StockDataApi) GetStockBaseInfo() {
SetResult(res). SetResult(res).
Post(tushareApiUrl) Post(tushareApiUrl)
//logger.SugaredLogger.Infof("GetStockBaseInfo %s", string(resp.Body())) //logger.SugaredLogger.Infof("GetStockBaseInfo %s", string(resp.Body()))
//resp.Body()写入文件 ioutil.WriteFile("stock_basic.json", resp.Body(), 0666)
//ioutil.WriteFile("stock_basic.json", resp.Body(), 0666)
//logger.SugaredLogger.Infof("GetStockBaseInfo %+v", res) //logger.SugaredLogger.Infof("GetStockBaseInfo %+v", res)
if err != nil { if err != nil {
logger.SugaredLogger.Error(err.Error()) logger.SugaredLogger.Error(err.Error())

File diff suppressed because one or more lines are too long

70
main.go
View File

@ -5,7 +5,7 @@ import (
"embed" "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/duke-git/lancet/v2/convertor" "github.com/duke-git/lancet/v2/slice"
"github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/menu" "github.com/wailsapp/wails/v2/pkg/menu"
@ -21,6 +21,7 @@ import (
"os" "os"
goruntime "runtime" goruntime "runtime"
"runtime/debug" "runtime/debug"
"strings"
"time" "time"
) )
@ -271,7 +272,7 @@ func initStockData(ctx context.Context) {
defer func() { defer func() {
go runtime.EventsEmit(ctx, "loadingMsg", "done") go runtime.EventsEmit(ctx, "loadingMsg", "done")
}() }()
fields := "ts_code,symbol,name,area,industry,cnspell,market,list_date,act_name,act_ent_type,fullname,exchange,list_status,curr_type,enname,delist_date,is_hs"
log.SugaredLogger.Info("init stock data") log.SugaredLogger.Info("init stock data")
res := &data.TushareStockBasicResponse{} res := &data.TushareStockBasicResponse{}
err := json.Unmarshal(stocksBin, res) err := json.Unmarshal(stocksBin, res)
@ -279,26 +280,24 @@ func initStockData(ctx context.Context) {
log.SugaredLogger.Error(err.Error()) log.SugaredLogger.Error(err.Error())
return return
} }
for _, item := range res.Data.Items { for _, item := range res.Data.Items {
stock := &data.StockBasic{} stock := &data.StockBasic{}
stock.Exchange = convertor.ToString(item[0]) stockData := map[string]any{}
stock.IsHs = convertor.ToString(item[1]) for _, field := range strings.Split(fields, ",") {
stock.Name = convertor.ToString(item[2]) //logger.SugaredLogger.Infof("field: %s", field)
stock.Industry = convertor.ToString(item[3]) idx := slice.IndexOf(res.Data.Fields, field)
stock.ListStatus = convertor.ToString(item[4]) if idx == -1 {
stock.ActName = convertor.ToString(item[5]) continue
stock.ID = uint(item[6].(float64)) }
stock.CurrType = convertor.ToString(item[7]) stockData[field] = item[idx]
stock.Area = convertor.ToString(item[8]) }
stock.ListDate = convertor.ToString(item[9]) jsonData, _ := json.Marshal(stockData)
stock.DelistDate = convertor.ToString(item[10]) err := json.Unmarshal(jsonData, stock)
stock.ActEntType = convertor.ToString(item[11]) if err != nil {
stock.TsCode = convertor.ToString(item[12]) continue
stock.Symbol = convertor.ToString(item[13]) }
stock.Cnspell = convertor.ToString(item[14]) stock.ID = 0
stock.Fullname = convertor.ToString(item[20])
stock.Ename = convertor.ToString(item[21])
var count int64 var count int64
db.Dao.Model(&data.StockBasic{}).Where("ts_code = ?", stock.TsCode).Count(&count) db.Dao.Model(&data.StockBasic{}).Where("ts_code = ?", stock.TsCode).Count(&count)
if count > 0 { if count > 0 {
@ -306,7 +305,38 @@ func initStockData(ctx context.Context) {
} else { } else {
db.Dao.Create(stock) db.Dao.Create(stock)
} }
//db.Dao.Model(&data.StockBasic{}).FirstOrCreate(stock, &data.StockBasic{TsCode: stock.TsCode}).Where("ts_code = ?", stock.TsCode).Updates(stock)
} }
//for _, item := range res.Data.Items {
// stock := &data.StockBasic{}
// stock.Exchange = convertor.ToString(item[0])
// stock.IsHs = convertor.ToString(item[1])
// stock.Name = convertor.ToString(item[2])
// stock.Industry = convertor.ToString(item[3])
// stock.ListStatus = convertor.ToString(item[4])
// stock.ActName = convertor.ToString(item[5])
// stock.ID = uint(item[6].(float64))
// stock.CurrType = convertor.ToString(item[7])
// stock.Area = convertor.ToString(item[8])
// stock.ListDate = convertor.ToString(item[9])
// stock.DelistDate = convertor.ToString(item[10])
// stock.ActEntType = convertor.ToString(item[11])
// stock.TsCode = convertor.ToString(item[12])
// stock.Symbol = convertor.ToString(item[13])
// stock.Cnspell = convertor.ToString(item[14])
// stock.Fullname = convertor.ToString(item[20])
// stock.Ename = convertor.ToString(item[21])
//
// var count int64
// db.Dao.Model(&data.StockBasic{}).Where("ts_code = ?", stock.TsCode).Count(&count)
// if count > 0 {
// continue
// } else {
// db.Dao.Create(stock)
// }
//}
} }
func checkDir(dir string) { func checkDir(dir string) {