#!/bin/bash

BASE_URL="http://localhost:5010"

# 1. 登录
echo "================================"
echo "查询用户资金账户数据"
echo "================================"
echo ""

LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/admin/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}')

TOKEN=$(echo $LOGIN_RESPONSE | python3 -c "
import sys, json
data = json.load(sys.stdin)
if data.get('success'):
    print(data['data']['token'])
" 2>/dev/null)

if [ -z "$TOKEN" ]; then
    echo "❌ 登录失败"
    echo "$LOGIN_RESPONSE"
    exit 1
fi

echo "✅ 登录成功"
echo ""

# 2. 查询用户列表
echo "步骤1: 查询用户列表..."
USER_LIST=$(curl -s "$BASE_URL/admin/user/list?pageNum=1&pageSize=100" \
  -H "Authorization: Bearer $TOKEN")

echo "$USER_LIST" | python3 -m json.tool 2>/dev/null || echo "$USER_LIST"
echo ""

# 提取用户ID列表
USER_IDS=$(echo "$USER_LIST" | python3 -c "
import sys, json
data = json.load(sys.stdin)
if data.get('success') and data['data'].get('list'):
    for user in data['data']['list']:
        print(user['id'])
" 2>/dev/null)

if [ -z "$USER_IDS" ]; then
    echo "❌ 没有找到用户"
    exit 1
fi

# 3. 查询每个用户的资金账户
echo "步骤2: 查询每个用户的资金账户..."
echo ""

for USER_ID in $USER_IDS; do
    echo "================================"
    echo "用户ID: $USER_ID"
    echo "================================"
    
    USER_DETAIL=$(curl -s "$BASE_URL/admin/user/detail?userId=$USER_ID" \
      -H "Authorization: Bearer $TOKEN")
    
    echo "$USER_DETAIL" | python3 -m json.tool 2>/dev/null || echo "$USER_DETAIL"
    echo ""
done

echo "================================"
echo "查询完成"
echo "================================"
