-- ============================================
-- 审核流服务数据库初始化脚本
-- 数据库: erp_db
-- ============================================

-- 创建审核规则表
CREATE TABLE IF NOT EXISTS `audit_rules` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '规则ID',
    `tenant_id` BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    `name` VARCHAR(100) NOT NULL COMMENT '规则名称',
    `type` VARCHAR(20) NOT NULL COMMENT '规则类型：order-订单审核，refund-退款审核',
    `conditions` TEXT COMMENT '条件配置（JSON数组）',
    `actions` TEXT COMMENT '动作配置（JSON数组）',
    `priority` INT NOT NULL DEFAULT 9999 COMMENT '优先级（数字越小优先级越高）',
    `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用：0-禁用，1-启用',
    `description` VARCHAR(500) DEFAULT NULL COMMENT '描述',
    `created_by` BIGINT NOT NULL COMMENT '创建人',
    `updated_by` BIGINT NOT NULL COMMENT '更新人',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` DATETIME DEFAULT NULL COMMENT '删除时间（软删除标记）',
    PRIMARY KEY (`id`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_type` (`type`),
    KEY `idx_is_active` (`is_active`),
    KEY `idx_priority` (`priority`),
    KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审核规则表';

-- 创建审核日志表
CREATE TABLE IF NOT EXISTS `audit_logs` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '日志ID',
    `tenant_id` BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    `user_id` BIGINT DEFAULT NULL COMMENT '用户ID',
    `user_name` VARCHAR(100) DEFAULT NULL COMMENT '用户名称',
    `module` VARCHAR(50) NOT NULL COMMENT '模块名称',
    `action` VARCHAR(50) NOT NULL COMMENT '操作类型：Create/Update/Delete/Approve/Reject',
    `target_type` VARCHAR(50) NOT NULL COMMENT '目标类型',
    `target_id` BIGINT NOT NULL COMMENT '目标ID',
    `old_values` TEXT COMMENT '修改前的值（JSON）',
    `new_values` TEXT COMMENT '修改后的值（JSON）',
    `ip` VARCHAR(50) DEFAULT NULL COMMENT 'IP地址',
    `user_agent` VARCHAR(500) DEFAULT NULL COMMENT 'User-Agent',
    `request_id` VARCHAR(100) DEFAULT NULL COMMENT '请求ID',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_module` (`module`),
    KEY `idx_action` (`action`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_target_type_id` (`target_type`, `target_id`),
    KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审核日志表';

-- ============================================
-- 初始化示例数据（可选）
-- ============================================

-- 插入示例规则（仅租户0可见，生产环境请根据需要调整）
INSERT INTO `audit_rules` (`tenant_id`, `name`, `type`, `conditions`, `actions`, `priority`, `is_active`, `description`, `created_by`, `updated_by`) VALUES
(0, '高额订单自动审核', 'order', '[{"field":"total_amount","operator":"gte","value":10000}]', '[{"type":"pass","params":{}}]', 1, 1, '订单金额大于等于10000元自动通过', 1, 1),
(0, '高额退款需审批', 'refund', '[{"field":"refund_amount","operator":"gt","value":5000}]', '[{"type":"escalate","params":{"approver_role":"manager"}}]', 1, 1, '退款金额大于5000元需经理审批', 1, 1);
