package main

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

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

func main() {
	dsn := "root:bindbox2025kdy@tcp(150.158.78.154:3306)/bindbox_game?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal("连接失败:", err)
	}
	defer db.Close()

	orderNo := "O20260115145414801"

	// 查询订单和抽奖记录
	fmt.Println("=== 订单抽奖记录 ===")
	rows, err := db.Query(`
		SELECT 
			o.id as order_id,
			o.order_no,
			o.user_id,
			dl.id as draw_log_id,
			dl.reward_id,
			dl.issue_id,
			dl.created_at as draw_time,
			ar.product_id,
			ar.weight,
			p.name as reward_name
		FROM orders o 
		LEFT JOIN activity_draw_logs dl ON dl.order_id = o.id 
		LEFT JOIN activity_reward_settings ar ON ar.id = dl.reward_id 
		LEFT JOIN products p ON p.id = ar.product_id 
		WHERE o.order_no = ?
	`, orderNo)
	if err != nil {
		log.Fatal("查询订单失败:", err)
	}
	defer rows.Close()

	for rows.Next() {
		var orderID, userID, drawLogID, rewardID, issueID, productID sql.NullInt64
		var orderNoRes, drawTime, rewardName sql.NullString
		var weight sql.NullInt64
		err := rows.Scan(&orderID, &orderNoRes, &userID, &drawLogID, &rewardID, &issueID, &drawTime, &productID, &weight, &rewardName)
		if err != nil {
			log.Fatal("扫描失败:", err)
		}
		fmt.Printf("订单ID: %d, 用户ID: %d, 期次ID: %d\n", orderID.Int64, userID.Int64, issueID.Int64)
		fmt.Printf("抽奖记录ID: %d, 奖品ID: %d, 产品ID: %d, 权重: %d\n", drawLogID.Int64, rewardID.Int64, productID.Int64, weight.Int64)
		fmt.Printf("中奖奖品: %s, 抽奖时间: %s\n", rewardName.String, drawTime.String)
	}

	// 查询抽奖凭证
	fmt.Println("\n=== 抽奖凭证详情 ===")
	receiptRows, err := db.Query(`
		SELECT 
			dr.id,
			dr.draw_log_id,
			dr.algo_version,
			dr.server_seed_hash,
			dr.server_sub_seed,
			dr.rand_salt,
			dr.weights_snapshot,
			dr.rand_value,
			dr.result_reward_id
		FROM activity_draw_receipts dr
		JOIN activity_draw_logs dl ON dl.id = dr.draw_log_id
		JOIN orders o ON o.id = dl.order_id
		WHERE o.order_no = ?
	`, orderNo)
	if err != nil {
		log.Fatal("查询凭证失败:", err)
	}
	defer receiptRows.Close()

	for receiptRows.Next() {
		var id, drawLogID, resultRewardID sql.NullInt64
		var algoVersion, seedHash, subSeed, salt, weights sql.NullString
		var randValue sql.NullInt64
		err := receiptRows.Scan(&id, &drawLogID, &algoVersion, &seedHash, &subSeed, &salt, &weights, &randValue, &resultRewardID)
		if err != nil {
			log.Fatal("扫描凭证失败:", err)
		}
		fmt.Printf("凭证ID: %d, 抽奖记录ID: %d\n", id.Int64, drawLogID.Int64)
		fmt.Printf("算法版本: %s\n", algoVersion.String)
		fmt.Printf("种子哈希: %s\n", seedHash.String)
		fmt.Printf("子种子: %s\n", subSeed.String)
		fmt.Printf("Salt: %s\n", salt.String)
		fmt.Printf("随机值: %d\n", randValue.Int64)
		fmt.Printf("结果奖品ID: %d\n", resultRewardID.Int64)
		fmt.Printf("权重快照: %s\n", weights.String)
	}
}
