#!/bin/bash

# 数据库验证脚本 - 直接查询数据库验证审批结果

DB_HOST="8.155.172.147"
DB_PORT="3306"
DB_NAME="monisuo"
DB_USER="monisuo"
DB_PASS="JPJ8wYicSGC8aRnk"

echo "=========================================="
echo "数据库验证脚本"
echo "=========================================="
echo ""
echo "⚠️  注意: 此脚本需要 mysql 命令行工具"
echo ""

# 检查 mysql 命令
if ! command -v mysql &> /dev/null; then
    echo "❌ 未找到 mysql 命令"
    echo ""
    echo "请手动执行以下 SQL 查询:"
    echo ""
    echo "-- 1. 查询待审批订单"
    echo "SELECT id, order_no, user_id, type, status, amount, username"
    echo "FROM order_fund"
    echo "WHERE (type=1 AND status=2) OR (type=2 AND status=1)"
    echo "ORDER BY create_time DESC LIMIT 5;"
    echo ""
    echo "-- 2. 查询指定订单"
    echo "SELECT * FROM order_fund WHERE order_no = '你的订单号';"
    echo ""
    echo "-- 3. 查询用户资金账户"
    echo "SELECT * FROM account_fund WHERE user_id = 用户ID;"
    echo ""
    echo "-- 4. 查询用户资金流水"
    echo "SELECT * FROM account_flow WHERE user_id = 用户ID ORDER BY create_time DESC LIMIT 10;"
    echo ""
    exit 1
fi

echo "连接信息:"
echo "  主机: ${DB_HOST}:${DB_PORT}"
echo "  数据库: ${DB_NAME}"
echo "  用户: ${DB_USER}"
echo ""

# 1. 查询待审批订单
echo "=========================================="
echo "1. 查询待审批订单"
echo "=========================================="
mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} ${DB_NAME} -e "
SELECT 
    id AS 'ID',
    order_no AS '订单号',
    user_id AS '用户ID',
    username AS '用户名',
    CASE type
        WHEN 1 THEN '充值'
        WHEN 2 THEN '提现'
    END AS '类型',
    CASE 
        WHEN type=1 AND status=1 THEN '待付款'
        WHEN type=1 AND status=2 THEN '待确认'
        WHEN type=1 AND status=3 THEN '已完成'
        WHEN type=1 AND status=4 THEN '已驳回'
        WHEN type=2 AND status=1 THEN '待审批'
        WHEN type=2 AND status=2 THEN '已完成'
        WHEN type=2 AND status=3 THEN '已驳回'
        ELSE CONCAT('状态', status)
    END AS '状态',
    amount AS '金额',
    create_time AS '创建时间'
FROM order_fund
WHERE (type=1 AND status=2) OR (type=2 AND status=1)
ORDER BY create_time DESC
LIMIT 10;
" 2>/dev/null

if [ $? -ne 0 ]; then
    echo "❌ 查询失败，请检查数据库连接"
    exit 1
fi

echo ""

# 2. 提示输入订单号
echo "=========================================="
echo "2. 查询指定订单详情"
echo "=========================================="
read -p "请输入订单号（直接回车跳过）: " ORDER_NO

if [ ! -z "$ORDER_NO" ]; then
    echo ""
    mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} ${DB_NAME} -e "
    SELECT 
        order_no AS '订单号',
        user_id AS '用户ID',
        username AS '用户名',
        type AS '类型',
        status AS '状态',
        amount AS '金额',
        wallet_address AS '地址',
        pay_time AS '确认打款时间',
        confirm_time AS '完成时间',
        approve_admin_name AS '审批人',
        approve_time AS '审批时间',
        reject_reason AS '驳回原因',
        admin_remark AS '管理员备注',
        create_time AS '创建时间'
    FROM order_fund
    WHERE order_no = '${ORDER_NO}';
    " 2>/dev/null
    
    # 获取用户ID
    USER_ID=$(mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} ${DB_NAME} -N -e "
    SELECT user_id FROM order_fund WHERE order_no = '${ORDER_NO}';
    " 2>/dev/null)
    
    if [ ! -z "$USER_ID" ]; then
        echo ""
        echo "=========================================="
        echo "3. 查询用户资金账户"
        echo "=========================================="
        mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} ${DB_NAME} -e "
        SELECT 
            user_id AS '用户ID',
            balance AS '余额',
            frozen AS '冻结',
            total_deposit AS '累计充值',
            total_withdraw AS '累计提现',
            create_time AS '创建时间',
            update_time AS '更新时间'
        FROM account_fund
        WHERE user_id = ${USER_ID};
        " 2>/dev/null
        
        echo ""
        echo "=========================================="
        echo "4. 查询用户资金流水（最近10条）"
        echo "=========================================="
        mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} ${DB_NAME} -e "
        SELECT 
            flow_no AS '流水号',
            CASE flow_type
                WHEN 1 THEN '充值'
                WHEN 2 THEN '提现'
                WHEN 3 THEN '划转至资金账户'
                WHEN 4 THEN '划转至交易账户'
                ELSE CONCAT('类型', flow_type)
            END AS '类型',
            amount AS '金额',
            balance_before AS '变动前余额',
            balance_after AS '变动后余额',
            related_order_no AS '关联订单',
            remark AS '备注',
            create_time AS '时间'
        FROM account_flow
        WHERE user_id = ${USER_ID}
        ORDER BY create_time DESC
        LIMIT 10;
        " 2>/dev/null
    fi
fi

echo ""
echo "=========================================="
echo "验证完成"
echo "=========================================="
