CREATE TABLE `approval_flows` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL COMMENT '流程名称', `type` varchar(100) NOT NULL COMMENT '业务类型标识,如 leave, reimbursement', `steps` json NOT NULL COMMENT '审批步骤配置', `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用', `timeout_hours` int(11) NOT NULL DEFAULT '0' COMMENT '实例级超时时间(小时),0为不超时', `timeout_action` varchar(20) NOT NULL DEFAULT 'auto_reject' COMMENT 'auto_approve | auto_reject', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk_flow_type` (`type`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COMMENT='审批流程定义'; CREATE TABLE `approval_instances` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `flow_type` varchar(100) NOT NULL COMMENT '关联 approval_flows.type', `biz_id` varchar(255) NOT NULL COMMENT '业务ID', `biz_type` varchar(100) NOT NULL COMMENT '业务标识/表名', `form_data` json NOT NULL COMMENT '提交表单快照', `initiator_id` bigint(20) unsigned NOT NULL COMMENT '发起人', `status` varchar(20) NOT NULL DEFAULT 'processing' COMMENT 'processing|approved|rejected|revoked|timed_out', `current_step` int(11) NOT NULL DEFAULT '0' COMMENT '当前步骤索引', `round` int(11) NOT NULL DEFAULT '1' COMMENT '当前轮次:1=首次,重提+1', `finished_at` timestamp NULL DEFAULT NULL, `deadline_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_flow_type` (`flow_type`), KEY `idx_biz` (`biz_type`,`biz_id`), KEY `idx_initiator` (`initiator_id`), KEY `idx_status_updated` (`status`,`updated_at`) ) ENGINE=InnoDB AUTO_INCREMENT=68 DEFAULT CHARSET=utf8mb4 COMMENT='审批实例'; CREATE TABLE `approval_records` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `instance_id` bigint(20) unsigned NOT NULL COMMENT 'approval_instances.id', `approver_id` bigint(20) unsigned NOT NULL COMMENT '审批人', `step_index` int(11) NOT NULL COMMENT '步骤索引', `round` int(11) NOT NULL DEFAULT '1' COMMENT '所属轮次', `action` varchar(20) DEFAULT NULL COMMENT 'approve|reject,待办时为NULL', `status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT 'pending|approved|rejected|skipped|timeout', `comment` text COMMENT '审批意见', `approved_at` timestamp NULL DEFAULT NULL COMMENT '审批时间/关闭时间', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk_instance_step_approver` (`instance_id`,`step_index`,`approver_id`), KEY `idx_instance_step_status` (`instance_id`,`step_index`,`status`), KEY `idx_approver` (`approver_id`), KEY `idx_instance_step_round` (`instance_id`,`step_index`,`round`) ) ENGINE=InnoDB AUTO_INCREMENT=105 DEFAULT CHARSET=utf8mb4 COMMENT='审批记录'; CREATE TABLE `approval_transfers` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `instance_id` bigint(20) unsigned NOT NULL, `step_index` int(11) NOT NULL, `record_id` bigint(20) unsigned NOT NULL COMMENT '转交后仍然有效的记录ID(若合并则为目标记录ID)', `from_user_id` bigint(20) unsigned NOT NULL, `to_user_id` bigint(20) unsigned NOT NULL, `reason` text, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_record` (`record_id`), KEY `idx_instance_step` (`instance_id`,`step_index`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审批转交日志'; CREATE TABLE `approval_ccs` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `instance_id` bigint(20) unsigned NOT NULL, `user_id` bigint(20) unsigned NOT NULL, `notified_at` timestamp NULL DEFAULT NULL, `read_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk_instance_user` (`instance_id`,`user_id`), KEY `idx_user` (`user_id`), KEY `idx_instance` (`instance_id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COMMENT='审批抄送'; CREATE TABLE `approval_discussions` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `instance_id` bigint(20) unsigned NOT NULL, `user_id` bigint(20) unsigned NOT NULL, `is_internal` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0=公开,1=仅审批人可见', `content` text NOT NULL, `attachments` json DEFAULT NULL, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_instance_id` (`instance_id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='审批讨论';