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


CREATE TABLE public.product_outbound_records (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 product_id UUID NOT NULL REFERENCES public.products(id) ON DELETE CASCADE,
 company_id UUID REFERENCES public.companies(id) ON DELETE CASCADE,
 batch_no TEXT NOT NULL DEFAULT '',
 rolls INTEGER NOT NULL DEFAULT 0,
 meters NUMERIC NOT NULL DEFAULT 0,
 notes TEXT,
 operator_id UUID REFERENCES auth.users(id) ON DELETE SET NULL,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 -- 关联水洗计划（如果是自动出库）
 washing_plan_id UUID,
 -- 出库类型: manual(手动), auto_washing(水洗计划自动出库)
 outbound_type TEXT NOT NULL DEFAULT 'manual'
);

-- 创建索引
CREATE INDEX idx_product_outbound_product_id ON public.product_outbound_records(product_id);
CREATE INDEX idx_product_outbound_company_id ON public.product_outbound_records(company_id);
CREATE INDEX idx_product_outbound_created_at ON public.product_outbound_records(created_at DESC);
CREATE INDEX idx_product_outbound_washing_plan ON public.product_outbound_records(washing_plan_id);

-- 启用 RLS
ALTER TABLE public.product_outbound_records ENABLE ROW LEVEL SECURITY;

-- RLS 策略
CREATE POLICY company_select_outbound ON public.product_outbound_records
 FOR SELECT USING (company_id = get_user_master_company_id());

CREATE POLICY company_insert_outbound ON public.product_outbound_records
 FOR INSERT WITH CHECK (company_id = get_user_master_company_id());

CREATE POLICY company_delete_outbound ON public.product_outbound_records
 FOR DELETE USING (company_id = get_user_master_company_id());

