add
This commit is contained in:
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user