# 设置输出编码为UTF-8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $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" $OUTPUT_DIR = "./model" $TEMPLATE_DIR = "../template" $TARGET_DIR = "../../app/user/model" # 表名列表 - 每个元素后必须有逗号分隔 $tables = @( # "agent", # "agent_active_stat", # "agent_audit", # "agent_real_name" # "agent_closure", # "agent_commission", # "agent_commission_deduction", # "agent_link", # "agent_membership_config", # "agent_membership_recharge_order", # "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", # "user", # "user_auth", # "example", "authorization" # "authorization_face" ) # 为每个表生成模型 foreach ($table in $tables) { 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 } } Write-Output "=========================================" Write-Output "Script execution completed."