54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"level_go/internal/model"
|
|
"level_go/internal/service"
|
|
"net/http"
|
|
)
|
|
|
|
// StockHandler 承载股票行情和走势分析接口。
|
|
type StockHandler struct {
|
|
Service *service.StockService // 股票服务,封装行情查询和区间分析逻辑。
|
|
}
|
|
|
|
// NewStockHandler 创建股票接口 Handler。
|
|
func NewStockHandler(s *service.StockService) *StockHandler {
|
|
return &StockHandler{Service: s}
|
|
}
|
|
|
|
// GetKlineData 获取指定股票的历史 K 线数据。
|
|
//
|
|
// 股票代码来自路径参数 :code,返回按交易日升序排列的 stock_daily 记录。
|
|
func (h *StockHandler) GetKlineData(c *gin.Context) {
|
|
code := c.Param("code")
|
|
if code == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "股票代码不能为空"})
|
|
return
|
|
}
|
|
|
|
data, err := h.Service.GetHistoryKline(code)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, data)
|
|
}
|
|
|
|
// Analyze 根据前端传入的起止日期分析涨停后走势。
|
|
func (h *StockHandler) Analyze(c *gin.Context) {
|
|
var req model.AnalyzeRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
|
return
|
|
}
|
|
|
|
results, err := h.Service.AnalyzePerformance(req.StartDate, req.EndDate)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, results)
|
|
}
|