New admin triggers for db maintenance, updating display of prize results and logic fix
This commit is contained in:
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"synlotto-website/handlers"
|
||||
"synlotto-website/helpers"
|
||||
@@ -91,3 +92,74 @@ func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, er
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func UpdateMissingPrizes(db *sql.DB) error {
|
||||
type TicketInfo struct {
|
||||
ID int
|
||||
GameType string
|
||||
DrawDate string
|
||||
Main int
|
||||
Bonus int
|
||||
}
|
||||
|
||||
var tickets []TicketInfo
|
||||
|
||||
// Step 1: Load all relevant tickets
|
||||
rows, err := db.Query(`
|
||||
SELECT id, game_type, draw_date, matched_main, matched_bonus
|
||||
FROM my_tickets
|
||||
WHERE is_winner = 1 AND (prize_label IS NULL OR prize_label = '')
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var t TicketInfo
|
||||
if err := rows.Scan(&t.ID, &t.GameType, &t.DrawDate, &t.Main, &t.Bonus); err != nil {
|
||||
log.Println("⚠️ Failed to scan row:", err)
|
||||
continue
|
||||
}
|
||||
tickets = append(tickets, t)
|
||||
}
|
||||
|
||||
// Step 2: Now that the reader is closed, perform updates
|
||||
for _, t := range tickets {
|
||||
if t.GameType != "Thunderball" {
|
||||
continue
|
||||
}
|
||||
|
||||
idx, ok := rules.GetThunderballPrizeIndex(t.Main, t.Bonus)
|
||||
if !ok {
|
||||
log.Printf("❌ No index for %d main, %d bonus", t.Main, t.Bonus)
|
||||
continue
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`SELECT prize%d_per_winner FROM prizes_thunderball WHERE draw_date = ?`, idx)
|
||||
|
||||
var amount int
|
||||
err := db.QueryRow(query, t.DrawDate).Scan(&amount)
|
||||
if err != nil {
|
||||
log.Printf("❌ Prize lookup failed for ticket %d: %v", t.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
label := "Free Ticket"
|
||||
if amount > 0 {
|
||||
label = fmt.Sprintf("£%.2f", float64(amount))
|
||||
}
|
||||
|
||||
_, err = db.Exec(`
|
||||
UPDATE my_tickets SET prize_amount = ?, prize_label = ? WHERE id = ?
|
||||
`, float64(amount), label, t.ID)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("❌ Failed to update ticket %d: %v", t.ID, err)
|
||||
} else {
|
||||
log.Printf("✅ Updated ticket %d → %s", t.ID, label)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user