From 054445e1cab564e419e10a1377fd4ce326e23b48 Mon Sep 17 00:00:00 2001 From: vipg Date: Fri, 19 Dec 2025 16:53:46 +0800 Subject: [PATCH] add --- .../src/crud/create.go | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/services/cn_futures_trading_records/src/crud/create.go b/services/cn_futures_trading_records/src/crud/create.go index 1701c08..21b3e6c 100644 --- a/services/cn_futures_trading_records/src/crud/create.go +++ b/services/cn_futures_trading_records/src/crud/create.go @@ -2,6 +2,7 @@ package crud import ( "cn_futures_trading_records/db" + "encoding/json" // 新增 "net/http" "time" @@ -22,7 +23,7 @@ type TradingRecordsCreateRequest struct { Direction string `json:"direction" binding:"required,oneof=long short"` // 交易方向:long 多头 / short 空头 OpenPrice float64 `json:"open_price" binding:"required"` // 开仓价格(单位:元) OpenFee float64 `json:"open_fee" binding:"required,min=0"` // 开仓手续费(≥0) - CloseYear int `json:"close_year" binding:"required,min=1900,max=2100"` // 平仓时间:年(1900-2200) + CloseYear int `json:"close_year" binding:"required,min=1900,max=2200"` // 平仓时间:年(1900-2200) CloseMonth int `json:"close_month" binding:"required,min=1,max=12"` // 平仓时间:月(1-12) CloseDay int `json:"close_day" binding:"required,min=1,max=31"` // 平仓时间:日(1-31) ClosePrice float64 `json:"close_price" binding:"required"` // 平仓价格(单位:元) @@ -115,14 +116,29 @@ func CreateHandler(c *gin.Context) { } }() + // 将 payload 结构体转为 JSONB 可用的 []byte + payloadBytes, err := json.Marshal(req.Payload) + if err != nil { + tx.Rollback() + zap.L().Error("❌ payload 序列化失败", + zap.String("req_id", reqID), + zap.Error(err), + ) + c.JSON(http.StatusOK, TradingRecordsCreateResponse{ + Success: false, + Message: "内部数据转换错误", + }) + return + } + // 插入交易记录 var recordID string err = tx.QueryRow( `INSERT INTO trading_records (event_type, payload) - VALUES ('交易中', $2) + VALUES ($1, $2) RETURNING id`, req.EventType, - req.Payload, + payloadBytes, ).Scan(&recordID) if err != nil { tx.Rollback() @@ -166,4 +182,4 @@ func CreateHandler(c *gin.Context) { RecordID: recordID, }, }) -} +} \ No newline at end of file