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


-- 生产计划表
CREATE TABLE public.production_plans (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 plan_code TEXT UNIQUE NOT NULL,
 product_name TEXT NOT NULL,
 color TEXT NOT NULL,
 fabric_code TEXT NOT NULL,
 color_code TEXT NOT NULL DEFAULT '01',
 remark TEXT,
 purchaser_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
 target_quantity INTEGER NOT NULL DEFAULT 0,
 completed_quantity INTEGER NOT NULL DEFAULT 0,
 yarn_usage_per_meter DECIMAL DEFAULT 0,
 production_price DECIMAL DEFAULT 0,
 status public.plan_status NOT NULL DEFAULT 'pending',
 start_time TIMESTAMPTZ,
 created_by UUID REFERENCES auth.users(id) ON DELETE SET NULL,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 计划与工厂关联表
CREATE TABLE public.plan_factories (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 plan_id UUID NOT NULL REFERENCES public.production_plans(id) ON DELETE CASCADE,
 factory_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
 factory_type public.factory_type NOT NULL,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 UNIQUE(plan_id, factory_id)
);

-- 纱线配比表
CREATE TABLE public.yarn_ratios (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 plan_id UUID NOT NULL REFERENCES public.production_plans(id) ON DELETE CASCADE,
 yarn_name TEXT NOT NULL,
 ratio DECIMAL NOT NULL DEFAULT 1,
 amount_per_meter DECIMAL NOT NULL DEFAULT 0,
 total_amount DECIMAL NOT NULL DEFAULT 0,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

