@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

REM 中介类型修改修复测试脚本（Windows版本）
REM 测试个人和机构中介的修改功能

set BASE_URL=http://localhost:8080
set USERNAME=admin
set PASSWORD=admin123

REM 创建输出目录
if not exist "doc\scripts\test_output" mkdir "doc\scripts\test_output"

REM 生成报告文件名
set REPORT_FILE=doc\scripts\test_output\test_report_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt
set REPORT_FILE=%REPORT_FILE: =0%

echo ======================================== > "%REPORT_FILE%"
echo 中介类型修改修复测试 >> "%REPORT_FILE%"
echo 测试时间: %date% %time% >> "%REPORT_FILE%"
echo ======================================== >> "%REPORT_FILE%"
echo. >> "%REPORT_FILE%"

REM 测试统计
set TOTAL_TESTS=0
set PASSED_TESTS=0
set FAILED_TESTS=0

echo [TEST] 开始测试...

REM 登录获取Token
echo [TEST] 登录获取Token...
curl -s -X POST "%BASE_URL%/login/test" -H "Content-Type: application/json" -d "{\"username\":\"%USERNAME%\",\"password\":\"%PASSWORD%\"}" > temp_response.json

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

if "!TOKEN!"=="" (
    echo [ERROR] 登录失败，无法获取Token >> "%REPORT_FILE%"
    del temp_response.json
    exit /b 1
)

echo [INFO] 登录成功 >> "%REPORT_FILE%"
del temp_response.json

REM 测试1: 获取个人中介列表
echo. >> "%REPORT_FILE%"
echo [TEST] === 测试1: 获取个人中介列表 === >> "%REPORT_FILE%"
curl -s -X GET "%BASE_URL%/dpc/intermediary/list?intermediaryType=1&pageNum=1&pageSize=1" -H "Authorization: Bearer !TOKEN!" > temp_response.json

REM 提取第一个个人中介ID
for /f "tokens=2 delims=:" %%a in ('findstr /c:"\"intermediaryId\"" temp_response.json') do (
    set PERSON_ID=%%a
    set PERSON_ID=!PERSON_ID:,=!
    goto :found_person_id
)
:found_person_id

if "!PERSON_ID!"=="" (
    echo [ERROR] 未能获取个人中介ID >> "%REPORT_FILE%"
    set /a FAILED_TESTS+=1
) else (
    echo [INFO] 获取个人中介ID: !PERSON_ID! >> "%REPORT_FILE%"
    set /a PASSED_TESTS+=1
)
set /a TOTAL_TESTS+=1

REM 测试2: 修改个人中介
if not "!PERSON_ID!"=="" (
    echo. >> "%REPORT_FILE%"
    echo [TEST] === 测试2: 修改个人中介 === >> "%REPORT_FILE%"

    REM 创建请求数据文件
    echo {> update_person.json
    echo   "intermediaryId": !PERSON_ID!,>> update_person.json
    echo   "name": "测试个人修改",>> update_person.json
    echo   "certificateNo": "110101199001011234",>> update_person.json
    echo   "indivType": "中介",>> update_person.json
    echo   "indivSubType": "本人",>> update_person.json
    echo   "indivGender": "M",>> update_person.json
    echo   "indivCertType": "身份证",>> update_person.json
    echo   "indivPhone": "13800138000",>> update_person.json
    echo   "indivCompany": "测试公司",>> update_person.json
    echo   "indivPosition": "经纪人",>> update_person.json
    echo   "status": "0",>> update_person.json
    echo   "remark": "修改测试">> update_person.json
    echo }>> update_person.json

    curl -s -X PUT "%BASE_URL%/dpc/intermediary/person" -H "Authorization: Bearer !TOKEN!" -H "Content-Type: application/json" -d @update_person.json > update_result.json

    type update_result.json >> "%REPORT_FILE%"
    echo. >> "%REPORT_FILE%"

    findstr /c:"\"code\":200" update_result.json >nul
    if !errorlevel! equ 0 (
        echo [INFO] 个人中介修改接口调用成功 >> "%REPORT_FILE%"
        set /a PASSED_TESTS+=1
    ) else (
        echo [ERROR] 个人中介修改接口调用失败 >> "%REPORT_FILE%"
        set /a FAILED_TESTS+=1
    )
    set /a TOTAL_TESTS+=1

    del update_person.json
    del update_result.json
)

REM 测试3: 获取机构中介列表
echo. >> "%REPORT_FILE%"
echo [TEST] === 测试3: 获取机构中介列表 === >> "%REPORT_FILE%"
curl -s -X GET "%BASE_URL%/dpc/intermediary/list?intermediaryType=2&pageNum=1&pageSize=1" -H "Authorization: Bearer !TOKEN!" > temp_response.json

REM 提取第一个机构中介ID
for /f "tokens=2 delims=:" %%a in ('findstr /c:"\"intermediaryId\"" temp_response.json') do (
    set ENTITY_ID=%%a
    set ENTITY_ID=!ENTITY_ID:,=!
    goto :found_entity_id
)
:found_entity_id

if "!ENTITY_ID!"=="" (
    echo [ERROR] 未能获取机构中介ID >> "%REPORT_FILE%"
    set /a FAILED_TESTS+=1
) else (
    echo [INFO] 获取机构中介ID: !ENTITY_ID! >> "%REPORT_FILE%"
    set /a PASSED_TESTS+=1
)
set /a TOTAL_TESTS+=1

REM 测试4: 修改机构中介
if not "!ENTITY_ID!"=="" (
    echo. >> "%REPORT_FILE%"
    echo [TEST] === 测试4: 修改机构中介 === >> "%REPORT_FILE%"

    REM 创建请求数据文件
    echo {> update_entity.json
    echo   "intermediaryId": !ENTITY_ID!,>> update_entity.json
    echo   "name": "测试机构修改",>> update_entity.json
    echo   "certificateNo": "91110000XXXXXXXXXX",>> update_entity.json
    echo   "corpCreditCode": "91110000XXXXXXXXXX",>> update_entity.json
    echo   "corpType": "有限责任公司",>> update_entity.json
    echo   "corpNature": "民企",>> update_entity.json
    echo   "corpIndustry": "房地产",>> update_entity.json
    echo   "corpAddress": "北京市朝阳区",>> update_entity.json
    echo   "corpLegalRep": "张三",>> update_entity.json
    echo   "status": "0",>> update_entity.json
    echo   "remark": "修改测试">> update_entity.json
    echo }>> update_entity.json

    curl -s -X PUT "%BASE_URL%/dpc/intermediary/entity" -H "Authorization: Bearer !TOKEN!" -H "Content-Type: application/json" -d @update_entity.json > update_result.json

    type update_result.json >> "%REPORT_FILE%"
    echo. >> "%REPORT_FILE%"

    findstr /c:"\"code\":200" update_result.json >nul
    if !errorlevel! equ 0 (
        echo [INFO] 机构中介修改接口调用成功 >> "%REPORT_FILE%"
        set /a PASSED_TESTS+=1
    ) else (
        echo [ERROR] 机构中介修改接口调用失败 >> "%REPORT_FILE%"
        set /a FAILED_TESTS+=1
    )
    set /a TOTAL_TESTS+=1

    del update_entity.json
    del update_result.json
)

REM 清理临时文件
if exist temp_response.json del temp_response.json

REM 输出测试总结
echo. >> "%REPORT_FILE%"
echo ======================================== >> "%REPORT_FILE%"
echo 测试总结 >> "%REPORT_FILE%"
echo ======================================== >> "%REPORT_FILE%"
echo 总测试数: %TOTAL_TESTS% >> "%REPORT_FILE%"
echo 通过: %PASSED_TESTS% >> "%REPORT_FILE%"
echo 失败: %FAILED_TESTS% >> "%REPORT_FILE%"
echo ======================================== >> "%REPORT_FILE%"

echo.
echo ========================================
echo 测试总结
echo ========================================
echo 总测试数: %TOTAL_TESTS%
echo 通过: %PASSED_TESTS%
echo 失败: %FAILED_TESTS%
echo ========================================
echo.
echo 详细报告已保存到: %REPORT_FILE%

if %FAILED_TESTS% equ 0 (
    echo [INFO] 所有测试通过！
    exit /b 0
) else (
    echo [ERROR] 部分测试失败，请查看详细日志
    exit /b 1
)
