CREATE TABLE IF NOT EXISTS mci_sync_task (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    tenant_code VARCHAR(64) NOT NULL COMMENT '所属租户',
    task_type VARCHAR(32) NOT NULL COMMENT '任务类型：enterprise_contacts / account_contacts / account_conversations',
    channel_type VARCHAR(32) NOT NULL COMMENT '渠道：wecom / wechat_personal / dingtalk / feishu',
    target_id BIGINT COMMENT '关联目标 ID：主体 ID 或账号 ID',
    target_code VARCHAR(128) COMMENT '关联目标编码：如 userId',
    task_status TINYINT NOT NULL DEFAULT 0 COMMENT '任务状态：0待执行 1执行中 2成功 3失败',
    synced_count INT NOT NULL DEFAULT 0 COMMENT '已同步数量',
    failed_count INT NOT NULL DEFAULT 0 COMMENT '失败数量',
    error_message VARCHAR(1024) COMMENT '错误信息',
    retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
    finish_time DATETIME COMMENT '完成时间',
    create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted TINYINT NOT NULL DEFAULT 0,
    KEY idx_tenant_status (tenant_code, task_status),
    KEY idx_target (task_type, target_id),
    KEY idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='同步任务表';
