@echo off
REM ========================================
REM 信贷客户家庭关系 CRUD 功能测试脚本
REM ========================================

setlocal EnableDelayedExpansion

echo ========================================
echo 信贷客户家庭关系 CRUD 功能测试
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/7] 正在登录...
curl -s -X POST "%BASE_URL%/login/test" ^
  -H "Content-Type: application/json" ^
  -d "{\"username\":\"admin\",\"password\":\"admin123\"}" ^
  > test-results\01_login_response.json

echo 登录响应:
type test-results\01_login_response.json
echo.

REM 提取token (使用PowerShell辅助)
powershell -Command "$json = Get-Content test-results\01_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 步骤2: 测试新增功能
REM ========================================
echo [2/7] 测试新增功能...
curl -s -X POST "%BASE_URL%/ccdi/custFmyRelation" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -H "Content-Type: application/json" ^
  -d "{\"personId\":\"110101199001011234\",\"relationType\":\"配偶\",\"relationName\":\"张三\",\"gender\":\"男\",\"relationCertType\":\"身份证\",\"relationCertNo\":\"110101199001011235\",\"mobilePhone1\":\"13800138000\",\"remark\":\"测试数据\"}" ^
  > test-results\02_create_response.json

echo 新增响应:
type test-results\02_create_response.json
echo.

REM 提取创建的ID
powershell -Command "$json = Get-Content test-results\02_create_response.json -Raw | ConvertFrom-Json; if ($json.data) { $id = $json.data; Set-Content -Path test-results\created_id.txt -Value $id } else { Write-Output '0' | Out-File -FilePath test-results\created_id.txt }"

set /p CREATED_ID=<test-results\created_id.txt
echo 创建的记录ID: %CREATED_ID%
echo.

REM ========================================
REM 步骤3: 测试查询功能 (根据ID查询详情)
REM ========================================
echo [3/7] 测试查询详情功能...
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/%CREATED_ID%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\03_get_detail_response.json

echo 查询详情响应:
type test-results\03_get_detail_response.json
echo.

REM ========================================
REM 步骤4: 测试列表查询功能
REM ========================================
echo [4/7] 测试列表查询功能...
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\04_list_response.json

echo 列表查询响应:
type test-results\04_list_response.json
echo.

REM ========================================
REM 步骤5: 测试修改功能
REM ========================================
echo [5/7] 测试修改功能...
curl -s -X PUT "%BASE_URL%/ccdi/custFmyRelation" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -H "Content-Type: application/json" ^
  -d "{\"id\":%CREATED_ID%,\"personId\":\"110101199001011234\",\"relationType\":\"配偶\",\"relationName\":\"张三(已修改)\",\"gender\":\"男\",\"relationCertType\":\"身份证\",\"relationCertNo\":\"110101199001011235\",\"mobilePhone1\":\"13900139000\",\"remark\":\"测试数据-已修改\"}" ^
  > test-results\05_update_response.json

echo 修改响应:
type test-results\05_update_response.json
echo.

REM ========================================
REM 步骤6: 验证修改结果
REM ========================================
echo [6/7] 验证修改结果...
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/%CREATED_ID%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\06_verify_update_response.json

echo 验证修改响应:
type test-results\06_verify_update_response.json
echo.

REM ========================================
REM 步骤7: 测试删除功能
REM ========================================
echo [7/7] 测试删除功能...
curl -s -X DELETE "%BASE_URL%/ccdi/custFmyRelation/%CREATED_ID%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\07_delete_response.json

echo 删除响应:
type test-results\07_delete_response.json
echo.

REM ========================================
REM 验证删除结果
REM ========================================
echo 验证删除结果...
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/%CREATED_ID%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > test-results\08_verify_delete_response.json

echo 验证删除响应 (应该为空或错误):
type test-results\08_verify_delete_response.json
echo.

REM ========================================
REM 生成测试报告
REM ========================================
echo ========================================
echo 测试完成!
echo ========================================
echo.
echo 测试结果文件:
echo - 01_login_response.json (登录响应)
echo - 02_create_response.json (新增响应)
echo - 03_get_detail_response.json (查询详情响应)
echo - 04_list_response.json (列表查询响应)
echo - 05_update_response.json (修改响应)
echo - 06_verify_update_response.json (验证修改响应)
echo - 07_delete_response.json (删除响应)
echo - 08_verify_delete_response.json (验证删除响应)
echo.

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

powershell -Command ^
  "$create = Get-Content test-results\02_create_response.json -Raw | ConvertFrom-Json; "^
  "$update = Get-Content test-results\05_update_response.json -Raw | ConvertFrom-Json; "^
  "$delete = Get-Content test-results\07_delete_response.json -Raw | ConvertFrom-Json; "^
  "Write-Host '新增功能: ' -NoNewline; if ($create.code -eq 200) { Write-Host '✓ 通过' -ForegroundColor Green } else { Write-Host '✗ 失败' -ForegroundColor Red }; "^
  "Write-Host '修改功能: ' -NoNewline; if ($update.code -eq 200) { Write-Host '✓ 通过' -ForegroundColor Green } else { Write-Host '✗ 失败' -ForegroundColor Red }; "^
  "Write-Host '删除功能: ' -NoNewline; if ($delete.code -eq 200) { Write-Host '✓ 通过' -ForegroundColor Green } else { Write-Host '✗ 失败' -ForegroundColor Red }"

echo.
pause
