iloom-flatten/migrations/20260530_114519_fix_auto_sync_plan_trigger.sql

31 lines
886 B
MySQL
Raw Permalink Normal View History

-- ============================================
-- 此文件由 Meoo Cloud 自动生成,请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================
CREATE OR REPLACE FUNCTION public.auto_sync_plan_to_factory()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
DECLARE
v_factory_id UUID;
v_factory_type factory_type;
BEGIN
FOR v_factory_id, v_factory_type IN
SELECT DISTINCT pf.factory_id, pf.factory_type
FROM plan_factories pf
JOIN production_plans pp ON pp.id = pf.plan_id
WHERE pp.purchaser_id = NEW.purchaser_id
AND pf.factory_type IN ('textile', 'washing')
LOOP
BEGIN
INSERT INTO plan_factories (plan_id, factory_id, factory_type)
VALUES (NEW.id, v_factory_id, v_factory_type);
EXCEPTION WHEN unique_violation THEN
NULL;
END;
END LOOP;
RETURN NEW;
END; $function$