244 lines
7.1 KiB
Go
244 lines
7.1 KiB
Go
package logic4variety
|
||
|
||
import (
|
||
"asset_assistant/db"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"fmt"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/google/uuid"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
// ReadRequest 读取请求参数结构
|
||
type ReadRequest struct {
|
||
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"` // 每页条数,可选
|
||
}
|
||
|
||
// ReadData 读取响应数据结构
|
||
type ReadData struct {
|
||
Total int64 `json:"total"` // 总条数
|
||
Page int `json:"page"` // 当前页码
|
||
PageSize int `json:"page_size"` // 每页条数
|
||
Items []VarietyInfoViewItem `json:"items"` // 数据列表
|
||
}
|
||
|
||
// 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 读取响应结构
|
||
type ReadResponse struct {
|
||
Success bool `json:"success"` // 操作是否成功
|
||
Message string `json:"message"` // 提示信息
|
||
Data ReadData `json:"data"` // 响应数据
|
||
}
|
||
|
||
// ReadHandler 处理信息查询逻辑
|
||
func ReadHandler(c *gin.Context) {
|
||
startTime := time.Now()
|
||
// 获取或生成请求ID
|
||
reqID := c.Request.Header.Get("X-ReadRequest-ID")
|
||
if reqID == "" {
|
||
reqID = uuid.New().String()
|
||
zap.L().Debug("✨ 生成新的请求ID", zap.String("req_id", reqID))
|
||
}
|
||
|
||
// 记录请求接收日志
|
||
zap.L().Info("📥 收到品种查询请求",
|
||
zap.String("req_id", reqID),
|
||
zap.String("path", c.Request.URL.Path),
|
||
zap.String("method", c.Request.Method),
|
||
)
|
||
|
||
// 绑定请求参数
|
||
var req ReadRequest
|
||
if err := c.ShouldBindQuery(&req); err != nil {
|
||
zap.L().Warn("⚠️ 请求参数解析失败",
|
||
zap.String("req_id", reqID),
|
||
zap.Error(err),
|
||
)
|
||
c.JSON(http.StatusBadRequest, ReadResponse{
|
||
Success: false,
|
||
Message: "请求参数格式错误",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 验证查询条件至少有一个不为空
|
||
if req.VarietyID == "" && req.Name == "" && req.Code == "" && req.ExchangeName == "" {
|
||
zap.L().Warn("⚠️ 请求参数验证失败",
|
||
zap.String("req_id", reqID),
|
||
zap.String("reason", "variety_id、name、code、exchange_name不能同时为空"),
|
||
)
|
||
c.JSON(http.StatusBadRequest, ReadResponse{
|
||
Success: false,
|
||
Message: "请求参数错误:variety_id、name、code、exchange_name不能同时为空",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 处理分页参数默认值
|
||
page, err := strconv.Atoi(req.Page)
|
||
if err != nil || page < 1 {
|
||
page = 1
|
||
}
|
||
pageSize, err := strconv.Atoi(req.PageSize)
|
||
if err != nil || pageSize < 1 {
|
||
pageSize = 20
|
||
}
|
||
|
||
zap.L().Debug("✅ 请求参数验证通过",
|
||
zap.String("req_id", reqID),
|
||
zap.String("variety_id", req.VarietyID),
|
||
zap.String("name", req.Name),
|
||
zap.String("code", req.Code),
|
||
zap.String("exchange_name", req.ExchangeName),
|
||
zap.Int("page", page),
|
||
zap.Int("page_size", pageSize),
|
||
)
|
||
|
||
// 构建查询条件和参数
|
||
whereClauses := []string{}
|
||
args := []interface{}{}
|
||
paramIndex := 1
|
||
|
||
if req.VarietyID != "" {
|
||
whereClauses = append(whereClauses, "variety_id = $"+strconv.Itoa(paramIndex))
|
||
args = append(args, req.VarietyID)
|
||
paramIndex++
|
||
}
|
||
if req.Name != "" {
|
||
whereClauses = append(whereClauses, "name LIKE $"+strconv.Itoa(paramIndex))
|
||
args = append(args, "%"+req.Name+"%")
|
||
paramIndex++
|
||
}
|
||
if req.ExchangeName != "" {
|
||
whereClauses = append(whereClauses, "exchange_name LIKE $"+strconv.Itoa(paramIndex))
|
||
args = append(args, "%"+req.ExchangeName+"%")
|
||
paramIndex++
|
||
}
|
||
if req.Code != "" {
|
||
whereClauses = append(whereClauses, "code LIKE $"+strconv.Itoa(paramIndex))
|
||
args = append(args, "%"+req.Code+"%")
|
||
paramIndex++
|
||
}
|
||
|
||
// 构建基础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
|
||
countSQL += whereStr
|
||
}
|
||
|
||
// 计算分页偏移量
|
||
offset := (page - 1) * pageSize
|
||
|
||
// 拼接分页SQL
|
||
querySQL := fmt.Sprintf("%s ORDER BY variety_id LIMIT $%d OFFSET $%d", baseSQL, paramIndex, paramIndex+1)
|
||
args = append(args, pageSize, offset)
|
||
|
||
// 查询总条数
|
||
var total int64
|
||
countArgs := args[:len(args)-2] // 排除分页参数
|
||
err = db.DB.QueryRow(countSQL, countArgs...).Scan(&total)
|
||
if err != nil {
|
||
zap.L().Error("❌ 查询总条数失败",
|
||
zap.String("req_id", reqID),
|
||
zap.Error(err),
|
||
)
|
||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
||
Success: false,
|
||
Message: "查询数据失败,请稍后重试",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 执行分页查询
|
||
rows, err := db.DB.Query(querySQL, args...)
|
||
if err != nil {
|
||
zap.L().Error("❌ 分页查询失败",
|
||
zap.String("req_id", reqID),
|
||
zap.Error(err),
|
||
)
|
||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
||
Success: false,
|
||
Message: "查询数据失败,请稍后重试",
|
||
})
|
||
return
|
||
}
|
||
defer rows.Close()
|
||
|
||
// 处理查询结果
|
||
var items []VarietyInfoViewItem
|
||
for rows.Next() {
|
||
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),
|
||
)
|
||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
||
Success: false,
|
||
Message: "数据处理失败,请稍后重试",
|
||
})
|
||
return
|
||
}
|
||
items = append(items, item)
|
||
}
|
||
|
||
// 检查行迭代过程中是否发生错误
|
||
if err := rows.Err(); err != nil {
|
||
zap.L().Error("❌ 行迭代错误",
|
||
zap.String("req_id", reqID),
|
||
zap.Error(err),
|
||
)
|
||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
||
Success: false,
|
||
Message: "查询数据失败,请稍后重试",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 记录请求处理耗时
|
||
duration := time.Since(startTime)
|
||
zap.L().Info("✅ 品种查询请求处理完成",
|
||
zap.String("req_id", reqID),
|
||
zap.Int64("total", total),
|
||
zap.Int("page", page),
|
||
zap.Int("page_size", pageSize),
|
||
zap.Duration("duration", duration),
|
||
)
|
||
|
||
// 返回成功响应
|
||
c.JSON(http.StatusOK, ReadResponse{
|
||
Success: true,
|
||
Message: "查询成功",
|
||
Data: ReadData{
|
||
Total: total,
|
||
Page: page,
|
||
PageSize: pageSize,
|
||
Items: items,
|
||
},
|
||
})
|
||
}
|