@echo off
REM 信贷客户家庭关系导入功能测试脚本
REM 测试对齐后的导入功能

echo ========================================
echo 信贷客户家庭关系导入功能测试
echo ========================================
echo.

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

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

REM 提取token
for /f "tokens=2 delims=:\"" %%a in ('findstr /C:"\"token\"" login_response.json') do (
    set TOKEN=%%a
    goto :token_found
)
:token_found

echo 登录成功! Token: %TOKEN:~0,20%...
echo.

REM 步骤2: 下载导入模板
echo [2/6] 下载导入模板...
curl -s -X POST "%BASE_URL%/ccdi/custFmyRelation/importTemplate" ^
  -H "Authorization: Bearer %TOKEN%" ^
  --output 信贷客户家庭关系导入模板.xlsx
echo 模板已下载: 信贷客户家庭关系导入模板.xlsx
echo.

REM 步骤3: 测试导入接口(使用测试数据)
echo [3/6] 测试导入接口...
echo 创建测试Excel文件...

REM 步骤4: 提交导入任务
echo [4/6] 提交导入任务...
curl -s -X POST "%BASE_URL%/ccdi/custFmyRelation/importData" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -F "file=@测试数据_信贷客户家庭关系.xlsx" ^
  > import_response.json

echo 导入响应:
type import_response.json
echo.

REM 提取taskId
for /f "tokens=2 delims=:\"" %%a in ('findstr /C:"\"taskId\"" import_response.json') do (
    set TASK_ID=%%a
    goto :task_found
)
:task_found
echo 任务ID: %TASK_ID%
echo.

REM 步骤5: 查询导入状态
echo [5/6] 查询导入状态(等待3秒)...
timeout /t 3 /nobreak >nul

curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/importStatus/%TASK_ID%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > status_response.json

echo 导入状态:
type status_response.json
echo.

REM 步骤6: 查询导入失败记录
echo [6/6] 查询导入失败记录...
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/importFailures/%TASK_ID%?pageNum=1&pageSize=10" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > failures_response.json

echo 失败记录:
type failures_response.json
echo.

REM 测试查询接口
echo [额外] 测试查询接口...
curl -s -X GET "%BASE_URL%/ccdi/custFmyRelation/list?pageNum=1&pageSize=10" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > list_response.json

echo 查询结果:
type list_response.json
echo.

echo ========================================
echo 测试完成!
echo ========================================
echo.
echo 生成的文件:
echo - login_response.json (登录响应)
echo - import_response.json (导入响应)
echo - status_response.json (状态响应)
echo - failures_response.json (失败记录)
echo - list_response.json (查询结果)
echo - 信贷客户家庭关系导入模板.xlsx (导入模板)
echo.

pause
