iloom-flatten/migrations/20260524_144737_create_washing_plans_table.sql

67 lines
2.5 KiB
MySQL
Raw Permalink Normal View History

-- ============================================
-- 此文件由 Meoo Cloud 自动生成,请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================
-- 创建水洗计划表
CREATE TABLE washing_plans (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
plan_code TEXT UNIQUE NOT NULL,
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
product_id UUID NOT NULL REFERENCES products(id) ON DELETE CASCADE,
washing_factory_id UUID REFERENCES companies(id) ON DELETE SET NULL,
-- 计划信息
planned_meters DECIMAL(12, 2) NOT NULL DEFAULT 0,
washing_price DECIMAL(10, 2) NOT NULL DEFAULT 0,
estimated_shrinkage_rate DECIMAL(5, 2) NOT NULL DEFAULT 0,
-- 自动计算字段
estimated_washed_meters DECIMAL(12, 2) GENERATED ALWAYS AS (planned_meters * (1 - estimated_shrinkage_rate / 100)) STORED,
estimated_total_cost DECIMAL(12, 2) GENERATED ALWAYS AS (planned_meters * washing_price) STORED,
-- 实际完成数据
actual_shrinkage_rate DECIMAL(5, 2),
actual_washed_meters DECIMAL(12, 2),
actual_total_cost DECIMAL(12, 2),
washing_date DATE,
-- 状态
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'processing', 'completed')),
-- 备注
notes TEXT,
-- 元数据
created_by UUID REFERENCES profiles(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- 创建索引
CREATE INDEX idx_washing_plans_company ON washing_plans(company_id);
CREATE INDEX idx_washing_plans_product ON washing_plans(product_id);
CREATE INDEX idx_washing_plans_factory ON washing_plans(washing_factory_id);
CREATE INDEX idx_washing_plans_status ON washing_plans(status);
-- 启用 RLS
ALTER TABLE washing_plans ENABLE ROW LEVEL SECURITY;
-- RLS 策略:公司成员可查看本公司水洗计划
CREATE POLICY company_select_washing_plans ON washing_plans
FOR SELECT USING (company_id = get_user_master_company_id());
-- RLS 策略:公司成员可创建本公司水洗计划
CREATE POLICY company_insert_washing_plans ON washing_plans
FOR INSERT WITH CHECK (company_id = get_user_master_company_id());
-- RLS 策略:公司成员可更新本公司水洗计划
CREATE POLICY company_update_washing_plans ON washing_plans
FOR UPDATE USING (company_id = get_user_master_company_id());
-- RLS 策略:公司成员可删除本公司水洗计划
CREATE POLICY company_delete_washing_plans ON washing_plans
FOR DELETE USING (company_id = get_user_master_company_id());