-- ============================================
-- 修复水洗计划表RLS策略，让水洗厂能访问关联计划
-- ============================================

-- 删除旧的SELECT策略
DROP POLICY IF EXISTS company_select_washing_plans ON washing_plans;

-- 创建新的SELECT策略：允许创建公司或关联的水洗厂查看
CREATE POLICY company_select_washing_plans ON washing_plans
  FOR SELECT USING (
    company_id = get_user_master_company_id() 
    OR washing_factory_id = get_user_master_company_id()
  );

-- 更新策略：只允许创建公司更新
DROP POLICY IF EXISTS company_update_washing_plans ON washing_plans;
CREATE POLICY company_update_washing_plans ON washing_plans
  FOR UPDATE USING (
    company_id = get_user_master_company_id()
    OR washing_factory_id = get_user_master_company_id()
  );

-- 添加注释说明
COMMENT ON POLICY company_select_washing_plans ON washing_plans IS '允许采购商(创建公司)或关联的水洗厂查看计划';
COMMENT ON POLICY company_update_washing_plans ON washing_plans IS '允许采购商(创建公司)或关联的水洗厂更新计划';
