@echo off
chcp 65001 >nul
setlocal

set "BASE_URL=http://localhost:8080"
set "OUTPUT_DIR=doc\implementation\test-results"
set "TEST_FILE=%OUTPUT_DIR%\staff-enterprise-relation-status-fix-test_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt"
set "TEST_FILE=%TEST_FILE: =0%"

echo ========================================
echo 员工实体关系状态默认值修复验证测试
echo ========================================
echo 测试时间: %date% %time%
echo.

REM 创建输出目录
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"

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

REM 提取token
for /f "tokens=2 delims=:," %%a in ('findstr /C:"\"token\"" "%OUTPUT_DIR%\login_response.json"') do (
  set "token_line=%%a"
  set "token=%%a"
)
REM 去除引号和空格
set "TOKEN=%token_line:"=%"
set "TOKEN=%TOKEN: =%"

echo Token获取成功: %TOKEN:~0,20%...
echo.

REM ========================================
REM 2. 测试新增接口(不传status字段)
REM ========================================
echo [步骤2] 测试新增接口(不传status字段)...
set "TEST_ID_1=%random%"
curl -s -X POST "%BASE_URL%/ccdi/staffEnterpriseRelation" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -H "Content-Type: application/json" ^
  -d "{\"personId\":\"11010119900101123%TEST_ID_1%\",\"socialCreditCode\":\"91110000123456789%TEST_ID_1%\",\"enterpriseName\":\"测试企业A\",\"relationPersonPost\":\"测试职务\"}" ^
  > "%OUTPUT_DIR%\add_test1_response.json"

echo.
echo 响应结果:
type "%OUTPUT_DIR%\add_test1_response.json"
echo.

REM 解析响应中的ID
for /f "tokens=2 delims=:," %%a in ('findstr /C:"\"data\"" "%OUTPUT_DIR%\add_test1_response.json"') do set "INSERT_ID_1=%%a"
set "INSERT_ID_1=%INSERT_ID_1:" =%"
set "INSERT_ID_1=%INSERT_ID_1:}=%"

echo 新增记录ID: %INSERT_ID_1%
echo.

REM ========================================
REM 3. 查询新增记录的状态
REM ========================================
echo [步骤3] 查询新增记录的状态...
curl -s -X GET "%BASE_URL%/ccdi/staffEnterpriseRelation/%INSERT_ID_1%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > "%OUTPUT_DIR%\query_test1_response.json"

echo.
echo 查询结果:
type "%OUTPUT_DIR%\query_test1_response.json"
echo.

REM ========================================
REM 4. 测试新增接口(传status=0,应被覆盖为1)
REM ========================================
echo [步骤4] 测试新增接口(传status=0,应被覆盖为1)...
set "TEST_ID_2=%random%"
curl -s -X POST "%BASE_URL%/ccdi/staffEnterpriseRelation" ^
  -H "Authorization: Bearer %TOKEN%" ^
  -H "Content-Type: application/json" ^
  -d "{\"personId\":\"11010119900101124%TEST_ID_2%\",\"socialCreditCode\":\"91110000123456780%TEST_ID_2%\",\"enterpriseName\":\"测试企业B\",\"relationPersonPost\":\"测试职务\",\"status\":0}" ^
  > "%OUTPUT_DIR%\add_test2_response.json"

echo.
echo 响应结果:
type "%OUTPUT_DIR%\add_test2_response.json"
echo.

REM 解析响应中的ID
for /f "tokens=2 delims=:," %%a in ('findstr /C:"\"data\"" "%OUTPUT_DIR%\add_test2_response.json"') do set "INSERT_ID_2=%%a"
set "INSERT_ID_2=%INSERT_ID_2:" =%"
set "INSERT_ID_2=%INSERT_ID_2:}=%"

echo 新增记录ID: %INSERT_ID_2%
echo.

REM ========================================
REM 5. 查询第二条记录的状态
REM ========================================
echo [步骤5] 查询第二条记录的状态(验证是否被强制设置为1)...
curl -s -X GET "%BASE_URL%/ccdi/staffEnterpriseRelation/%INSERT_ID_2%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > "%OUTPUT_DIR%\query_test2_response.json"

echo.
echo 查询结果:
type "%OUTPUT_DIR%\query_test2_response.json"
echo.

REM ========================================
REM 6. 清理测试数据
REM ========================================
echo [步骤6] 清理测试数据...
curl -s -X DELETE "%BASE_URL%/ccdi/staffEnterpriseRelation/%INSERT_ID_1%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > "%OUTPUT_DIR%\delete_test1_response.json"

curl -s -X DELETE "%BASE_URL%/ccdi/staffEnterpriseRelation/%INSERT_ID_2%" ^
  -H "Authorization: Bearer %TOKEN%" ^
  > "%OUTPUT_DIR%\delete_test2_response.json"

echo 测试数据已清理
echo.

REM ========================================
REM 7. 生成测试报告
REM ========================================
echo ========================================
echo 测试结果分析
echo ========================================
echo.
echo 测试用例1: 不传status字段
echo   预期结果: status = 1 (有效)
echo   实际结果: 请查看 query_test1_response.json 中的status字段
echo.
echo 测试用例2: 传status=0
echo   预期结果: status = 1 (有效,被强制覆盖)
echo   实际结果: 请查看 query_test2_response.json 中的status字段
echo.
echo 详细响应数据保存在: %OUTPUT_DIR%\
echo.

REM 将所有输出保存到测试文件
(
  echo ========================================
  echo 员工实体关系状态默认值修复验证测试报告
  echo ========================================
  echo 测试时间: %date% %time%
  echo.
  echo ========================================
  echo 测试用例1: 不传status字段
  echo ========================================
  echo 请求: POST /ccdi/staffEnterpriseRelation
  echo 请求体: {personId, socialCreditCode, enterpriseName, relationPersonPost}
  echo.
  echo 新增响应:
  type "%OUTPUT_DIR%\add_test1_response.json"
  echo.
  echo 查询响应:
  type "%OUTPUT_DIR%\query_test1_response.json"
  echo.
  echo ========================================
  echo 测试用例2: 传status=0
  echo ========================================
  echo 请求: POST /ccdi/staffEnterpriseRelation
  echo 请求体: {personId, socialCreditCode, enterpriseName, relationPersonPost, status: 0}
  echo.
  echo 新增响应:
  type "%OUTPUT_DIR%\add_test2_response.json"
  echo.
  echo 查询响应:
  type "%OUTPUT_DIR%\query_test2_response.json"
  echo.
  echo ========================================
  echo 结论
  echo ========================================
  echo 如果两个测试用例的查询结果中status字段都为1,
  echo 则说明修复成功,新增操作强制设置状态为有效。
  echo.
) > "%TEST_FILE%"

echo 测试完成!报告已保存至: %TEST_FILE%
echo.
pause
