```sql

-- 传入你的 taskId
set @taskId = 'f5e730e3bfaf57ce00af7c0f7228958a';

start transaction;

-- 1. 归档到历史表
insert into sys_flow_task_his (his_id,
                               task_id,
                               instance_id,
                               node_code,
                               node_name,
                               approver_id,
                               status,
                               comment,
                               audit_time,
                               company_id,
                               is_read,
                               create_by,
                               create_time,
                               update_by,
                               update_time)
select replace(uuid(), '-', '')                      as his_id,
       t.task_id,
       t.instance_id,
       t.node_code,
       t.node_name,
       t.approver_id,
       case
           when i.status = 1 then 1
           when i.status in (2, 9) then 2
           else 2
           end                                       as status,
       '手工归档待办'                                as comment,
       coalesce(i.finish_time, t.update_time, now()) as audit_time,
       t.company_id,
       t.is_read,
       t.create_by,
       t.create_time,
       t.update_by,
       t.update_time
from sys_flow_task t
         left join sys_flow_instance i
                   on i.instance_id = t.instance_id
where t.task_id = @taskId
  and not exists (select 1
                  from sys_flow_task_his th
                  where th.task_id = t.task_id
                    and th.instance_id = t.instance_id);

-- 2. 删除当前待办
delete
from sys_flow_task
where task_id = @taskId
  and exists (select 1
              from sys_flow_task_his th
              where th.task_id = @taskId
                and th.instance_id = sys_flow_task.instance_id);

commit;

```
