Files
tick-computing/internal/tick_recv.go

83 lines
2.1 KiB
Go
Raw Normal View History

2026-04-12 23:24:43 +08:00
package internal
import (
2026-04-13 13:14:14 +08:00
"encoding/json"
"log"
2026-04-12 23:24:43 +08:00
"net/http"
2026-04-13 13:14:14 +08:00
"time"
2026-04-12 23:24:43 +08:00
2026-04-13 13:14:14 +08:00
"git.apinb.com/bsm-sdk/core/utils"
2026-04-12 23:24:43 +08:00
"github.com/gin-gonic/gin"
)
2026-04-13 14:11:08 +08:00
// Tick 行情数据
type Tick struct {
LastPrice float64 `json:"lastPrice"`
Open float64 `json:"open"`
High float64 `json:"high"`
Low float64 `json:"low"`
LastClose float64 `json:"lastClose"`
Volume int64 `json:"volume"`
Amount float64 `json:"amount"`
PVolume int64 `json:"pvolume"`
BidPrice []float64 `json:"bidPrice"`
BidVol []int `json:"bidVol"`
AskPrice []float64 `json:"askPrice"`
AskVol []int `json:"askVol"`
Time int64 `json:"time"`
TimeTag string `json:"timetag"`
StockStatus int `json:"stockStatus"`
LastSettlementPrice float64 `json:"lastSettlementPrice"`
SettlementPrice float64 `json:"settlementPrice"`
OpenInt int `json:"openInt"`
}
2026-04-12 23:24:43 +08:00
func TickRecv(c *gin.Context) {
2026-04-13 13:14:14 +08:00
log.Println("TickRecv called")
stocks := GetAllowStocks()
if len(stocks) == 0 {
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"num": len(stocks),
})
return
}
2026-04-12 23:24:43 +08:00
2026-04-13 14:11:08 +08:00
data := map[string]Tick{}
2026-04-12 23:24:43 +08:00
// Gin 的 Bind 方法会自动根据 Content-Type 解析
if err := c.Bind(&data); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
2026-04-13 13:14:14 +08:00
for key, value := range data {
if utils.ArrayInString(key, stocks) {
2026-04-13 14:11:08 +08:00
err := RedisService.Set(key, value, 1*time.Hour)
if err != nil {
log.Println("Error: Redis Set", err.Error())
}
2026-04-13 13:14:14 +08:00
}
}
2026-04-12 23:24:43 +08:00
// 处理数据...
c.JSON(http.StatusOK, gin.H{
2026-04-13 09:08:38 +08:00
"status": "OK",
2026-04-12 23:24:43 +08:00
})
}
2026-04-13 13:14:14 +08:00
func GetAllowStocks() []string {
result, err := utils.HttpGet("http://139.224.247.176:13499/a/pass_codes")
if err != nil {
log.Println("Error: Http Get pass_codes", err.Error())
return nil
}
var stocks []string
err = json.Unmarshal(result, &stocks)
if err != nil {
log.Println("Error: Json Unmarshal pass_codes", err.Error())
return nil
}
return stocks
}