#!/bin/bash
# ERP微服务批量启动脚本

BASE_DIR="/var/www/myapp/erp-java/services"
LOG_DIR="/var/www/myapp/erp-java/logs"
NACOS_ADDR="localhost:8848"
MYSQL_HOST="localhost"
MYSQL_PORT="3307"
REDIS_HOST="localhost"
REDIS_PORT="6379"

mkdir -p $LOG_DIR

# 需要启动的服务列表（有JAR包的）
SERVICES=(
    "user-service"
    "tenant-service"
    "permission-service"
    "product-service"
    "category-service"
    "order-service"
    "inventory-service"
    "finance-service"
    "invoice-service"
    "logistics-service"
    "waybill-service"
    "customer-service"
    "supplier-service"
    "purchase-service"
    "notification-service"
    "ai-service"
    "dashboard-service"
    "report-service"
    "reconciliation-service"
    "approval-flow-service"
    "audit-log-service"
    "platform-sync-service"
    "print-service"
    "scheduled-task-service"
    "data-import-export-service"
    "sku-match-service"
    "system-tool-service"
)

start_service() {
    local svc=$1
    local dir="$BASE_DIR/$svc"
    local jar=$(ls $dir/target/*.jar 2>/dev/null | head -1)
    
    if [ -f "$jar" ]; then
        echo "启动 $svc..."
        nohup java -jar "$jar" \
            --spring.cloud.nacos.discovery.server-addr=$NACOS_ADDR \
            --spring.cloud.nacos.config.server-addr=$NACOS_ADDR \
            --spring.datasource.url="jdbc:mysql://$MYSQL_HOST:$MYSQL_PORT/erp_java?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai" \
            --spring.datasource.username=root \
            --spring.datasource.password=root123456 \
            --spring.redis.host=$REDIS_HOST \
            --spring.redis.port=$REDIS_PORT \
            > $LOG_DIR/$svc.log 2>&1 &
        echo "$svc started (PID: $!)"
    else
        echo "$svc: JAR not found, skipping"
    fi
}

echo "=========================================="
echo "ERP微服务批量启动"
echo "=========================================="

for svc in "${SERVICES[@]}"; do
    start_service "$svc"
done

echo ""
echo "=========================================="
echo "启动完成！检查日志: tail -f $LOG_DIR/*.log"
echo "=========================================="
