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