-- ERP API 管理平台数据库表创建脚本 (SQL Server 版本)
-- 执行顺序：先创建 erp_api_config，再创建 erp_api_param，最后创建 erp_api_stats

-- 1. API 配置主表
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='erp_api_config' AND xtype='U')
BEGIN
    CREATE TABLE erp_api_config (
        api_id BIGINT IDENTITY(1,1) PRIMARY KEY,
        api_name NVARCHAR(100) NOT NULL,
        api_path NVARCHAR(200) NOT NULL,
        api_method NVARCHAR(10) NOT NULL DEFAULT 'GET',
        api_desc NVARCHAR(500),
        api_version NVARCHAR(10) DEFAULT 'v1',

        -- 数据源配置
        data_source NVARCHAR(50) DEFAULT 'erp',

        -- SQL配置
        sql_template NVARCHAR(MAX) NOT NULL,
        result_type NVARCHAR(20) NOT NULL DEFAULT 'LIST',

        -- 分页配置
        support_pagination TINYINT DEFAULT 0,
        page_param_name NVARCHAR(50) DEFAULT 'pageNum',
        size_param_name NVARCHAR(50) DEFAULT 'pageSize',

        -- 权限配置
        require_auth TINYINT DEFAULT 0,
        permission_code NVARCHAR(100),

        -- 缓存配置
        enable_cache TINYINT DEFAULT 0,
        cache_key_template NVARCHAR(200),
        cache_ttl INT DEFAULT 300,

        -- 来源表信息
        source_table NVARCHAR(100),
        source_table_comment NVARCHAR(500),

        -- 状态
        status TINYINT DEFAULT 1,
        create_time DATETIME DEFAULT GETDATE(),
        update_time DATETIME DEFAULT GETDATE(),
        create_by NVARCHAR(50),
        update_by NVARCHAR(50),
        remark NVARCHAR(500)
    );

    PRINT 'Table erp_api_config created successfully';
END
ELSE
BEGIN
    PRINT 'Table erp_api_config already exists';
END
GO

-- 创建索引
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name='idx_api_path_method' AND object_id=OBJECT_ID('erp_api_config'))
    CREATE INDEX idx_api_path_method ON erp_api_config(api_path, api_method, api_version);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name='idx_status' AND object_id=OBJECT_ID('erp_api_config'))
    CREATE INDEX idx_status ON erp_api_config(status);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name='idx_source_table' AND object_id=OBJECT_ID('erp_api_config'))
    CREATE INDEX idx_source_table ON erp_api_config(source_table);
GO

-- 2. API 参数配置表
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='erp_api_param' AND xtype='U')
BEGIN
    CREATE TABLE erp_api_param (
        param_id BIGINT IDENTITY(1,1) PRIMARY KEY,
        api_id BIGINT NOT NULL,

        -- 参数基本信息
        param_name NVARCHAR(100) NOT NULL,
        param_desc NVARCHAR(500),
        param_type NVARCHAR(20) NOT NULL DEFAULT 'String',

        -- 参数位置
        param_position NVARCHAR(20) NOT NULL DEFAULT 'QUERY',

        -- 参数验证
        is_required TINYINT DEFAULT 0,
        default_value NVARCHAR(200),

        -- SQL映射
        sql_param_name NVARCHAR(100),

        -- 排序
        sort INT DEFAULT 0,
        create_time DATETIME DEFAULT GETDATE(),
        update_time DATETIME DEFAULT GETDATE()
    );

    PRINT 'Table erp_api_param created successfully';
END
ELSE
BEGIN
    PRINT 'Table erp_api_param already exists';
END
GO

-- 创建索引和外键
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name='idx_api_id' AND object_id=OBJECT_ID('erp_api_param'))
    CREATE INDEX idx_api_id ON erp_api_param(api_id);

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE name='fk_api_param_api')
BEGIN
    ALTER TABLE erp_api_param ADD CONSTRAINT fk_api_param_api
        FOREIGN KEY (api_id) REFERENCES erp_api_config(api_id) ON DELETE CASCADE;
END
GO

-- 3. API 调用统计表
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='erp_api_stats' AND xtype='U')
BEGIN
    CREATE TABLE erp_api_stats (
        stats_id BIGINT IDENTITY(1,1) PRIMARY KEY,
        api_id BIGINT NOT NULL,

        -- 调用信息
        call_time DATETIME NOT NULL,
        call_params NVARCHAR(MAX),
        response_time INT,
        call_status NVARCHAR(10),

        -- 错误信息
        error_message NVARCHAR(MAX),
        error_stack NVARCHAR(MAX),

        -- 客户端信息
        client_ip NVARCHAR(50),
        user_id NVARCHAR(50),

        create_time DATETIME DEFAULT GETDATE()
    );

    PRINT 'Table erp_api_stats created successfully';
END
ELSE
BEGIN
    PRINT 'Table erp_api_stats already exists';
END
GO

-- 创建索引
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name='idx_api_id_time' AND object_id=OBJECT_ID('erp_api_stats'))
    CREATE INDEX idx_api_id_time ON erp_api_stats(api_id, call_time);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name='idx_call_status' AND object_id=OBJECT_ID('erp_api_stats'))
    CREATE INDEX idx_call_status ON erp_api_stats(call_status);

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name='idx_response_time' AND object_id=OBJECT_ID('erp_api_stats'))
    CREATE INDEX idx_response_time ON erp_api_stats(response_time);
GO

PRINT 'All ERP API management tables created successfully';