-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


-- plan_factories 表 RLS 策略
-- 工厂可以查看和管理关联到自己的计划
CREATE POLICY factory_select_plan_factories ON public.plan_factories
FOR SELECT USING (
 factory_id = public.get_user_master_company_id()
);

-- 采购商可以查看自己计划的工厂关联
CREATE POLICY purchaser_select_plan_factories ON public.plan_factories
FOR SELECT USING (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

-- 采购商可以创建和删除工厂关联
CREATE POLICY purchaser_insert_plan_factories ON public.plan_factories
FOR INSERT WITH CHECK (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

CREATE POLICY purchaser_delete_plan_factories ON public.plan_factories
FOR DELETE USING (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

