package main

import (
	"bindbox-game/configs"
	"bindbox-game/internal/repository/mysql"
	"bindbox-game/internal/repository/mysql/model"
	"fmt"
	"log"
)

func main() {
	configs.Init()

	repo, err := mysql.New()
	if err != nil {
		log.Fatalf("mysql init failed: %v", err)
	}

	db := repo.GetDbW()

	// Use raw SQL to add column if not exists
	tableName := model.TableNameDouyinOrders
	columnName := "product_count"

	// Check if column exists
	var count int64
	checkSQL := fmt.Sprintf("SELECT count(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '%s' AND COLUMN_NAME = '%s'", tableName, columnName)
	if err := db.Raw(checkSQL).Scan(&count).Error; err != nil {
		log.Fatalf("check column failed: %v", err)
	}

	if count == 0 {
		log.Printf("Adding column %s to table %s...", columnName, tableName)
		// Add column
		alterSQL := fmt.Sprintf("ALTER TABLE `%s` ADD COLUMN `%s` INT NOT NULL DEFAULT 1 COMMENT '商品数量';", tableName, columnName)
		if err := db.Exec(alterSQL).Error; err != nil {
			log.Fatalf("add column failed: %v", err)
		}
		log.Println("Column added successfully.")
	} else {
		log.Println("Column already exists.")
	}
}
