refactor(distribution): 重写分销迁移并落地利润百分比分成与货架比例
将 000064-000078 合并为 064/065/066,默认规则改为收号 35%、卖出 40%、服务费 25%。 货架调价改为增加分站 1 元兑换哈夫币比例。sql-guard 仅保护 main 已发布迁移。
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 分销站点、规则、分账、结算,以及主站表上的租户隔离列。
|
||||
-- 分成字段存百分数:收号 35、卖出 40、服务费 25。
|
||||
-- 货架调价存 shelf_ratio_delta(分站 1 元兑换哈夫币比例相对主站的增量)。
|
||||
|
||||
CREATE TABLE distribution_sites (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
site_code VARCHAR(64) NOT NULL,
|
||||
@@ -7,6 +11,11 @@ CREATE TABLE distribution_sites (
|
||||
domain VARCHAR(255) NOT NULL,
|
||||
status VARCHAR(16) NOT NULL DEFAULT 'active',
|
||||
display_account_enabled TINYINT(1) NOT NULL DEFAULT 1,
|
||||
logo_url VARCHAR(512) NOT NULL DEFAULT '',
|
||||
favicon_url VARCHAR(512) NOT NULL DEFAULT '',
|
||||
banners_json TEXT NULL,
|
||||
announcements_json TEXT NULL,
|
||||
footer_info_json TEXT NULL,
|
||||
default_support_group_id BIGINT UNSIGNED NULL,
|
||||
tax_enabled TINYINT(1) NOT NULL DEFAULT 0,
|
||||
tax_rate_bps BIGINT NOT NULL DEFAULT 100,
|
||||
@@ -40,6 +49,8 @@ CREATE TABLE distribution_site_accounts (
|
||||
account_id BIGINT UNSIGNED NOT NULL,
|
||||
listing_id BIGINT UNSIGNED NOT NULL,
|
||||
display_status VARCHAR(16) NOT NULL DEFAULT 'enabled',
|
||||
markup_amount_cent BIGINT NOT NULL DEFAULT 0,
|
||||
shelf_ratio_delta DECIMAL(10,2) NOT NULL DEFAULT 0,
|
||||
display_started_at DATETIME NULL,
|
||||
display_ended_at DATETIME NULL,
|
||||
created_at DATETIME NOT NULL,
|
||||
@@ -64,7 +75,7 @@ CREATE TABLE distribution_rule_versions (
|
||||
distributor_a_ratio DECIMAL(10,2) NOT NULL DEFAULT 0,
|
||||
distributor_b_ratio DECIMAL(10,2) NOT NULL DEFAULT 0,
|
||||
service_fee_value DECIMAL(12,4) NOT NULL DEFAULT 0,
|
||||
service_fee_unit VARCHAR(24) NOT NULL DEFAULT 'ratio',
|
||||
service_fee_unit VARCHAR(24) NOT NULL DEFAULT 'percent',
|
||||
payment_fee_bps BIGINT NOT NULL DEFAULT 50,
|
||||
tax_enabled TINYINT(1) NOT NULL DEFAULT 0,
|
||||
tax_rate_bps BIGINT NOT NULL DEFAULT 100,
|
||||
@@ -90,7 +101,7 @@ CREATE TABLE distribution_order_financials (
|
||||
payment_fee_bps BIGINT NOT NULL DEFAULT 50,
|
||||
payment_fee_cent BIGINT NOT NULL DEFAULT 0,
|
||||
service_fee_value DECIMAL(12,4) NOT NULL DEFAULT 0,
|
||||
service_fee_unit VARCHAR(24) NOT NULL DEFAULT 'ratio',
|
||||
service_fee_unit VARCHAR(24) NOT NULL DEFAULT 'percent',
|
||||
service_fee_cent BIGINT NOT NULL DEFAULT 0,
|
||||
markup_amount_cent BIGINT NOT NULL DEFAULT 0,
|
||||
tax_enabled TINYINT(1) NOT NULL DEFAULT 0,
|
||||
@@ -182,19 +193,54 @@ CREATE TABLE distribution_site_payment_channels (
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='分销网站收款通道绑定';
|
||||
|
||||
ALTER TABLE rental_orders
|
||||
ADD COLUMN base_price_cent BIGINT NOT NULL DEFAULT 0 AFTER platform_fee_cent,
|
||||
ADD COLUMN shelf_markup_cent BIGINT NOT NULL DEFAULT 0 AFTER base_price_cent,
|
||||
ADD COLUMN markup_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER shelf_markup_cent,
|
||||
ADD COLUMN shelf_ratio DECIMAL(10,2) NOT NULL DEFAULT 0 AFTER markup_site_id,
|
||||
ADD COLUMN shelf_ratio_delta DECIMAL(10,2) NOT NULL DEFAULT 0 AFTER shelf_ratio,
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER source_channel,
|
||||
ADD COLUMN distribution_mode VARCHAR(32) NOT NULL DEFAULT '' AFTER distribution_site_id,
|
||||
ADD COLUMN distribution_rule_version_id BIGINT UNSIGNED NULL AFTER distribution_mode,
|
||||
ADD KEY idx_rental_orders_markup_site (markup_site_id),
|
||||
ADD KEY idx_rental_orders_distribution_site (distribution_site_id, distribution_mode),
|
||||
ADD CONSTRAINT fk_rental_orders_distribution_site FOREIGN KEY (distribution_site_id) REFERENCES distribution_sites (id),
|
||||
ADD CONSTRAINT fk_rental_orders_distribution_rule FOREIGN KEY (distribution_rule_version_id) REFERENCES distribution_rule_versions (id);
|
||||
|
||||
ALTER TABLE rental_listings
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER managed_admin_id,
|
||||
ADD COLUMN source_channel VARCHAR(64) NOT NULL DEFAULT '' AFTER distribution_site_id,
|
||||
ADD KEY idx_rental_listings_distribution_site (distribution_site_id, status);
|
||||
|
||||
ALTER TABLE chat_qrcode_pool
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER group_name,
|
||||
ADD KEY idx_chat_qrcode_distribution_site (distribution_site_id, status),
|
||||
ADD CONSTRAINT fk_chat_qrcode_distribution_site FOREIGN KEY (distribution_site_id) REFERENCES distribution_sites (id);
|
||||
|
||||
ALTER TABLE chat_conversations
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER listing_id,
|
||||
ADD KEY idx_chat_conversations_distribution_site (distribution_site_id, status);
|
||||
|
||||
ALTER TABLE wallet_accounts
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id,
|
||||
DROP KEY uk_wallet_accounts_user_id,
|
||||
ADD UNIQUE KEY uk_wallet_accounts_user_site (user_id, distribution_site_id);
|
||||
|
||||
ALTER TABLE wallet_ledger
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id,
|
||||
ADD KEY idx_wallet_ledger_user_site (user_id, distribution_site_id);
|
||||
|
||||
ALTER TABLE withdrawal_requests
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id,
|
||||
ADD KEY idx_withdrawal_user_site (user_id, distribution_site_id);
|
||||
|
||||
ALTER TABLE notifications
|
||||
ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER biz_id,
|
||||
ADD KEY idx_notifications_user_site (user_id, distribution_site_id, id);
|
||||
|
||||
INSERT INTO permissions (code, name, resource, action) VALUES
|
||||
('distribution:site_view', '查看分销网站', 'distribution', 'site_view'),
|
||||
('distribution:site_manage', '管理分销网站', 'distribution', 'site_manage'),
|
||||
('distribution:order_view', '查看分销订单', 'distribution', 'order_view'),
|
||||
('distribution:process_view', '查看分销订单过程', 'distribution', 'process_view'),
|
||||
('distribution:finance_view', '查看分销财务', 'distribution', 'finance_view'),
|
||||
('distribution:settlement_view', '查看分销结算', 'distribution', 'settlement_view'),
|
||||
('distribution:settlement_manage', '管理分销结算', 'distribution', 'settlement_manage'),
|
||||
('distribution:rule_view', '查看分销规则', 'distribution', 'rule_view'),
|
||||
@@ -203,14 +249,19 @@ INSERT INTO permissions (code, name, resource, action) VALUES
|
||||
('distribution:account_display', '管理分销账号展示', 'distribution', 'account_display'),
|
||||
('distribution:payment_view', '查看分销支付通道', 'distribution', 'payment_view'),
|
||||
('distribution:payment_manage', '管理分销支付通道', 'distribution', 'payment_manage'),
|
||||
('distribution:chat_view', '查看分销客服消息', 'distribution', 'chat_view'),
|
||||
('distribution:chat_send', '发送分销客服消息', 'distribution', 'chat_send')
|
||||
('distribution:all_view', '查看全部分销站点', 'distribution', 'all_view')
|
||||
ON DUPLICATE KEY UPDATE name = VALUES(name), resource = VALUES(resource), action = VALUES(action);
|
||||
|
||||
INSERT IGNORE INTO role_permissions (role_id, permission_id)
|
||||
SELECT r.id, p.id FROM roles r CROSS JOIN permissions p
|
||||
WHERE r.code = 'super_admin' AND p.code LIKE 'distribution:%';
|
||||
|
||||
INSERT IGNORE INTO role_permissions (role_id, permission_id)
|
||||
SELECT r.id, p.id FROM roles r CROSS JOIN permissions p
|
||||
WHERE r.code IN ('ops', 'finance')
|
||||
AND p.code LIKE 'distribution:%'
|
||||
AND p.code <> 'distribution:all_view';
|
||||
|
||||
INSERT INTO distribution_rule_versions (
|
||||
site_id, mode, version, status,
|
||||
platform_receive_ratio, platform_sell_ratio,
|
||||
@@ -219,29 +270,64 @@ INSERT INTO distribution_rule_versions (
|
||||
service_fee_value, service_fee_unit, payment_fee_bps,
|
||||
tax_enabled, tax_rate_bps, effective_from, created_at
|
||||
) VALUES
|
||||
(NULL, 'two_party_distributor_receive', 1, 'active', 0, 4, 3.5, 0, 0, 0, 2.5, 'ratio', 50, 0, 100, NOW(), NOW()),
|
||||
(NULL, 'two_party_platform_receive', 1, 'active', 3.5, 0, 0, 4, 0, 0, 2.5, 'ratio', 50, 0, 100, NOW(), NOW()),
|
||||
(NULL, 'three_party', 1, 'active', 0, 0, 0, 0, 3.5, 4, 2.5, 'ratio', 50, 0, 100, NOW(), NOW())
|
||||
ON DUPLICATE KEY UPDATE
|
||||
status = VALUES(status),
|
||||
service_fee_value = VALUES(service_fee_value),
|
||||
service_fee_unit = VALUES(service_fee_unit),
|
||||
payment_fee_bps = VALUES(payment_fee_bps);
|
||||
(NULL, 'two_party_distributor_receive', 1, 'active', 0, 40, 35, 0, 0, 0, 25, 'percent', 50, 0, 100, NOW(), NOW()),
|
||||
(NULL, 'two_party_platform_receive', 1, 'active', 35, 0, 0, 40, 0, 0, 25, 'percent', 50, 0, 100, NOW(), NOW()),
|
||||
(NULL, 'three_party', 1, 'active', 0, 0, 0, 0, 35, 40, 25, 'percent', 50, 0, 100, NOW(), NOW());
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP TABLE IF EXISTS distribution_order_financials;
|
||||
DROP TABLE IF EXISTS distribution_site_payment_channels;
|
||||
DROP TABLE IF EXISTS distribution_settlement_items;
|
||||
DROP TABLE IF EXISTS distribution_settlement_batches;
|
||||
DROP TABLE IF EXISTS distribution_order_parties;
|
||||
ALTER TABLE notifications
|
||||
DROP KEY idx_notifications_user_site,
|
||||
DROP COLUMN distribution_site_id;
|
||||
|
||||
ALTER TABLE withdrawal_requests
|
||||
DROP KEY idx_withdrawal_user_site,
|
||||
DROP COLUMN distribution_site_id;
|
||||
|
||||
ALTER TABLE wallet_ledger
|
||||
DROP KEY idx_wallet_ledger_user_site,
|
||||
DROP COLUMN distribution_site_id;
|
||||
|
||||
DELETE FROM wallet_accounts WHERE distribution_site_id <> 0;
|
||||
|
||||
ALTER TABLE wallet_accounts
|
||||
DROP KEY uk_wallet_accounts_user_site,
|
||||
DROP COLUMN distribution_site_id,
|
||||
ADD UNIQUE KEY uk_wallet_accounts_user_id (user_id);
|
||||
|
||||
ALTER TABLE chat_conversations
|
||||
DROP KEY idx_chat_conversations_distribution_site,
|
||||
DROP COLUMN distribution_site_id;
|
||||
|
||||
ALTER TABLE chat_qrcode_pool
|
||||
DROP FOREIGN KEY fk_chat_qrcode_distribution_site,
|
||||
DROP KEY idx_chat_qrcode_distribution_site,
|
||||
DROP COLUMN distribution_site_id;
|
||||
|
||||
ALTER TABLE rental_listings
|
||||
DROP KEY idx_rental_listings_distribution_site,
|
||||
DROP COLUMN source_channel,
|
||||
DROP COLUMN distribution_site_id;
|
||||
|
||||
ALTER TABLE rental_orders
|
||||
DROP FOREIGN KEY fk_rental_orders_distribution_rule,
|
||||
DROP FOREIGN KEY fk_rental_orders_distribution_site,
|
||||
DROP KEY idx_rental_orders_distribution_site,
|
||||
DROP KEY idx_rental_orders_markup_site,
|
||||
DROP COLUMN distribution_rule_version_id,
|
||||
DROP COLUMN distribution_mode,
|
||||
DROP COLUMN distribution_site_id;
|
||||
DROP COLUMN distribution_site_id,
|
||||
DROP COLUMN shelf_ratio_delta,
|
||||
DROP COLUMN shelf_ratio,
|
||||
DROP COLUMN markup_site_id,
|
||||
DROP COLUMN shelf_markup_cent,
|
||||
DROP COLUMN base_price_cent;
|
||||
|
||||
DROP TABLE IF EXISTS distribution_settlement_items;
|
||||
DROP TABLE IF EXISTS distribution_settlement_batches;
|
||||
DROP TABLE IF EXISTS distribution_order_financials;
|
||||
DROP TABLE IF EXISTS distribution_order_parties;
|
||||
DROP TABLE IF EXISTS distribution_site_payment_channels;
|
||||
DROP TABLE IF EXISTS distribution_rule_versions;
|
||||
DROP TABLE IF EXISTS distribution_site_accounts;
|
||||
DROP TABLE IF EXISTS distribution_site_members;
|
||||
@@ -251,9 +337,4 @@ DELETE rp FROM role_permissions rp
|
||||
JOIN permissions p ON p.id = rp.permission_id
|
||||
WHERE p.code LIKE 'distribution:%';
|
||||
|
||||
DELETE FROM permissions WHERE code IN (
|
||||
'distribution:site_view', 'distribution:site_manage', 'distribution:order_view', 'distribution:process_view',
|
||||
'distribution:finance_view', 'distribution:settlement_view', 'distribution:settlement_manage',
|
||||
'distribution:rule_view', 'distribution:rule_manage', 'distribution:account_view', 'distribution:account_display',
|
||||
'distribution:payment_view', 'distribution:payment_manage', 'distribution:chat_view', 'distribution:chat_send'
|
||||
);
|
||||
DELETE FROM permissions WHERE code LIKE 'distribution:%';
|
||||
|
||||
Reference in New Issue
Block a user