Files
ssq-desk/internal/api/ssq_api.go
2026-01-14 14:17:38 +08:00

56 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"ssq-desk/internal/service"
"ssq-desk/internal/storage/repository"
)
// SsqAPI 双色球查询 API
type SsqAPI struct {
queryService *service.QueryService
}
// NewSsqAPI 创建双色球查询 API
func NewSsqAPI() (*SsqAPI, error) {
// 优先使用 MySQL数据源
repo, err := repository.NewMySQLSsqRepository()
if err != nil {
// MySQL 连接失败,降级使用本地 SQLite
repo, err = repository.NewSQLiteSsqRepository()
if err != nil {
return nil, err
}
}
queryService := service.NewQueryService(repo)
return &SsqAPI{
queryService: queryService,
}, nil
}
// QueryRequest 查询请求参数
type QueryRequest struct {
RedBalls []int `json:"red_balls"` // 红球列表
BlueBall int `json:"blue_ball"` // 蓝球0表示不限制
BlueBallRange []int `json:"blue_ball_range"` // 蓝球筛选范围
}
// QueryHistory 查询历史数据
func (api *SsqAPI) QueryHistory(req QueryRequest) (*service.QueryResult, error) {
if api.queryService == nil {
// 重新初始化
newAPI, err := NewSsqAPI()
if err != nil {
return nil, err
}
api.queryService = newAPI.queryService
}
return api.queryService.Query(service.QueryRequest{
RedBalls: req.RedBalls,
BlueBall: req.BlueBall,
BlueBallRange: req.BlueBallRange,
})
}