package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:ttx2011@tcp(db.freeicu.top:32000)/ashare?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Test 1: dates from trading_day
	var dates []string
	rows, _ := db.Query("SELECT DATE(date) as d FROM trading_day ORDER BY date DESC LIMIT 3")
	for rows.Next() {
		var d string
		rows.Scan(&d)
		dates = append(dates, d)
		fmt.Printf("date: [%s]\n", d)
	}
	rows.Close()

	if len(dates) == 0 {
		fmt.Println("NO DATES!")
		return
	}
	latest := dates[0]
	fmt.Printf("latest: [%s]\n", latest)

	// Test 2: today's limit-up count
	var cnt int
	db.QueryRow("SELECT COUNT(*) FROM stock_daily WHERE date = ? AND pct_change >= 9.5", latest).Scan(&cnt)
	fmt.Printf("limit-up count on %s: %d\n", latest, cnt)

	// Test 3: with JOIN
	var cnt2 int
	db.QueryRow(`
		SELECT COUNT(*)
		FROM stock_daily sd
		LEFT JOIN stock_info si ON sd.code = si.code
		WHERE sd.date = ? AND sd.pct_change >= 9.5
	`, latest).Scan(&cnt2)
	fmt.Printf("limit-up with JOIN: %d\n", cnt2)

	// Test 4: limit-up rows
	rows2, _ := db.Query("SELECT code, DATE(date) as d FROM stock_daily WHERE pct_change >= 9.5 AND date >= ? LIMIT 5", dates[len(dates)-1])
	for rows2.Next() {
		var code, d string
		rows2.Scan(&code, &d)
		fmt.Printf("  limitRow: code=%s date=[%s]\n", code, d)
	}
	rows2.Close()
}
