qnc-server-tob/deploy/script/gen_models.ps1

140 lines
4.5 KiB
PowerShell
Raw Normal View History

2025-04-11 13:10:17 +08:00
# 设置输出编码为UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
2025-05-24 14:26:20 +08:00
$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
# 启用详细输出和错误处理
$ErrorActionPreference = "Stop"
$VerbosePreference = "Continue"
# 检查并创建必要的目录
if (-not (Test-Path "./model")) {
Write-Output "Creating model directory..."
New-Item -ItemType Directory -Path "./model" | Out-Null
}
if (-not (Test-Path "../../app/user/model")) {
Write-Output "Creating target directory..."
New-Item -ItemType Directory -Path "../../app/user/model" -Force | Out-Null
}
# 将表名转换为驼峰命名法的函数
function ConvertToCamelCase {
param (
[string]$tableName
)
try {
# 将表名按_分割并将每个部分首字母大写
$parts = $tableName -split '_'
$camelCase = ""
foreach ($part in $parts) {
if ($part.Length -gt 0) {
$camelCase += $part.Substring(0, 1).ToUpper() + $part.Substring(1).ToLower()
}
}
return $camelCase
}
catch {
Write-Output "Error in ConvertToCamelCase for table: $tableName"
Write-Output $_.Exception.Message
return $tableName # 出错时返回原表名
}
}
# 数据库连接信息
$DB_URL = "qnc:5vg67b3UNHu8@tcp(127.0.0.1:21001)/qnc"
2025-04-11 13:10:17 +08:00
$OUTPUT_DIR = "./model"
$TEMPLATE_DIR = "../template"
2025-05-24 14:26:20 +08:00
$TARGET_DIR = "../../app/user/model"
2025-04-11 13:10:17 +08:00
2025-05-24 14:26:20 +08:00
# 表名列表 - 每个元素后必须有逗号分隔
2025-04-11 13:10:17 +08:00
$tables = @(
2025-05-24 14:26:20 +08:00
"agent",
2025-04-11 13:10:17 +08:00
# "agent_active_stat",
2025-05-24 14:26:20 +08:00
"agent_audit",
"agent_real_name"
2025-04-11 13:10:17 +08:00
# "agent_closure",
# "agent_commission",
# "agent_commission_deduction",
# "agent_link",
# "agent_membership_config",
2025-05-24 14:26:20 +08:00
# "agent_membership_recharge_order",
2025-04-11 13:10:17 +08:00
# "agent_membership_user_config",
# "agent_order",
# "agent_platform_deduction",
# "agent_product_config",
# "agent_rewards",
# "agent_wallet",
# "agent_withdrawal",
# "feature",
# "global_notifications",
# "order",
# "product",
# "product_feature",
# "query",
2025-05-24 14:26:20 +08:00
# "user",
# "user_auth",
# "example",
# "authorization",
# "authorization_face"
2025-04-11 13:10:17 +08:00
)
# 为每个表生成模型
foreach ($table in $tables) {
2025-05-24 14:26:20 +08:00
try {
Write-Output "========================================="
Write-Output "Processing table: $table"
# 生成模型
Write-Output "Generating model..."
$command = "goctl model mysql datasource -url=`"$DB_URL`" -table=`"$table`" -dir=`"$OUTPUT_DIR`" --home=`"$TEMPLATE_DIR`" -cache=true --style=goZero"
Write-Output "Running command: $command"
Invoke-Expression $command
# 将表名转换为驼峰命名法
$camelCaseName = ConvertToCamelCase -tableName $table
Write-Output "Table name converted to: $camelCaseName"
# 定义源文件和目标文件路径
$sourceModelFile = "$OUTPUT_DIR/${camelCaseName}Model.go"
$sourceModelGenFile = "$OUTPUT_DIR/${camelCaseName}Model_gen.go"
$targetModelFile = "$TARGET_DIR/${camelCaseName}Model.go"
$targetModelGenFile = "$TARGET_DIR/${camelCaseName}Model_gen.go"
Write-Output "Source files:"
Write-Output " - $sourceModelFile"
Write-Output " - $sourceModelGenFile"
Write-Output "Target files:"
Write-Output " - $targetModelFile"
Write-Output " - $targetModelGenFile"
# 检查源文件是否存在
if (-not (Test-Path $sourceModelFile)) {
Write-Output "WARNING: Source file not found: $sourceModelFile"
}
if (-not (Test-Path $sourceModelGenFile)) {
Write-Output "WARNING: Source file not found: $sourceModelGenFile"
}
# 移动文件
if (Test-Path $sourceModelFile) {
Write-Output "Moving $sourceModelFile to $targetModelFile"
Move-Item -Path $sourceModelFile -Destination $targetModelFile -Force
}
if (Test-Path $sourceModelGenFile) {
Write-Output "Moving $sourceModelGenFile to $targetModelGenFile"
Move-Item -Path $sourceModelGenFile -Destination $targetModelGenFile -Force
}
Write-Output "Processing completed for table: $table"
}
catch {
Write-Output "ERROR processing table: $table"
Write-Output $_.Exception.Message
Write-Output $_.ScriptStackTrace
}
2025-04-11 13:10:17 +08:00
}
2025-05-24 14:26:20 +08:00
Write-Output "========================================="
Write-Output "Script execution completed."