来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
66 lines
3.2 KiB
SQL
66 lines
3.2 KiB
SQL
-- 报价单重构:一张报价单含多商品,可绑定多客户;到期前提醒、未更新则顺延
|
|
|
|
CREATE TABLE IF NOT EXISTS customer_quote_sheets (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
quote_sheet_code VARCHAR(50) UNIQUE NOT NULL,
|
|
title VARCHAR(200),
|
|
version_no INTEGER NOT NULL DEFAULT 1,
|
|
quote_cycle VARCHAR(20),
|
|
effective_at TIMESTAMPTZ,
|
|
expires_at TIMESTAMPTZ,
|
|
status INTEGER NOT NULL DEFAULT 0,
|
|
approval_status INTEGER NOT NULL DEFAULT 10,
|
|
approval_remark TEXT,
|
|
approved_by UUID,
|
|
approved_at TIMESTAMPTZ,
|
|
reminder_sent_at TIMESTAMPTZ,
|
|
rollover_count INTEGER NOT NULL DEFAULT 0,
|
|
parent_sheet_code VARCHAR(50),
|
|
branch_code VARCHAR(50) REFERENCES branches(branch_code),
|
|
created_by UUID,
|
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
updated_at TIMESTAMPTZ DEFAULT NOW()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS customer_quote_sheet_items (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
quote_sheet_code VARCHAR(50) NOT NULL REFERENCES customer_quote_sheets(quote_sheet_code) ON DELETE CASCADE,
|
|
sku_code VARCHAR(50) NOT NULL REFERENCES skus(sku_code),
|
|
sku_name VARCHAR(200),
|
|
unit VARCHAR(20),
|
|
price DECIMAL(10,2) NOT NULL,
|
|
sort_order INTEGER NOT NULL DEFAULT 0,
|
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS customer_quote_sheet_customers (
|
|
quote_sheet_code VARCHAR(50) NOT NULL REFERENCES customer_quote_sheets(quote_sheet_code) ON DELETE CASCADE,
|
|
customer_code VARCHAR(50) NOT NULL REFERENCES customers(customer_code),
|
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
PRIMARY KEY (quote_sheet_code, customer_code)
|
|
);
|
|
|
|
ALTER TABLE customer_quote_versions ADD COLUMN IF NOT EXISTS quote_sheet_code VARCHAR(50);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_quote_sheets_active ON customer_quote_sheets(status, approval_status, expires_at DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_quote_sheets_branch ON customer_quote_sheets(branch_code, version_no DESC);
|
|
CREATE INDEX IF NOT EXISTS idx_quote_sheet_items_sheet ON customer_quote_sheet_items(quote_sheet_code, sort_order);
|
|
CREATE INDEX IF NOT EXISTS idx_quote_sheet_customers_code ON customer_quote_sheet_customers(customer_code);
|
|
CREATE INDEX IF NOT EXISTS idx_quote_versions_sheet ON customer_quote_versions(quote_sheet_code);
|
|
|
|
ALTER TABLE customer_quote_sheets ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE customer_quote_sheet_items ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE customer_quote_sheet_customers ENABLE ROW LEVEL SECURITY;
|
|
|
|
DO $$ BEGIN
|
|
IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE tablename = 'customer_quote_sheets' AND policyname = 'anon_all_quote_sheets') THEN
|
|
CREATE POLICY anon_all_quote_sheets ON customer_quote_sheets FOR ALL USING (true) WITH CHECK (true);
|
|
END IF;
|
|
IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE tablename = 'customer_quote_sheet_items' AND policyname = 'anon_all_quote_sheet_items') THEN
|
|
CREATE POLICY anon_all_quote_sheet_items ON customer_quote_sheet_items FOR ALL USING (true) WITH CHECK (true);
|
|
END IF;
|
|
IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE tablename = 'customer_quote_sheet_customers' AND policyname = 'anon_all_quote_sheet_customers') THEN
|
|
CREATE POLICY anon_all_quote_sheet_customers ON customer_quote_sheet_customers FOR ALL USING (true) WITH CHECK (true);
|
|
END IF;
|
|
END $$;
|