#!/bin/bash
# 数据库诊断脚本 - 检查订单审批状态

MYSQL_HOST="8.155.172.147"
MYSQL_USER="monisuo"
MYSQL_PASS="JPJ8wYicSGC8aRnk"
MYSQL_DB="monisuo"

echo "=========================================="
echo "订单审批数据库诊断"
echo "=========================================="

echo ""
echo "1. 检查 order_fund 表结构:"
echo "------------------------------------------"
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -e "DESCRIBE order_fund;" 2>/dev/null

echo ""
echo "2. 查询所有待审批订单:"
echo "------------------------------------------"
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -e "
SELECT id, order_no, type,
       CASE type WHEN 1 THEN '充值' WHEN 2 THEN '提现' END as type_name,
       status,
       CASE
           WHEN type = 1 THEN
               CASE status
                   WHEN 1 THEN '待付款'
                   WHEN 2 THEN '待确认'
                   WHEN 3 THEN '已完成'
                   WHEN 4 THEN '已驳回'
                   WHEN 5 THEN '已取消'
               END
           WHEN type = 2 THEN
               CASE status
                   WHEN 1 THEN '待审批'
                   WHEN 2 THEN '已完成'
                   WHEN 3 THEN '已驳回'
                   WHEN 4 THEN '已取消'
               END
       END as status_name,
       amount, approve_admin_id, approve_time, update_time
FROM order_fund
ORDER BY create_time DESC
LIMIT 10;
" 2>/dev/null

echo ""
echo "3. 查询最近更新的订单:"
echo "------------------------------------------"
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -e "
SELECT id, order_no, status, approve_admin_id, approve_admin_name, approve_time, update_time
FROM order_fund
WHERE update_time IS NOT NULL
ORDER BY update_time DESC
LIMIT 5;
" 2>/dev/null

echo ""
echo "4. 检查 account_fund 表:"
echo "------------------------------------------"
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB -e "
SELECT id, user_id, balance, frozen, total_deposit, total_withdraw, update_time
FROM account_fund
ORDER BY update_time DESC
LIMIT 5;
" 2>/dev/null

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