fix backend
This commit is contained in:
@@ -102,64 +102,42 @@ func (r *VoucherRepository) GetRandomVouchersByRows(ctx context.Context, rows in
|
||||
var allVouchers []entities.Voucher
|
||||
var err error
|
||||
|
||||
// First, try to get vouchers based on winner_number parameter
|
||||
// Get all non-winner vouchers (regardless of winner_number)
|
||||
nonWinnerQuery := r.db.WithContext(ctx).Where("is_winner = ?", false)
|
||||
err = nonWinnerQuery.Find(&allVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If winner_number is provided, also include vouchers with that specific winner_number
|
||||
// (even if they're already winners, to have a pool to select from)
|
||||
if winnerNumber != nil {
|
||||
// If winner_number is provided, filter by it and exclude already won vouchers
|
||||
query := r.db.WithContext(ctx).Where("winner_number = ? AND is_winner = ?", *winnerNumber, false)
|
||||
err = query.Find(&allVouchers).Error
|
||||
var winnerNumberVouchers []entities.Voucher
|
||||
winnerQuery := r.db.WithContext(ctx).Where("winner_number = ?", *winnerNumber)
|
||||
err = winnerQuery.Find(&winnerNumberVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If no vouchers found for the specified winner_number, fallback to winner_number = 0
|
||||
if len(allVouchers) == 0 {
|
||||
fallbackQuery := r.db.WithContext(ctx).Where("winner_number = ? AND is_winner = ?", 0, false)
|
||||
err = fallbackQuery.Find(&allVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Merge the two lists, avoiding duplicates
|
||||
existingIDs := make(map[int64]bool)
|
||||
for _, voucher := range allVouchers {
|
||||
existingIDs[voucher.ID] = true
|
||||
}
|
||||
|
||||
// If still no vouchers found, try without is_winner filter for winner_number = 0
|
||||
if len(allVouchers) == 0 {
|
||||
fallbackQuery2 := r.db.WithContext(ctx).Where("winner_number = ?", 0)
|
||||
err = fallbackQuery2.Find(&allVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for _, voucher := range winnerNumberVouchers {
|
||||
if !existingIDs[voucher.ID] {
|
||||
allVouchers = append(allVouchers, voucher)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If still no vouchers found, get any vouchers available
|
||||
if len(allVouchers) == 0 {
|
||||
err = r.db.WithContext(ctx).Find(&allVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If winner_number is not provided, use default winner_number = 0 and exclude already won vouchers
|
||||
query := r.db.WithContext(ctx).Where("winner_number = ? AND is_winner = ?", 0, false)
|
||||
err = query.Find(&allVouchers).Error
|
||||
// If still no vouchers found, get any vouchers available as fallback
|
||||
if len(allVouchers) == 0 {
|
||||
err = r.db.WithContext(ctx).Find(&allVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If no vouchers found, try without is_winner filter for winner_number = 0
|
||||
if len(allVouchers) == 0 {
|
||||
fallbackQuery := r.db.WithContext(ctx).Where("winner_number = ?", 0)
|
||||
err = fallbackQuery.Find(&allVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// If still no vouchers found, get any vouchers available
|
||||
if len(allVouchers) == 0 {
|
||||
err = r.db.WithContext(ctx).Find(&allVouchers).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(allVouchers) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user