@echo off
REM 员工企业关系管理完整测试脚本 (Windows版本)
REM 测试员工企业关系信息的所有接口功能

setlocal enabledelayedexpansion

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

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

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

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 [INFO] 开始测试...
echo [INFO] 测试报告: %REPORT_FILE%
echo.

REM ============ 测试1: 登录 ============
echo [TEST] 测试1: 登录获取Token...

curl -s -X POST "%BASE_URL%/login/test" ^
  -H "Content-Type: application/json" ^
  -d "{\"username\":\"%USERNAME%\",\"password\":\"%PASSWORD%}" ^
  > temp_login_response.json

REM 提取token (Windows下使用jq或手动解析)
REM 这里假设使用jq工具，如果没有安装jq，需要手动处理
for /f "tokens=2 delims=:\"" %%a in ('findstr /C:"\"token\"" temp_login_response.json') do (
    set TOKEN=%%a
    goto :found_token
)
:found_token

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

echo [INFO] 登录成功，Token: %TOKEN:~0,20%... >> "%REPORT_FILE%"
echo [INFO] 登录成功
echo.

REM ============ 测试2: 查询列表 ============
echo [TEST] 测试2: 查询员工企业关系列表...

curl -s -X GET "%BASE_URL%/ccdi/staffEnterpriseRelation/list?pageNum=1&pageSize=10" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > temp_list_response.json

type temp_list_response.json >> "%REPORT_FILE%"
findstr /C:"\"code\":200" temp_list_response.json >nul
if errorlevel 1 (
    echo [ERROR] 查询列表失败 >> "%REPORT_FILE%"
    set /a FAILED_TESTS+=1
) else (
    echo [INFO] 查询列表成功 >> "%REPORT_FILE%"
    set /a PASSED_TESTS+=1
)
set /a TOTAL_TESTS+=1
echo.
echo [INFO] 测试2完成
echo.

REM ============ 测试3: 新增员工企业关系 ============
echo [TEST] 测试3: 新增员工企业关系...

curl -s -X POST "%BASE_URL%/ccdi/staffEnterpriseRelation" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -H "Content-Type: application/json" ^
  -d "{\"personId\":\"110101199001019998\",\"personName\":\"测试员工\",\"socialCreditCode\":\"91110000999999999X\",\"enterpriseName\":\"测试企业\",\"relationPersonPost\":\"测试岗位\",\"isEmpFamily\":1,\"status\":1}" ^
  > temp_add_response.json

type temp_add_response.json >> "%REPORT_FILE%"
findstr /C:"\"code\":200" temp_add_response.json >nul
if errorlevel 1 (
    echo [ERROR] 新增失败 >> "%REPORT_FILE%"
    set /a FAILED_TESTS+=1
    set NEW_ID=
) else (
    echo [INFO] 新增成功 >> "%REPORT_FILE%"
    set /a PASSED_TESTS+=1
    REM 简化处理：假设新增成功后需要通过列表查询获取ID
)
set /a TOTAL_TESTS+=1
echo.
echo [INFO] 测试3完成
echo.

REM ============ 测试4: 查询详情 ============
echo [TEST] 测试4: 查询员工企业关系详情...

REM 先通过列表查询获取一个ID
curl -s -X GET "%BASE_URL%/ccdi/staffEnterpriseRelation/list?pageNum=1&pageSize=1" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > temp_get_list.json

REM 简化处理：这里应该解析JSON获取第一个ID，但Windows批处理处理JSON很困难
REM 实际测试时建议使用bash版本或PowerShell版本

echo [WARNING] 查询详情测试需要手动指定ID >> "%REPORT_FILE%"
echo [INFO] 测试4完成（跳过）
echo.

REM ============ 测试5: 下载导入模板 ============
echo [TEST] 测试5: 下载导入模板...

curl -s -X POST "%BASE_URL%/ccdi/staffEnterpriseRelation/importTemplate" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -o "doc\implementation\scripts\test_output\test5_import_template.xlsx" ^
  -w "%%{http_code}" > temp_http_code.txt

set /p HTTP_CODE=<temp_http_code.txt
if "%HTTP_CODE%"=="200" (
    echo [INFO] 下载导入模板成功 >> "%REPORT_FILE%"
    echo [INFO] 模板文件已保存到: doc\implementation\scripts\test_output\test5_import_template.xlsx >> "%REPORT_FILE%"
    set /a PASSED_TESTS+=1
) else (
    echo [ERROR] 下载导入模板失败 (HTTP %HTTP_CODE%) >> "%REPORT_FILE%"
    set /a FAILED_TESTS+=1
)
set /a TOTAL_TESTS+=1
echo.
echo [INFO] 测试5完成
echo.

REM ============ 测试6: 导出数据 ============
echo [TEST] 测试6: 导出员工企业关系数据...

curl -s -X POST "%BASE_URL%/ccdi/staffEnterpriseRelation/export" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -H "Content-Type: application/json" ^
  -d "{}" ^
  -o "doc\implementation\scripts\test_output\test6_export.xlsx" ^
  -w "%%{http_code}" > temp_http_code.txt

set /p HTTP_CODE=<temp_http_code.txt
if "%HTTP_CODE%"=="200" (
    echo [INFO] 导出数据成功 >> "%REPORT_FILE%"
    echo [INFO] 导出文件已保存到: doc\implementation\scripts\test_output\test6_export.xlsx >> "%REPORT_FILE%"
    set /a PASSED_TESTS+=1
) else (
    echo [ERROR] 导出数据失败 (HTTP %HTTP_CODE%) >> "%REPORT_FILE%"
    set /a FAILED_TESTS+=1
)
set /a TOTAL_TESTS+=1
echo.
echo [INFO] 测试6完成
echo.

REM 清理临时文件
del temp_login_response.json 2>nul
del temp_list_response.json 2>nul
del temp_add_response.json 2>nul
del temp_get_list.json 2>nul
del temp_http_code.txt 2>nul

REM ============ 输出测试总结 ============
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 详细日志已保存到: %REPORT_FILE%
echo.

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