#!/bin/bash

# =============================================
# 停车场和车型参数模块 - API 测试脚本
# 创建时间: 2026-03-12
# =============================================

# 配置
BASE_URL="http://localhost:48080/admin-api"
TOKEN="YOUR_TOKEN_HERE"  # 需要替换为实际的 token

# 颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 测试计数
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# 测试函数
test_api() {
    local test_name=$1
    local method=$2
    local url=$3
    local data=$4
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    
    echo ""
    echo "=========================================="
    echo "测试 ${TOTAL_TESTS}: ${test_name}"
    echo "=========================================="
    
    if [ -z "$data" ]; then
        response=$(curl -s -X ${method} "${BASE_URL}${url}" \
            -H "Authorization: Bearer ${TOKEN}" \
            -H "Content-Type: application/json")
    else
        response=$(curl -s -X ${method} "${BASE_URL}${url}" \
            -H "Authorization: Bearer ${TOKEN}" \
            -H "Content-Type: application/json" \
            -d "${data}")
    fi
    
    echo "响应: ${response}"
    
    # 检查响应是否包含 "code":0
    if echo "$response" | grep -q '"code":0'; then
        echo -e "${GREEN}✅ 测试通过${NC}"
        PASSED_TESTS=$((PASSED_TESTS + 1))
        return 0
    else
        echo -e "${RED}❌ 测试失败${NC}"
        FAILED_TESTS=$((FAILED_TESTS + 1))
        return 1
    fi
}

# 开始测试
echo "=========================================="
echo "开始 API 测试"
echo "=========================================="
echo "基础 URL: ${BASE_URL}"
echo "时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo ""

# ==========================================
# 停车场管理接口测试
# ==========================================

echo ""
echo "=========================================="
echo "一、停车场管理接口测试"
echo "=========================================="

# 测试 1：创建停车场
test_api "创建停车场" "POST" "/asset/parking/create" '{
  "name": "杭州停车场",
  "principal": "张三",
  "contactName": "李四",
  "contactPhone": "13800138000",
  "address": "浙江省杭州市西湖区",
  "longitude": "120.123456",
  "latitude": "30.123456",
  "capacity": 100,
  "parkedAmount": 0,
  "stockArea": 1,
  "unusualActionCity": 1,
  "remark": "测试停车场"
}'

# 保存停车场 ID（假设返回的 ID 为 1）
PARKING_ID=1

# 测试 2：获取停车场详情
test_api "获取停车场详情" "GET" "/asset/parking/get?id=${PARKING_ID}"

# 测试 3：分页查询停车场
test_api "分页查询停车场" "GET" "/asset/parking/page?pageNo=1&pageSize=10"

# 测试 4：获取停车场精简列表
test_api "获取停车场精简列表" "GET" "/asset/parking/simple-list"

# 测试 5：更新停车场
test_api "更新停车场" "PUT" "/asset/parking/update" '{
  "id": '${PARKING_ID}',
  "name": "杭州停车场（已更新）",
  "principal": "王五",
  "contactName": "李四",
  "contactPhone": "13800138000",
  "address": "浙江省杭州市西湖区",
  "longitude": "120.123456",
  "latitude": "30.123456",
  "capacity": 150,
  "parkedAmount": 10,
  "stockArea": 1,
  "unusualActionCity": 1,
  "remark": "测试停车场（已更新）"
}'

# ==========================================
# 车型参数管理接口测试
# ==========================================

echo ""
echo "=========================================="
echo "二、车型参数管理接口测试"
echo "=========================================="

# 测试 6：创建车型参数（带保养项目）
test_api "创建车型参数（带保养项目）" "POST" "/asset/vehicle-model/create" '{
  "brand": 1,
  "model": 1,
  "modelCode": "HFC1043K1",
  "modelName": "江淮骏铃V6",
  "noticeModel": "HFC1043K1R8T",
  "hydrogenUnit": 1,
  "hydrogenCapacity": 165,
  "electricityMileage": 300,
  "reserveElectricity": 80.00,
  "hydrogenMileage": 400,
  "fuelType": 1,
  "tireSize": "225/70R19.5",
  "tireNumber": 6,
  "truckSize": "5995×2100×2850",
  "onlineSpreadEnterprise": "上海重塑",
  "batteryFactory": "宁德时代",
  "refrigeratorFactory": "开山",
  "maintainItems": [
    {
      "maintainItem": "更换机油",
      "maintainContent": "更换机油、机滤",
      "materialsExpenses": 200.00,
      "hourFee": 100.00,
      "kilometerCycle": 5000,
      "timeCycle": 6
    },
    {
      "maintainItem": "更换空滤",
      "maintainContent": "更换空气滤芯",
      "materialsExpenses": 50.00,
      "hourFee": 50.00,
      "kilometerCycle": 10000,
      "timeCycle": 12
    }
  ]
}'

# 保存车型参数 ID（假设返回的 ID 为 1）
VEHICLE_MODEL_ID=1

# 测试 7：获取车型参数详情（含保养项目）
test_api "获取车型参数详情（含保养项目）" "GET" "/asset/vehicle-model/get?id=${VEHICLE_MODEL_ID}"

# 测试 8：分页查询车型参数
test_api "分页查询车型参数" "GET" "/asset/vehicle-model/page?pageNo=1&pageSize=10"

# 测试 9：获取车型参数精简列表
test_api "获取车型参数精简列表" "GET" "/asset/vehicle-model/simple-list"

# 测试 10：根据品牌查询车型参数
test_api "根据品牌查询车型参数" "GET" "/asset/vehicle-model/list-by-brand?brand=1"

# 测试 11：根据车型查询车型参数
test_api "根据车型查询车型参数" "GET" "/asset/vehicle-model/list-by-model?model=1"

# 测试 12：更新车型参数（更新保养项目）
test_api "更新车型参数（更新保养项目）" "PUT" "/asset/vehicle-model/update" '{
  "id": '${VEHICLE_MODEL_ID}',
  "brand": 1,
  "model": 1,
  "modelCode": "HFC1043K1",
  "modelName": "江淮骏铃V6（已更新）",
  "noticeModel": "HFC1043K1R8T",
  "hydrogenUnit": 1,
  "hydrogenCapacity": 165,
  "electricityMileage": 300,
  "reserveElectricity": 80.00,
  "hydrogenMileage": 400,
  "fuelType": 1,
  "tireSize": "225/70R19.5",
  "tireNumber": 6,
  "truckSize": "5995×2100×2850",
  "onlineSpreadEnterprise": "上海重塑",
  "batteryFactory": "宁德时代",
  "refrigeratorFactory": "开山",
  "maintainItems": [
    {
      "maintainItem": "更换机油",
      "maintainContent": "更换机油、机滤、空滤",
      "materialsExpenses": 250.00,
      "hourFee": 120.00,
      "kilometerCycle": 5000,
      "timeCycle": 6
    }
  ]
}'

# ==========================================
# 清理测试数据（可选）
# ==========================================

echo ""
echo "=========================================="
echo "三、清理测试数据（可选）"
echo "=========================================="

# 测试 13：删除车型参数
# test_api "删除车型参数" "DELETE" "/asset/vehicle-model/delete?id=${VEHICLE_MODEL_ID}"

# 测试 14：删除停车场
# test_api "删除停车场" "DELETE" "/asset/parking/delete?id=${PARKING_ID}"

# ==========================================
# 测试总结
# ==========================================

echo ""
echo "=========================================="
echo "测试总结"
echo "=========================================="
echo "总测试数: ${TOTAL_TESTS}"
echo -e "${GREEN}通过: ${PASSED_TESTS}${NC}"
echo -e "${RED}失败: ${FAILED_TESTS}${NC}"
echo ""

if [ ${FAILED_TESTS} -eq 0 ]; then
    echo -e "${GREEN}✅ 所有测试通过！${NC}"
    exit 0
else
    echo -e "${RED}❌ 有 ${FAILED_TESTS} 个测试失败${NC}"
    exit 1
fi
