# 结伴客项目部署脚本 (PowerShell 版本)
# 用于将项目部署到远程 CentOS 服务器

param(
    [Parameter(Mandatory=$false)]
    [string]$Target = ""
)

# 配置信息
$REMOTE_HOST = "1.13.156.49"
$REMOTE_USER = "root"
$REMOTE_PASSWORD = "Aiotjkl$741515"
$BACKEND_REMOTE_PATH = "/data/nodejs/jiebanke"
$ADMIN_SYSTEM_REMOTE_PATH = "/data/vue/jiebanke"
$WEBSITE_REMOTE_PATH = "/data/website/jiebanke"

# 颜色输出函数
function Write-ColorOutput($ForegroundColor) {
    # Save the current color
    $fc = $host.UI.RawUI.ForegroundColor
    $host.UI.RawUI.ForegroundColor = $ForegroundColor
    # Output the content
    if ($args) {
        Write-Output $args
    }
    else {
        $input | Write-Output
    }
    # Restore the original color
    $host.UI.RawUI.ForegroundColor = $fc
}

Write-ColorOutput Green "========== 结伴客项目部署脚本 =========="

# 检查是否提供了部署目标参数
if ($Target -eq "") {
    Write-ColorOutput Yellow "使用方法:"
    Write-Output "  .\deploy.ps1 all        - 部署所有模块"
    Write-Output "  .\deploy.ps1 backend    - 部署后端服务"
    Write-Output "  .\deploy.ps1 admin      - 部署后台管理系统"
    Write-Output "  .\deploy.ps1 website    - 部署官方网站"
    exit 1
}

# 部署后端服务
function Deploy-Backend {
    Write-ColorOutput Green "正在部署后端服务到 $REMOTE_HOST:$BACKEND_REMOTE_PATH ..."
    
    # 同步文件到远程服务器
    Write-ColorOutput Green "正在同步文件到远程服务器..."
    $rsyncArgs = "-avz --delete --exclude node_modules --exclude .git --exclude logs ../backend/ $REMOTE_USER@$REMOTE_HOST`:$BACKEND_REMOTE_PATH/"
    Start-Process -FilePath "rsync" -ArgumentList $rsyncArgs -NoNewWindow -Wait
    
    Write-ColorOutput Green "后端服务部署完成"
}

# 部署后台管理系统
function Deploy-AdminSystem {
    Write-ColorOutput Green "正在部署后台管理系统到 $REMOTE_HOST:$ADMIN_SYSTEM_REMOTE_PATH ..."
    
    # 同步文件到远程服务器
    Write-ColorOutput Green "正在同步文件到远程服务器..."
    $rsyncArgs = "-avz --delete --exclude node_modules --exclude .git --exclude dist ../admin-system/ $REMOTE_USER@$REMOTE_HOST`:$ADMIN_SYSTEM_REMOTE_PATH/"
    Start-Process -FilePath "rsync" -ArgumentList $rsyncArgs -NoNewWindow -Wait
    
    Write-ColorOutput Green "后台管理系统部署完成"
}

# 部署官方网站
function Deploy-Website {
    Write-ColorOutput Green "正在部署官方网站到 $REMOTE_HOST:$WEBSITE_REMOTE_PATH ..."
    
    # 同步文件到远程服务器
    Write-ColorOutput Green "正在同步文件到远程服务器..."
    $rsyncArgs = "-avz --delete --exclude node_modules --exclude .git ../website/ $REMOTE_USER@$REMOTE_HOST`:$WEBSITE_REMOTE_PATH/"
    Start-Process -FilePath "rsync" -ArgumentList $rsyncArgs -NoNewWindow -Wait
    
    Write-ColorOutput Green "官方网站部署完成"
}

# 部署所有模块
function Deploy-All {
    Write-ColorOutput Green "开始部署所有模块..."
    Deploy-Backend
    Deploy-AdminSystem
    Deploy-Website
    Write-ColorOutput Green "所有模块部署完成"
}

# 根据参数执行相应操作
switch ($Target) {
    "backend" {
        Deploy-Backend
    }
    "admin" {
        Deploy-AdminSystem
    }
    "website" {
        Deploy-Website
    }
    "all" {
        Deploy-All
    }
    default {
        Write-ColorOutput Red "未知参数: $Target"
        Write-ColorOutput Yellow "请使用: all, backend, admin, website"
        exit 1
    }
}

Write-ColorOutput Green "========== 部署完成 =========="