#!/bin/bash
# 排查 account3 和 account4 没有下单的问题

echo "=========================================="
echo "排查 account3 和 account4 未下单问题"
echo "=========================================="
echo ""

# 检查进程状态
echo "【1】检查进程状态"
echo "----------------------------------------"
for acc in 3 4; do
    echo ""
    echo "Account $acc:"
    supervisorctl status auto_sys_acc$acc 2>/dev/null || echo "  ❌ supervisorctl 命令失败"
done
echo ""

# 检查日志文件
echo "【2】检查最近的日志（最后50行）"
echo "----------------------------------------"
PROJECT_ROOT="/www/wwwroot/autosys_new"
for acc in 3 4; do
    echo ""
    echo "Account $acc 日志:"
    LOG_FILE="$PROJECT_ROOT/logs/trading_${acc}.log"
    if [ -f "$LOG_FILE" ]; then
        echo "  📄 日志文件: $LOG_FILE"
        echo "  📊 最后50行:"
        tail -n 50 "$LOG_FILE" | grep -E "(ERROR|WARNING|INFO|扫描|交易|下单|开仓|信号|过滤|跳过|余额|配置)" | tail -n 20
        echo ""
        echo "  🔍 最近的错误:"
        tail -n 200 "$LOG_FILE" | grep -i "error" | tail -n 5
        echo ""
        echo "  ⚠️  最近的警告:"
        tail -n 200 "$LOG_FILE" | grep -i "warning" | tail -n 5
    else
        echo "  ❌ 日志文件不存在: $LOG_FILE"
    fi
done
echo ""

# 检查配置
echo "【3】检查关键配置项"
echo "----------------------------------------"
echo "检查 account3 和 account4 的配置..."
echo "（需要查看数据库或前端配置页面）"
echo ""

# 检查市场扫描
echo "【4】检查市场扫描活动"
echo "----------------------------------------"
for acc in 3 4; do
    echo ""
    echo "Account $acc 扫描活动:"
    LOG_FILE="$PROJECT_ROOT/logs/trading_${acc}.log"
    if [ -f "$LOG_FILE" ]; then
        echo "  最近一次扫描时间:"
        tail -n 500 "$LOG_FILE" | grep -E "(开始扫描|扫描完成|等待.*秒后进行下次扫描)" | tail -n 3
        echo ""
        echo "  扫描到的交易对数量:"
        tail -n 500 "$LOG_FILE" | grep -E "(扫描到.*个交易对|处理交易对)" | tail -n 5
        echo ""
        echo "  交易信号分析:"
        tail -n 500 "$LOG_FILE" | grep -E "(技术指标分析|交易信号|should_trade|跳过自动交易)" | tail -n 10
    fi
done
echo ""

# 检查风险控制
echo "【5】检查风险控制是否阻止交易"
echo "----------------------------------------"
for acc in 3 4; do
    echo ""
    echo "Account $acc 风险控制:"
    LOG_FILE="$PROJECT_ROOT/logs/trading_${acc}.log"
    if [ -f "$LOG_FILE" ]; then
        echo "  风险检查结果:"
        tail -n 500 "$LOG_FILE" | grep -E "(风险检查|余额不足|仓位限制|最大持仓|每日限额)" | tail -n 10
        echo ""
        echo "  账户余额:"
        tail -n 500 "$LOG_FILE" | grep -E "(账户余额|余额|balance)" | tail -n 5
    fi
done
echo ""

# 检查API连接
echo "【6】检查API连接状态"
echo "----------------------------------------"
for acc in 3 4; do
    echo ""
    echo "Account $acc API状态:"
    LOG_FILE="$PROJECT_ROOT/logs/trading_${acc}.log"
    if [ -f "$LOG_FILE" ]; then
        echo "  API连接:"
        tail -n 200 "$LOG_FILE" | grep -E "(币安客户端|API|连接|权限|密钥)" | tail -n 5
        echo ""
        echo "  最近的API错误:"
        tail -n 200 "$LOG_FILE" | grep -iE "(api.*error|api.*fail|连接失败|权限)" | tail -n 5
    fi
done
echo ""

# 检查持仓
echo "【7】检查当前持仓状态"
echo "----------------------------------------"
for acc in 3 4; do
    echo ""
    echo "Account $acc 持仓:"
    LOG_FILE="$PROJECT_ROOT/logs/trading_${acc}.log"
    if [ -f "$LOG_FILE" ]; then
        echo "  当前持仓数量:"
        tail -n 500 "$LOG_FILE" | grep -E "(当前持仓|持仓数量|active_positions)" | tail -n 5
        echo ""
        echo "  最大持仓限制:"
        tail -n 500 "$LOG_FILE" | grep -E "(MAX_OPEN_POSITIONS|最大持仓|持仓上限)" | tail -n 3
    fi
done
echo ""

# 检查时间
echo "【8】检查进程运行时间"
echo "----------------------------------------"
for acc in 3 4; do
    echo ""
    echo "Account $acc:"
    ps aux | grep -E "trading_system.*main.*acc$acc|auto_sys_acc$acc" | grep -v grep | head -n 1 | awk '{print "  PID: "$2", 运行时间: "$10", 启动时间: "$9}'
done
echo ""

echo "=========================================="
echo "排查完成"
echo "=========================================="
echo ""
echo "💡 建议下一步操作:"
echo "1. 如果进程未运行，检查 supervisor 配置和启动日志"
echo "2. 如果进程运行但无交易，检查:"
echo "   - 配置是否正确（特别是 MIN_SIGNAL_STRENGTH, MAX_OPEN_POSITIONS 等）"
echo "   - 市场扫描是否正常（查看'开始扫描'日志）"
echo "   - 是否有交易信号但被过滤（查看'跳过自动交易'日志）"
echo "   - 风险控制是否阻止（查看'风险检查'日志）"
echo "   - 账户余额是否充足"
echo "3. 查看完整日志: tail -f $PROJECT_ROOT/logs/trading_3.log"
echo "4. 检查前端配置页面，确认 account3 和 account4 的配置项"
