#!/bin/bash

# 充值审批余额更新验证脚本

echo "================================"
echo "充值审批余额更新验证"
echo "================================"
echo ""

# 配置
BASE_URL="http://localhost:5010"
MYSQL_HOST="8.155.172.147"
MYSQL_USER="monisuo"
MYSQL_PASS="JPJ8wYicSGC8aRnk"
MYSQL_DB="monisuo"

# 1. 检查服务是否运行
echo "步骤1: 检查服务状态..."
if pgrep -f "monisuo-1.0.jar" > /dev/null; then
    echo "✅ 服务正在运行"
    echo "   PID: $(pgrep -f 'monisuo-1.0.jar')"
else
    echo "❌ 服务未运行！"
    echo ""
    echo "启动服务："
    echo "cd ~/Desktop/projects/monisuo"
    echo "java -jar target/monisuo-1.0.jar --server.port=5010"
    exit 1
fi
echo ""

# 2. 检查代码版本
echo "步骤2: 检查代码版本..."
cd ~/Desktop/projects/monisuo
LATEST_COMMIT=$(git log --oneline -1)
echo "最新提交: $LATEST_COMMIT"

if echo "$LATEST_COMMIT" | grep -q "06f546f"; then
    echo "✅ 代码已是最新版本（包含余额更新修复）"
else
    echo "⚠️  代码可能不是最新版本"
    echo "   建议执行: git pull origin main"
fi
echo ""

# 3. 检查日志文件
echo "步骤3: 检查最近审批日志..."
if [ -f "logs/spring.log" ]; then
    echo "最近审批日志（最后20行）:"
    echo "---"
    grep -A20 "充值审批成功\|账户更新结果" logs/spring.log | tail -20
    echo "---"
    echo ""
    
    # 检查是否有更新失败的日志
    if grep -q "账户更新结果: 0" logs/spring.log; then
        echo "❌ 发现账户更新失败的日志！"
        echo "   查看详细日志: grep -A10 '账户更新结果: 0' logs/spring.log"
    fi
    
    # 检查是否有验证失败的日志
    if grep -q "余额更新失败\|验证失败" logs/spring.log; then
        echo "❌ 发现余额验证失败的日志！"
        echo "   查看详细日志: grep -A10 '余额更新失败' logs/spring.log"
    fi
else
    echo "⚠️  日志文件不存在: logs/spring.log"
fi
echo ""

# 4. 数据库验证
echo "步骤4: 数据库验证..."
echo "查询最近的充值订单..."
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB << EOF
-- 查询最近5笔已完成的充值订单
SELECT 
    id,
    order_no,
    user_id,
    amount,
    status,
    approve_time,
    update_time
FROM order_fund
WHERE type = 1 AND status = 3
ORDER BY approve_time DESC
LIMIT 5;
EOF
echo ""

# 5. 提示用户输入订单号进行验证
echo "================================"
echo "详细验证"
echo "================================"
read -p "请输入订单号进行验证（直接回车跳过）: " ORDER_NO

if [ -n "$ORDER_NO" ]; then
    echo ""
    echo "查询订单信息..."
    mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB << EOF
SELECT 
    order_no,
    user_id,
    amount,
    status,
    approve_admin_id,
    approve_time
FROM order_fund
WHERE order_no = '$ORDER_NO';
EOF
    echo ""

    # 获取用户ID
    USER_ID=$(mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -se "SELECT user_id FROM order_fund WHERE order_no='$ORDER_NO'")
    
    if [ -n "$USER_ID" ]; then
        echo "查询用户资金账户（user_id=$USER_ID）..."
        mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB << EOF
SELECT 
    id,
    user_id,
    balance,
    total_deposit,
    update_time
FROM account_fund
WHERE user_id = $USER_ID;
EOF
        echo ""

        echo "查询资金流水记录..."
        mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB << EOF
SELECT 
    flow_no,
    flow_type,
    amount,
    balance_before,
    balance_after,
    related_order_no,
    create_time
FROM account_flow
WHERE related_order_no = '$ORDER_NO'
ORDER BY create_time DESC;
EOF
        echo ""

        # 计算预期余额
        AMOUNT=$(mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -se "SELECT amount FROM order_fund WHERE order_no='$ORDER_NO'")
        CURRENT_BALANCE=$(mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -se "SELECT balance FROM account_fund WHERE user_id=$USER_ID")

        echo "================================"
        echo "余额验证"
        echo "================================"
        echo "充值金额: $AMOUNT"
        echo "当前余额: $CURRENT_BALANCE"
        echo ""

        # 获取充值前余额（从流水记录）
        BALANCE_BEFORE=$(mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -se "SELECT balance_before FROM account_flow WHERE related_order_no='$ORDER_NO' ORDER BY create_time DESC LIMIT 1")
        
        if [ -n "$BALANCE_BEFORE" ]; then
            EXPECTED_BALANCE=$(echo "$BALANCE_BEFORE + $AMOUNT" | bc)
            echo "充值前余额: $BALANCE_BEFORE"
            echo "预期余额: $EXPECTED_BALANCE"
            echo ""

            if [ "$CURRENT_BALANCE" == "$EXPECTED_BALANCE" ]; then
                echo "✅ 余额正确！"
            else
                echo "❌ 余额不匹配！"
                echo "   预期: $EXPECTED_BALANCE"
                echo "   实际: $CURRENT_BALANCE"
                echo ""
                echo "可能的原因:"
                echo "1. 事务回滚了"
                echo "2. 更新被其他操作覆盖"
                echo "3. 代码未正确执行"
            fi
        else
            echo "⚠️  未找到流水记录"
        fi
    fi
fi

echo ""
echo "================================"
echo "诊断完成"
echo "================================"
