package repository import ( "fmt" "gorm.io/gorm" "ssq-desk/internal/database" "ssq-desk/internal/storage/models" "time" ) // SQLiteSsqRepository SQLite 实现 type SQLiteSsqRepository struct { db *gorm.DB } // NewSQLiteSsqRepository 创建 SQLite 仓库 func NewSQLiteSsqRepository() (SsqRepository, error) { db := database.GetSQLite() if db == nil { return nil, fmt.Errorf("SQLite 数据库未初始化或初始化失败") } // 自动迁移表结构(数据库初始化时已迁移,这里再次确保) err := db.AutoMigrate(&models.SsqHistory{}) if err != nil { return nil, fmt.Errorf("SQLite 表迁移失败: %v", err) } return &SQLiteSsqRepository{db: db}, nil } // FindAll 查询所有历史数据 func (r *SQLiteSsqRepository) FindAll() ([]models.SsqHistory, error) { var histories []models.SsqHistory err := r.db.Order("issue_number DESC").Find(&histories).Error return histories, err } // FindByIssue 根据期号查询 func (r *SQLiteSsqRepository) FindByIssue(issueNumber string) (*models.SsqHistory, error) { var history models.SsqHistory err := r.db.Where("issue_number = ?", issueNumber).First(&history).Error if err == gorm.ErrRecordNotFound { return nil, nil } return &history, err } // FindByRedBalls 根据红球查询(支持部分匹配) func (r *SQLiteSsqRepository) FindByRedBalls(redBalls []int) ([]models.SsqHistory, error) { if len(redBalls) == 0 { return r.FindAll() } var histories []models.SsqHistory query := r.db // 构建查询条件:红球在输入的红球列表中 for _, ball := range redBalls { query = query.Where("red_ball_1 = ? OR red_ball_2 = ? OR red_ball_3 = ? OR red_ball_4 = ? OR red_ball_5 = ? OR red_ball_6 = ?", ball, ball, ball, ball, ball, ball) } err := query.Order("issue_number DESC").Find(&histories).Error return histories, err } // FindByRedAndBlue 根据红球和蓝球查询 func (r *SQLiteSsqRepository) FindByRedAndBlue(redBalls []int, blueBall int, blueBallRange []int) ([]models.SsqHistory, error) { var histories []models.SsqHistory query := r.db // 红球条件 if len(redBalls) > 0 { for _, ball := range redBalls { query = query.Where("red_ball_1 = ? OR red_ball_2 = ? OR red_ball_3 = ? OR red_ball_4 = ? OR red_ball_5 = ? OR red_ball_6 = ?", ball, ball, ball, ball, ball, ball) } } // 蓝球条件 if blueBall > 0 { query = query.Where("blue_ball = ?", blueBall) } else if len(blueBallRange) > 0 { query = query.Where("blue_ball IN ?", blueBallRange) } err := query.Order("issue_number DESC").Find(&histories).Error return histories, err } // Create 创建记录 func (r *SQLiteSsqRepository) Create(history *models.SsqHistory) error { now := time.Now() if history.CreatedAt.IsZero() { history.CreatedAt = now } if history.UpdatedAt.IsZero() { history.UpdatedAt = now } return r.db.Create(history).Error } // BatchCreate 批量创建 func (r *SQLiteSsqRepository) BatchCreate(histories []models.SsqHistory) error { if len(histories) == 0 { return nil } now := time.Now() for i := range histories { if histories[i].CreatedAt.IsZero() { histories[i].CreatedAt = now } if histories[i].UpdatedAt.IsZero() { histories[i].UpdatedAt = now } } return r.db.CreateInBatches(histories, 100).Error } // GetLatestIssue 获取最新期号 func (r *SQLiteSsqRepository) GetLatestIssue() (string, error) { var history models.SsqHistory err := r.db.Order("issue_number DESC").First(&history).Error if err == gorm.ErrRecordNotFound { return "", nil } return history.IssueNumber, err } // Count 统计总数 func (r *SQLiteSsqRepository) Count() (int64, error) { var count int64 err := r.db.Model(&models.SsqHistory{}).Count(&count).Error return count, err }