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


CREATE TABLE IF NOT EXISTS public.yarn_stock_records (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 company_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
 yarn_stock_id UUID REFERENCES public.yarn_stock(id) ON DELETE SET NULL,
 plan_id UUID REFERENCES public.production_plans(id) ON DELETE SET NULL,
 record_type TEXT NOT NULL CHECK (record_type IN ('inbound', 'outbound')),
 quantity DECIMAL NOT NULL DEFAULT 0,
 unit TEXT NOT NULL DEFAULT 'kg',
 batch_no TEXT,
 notes TEXT,
 operator_id UUID REFERENCES auth.users(id) ON DELETE SET NULL,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_yarn_stock_records_company_id ON public.yarn_stock_records(company_id);
CREATE INDEX IF NOT EXISTS idx_yarn_stock_records_yarn_stock_id ON public.yarn_stock_records(yarn_stock_id);
CREATE INDEX IF NOT EXISTS idx_yarn_stock_records_plan_id ON public.yarn_stock_records(plan_id);
CREATE INDEX IF NOT EXISTS idx_yarn_stock_records_created_at ON public.yarn_stock_records(created_at DESC);

