ad
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package logic4exchange
|
||||
package logic4variety
|
||||
|
||||
import (
|
||||
"asset_assistant/db"
|
||||
@@ -16,10 +16,10 @@ import (
|
||||
|
||||
// ReadRequest 读取请求参数结构
|
||||
type ReadRequest struct {
|
||||
ExchangeID string `form:"exchange_id"` // 交易所ID,可选
|
||||
Name string `form:"name"` // 交易所名称,可选
|
||||
Code string `form:"code"` // 交易所代码,可选
|
||||
ShortName string `form:"short_name"` // 交易所短名称,新增查询条件
|
||||
VarietyID string `form:"variety_id"` // 品种ID,可选
|
||||
Name string `form:"name"` // 品种名称,可选
|
||||
Code string `form:"code"` // 品种代码,可选
|
||||
ExchangeName string `form:"exchange_name"` // 交易所名称,可选
|
||||
Page string `form:"page"` // 页码,可选
|
||||
PageSize string `form:"page_size"` // 每页条数,可选
|
||||
}
|
||||
@@ -29,15 +29,19 @@ type ReadData struct {
|
||||
Total int64 `json:"total"` // 总条数
|
||||
Page int `json:"page"` // 当前页码
|
||||
PageSize int `json:"page_size"` // 每页条数
|
||||
Items []ExchangeInfoViewItem `json:"items"` // 数据列表
|
||||
Items []VarietyInfoViewItem `json:"items"` // 数据列表
|
||||
}
|
||||
|
||||
// ExchangeInfoViewItem 视图数据项结构
|
||||
type ExchangeInfoViewItem struct {
|
||||
ExchangeID string `json:"exchange_id"` // 交易所ID
|
||||
Name string `json:"name"` // 交易所名称
|
||||
Code string `json:"code"` // 交易所代码
|
||||
ShortName string `json:"short_name"` // 新增:交易所短名称
|
||||
// VarietyInfoViewItem 视图数据项结构
|
||||
type VarietyInfoViewItem struct {
|
||||
VarietyID string `json:"variety_id"` // 品种ID
|
||||
Name string `json:"name"` // 品种名称
|
||||
Code string `json:"code"` // 品种代码
|
||||
ExchangeName string `json:"exchange_name"` // 交易所名称
|
||||
Tick string `json:"tick"` // 最小变动价位
|
||||
TickPrice string `json:"tick_price"` // 最小变动价位对应的价值
|
||||
TickOriginal float64 `json:"tick_original"` // 最小变动价位(原始值)
|
||||
TickPriceOriginal float64 `json:"tick_price_original"` // 最小变动价位对应的价值(原始值)
|
||||
}
|
||||
|
||||
// ReadResponse 读取响应结构
|
||||
@@ -58,7 +62,7 @@ func ReadHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 记录请求接收日志
|
||||
zap.L().Info("📥 收到交易所查询请求",
|
||||
zap.L().Info("📥 收到品种查询请求",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("path", c.Request.URL.Path),
|
||||
zap.String("method", c.Request.Method),
|
||||
@@ -78,15 +82,15 @@ func ReadHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证查询条件至少有一个不为空(新增short_name作为可选条件)
|
||||
if req.ExchangeID == "" && req.Name == "" && req.Code == "" && req.ShortName == "" {
|
||||
// 验证查询条件至少有一个不为空
|
||||
if req.VarietyID == "" && req.Name == "" && req.Code == "" && req.ExchangeName == "" {
|
||||
zap.L().Warn("⚠️ 请求参数验证失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("reason", "exchange_id、name、code、short_name不能同时为空"),
|
||||
zap.String("reason", "variety_id、name、code、exchange_name不能同时为空"),
|
||||
)
|
||||
c.JSON(http.StatusBadRequest, ReadResponse{
|
||||
Success: false,
|
||||
Message: "请求参数错误:exchange_id、name、code、short_name不能同时为空",
|
||||
Message: "请求参数错误:variety_id、name、code、exchange_name不能同时为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -103,10 +107,10 @@ func ReadHandler(c *gin.Context) {
|
||||
|
||||
zap.L().Debug("✅ 请求参数验证通过",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("exchange_id", req.ExchangeID),
|
||||
zap.String("variety_id", req.VarietyID),
|
||||
zap.String("name", req.Name),
|
||||
zap.String("code", req.Code),
|
||||
zap.String("short_name", req.ShortName), // 新增短名称日志
|
||||
zap.String("exchange_name", req.ExchangeName),
|
||||
zap.Int("page", page),
|
||||
zap.Int("page_size", pageSize),
|
||||
)
|
||||
@@ -116,9 +120,9 @@ func ReadHandler(c *gin.Context) {
|
||||
args := []interface{}{}
|
||||
paramIndex := 1
|
||||
|
||||
if req.ExchangeID != "" {
|
||||
whereClauses = append(whereClauses, "exchange_id = $"+strconv.Itoa(paramIndex))
|
||||
args = append(args, req.ExchangeID)
|
||||
if req.VarietyID != "" {
|
||||
whereClauses = append(whereClauses, "variety_id = $"+strconv.Itoa(paramIndex))
|
||||
args = append(args, req.VarietyID)
|
||||
paramIndex++
|
||||
}
|
||||
if req.Name != "" {
|
||||
@@ -126,10 +130,9 @@ func ReadHandler(c *gin.Context) {
|
||||
args = append(args, "%"+req.Name+"%")
|
||||
paramIndex++
|
||||
}
|
||||
// 新增:短名称查询条件
|
||||
if req.ShortName != "" {
|
||||
whereClauses = append(whereClauses, "short_name LIKE $"+strconv.Itoa(paramIndex))
|
||||
args = append(args, "%"+req.ShortName+"%")
|
||||
if req.ExchangeName != "" {
|
||||
whereClauses = append(whereClauses, "exchange_name LIKE $"+strconv.Itoa(paramIndex))
|
||||
args = append(args, "%"+req.ExchangeName+"%")
|
||||
paramIndex++
|
||||
}
|
||||
if req.Code != "" {
|
||||
@@ -138,9 +141,9 @@ func ReadHandler(c *gin.Context) {
|
||||
paramIndex++
|
||||
}
|
||||
|
||||
// 构建基础SQL(新增查询short_name字段)
|
||||
baseSQL := "SELECT exchange_id, name, short_name, code FROM exchange_info_view"
|
||||
countSQL := "SELECT COUNT(*) FROM exchange_info_view"
|
||||
// 构建基础SQL
|
||||
baseSQL := "SELECT variety_id, name, code, exchange_name, tick, tick_price, tick_original, tick_price_original FROM variety_info_view"
|
||||
countSQL := "SELECT COUNT(*) FROM variety_info_view"
|
||||
if len(whereClauses) > 0 {
|
||||
whereStr := " WHERE " + strings.Join(whereClauses, " AND ")
|
||||
baseSQL += whereStr
|
||||
@@ -151,7 +154,7 @@ func ReadHandler(c *gin.Context) {
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
// 拼接分页SQL
|
||||
querySQL := fmt.Sprintf("%s ORDER BY exchange_id LIMIT $%d OFFSET $%d", baseSQL, paramIndex, paramIndex+1)
|
||||
querySQL := fmt.Sprintf("%s ORDER BY variety_id LIMIT $%d OFFSET $%d", baseSQL, paramIndex, paramIndex+1)
|
||||
args = append(args, pageSize, offset)
|
||||
|
||||
// 查询总条数
|
||||
@@ -185,11 +188,11 @@ func ReadHandler(c *gin.Context) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// 处理查询结果(新增扫描short_name字段)
|
||||
var items []ExchangeInfoViewItem
|
||||
// 处理查询结果
|
||||
var items []VarietyInfoViewItem
|
||||
for rows.Next() {
|
||||
var item ExchangeInfoViewItem
|
||||
if err := rows.Scan(&item.ExchangeID, &item.Name, &item.ShortName, &item.Code); err != nil {
|
||||
var item VarietyInfoViewItem
|
||||
if err := rows.Scan(&item.VarietyID, &item.Name, &item.Code, &item.ExchangeName, &item.Tick, &item.TickPrice, &item.TickOriginal, &item.TickPriceOriginal); err != nil {
|
||||
zap.L().Error("❌ 解析查询结果失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.Error(err),
|
||||
@@ -218,7 +221,7 @@ func ReadHandler(c *gin.Context) {
|
||||
|
||||
// 记录请求处理耗时
|
||||
duration := time.Since(startTime)
|
||||
zap.L().Info("✅ 交易所查询请求处理完成",
|
||||
zap.L().Info("✅ 品种查询请求处理完成",
|
||||
zap.String("req_id", reqID),
|
||||
zap.Int64("total", total),
|
||||
zap.Int("page", page),
|
||||
|
||||
Reference in New Issue
Block a user