#!/bin/bash

################################################################################
# 中介黑名单管理 API 测试脚本
# 功能: 测试中介黑名单管理模块的所有接口
# 作者: Claude Code
# 日期: 2026-02-04
################################################################################

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

# 配置
BASE_URL="http://localhost:8080"
TEST_USERNAME="admin"
TEST_PASSWORD="admin123"

# 输出函数
print_header() {
    echo ""
    echo "========================================"
    echo "$1"
    echo "========================================"
}

print_section() {
    echo ""
    echo -e "${YELLOW}=== $1 ===${NC}"
}

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_error() {
    echo -e "${RED}✗ $1${NC}"
}

# 获取Token
get_token() {
    print_section "获取Token"

    TOKEN=$(curl -s -X POST "${BASE_URL}/login/test" \
        -H "Content-Type: application/json" \
        -d "{\"username\":\"${TEST_USERNAME}\",\"password\":\"${TEST_PASSWORD}\"}" | jq -r '.data.token')

    if [ "$TOKEN" != "null" ] && [ -n "$TOKEN" ]; then
        print_success "Token获取成功: ${TOKEN:0:20}..."
        echo "$TOKEN"
    else
        print_error "Token获取失败"
        exit 1
    fi
}

# 测试查询列表
test_list() {
    print_section "测试查询列表"

    response=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?pageNum=1&pageSize=10" \
        -H "Authorization: Bearer $TOKEN")

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        print_success "查询列表成功"
        total=$(echo "$response" | jq -r '.total')
        echo "总记录数: $total"
    else
        print_error "查询列表失败"
    fi
}

# 测试新增个人中介
test_add_person() {
    print_section "测试新增个人中介"

    response=$(curl -s -X POST "${BASE_URL}/ccdi/intermediary/person" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "name": "测试中介个人",
            "personType": "中介",
            "personSubType": "本人",
            "relationType": "正常",
            "gender": "M",
            "idType": "身份证",
            "personId": "110101199001019999",
            "mobile": "13800138000",
            "wechatNo": "test_wx",
            "contactAddress": "北京市朝阳区测试地址",
            "company": "测试公司",
            "position": "经纪人",
            "remark": "自动化测试数据"
        }')

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        print_success "新增个人中介成功"
        # 保存bizId用于后续测试
        PERSON_BIZ_ID=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?name=测试中介个人" \
            -H "Authorization: Bearer $TOKEN" | jq -r '.rows[0].bizId // empty')
        if [ -n "$PERSON_BIZ_ID" ]; then
            echo "获取到个人中介bizId: $PERSON_BIZ_ID"
        fi
    else
        print_error "新增个人中介失败: $(echo "$response" | jq -r '.msg')"
    fi
}

# 测试新增实体中介
test_add_entity() {
    print_section "测试新增实体中介"

    response=$(curl -s -X POST "${BASE_URL}/ccdi/intermediary/entity" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "enterpriseName": "测试中介公司",
            "socialCreditCode": "91110000123456789X",
            "enterpriseType": "有限责任公司",
            "enterpriseNature": "民企",
            "industryClass": "房地产",
            "industryName": "房地产业",
            "establishDate": "2020-01-01",
            "registerAddress": "北京市朝阳区注册地址",
            "legalRepresentative": "张三",
            "legalCertType": "身份证",
            "legalCertNo": "110101199001011234",
            "shareholder1": "李四",
            "shareholder2": "王五",
            "remark": "自动化测试数据"
        }')

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        print_success "新增实体中介成功"
        # 保存socialCreditCode用于后续测试
        ENTITY_CREDIT_CODE="91110000123456789X"
        echo "实体中介统一社会信用代码: $ENTITY_CREDIT_CODE"
    else
        print_error "新增实体中介失败: $(echo "$response" | jq -r '.msg')"
    fi
}

# 测试查询个人中介详情
test_get_person_detail() {
    print_section "测试查询个人中介详情"

    if [ -z "$PERSON_BIZ_ID" ]; then
        print_error "没有可用的个人中介bizId,跳过测试"
        return
    fi

    response=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/person/${PERSON_BIZ_ID}" \
        -H "Authorization: Bearer $TOKEN")

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        print_success "查询个人中介详情成功"
    else
        print_error "查询个人中介详情失败: $(echo "$response" | jq -r '.msg')"
    fi
}

# 测试查询实体中介详情
test_get_entity_detail() {
    print_section "测试查询实体中介详情"

    if [ -z "$ENTITY_CREDIT_CODE" ]; then
        print_error "没有可用的实体中介统一社会信用代码,跳过测试"
        return
    fi

    response=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/entity/${ENTITY_CREDIT_CODE}" \
        -H "Authorization: Bearer $TOKEN")

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        print_success "查询实体中介详情成功"
    else
        print_error "查询实体中介详情失败: $(echo "$response" | jq -r '.msg')"
    fi
}

# 测试校验人员ID唯一性
test_check_person_id() {
    print_section "测试校验人员ID唯一性"

    response=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/checkPersonIdUnique?personId=110101199001019999" \
        -H "Authorization: Bearer $TOKEN")

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        unique=$(echo "$response" | jq -r '.data')
        print_success "校验人员ID唯一性成功, unique=$unique"
    else
        print_error "校验人员ID唯一性失败"
    fi
}

# 测试校验统一社会信用代码唯一性
test_check_social_credit_code() {
    print_section "测试校验统一社会信用代码唯一性"

    response=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/checkSocialCreditCodeUnique?socialCreditCode=91110000123456789X" \
        -H "Authorization: Bearer $TOKEN")

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        unique=$(echo "$response" | jq -r '.data')
        print_success "校验统一社会信用代码唯一性成功, unique=$unique"
    else
        print_error "校验统一社会信用代码唯一性失败"
    fi
}

# 测试修改个人中介
test_edit_person() {
    print_section "测试修改个人中介"

    if [ -z "$PERSON_BIZ_ID" ]; then
        print_error "没有可用的个人中介bizId,跳过测试"
        return
    fi

    response=$(curl -s -X PUT "${BASE_URL}/ccdi/intermediary/person" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d "{
            \"bizId\": \"$PERSON_BIZ_ID\",
            \"name\": \"测试中介个人(已修改)\",
            \"personType\": \"中介\",
            \"gender\": \"M\",
            \"idType\": \"身份证\",
            \"personId\": \"110101199001019999\",
            \"mobile\": \"13900139000\",
            \"company\": \"新公司\",
            \"position\": \"高级经纪人\",
            \"remark\": \"修改后的测试数据\"
        }")

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        print_success "修改个人中介成功"
    else
        print_error "修改个人中介失败: $(echo "$response" | jq -r '.msg')"
    fi
}

# 测试修改实体中介
test_edit_entity() {
    print_section "测试修改实体中介"

    if [ -z "$ENTITY_CREDIT_CODE" ]; then
        print_error "没有可用的实体中介统一社会信用代码,跳过测试"
        return
    fi

    response=$(curl -s -X PUT "${BASE_URL}/ccdi/intermediary/entity" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d "{
            \"socialCreditCode\": \"$ENTITY_CREDIT_CODE\",
            \"enterpriseName\": \"测试中介公司(已修改)\",
            \"enterpriseType\": \"股份有限公司\",
            \"enterpriseNature\": \"国企\",
            \"industryClass\": \"金融\",
            \"industryName\": \"金融业\",
            \"registerAddress\": \"北京市海淀区新地址\",
            \"legalRepresentative\": \"李四\",
            \"shareholder1\": \"赵六\",
            \"shareholder2\": \"钱七\",
            \"remark\": \"修改后的测试数据\"
        }")

    echo "$response" | jq '.'
    code=$(echo "$response" | jq -r '.code')

    if [ "$code" == "200" ]; then
        print_success "修改实体中介成功"
    else
        print_error "修改实体中介失败: $(echo "$response" | jq -r '.msg')"
    fi
}

# 测试条件查询
test_query_by_type() {
    print_section "测试按中介类型查询"

    # 查询个人中介
    print_section "查询个人中介"
    response=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?intermediaryType=1&pageNum=1&pageSize=10" \
        -H "Authorization: Bearer $TOKEN")

    echo "$response" | jq '.'
    total=$(echo "$response" | jq -r '.total')
    print_success "查询到个人中介 $total 条"

    # 查询实体中介
    print_section "查询实体中介"
    response=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?intermediaryType=2&pageNum=1&pageSize=10" \
        -H "Authorization: Bearer $TOKEN")

    echo "$response" | jq '.'
    total=$(echo "$response" | jq -r '.total')
    print_success "查询到实体中介 $total 条"
}

# 主函数
main() {
    print_header "中介黑名单管理 API 测试开始"

    # 检查jq命令
    if ! command -v jq &> /dev/null; then
        print_error "jq命令未安装,请先安装: apt-get install jq 或 yum install jq"
        exit 1
    fi

    # 获取Token
    get_token

    # 执行测试
    test_list
    test_add_person
    test_add_entity
    test_get_person_detail
    test_get_entity_detail
    test_check_person_id
    test_check_social_credit_code
    test_edit_person
    test_edit_entity
    test_query_by_type

    print_header "测试完成"
    echo ""
    echo "注意事项:"
    echo "1. 请确保后端服务已启动 (${BASE_URL})"
    echo "2. 测试数据已创建,可手动清理"
    echo "3. 如需删除测试数据,请使用清理脚本"
    echo ""
}

# 执行主函数
main
