INSERT INTO public.dms_template_upgrade_notifications (tenant_id,task_id,audit_id,template_id,document_no,document_name,version_label,actor_id) SELECT a.f_tenant_id,a.f_flow_task_id,a.f_id,(CASE WHEN a.wenjian IS JSON ARRAY THEN a.wenjian::jsonb ELSE '[]'::jsonb END)->0->>'laiyuan_muban_id', a.wenjian_bianhao,a.wenjian_mingcheng,a.banbenhao, COALESCE(NULLIF(t.f_last_modify_user_id,''),t.f_creator_user_id) FROM public.dms_wenjian_xiuding_shenhe a JOIN public.workflow_task t ON t.f_tenant_id=a.f_tenant_id AND t.f_id=a.f_flow_task_id AND (t.f_flow_id=a.f_flow_id OR t.f_template_id=a.f_flow_id) WHERE a.f_delete_mark IS NULL AND a.f_flow_state=2 AND a.biangeng_fangshi='shengji' AND t.f_delete_mark IS NULL AND t.f_status=2 AND t.f_end_time IS NOT NULL AND CASE WHEN NULLIF(a.wenjian,'') IS NULL THEN false ELSE jsonb_typeof((CASE WHEN a.wenjian IS JSON ARRAY THEN a.wenjian::jsonb ELSE '[]'::jsonb END))='array' AND jsonb_array_length((CASE WHEN a.wenjian IS JSON ARRAY THEN a.wenjian::jsonb ELSE '[]'::jsonb END))=1 AND NULLIF((CASE WHEN a.wenjian IS JSON ARRAY THEN a.wenjian::jsonb ELSE '[]'::jsonb END)->0->>'laiyuan_muban_id','') IS NOT NULL END AND NOT EXISTS (SELECT 1 FROM public.dms_template_upgrade_notifications n WHERE n.tenant_id=a.f_tenant_id AND n.task_id=a.f_flow_task_id) ORDER BY t.f_end_time,a.f_flow_task_id LIMIT 100 ON CONFLICT (tenant_id,task_id) DO NOTHING UPDATE public.dms_template_upgrade_notifications SET lease_token=#{token}, lease_until=CURRENT_TIMESTAMP+INTERVAL '5 minutes',attempts=attempts+1 WHERE tenant_id=#{event.tenantId} AND task_id=#{event.taskId} AND completed_at IS NULL AND next_attempt_at <= CURRENT_TIMESTAMP AND (lease_until IS NULL OR lease_until <= CURRENT_TIMESTAMP) UPDATE public.dms_template_upgrade_notifications SET lease_token=NULL,lease_until=NULL, last_error=#{error,jdbcType=VARCHAR},outcome=#{outcome,jdbcType=VARCHAR}, completed_at=CASE WHEN #{error,jdbcType=VARCHAR} IS NULL THEN CURRENT_TIMESTAMP ELSE NULL END, next_attempt_at=CURRENT_TIMESTAMP+INTERVAL '1 minute'*LEAST(60,GREATEST(1,attempts)) WHERE tenant_id=#{event.tenantId} AND task_id=#{event.taskId} AND lease_token=#{token}