package main

import (
	"fmt"
	"log"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func main() {
	// 数据库连接字符串
	dsn := "host=cxd.whcxd.cn port=16159 user=erp_pgsql password=erp_2025 dbname=junhong_cmp_test sslmode=disable"

	// 连接数据库
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalf("连接数据库失败: %v", err)
	}

	// 查询所有以 personal_customer 开头的表
	var tables []string
	result := db.Raw(`
		SELECT table_name
		FROM information_schema.tables
		WHERE table_schema = 'public'
		AND table_name LIKE 'personal_customer%'
		ORDER BY table_name
	`).Scan(&tables)

	if result.Error != nil {
		log.Fatalf("查询表失败: %v", result.Error)
	}

	fmt.Println("✅ 个人客户相关表列表:")
	for _, table := range tables {
		fmt.Printf("  - %s\n", table)
	}

	// 验证每个表的字段
	checkTableColumns(db, "personal_customer_phone")
	checkTableColumns(db, "personal_customer_iccid")
	checkTableColumns(db, "personal_customer_device")

	fmt.Println("\n✅ 数据库迁移验证成功！")
}

func checkTableColumns(db *gorm.DB, tableName string) {
	var columns []struct {
		ColumnName string
		DataType   string
	}

	result := db.Raw(`
		SELECT column_name, data_type
		FROM information_schema.columns
		WHERE table_name = ?
		ORDER BY ordinal_position
	`, tableName).Scan(&columns)

	if result.Error != nil {
		log.Printf("⚠️  查询表 %s 的字段失败: %v", tableName, result.Error)
		return
	}

	fmt.Printf("\n表 %s 的字段:\n", tableName)
	for _, col := range columns {
		fmt.Printf("  - %-20s %s\n", col.ColumnName, col.DataType)
	}
}
