#!/bin/bash

################################################################################
# 中介黑名单管理测试数据清理脚本
# 功能: 清理测试脚本创建的测试数据
# 作者: 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获取成功"
    else
        print_error "Token获取失败"
        exit 1
    fi
}

# 查询测试数据
query_test_data() {
    print_section "查询测试数据"

    echo "查询测试个人中介:"
    PERSON_RESPONSE=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?name=测试中介个人&intermediaryType=1" \
        -H "Authorization: Bearer $TOKEN")

    echo "$PERSON_RESPONSE" | jq '.'

    PERSON_IDS=$(echo "$PERSON_RESPONSE" | jq -r '.rows[].bizId // empty')

    echo ""
    echo "查询测试实体中介:"
    ENTITY_RESPONSE=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?name=测试中介公司&intermediaryType=2" \
        -H "Authorization: Bearer $TOKEN")

    echo "$ENTITY_RESPONSE" | jq '.'

    ENTITY_IDS=$(echo "$ENTITY_RESPONSE" | jq -r '.rows[].bizId // empty')
}

# 删除测试数据
delete_test_data() {
    print_section "删除测试数据"

    # 删除测试个人中介
    if [ -n "$PERSON_IDS" ]; then
        echo "删除测试个人中介: $PERSON_IDS"
        DELETE_RESPONSE=$(curl -s -X DELETE "${BASE_URL}/ccdi/intermediary/${PERSON_IDS}" \
            -H "Authorization: Bearer $TOKEN")

        echo "$DELETE_RESPONSE" | jq '.'

        code=$(echo "$DELETE_RESPONSE" | jq -r '.code')
        if [ "$code" == "200" ]; then
            print_success "测试个人中介删除成功"
        else
            print_error "测试个人中介删除失败"
        fi
    else
        echo "没有找到测试个人中介数据"
    fi

    # 删除测试实体中介
    if [ -n "$ENTITY_IDS" ]; then
        echo ""
        echo "删除测试实体中介: $ENTITY_IDS"
        DELETE_RESPONSE=$(curl -s -X DELETE "${BASE_URL}/ccdi/intermediary/${ENTITY_IDS}" \
            -H "Authorization: Bearer $TOKEN")

        echo "$DELETE_RESPONSE" | jq '.'

        code=$(echo "$DELETE_RESPONSE" | jq -r '.code')
        if [ "$code" == "200" ]; then
            print_success "测试实体中介删除成功"
        else
            print_error "测试实体中介删除失败"
        fi
    else
        echo ""
        echo "没有找到测试实体中介数据"
    fi
}

# 验证删除结果
verify_deletion() {
    print_section "验证删除结果"

    echo "验证测试个人中介是否已删除:"
    VERIFY_PERSON=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?name=测试中介个人&intermediaryType=1" \
        -H "Authorization: Bearer $TOKEN")

    TOTAL=$(echo "$VERIFY_PERSON" | jq -r '.total')
    if [ "$TOTAL" == "0" ]; then
        print_success "测试个人中介已全部删除"
    else
        print_error "仍有 $TOTAL 条测试个人中介数据未删除"
    fi

    echo ""
    echo "验证测试实体中介是否已删除:"
    VERIFY_ENTITY=$(curl -s -X GET "${BASE_URL}/ccdi/intermediary/list?name=测试中介公司&intermediaryType=2" \
        -H "Authorization: Bearer $TOKEN")

    TOTAL=$(echo "$VERIFY_ENTITY" | jq -r '.total')
    if [ "$TOTAL" == "0" ]; then
        print_success "测试实体中介已全部删除"
    else
        print_error "仍有 $TOTAL 条测试实体中介数据未删除"
    fi
}

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

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

    # 获取Token
    get_token

    # 查询测试数据
    query_test_data

    # 删除测试数据
    delete_test_data

    # 验证删除结果
    verify_deletion

    print_header "清理完成"
}

# 执行主函数
main
