#!/bin/bash

# 创建新订单并测试完整审批流程

BASE_URL="http://localhost:5010"
USER_TOKEN=""
ADMIN_TOKEN=""

echo "=========================================="
echo "创建新订单并测试审批流程"
echo "=========================================="
echo ""

# 1. 用户登录
echo "[步骤1] 用户登录"
USER_LOGIN=$(curl -s -X POST "$BASE_URL/api/user/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "abcd",
    "password": "abcd1234"
  }')

echo "用户登录响应:"
echo "$USER_LOGIN" | jq '.'

USER_TOKEN=$(echo "$USER_LOGIN" | jq -r '.data.token // empty')

if [ -z "$USER_TOKEN" ]; then
  echo "✗ 用户登录失败"
  exit 1
fi

echo "✓ 用户登录成功"
echo ""

# 2. 创建充值订单
echo "[步骤2] 创建充值订单"
DEPOSIT_RESPONSE=$(curl -s -X POST "$BASE_URL/api/fund/deposit" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": 100,
    "remark": "测试充值"
  }')

echo "充值订单响应:"
echo "$DEPOSIT_RESPONSE" | jq '.'

ORDER_NO=$(echo "$DEPOSIT_RESPONSE" | jq -r '.data.orderNo // empty')

if [ -z "$ORDER_NO" ]; then
  echo "✗ 创建充值订单失败"
  exit 1
fi

echo "✓ 充值订单创建成功: $ORDER_NO"
echo ""

# 3. 用户确认打款
echo "[步骤3] 用户确认打款"
CONFIRM_RESPONSE=$(curl -s -X POST "$BASE_URL/api/fund/confirmPay" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"orderNo\": \"$ORDER_NO\"
  }")

echo "确认打款响应:"
echo "$CONFIRM_RESPONSE" | jq '.'

echo "✓ 已确认打款"
echo ""

# 4. 管理员登录
echo "[步骤4] 管理员登录"
ADMIN_LOGIN=$(curl -s -X POST "$BASE_URL/admin/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }')

ADMIN_TOKEN=$(echo "$ADMIN_LOGIN" | jq -r '.data.token // empty')

if [ -z "$ADMIN_TOKEN" ]; then
  echo "✗ 管理员登录失败"
  exit 1
fi

echo "✓ 管理员登录成功"
echo ""

# 5. 查询用户当前余额
echo "[步骤5] 查询用户当前余额（审批前）"
USER_DETAIL_BEFORE=$(curl -s -X GET "$BASE_URL/admin/user/detail?userId=5" \
  -H "Authorization: Bearer $ADMIN_TOKEN")

BEFORE_BALANCE=$(echo "$USER_DETAIL_BEFORE" | jq -r '.data.fund.balance')
BEFORE_TOTAL_DEPOSIT=$(echo "$USER_DETAIL_BEFORE" | jq -r '.data.fund.totalDeposit')

echo "审批前余额: $BEFORE_BALANCE USDT"
echo "审批前累计充值: $BEFORE_TOTAL_DEPOSIT USDT"
echo ""

# 6. 管理员审批订单
echo "[步骤6] 管理员审批订单"
echo "订单号: $ORDER_NO"
echo ""

APPROVE_RESPONSE=$(curl -s -X POST "$BASE_URL/admin/order/approve" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"orderNo\": \"$ORDER_NO\",
    \"status\": 2,
    \"adminRemark\": \"测试审批 - $(date '+%Y-%m-%d %H:%M:%S')\"
  }")

echo "审批响应:"
echo "$APPROVE_RESPONSE" | jq '.'

APPROVE_SUCCESS=$(echo "$APPROVE_RESPONSE" | jq -r '.success')

if [ "$APPROVE_SUCCESS" = "true" ]; then
  echo "✓ 审批请求成功"
else
  echo "✗ 审批请求失败"
  exit 1
fi
echo ""

# 等待数据库更新
echo "等待 2 秒..."
sleep 2
echo ""

# 7. 查询订单新状态
echo "[步骤7] 查询订单新状态"
ORDER_DETAIL=$(curl -s -X GET "$BASE_URL/admin/order/list?pageNum=1&pageSize=1" \
  -H "Authorization: Bearer $ADMIN_TOKEN")

UPDATED_ORDER=$(echo "$ORDER_DETAIL" | jq -r --arg orderNo "$ORDER_NO" '.data.list[] | select(.orderNo == $orderNo)')
NEW_STATUS=$(echo "$UPDATED_ORDER" | jq -r '.status')

echo "订单号: $ORDER_NO"
echo "新状态: $NEW_STATUS"

if [ "$NEW_STATUS" -eq 3 ]; then
  echo "✓ 订单状态正确 (3 = 已完成)"
else
  echo "✗ 订单状态错误，期望 3，实际 $NEW_STATUS"
fi
echo ""

# 8. 查询用户新余额
echo "[步骤8] 查询用户新余额（审批后）"
USER_DETAIL_AFTER=$(curl -s -X GET "$BASE_URL/admin/user/detail?userId=5" \
  -H "Authorization: Bearer $ADMIN_TOKEN")

AFTER_BALANCE=$(echo "$USER_DETAIL_AFTER" | jq -r '.data.fund.balance')
AFTER_TOTAL_DEPOSIT=$(echo "$USER_DETAIL_AFTER" | jq -r '.data.fund.totalDeposit')

echo "审批后余额: $AFTER_BALANCE USDT (变化: $(echo "$AFTER_BALANCE - $BEFORE_BALANCE" | bc) USDT)"
echo "审批后累计充值: $AFTER_TOTAL_DEPOSIT USDT (变化: $(echo "$AFTER_TOTAL_DEPOSIT - $BEFORE_TOTAL_DEPOSIT" | bc) USDT)"
echo ""

# 9. 验证结果
echo "[步骤9] 验证审批结果"

BALANCE_DIFF=$(echo "$AFTER_BALANCE - $BEFORE_BALANCE" | bc)
DEPOSIT_DIFF=$(echo "$AFTER_TOTAL_DEPOSIT - $BEFORE_TOTAL_DEPOSIT" | bc)

if [ "$(echo "$BALANCE_DIFF == 100" | bc)" -eq 1 ]; then
  echo "✓ 余额增加正确: +100 USDT"
else
  echo "✗ 余额增加错误: 期望 +100，实际 +$BALANCE_DIFF"
fi

if [ "$(echo "$DEPOSIT_DIFF == 100" | bc)" -eq 1 ]; then
  echo "✓ 累计充值增加正确: +100 USDT"
else
  echo "✗ 累计充值增加错误: 期望 +100，实际 +$DEPOSIT_DIFF"
fi

if [ "$NEW_STATUS" -eq 3 ]; then
  echo "✓ 订单状态更新正确"
else
  echo "✗ 订单状态未更新"
fi

echo ""
echo "=========================================="
echo "测试完成"
echo "=========================================="
