#!/bin/bash

# CRM系统启动脚本

# 加载环境变量 (如果存在.env文件)
if [ -f .env ]; then
    export $(cat .env | grep -v '^#' | xargs)
fi

# 默认配置
export DB_HOST=${DB_HOST:-localhost}
export DB_PORT=${DB_PORT:-3306}
export DB_USER=${DB_USER:-root}
export DB_PASSWORD=${DB_PASSWORD:-}
export DB_NAME=${DB_NAME:-crm_db}
export STORAGE_MODE=${STORAGE_MODE:-mysql}
export PORT=${PORT:-8081}

echo "====================================="
echo "CRM 系统启动配置"
echo "====================================="
echo "存储模式: $STORAGE_MODE"
if [ "$STORAGE_MODE" = "mysql" ]; then
    echo "数据库主机: $DB_HOST:$DB_PORT"
    echo "数据库名称: $DB_NAME"
    echo "数据库用户: $DB_USER"
fi
echo "服务端口: $PORT"
echo "====================================="

# 构建项目
echo "正在构建项目..."
go build -o crm-server ./cmd/server/

if [ $? -eq 0 ]; then
    echo "构建成功，启动服务..."
    ./crm-server
else
    echo "构建失败！"
    exit 1
fi
