feat(consignment): 寄卖配置、内部匹配与打手端升级

This commit is contained in:
2026-11-05 15:39:40 +08:00
parent f94b914ab1
commit 2ef16ddcc7
34 changed files with 3705 additions and 350 deletions
@@ -0,0 +1,167 @@
-- 129_consignment_matching.sql
-- 寄卖配置化 + 库存数量/售价 + 内部匹配/自动匹配 + 拼单库存分配。
-- 全部语句幂等,可安全重跑;不改动历史迁移。
-- ===========================================================================
-- 1. 分类级寄卖配置(参考 order_service_profiles 的草稿/发布版本模式)
-- ===========================================================================
CREATE TABLE IF NOT EXISTS consignment_profiles (
id BIGSERIAL PRIMARY KEY,
category_id BIGINT NOT NULL REFERENCES work_categories(id) ON DELETE CASCADE,
name TEXT NOT NULL,
title TEXT NOT NULL DEFAULT '',
description TEXT NOT NULL DEFAULT '',
tab_title TEXT NOT NULL DEFAULT '',
config_json JSONB NOT NULL DEFAULT '{}'::jsonb,
status TEXT NOT NULL DEFAULT 'draft',
current_version_id BIGINT,
created_by_admin_user_id BIGINT REFERENCES admin_users(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT consignment_profiles_status_check
CHECK (status IN ('draft', 'published', 'archived'))
);
CREATE UNIQUE INDEX IF NOT EXISTS uq_consignment_profiles_category
ON consignment_profiles (category_id);
CREATE INDEX IF NOT EXISTS idx_consignment_profiles_status
ON consignment_profiles (status);
CREATE TABLE IF NOT EXISTS consignment_profile_versions (
id BIGSERIAL PRIMARY KEY,
profile_id BIGINT NOT NULL REFERENCES consignment_profiles(id) ON DELETE CASCADE,
version_no INTEGER NOT NULL,
config_json JSONB NOT NULL DEFAULT '{}'::jsonb,
published_by_admin_user_id BIGINT REFERENCES admin_users(id) ON DELETE SET NULL,
published_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT uq_consignment_profile_versions_no UNIQUE (profile_id, version_no)
);
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'fk_consignment_profiles_current_version'
) THEN
ALTER TABLE consignment_profiles
ADD CONSTRAINT fk_consignment_profiles_current_version
FOREIGN KEY (current_version_id)
REFERENCES consignment_profile_versions(id) ON DELETE SET NULL;
END IF;
END $$;
-- ===========================================================================
-- 2. 提报与库存:配置快照、数量、寄卖售价
-- ===========================================================================
ALTER TABLE consignment_submissions
ADD COLUMN IF NOT EXISTS profile_id BIGINT REFERENCES consignment_profiles(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS profile_version_id BIGINT
REFERENCES consignment_profile_versions(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS profile_snapshot_json JSONB,
ADD COLUMN IF NOT EXISTS quantity INTEGER NOT NULL DEFAULT 1,
ADD COLUMN IF NOT EXISTS requested_sale_price_fen BIGINT NOT NULL DEFAULT 0;
ALTER TABLE consignment_inventories
ADD COLUMN IF NOT EXISTS profile_id BIGINT REFERENCES consignment_profiles(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS profile_version_id BIGINT
REFERENCES consignment_profile_versions(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS profile_snapshot_json JSONB,
ADD COLUMN IF NOT EXISTS quantity INTEGER NOT NULL DEFAULT 1,
ADD COLUMN IF NOT EXISTS remaining_quantity INTEGER NOT NULL DEFAULT 1,
ADD COLUMN IF NOT EXISTS sale_price_fen BIGINT NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS listed_at TIMESTAMPTZ,
ADD COLUMN IF NOT EXISTS price_updated_at TIMESTAMPTZ;
-- 存量库存:一件一条,剩余数量 = 总数量,寄卖售价回填收购价(后续可单独改价)。
UPDATE consignment_inventories
SET remaining_quantity = GREATEST(0, COALESCE(quantity, 1)),
sale_price_fen = CASE WHEN COALESCE(sale_price_fen, 0) = 0 THEN COALESCE(cost_price_fen, 0) ELSE sale_price_fen END,
listed_at = COALESCE(listed_at, inbound_at)
WHERE remaining_quantity IS DISTINCT FROM GREATEST(0, COALESCE(quantity, 1))
OR listed_at IS NULL
OR (COALESCE(sale_price_fen, 0) = 0 AND COALESCE(cost_price_fen, 0) > 0);
CREATE INDEX IF NOT EXISTS idx_consignment_inventories_profile_matching
ON consignment_inventories (profile_id, status, allow_matching, remaining_quantity)
WHERE status = 'in_stock';
CREATE INDEX IF NOT EXISTS idx_consignment_inventories_worker_active
ON consignment_inventories (worker_id, status, remaining_quantity)
WHERE worker_id IS NOT NULL;
-- ===========================================================================
-- 3. 工单:寄卖配置、内部匹配截止、大厅可见时间
-- ===========================================================================
ALTER TABLE work_orders
ADD COLUMN IF NOT EXISTS consignment_profile_id BIGINT
REFERENCES consignment_profiles(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS internal_match_deadline_at TIMESTAMPTZ,
ADD COLUMN IF NOT EXISTS hall_visible_at TIMESTAMPTZ,
ADD COLUMN IF NOT EXISTS internal_match_attempts INTEGER NOT NULL DEFAULT 0;
CREATE INDEX IF NOT EXISTS idx_work_orders_internal_matching
ON work_orders (internal_match_deadline_at)
WHERE status = 'internal_matching';
CREATE INDEX IF NOT EXISTS idx_work_orders_hall_visible
ON work_orders (hall_visible_at)
WHERE hall_visible_at IS NOT NULL;
-- ===========================================================================
-- 4. 库存-工单分配(一个工单/份额可占用一条或多条库存)
-- ===========================================================================
CREATE TABLE IF NOT EXISTS consignment_inventory_allocations (
id BIGSERIAL PRIMARY KEY,
inventory_id BIGINT NOT NULL REFERENCES consignment_inventories(id) ON DELETE CASCADE,
work_order_id BIGINT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE,
work_order_share_id BIGINT REFERENCES work_order_shares(id) ON DELETE SET NULL,
quantity INTEGER NOT NULL DEFAULT 1,
unit_price_fen BIGINT NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'reserved',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT consignment_inventory_allocations_status_check
CHECK (status IN ('reserved', 'consumed', 'released')),
CONSTRAINT consignment_inventory_allocations_quantity_check
CHECK (quantity > 0)
);
CREATE INDEX IF NOT EXISTS idx_consignment_allocations_work_order
ON consignment_inventory_allocations (work_order_id, status);
CREATE INDEX IF NOT EXISTS idx_consignment_allocations_inventory
ON consignment_inventory_allocations (inventory_id, status);
CREATE INDEX IF NOT EXISTS idx_consignment_allocations_share
ON consignment_inventory_allocations (work_order_share_id)
WHERE work_order_share_id IS NOT NULL;
ALTER TABLE work_order_shares
ADD COLUMN IF NOT EXISTS consignment_inventory_id BIGINT
REFERENCES consignment_inventories(id) ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS idx_work_order_shares_consignment_inventory
ON work_order_shares (consignment_inventory_id)
WHERE consignment_inventory_id IS NOT NULL;
-- ===========================================================================
-- 5. 注释
-- ===========================================================================
COMMENT ON TABLE consignment_profiles IS
'分类级寄卖配置:打手端 tab、发布字段、价格、保证金、服务费、数量限制、匹配与超时策略';
COMMENT ON TABLE consignment_profile_versions IS
'寄卖配置发布版本快照;提报/库存/工单固化 profile_version_id,后续改配置不影响在途数据';
COMMENT ON COLUMN consignment_inventories.quantity IS
'该寄卖批次总数量;同批次共享卖家账号资料与截图';
COMMENT ON COLUMN consignment_inventories.remaining_quantity IS
'剩余可匹配数量;部分匹配后继续保留 in_stock,扣到 0 才 consumed';
COMMENT ON COLUMN consignment_inventories.sale_price_fen IS
'寄卖售价(分):匹配工单时的结算价,可由打手/后台修改';
COMMENT ON COLUMN consignment_inventories.listed_at IS
'开始计算未售出时长的时间点(审核入库/发布)';
COMMENT ON COLUMN work_orders.internal_match_deadline_at IS
'内部匹配截止时间;超过后调度任务把工单发回大厅';
COMMENT ON COLUMN work_orders.hall_visible_at IS
'大厅可见时间;NULL = 立即可见,未来时间 = 延迟进入大厅';
COMMENT ON TABLE consignment_inventory_allocations IS
'库存占用明细:整单履约一条,拼单时每个份额一条;释放时回补 remaining_quantity';
COMMENT ON COLUMN work_order_shares.consignment_inventory_id IS
'拼单份额对应的寄卖库存;非寄卖份额为 NULL';