@echo off
REM ========================================
REM 信贷客户家庭关系列表查询功能测试脚本
REM ========================================

setlocal EnableDelayedExpansion

echo ========================================
echo 信贷客户家庭关系列表查询功能测试
echo ========================================
echo.

REM 设置后端服务地址
set BASE_URL=http://localhost:8080

REM 创建结果目录
if not exist "test-results" mkdir test-results

REM ========================================
REM 步骤1: 登录获取token
REM ========================================
echo [1/1] 正在登录...
curl -s -X POST "%BASE_URL%/login/test" ^
  -H "Content-Type: application/json" ^
  -d "{\"username\":\"admin\",\"password\":\"admin123\"}" ^
  > test-results\login_response.json

REM 提取token
powershell -Command "$json = Get-Content test-results\login_response.json -Raw | ConvertFrom-Json; $token = $json.token; Set-Content -Path test-results\token.txt -Value $token"

set /p TOKEN=<test-results\token.txt
echo Token: %TOKEN:~0,30%...
echo.

REM ========================================
REM 测试1: 基本列表查询
REM ========================================
echo ========================================
echo 测试1: 基本列表查询（无筛选条件）
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test01_basic_list.json

echo 响应内容:
type test-results\test01_basic_list.json
echo.
echo.

REM ========================================
REM 测试2: 分页功能测试
REM ========================================
echo ========================================
echo 测试2: 分页功能测试
echo ========================================

echo 第1页 (每页5条):
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=5" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test02_page1.json

type test-results\test02_page1.json
echo.

echo 第2页 (每页5条):
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=2&pageSize=5" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test02_page2.json

type test-results\test02_page2.json
echo.
echo.

REM ========================================
REM 测试3: 按身份证号筛选
REM ========================================
echo ========================================
echo 测试3: 按身份证号筛选
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10&personId=110101199001011234" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test03_filter_personId.json

echo 筛选条件: personId=110101199001011234
echo 响应内容:
type test-results\test03_filter_personId.json
echo.
echo.

REM ========================================
REM 测试4: 按关系类型筛选
REM ========================================
echo ========================================
echo 测试4: 按关系类型筛选
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10&relationType=01" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test04_filter_relationType.json

echo 筛选条件: relationType=01 (配偶)
echo 响应内容:
type test-results\test04_filter_relationType.json
echo.
echo.

REM ========================================
REM 测试5: 按姓名模糊查询
REM ========================================
echo ========================================
echo 测试5: 按姓名模糊查询
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10&relationName=张" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test05_filter_relationName.json

echo 筛选条件: relationName=张 (模糊查询)
echo 响应内容:
type test-results\test05_filter_relationName.json
echo.
echo.

REM ========================================
REM 测试6: 组合条件查询
REM ========================================
echo ========================================
echo 测试6: 组合条件查询
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10&personId=110101199001011234&relationType=01" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test06_combined_filter.json

echo 筛选条件: personId=110101199001011234 AND relationType=01
echo 响应内容:
type test-results\test06_combined_filter.json
echo.
echo.

REM ========================================
REM 测试7: 查询不存在的数据
REM ========================================
echo ========================================
echo 测试7: 查询不存在的数据
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10&personId=999999999999999999" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test07_no_data.json

echo 筛选条件: personId=999999999999999999 (不存在)
echo 响应内容:
type test-results\test07_no_data.json
echo.
echo.

REM ========================================
REM 测试8: 大页码查询
REM ========================================
echo ========================================
echo 测试8: 大页码查询
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=999&pageSize=10" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test08_large_pageNum.json

echo 筛选条件: pageNum=999 (超出范围)
echo 响应内容:
type test-results\test08_large_pageNum.json
echo.
echo.

REM ========================================
REM 测试9: 每页1条记录
REM ========================================
echo ========================================
echo 测试9: 最小分页大小
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=1" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test09_pageSize_1.json

echo 筛选条件: pageSize=1
echo 响应内容:
type test-results\test09_pageSize_1.json
echo.
echo.

REM ========================================
REM 测试10: 每页100条记录
REM ========================================
echo ========================================
echo 测试10: 大分页大小
echo ========================================
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=100" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\test10_pageSize_100.json

echo 筛选条件: pageSize=100
echo 响应内容:
type test-results\test10_pageSize_100.json | head -20
echo...
echo.
echo.

REM ========================================
REM 生成测试报告
REM ========================================
echo ========================================
echo 测试完成!
echo ========================================
echo.
echo 测试结果文件:
echo - test01_basic_list.json (基本列表查询)
echo - test02_page1.json (第1页)
echo - test02_page2.json (第2页)
echo - test03_filter_personId.json (按身份证号筛选)
echo - test04_filter_relationType.json (按关系类型筛选)
echo - test05_filter_relationName.json (按姓名模糊查询)
echo - test06_combined_filter.json (组合条件查询)
echo - test07_no_data.json (查询不存在的数据)
echo - test08_large_pageNum.json (大页码查询)
echo - test09_pageSize_1.json (最小分页)
echo - test10_pageSize_100.json (大分页)
echo.

REM 分析测试结果
echo ========================================
echo 测试结果分析:
echo ========================================

powershell -Command ^
  "$basic = Get-Content test-results\test01_basic_list.json -Raw | ConvertFrom-Json; "^
  "$filter1 = Get-Content test-results\test03_filter_personId.json -Raw | ConvertFrom-Json; "^
  "$noData = Get-Content test-results\test07_no_data.json -Raw | ConvertFrom-Json; "^
  "$largePage = Get-Content test-results\test08_large_pageNum.json -Raw | ConvertFrom-Json; "^
  "Write-Host '基本列表查询: ' -NoNewline; if ($basic.code -eq 200) { Write-Host '✓ 通过' -ForegroundColor Green } else { Write-Host '✗ 失败' -ForegroundColor Red }; "^
  "Write-Host '按身份证筛选: ' -NoNewline; if ($filter1.code -eq 200) { Write-Host '✓ 通过' -ForegroundColor Green } else { Write-Host '✗ 失败' -ForegroundColor Red }; "^
  "Write-Host '查询空结果: ' -NoNewline; if ($noData.code -eq 200 -and $noData.total -eq 0) { Write-Host '✓ 通过' -ForegroundColor Green } else { Write-Host '✗ 失败' -ForegroundColor Red }; "^
  "Write-Host '大页码处理: ' -NoNewline; if ($largePage.code -eq 200) { Write-Host '✓ 通过' -ForegroundColor Green } else { Write-Host '✗ 失败' -ForegroundColor Red }"

echo.
pause
