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:%';
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
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);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
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;
|
||||
@@ -1,34 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 分销后台权限:站点、规则、订单、账号展示、支付通道、结算批次。
|
||||
INSERT INTO permissions (code, name, resource, action) VALUES
|
||||
('distribution:site_view', '查看分销网站', 'distribution_site', 'view'),
|
||||
('distribution:site_manage', '管理分销网站', 'distribution_site', 'manage'),
|
||||
('distribution:rule_view', '查看分销规则', 'distribution_rule', 'view'),
|
||||
('distribution:rule_manage', '管理分销规则', 'distribution_rule', 'manage'),
|
||||
('distribution:order_view', '查看分销订单', 'distribution_order', 'view'),
|
||||
('distribution:account_view', '查看分销账号展示', 'distribution_account', 'view'),
|
||||
('distribution:account_display', '管理分销账号展示', 'distribution_account', 'display'),
|
||||
('distribution:payment_view', '查看分销支付通道', 'distribution_payment', 'view'),
|
||||
('distribution:payment_manage', '管理分销支付通道', 'distribution_payment', 'manage'),
|
||||
('distribution:settlement_view', '查看分销结算', 'distribution_settlement', 'view'),
|
||||
('distribution:settlement_manage', '管理分销结算', 'distribution_settlement', 'manage')
|
||||
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
|
||||
JOIN permissions p ON p.code LIKE 'distribution:%'
|
||||
WHERE r.code IN ('super_admin', 'ops', 'finance');
|
||||
|
||||
-- +goose Down
|
||||
|
||||
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 LIKE 'distribution:%';
|
||||
+4
-3
@@ -1,14 +1,15 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 私有文件上传归属:在业务记录写入前,仅允许上传者预览自己的临时文件。
|
||||
CREATE TABLE IF NOT EXISTS file_upload_owners (
|
||||
-- 私有文件上传归属:业务记录写入前,仅允许上传者预览自己的临时文件。
|
||||
CREATE TABLE file_upload_owners (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
user_id BIGINT UNSIGNED NOT NULL,
|
||||
object_key VARCHAR(512) NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY uk_file_upload_owners_object_key (object_key),
|
||||
KEY idx_file_upload_owners_user_id (user_id)
|
||||
KEY idx_file_upload_owners_user_id (user_id),
|
||||
KEY idx_file_upload_owners_created_at (created_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='私有文件上传归属';
|
||||
|
||||
-- +goose Down
|
||||
@@ -1,21 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 为长期未关联上传归属记录的定时清理提供索引。
|
||||
-- 兼容历史上已按旧编号执行过该索引的数据库,重复执行时跳过。
|
||||
SET @idx_exists := (
|
||||
SELECT COUNT(*) FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'file_upload_owners'
|
||||
AND index_name = 'idx_file_upload_owners_created_at'
|
||||
);
|
||||
SET @ddl := IF(@idx_exists > 0,
|
||||
'SELECT 1',
|
||||
'ALTER TABLE file_upload_owners ADD KEY idx_file_upload_owners_created_at (created_at)');
|
||||
PREPARE stmt FROM @ddl;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE file_upload_owners
|
||||
DROP INDEX idx_file_upload_owners_created_at;
|
||||
@@ -1,178 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 1. chat_conversations 表新增 distribution_site_id 与索引
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND COLUMN_NAME = 'distribution_site_id') = 0,
|
||||
'ALTER TABLE chat_conversations ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER listing_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND INDEX_NAME = 'idx_chat_conversations_distribution_site') = 0,
|
||||
'ALTER TABLE chat_conversations ADD KEY idx_chat_conversations_distribution_site (distribution_site_id, status)',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 2. wallet_accounts 表新增 distribution_site_id 并将唯一键变更为 (user_id, distribution_site_id)
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND COLUMN_NAME = 'distribution_site_id') = 0,
|
||||
'ALTER TABLE wallet_accounts ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_id') > 0,
|
||||
'ALTER TABLE wallet_accounts DROP KEY uk_wallet_accounts_user_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_site') = 1,
|
||||
'ALTER TABLE wallet_accounts DROP KEY uk_wallet_accounts_user_site',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_site') = 0,
|
||||
'ALTER TABLE wallet_accounts ADD UNIQUE KEY uk_wallet_accounts_user_site (user_id, distribution_site_id)',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 3. wallet_ledger 表新增 distribution_site_id 列与联合索引
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND COLUMN_NAME = 'distribution_site_id') = 0,
|
||||
'ALTER TABLE wallet_ledger ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND INDEX_NAME = 'idx_wallet_ledger_user_site') = 0,
|
||||
'ALTER TABLE wallet_ledger ADD KEY idx_wallet_ledger_user_site (user_id, distribution_site_id)',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 4. withdrawal_requests 表新增 distribution_site_id 列与联合索引
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND COLUMN_NAME = 'distribution_site_id') = 0,
|
||||
'ALTER TABLE withdrawal_requests ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND INDEX_NAME = 'idx_withdrawal_user_site') = 0,
|
||||
'ALTER TABLE withdrawal_requests ADD KEY idx_withdrawal_user_site (user_id, distribution_site_id)',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND INDEX_NAME = 'idx_withdrawal_user_site') > 0,
|
||||
'ALTER TABLE withdrawal_requests DROP KEY idx_withdrawal_user_site',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND COLUMN_NAME = 'distribution_site_id') > 0,
|
||||
'ALTER TABLE withdrawal_requests DROP COLUMN distribution_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND INDEX_NAME = 'idx_wallet_ledger_user_site') > 0,
|
||||
'ALTER TABLE wallet_ledger DROP KEY idx_wallet_ledger_user_site',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND COLUMN_NAME = 'distribution_site_id') > 0,
|
||||
'ALTER TABLE wallet_ledger DROP COLUMN distribution_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_site') > 0,
|
||||
'ALTER TABLE wallet_accounts DROP KEY uk_wallet_accounts_user_site',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_id') = 0,
|
||||
'ALTER TABLE wallet_accounts ADD UNIQUE KEY uk_wallet_accounts_user_id (user_id)',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND COLUMN_NAME = 'distribution_site_id') > 0,
|
||||
'ALTER TABLE wallet_accounts DROP COLUMN distribution_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND INDEX_NAME = 'idx_chat_conversations_distribution_site') > 0,
|
||||
'ALTER TABLE chat_conversations DROP KEY idx_chat_conversations_distribution_site',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND COLUMN_NAME = 'distribution_site_id') > 0,
|
||||
'ALTER TABLE chat_conversations DROP COLUMN distribution_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@@ -1,46 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 1. notifications 表新增 distribution_site_id 并添加联合索引
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND COLUMN_NAME = 'distribution_site_id') = 0,
|
||||
'ALTER TABLE notifications ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER biz_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND INDEX_NAME = 'idx_notifications_user_site') = 0,
|
||||
'ALTER TABLE notifications ADD KEY idx_notifications_user_site (user_id, distribution_site_id, id)',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 2. 回填历史分销订单的站内信通知归属
|
||||
UPDATE notifications n
|
||||
JOIN rental_orders o ON n.biz_id = o.id
|
||||
SET n.distribution_site_id = o.distribution_site_id
|
||||
WHERE n.biz_type = 'order' AND o.distribution_site_id IS NOT NULL AND o.distribution_site_id > 0;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND INDEX_NAME = 'idx_notifications_user_site') > 0,
|
||||
'ALTER TABLE notifications DROP KEY idx_notifications_user_site',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND COLUMN_NAME = 'distribution_site_id') > 0,
|
||||
'ALTER TABLE notifications DROP COLUMN distribution_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@@ -1,46 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 1. rental_listings 表新增 distribution_site_id 并添加索引
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_listings' AND COLUMN_NAME = 'distribution_site_id') = 0,
|
||||
'ALTER TABLE rental_listings ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER managed_admin_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_listings' AND INDEX_NAME = 'idx_rental_listings_distribution_site') = 0,
|
||||
'ALTER TABLE rental_listings ADD KEY idx_rental_listings_distribution_site (distribution_site_id, status)',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 2. 回填历史分销账号的归属站点
|
||||
UPDATE rental_listings l
|
||||
JOIN distribution_site_accounts dsa ON dsa.listing_id = l.id
|
||||
SET l.distribution_site_id = dsa.site_id
|
||||
WHERE l.distribution_site_id IS NULL;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_listings' AND INDEX_NAME = 'idx_rental_listings_distribution_site') > 0,
|
||||
'ALTER TABLE rental_listings DROP KEY idx_rental_listings_distribution_site',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_listings' AND COLUMN_NAME = 'distribution_site_id') > 0,
|
||||
'ALTER TABLE rental_listings DROP COLUMN distribution_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@@ -1,11 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 修复 000072 中误将展示货架 (distribution_site_accounts) 历史商品回填为分销站点来源的问题
|
||||
-- 历史主站商品(在分销上传功能上线前创建的商品)来源应保持 distribution_site_id = NULL
|
||||
UPDATE rental_listings l
|
||||
JOIN distribution_site_accounts dsa ON dsa.listing_id = l.id
|
||||
SET l.distribution_site_id = NULL
|
||||
WHERE l.created_at < '2026-09-08 14:00:00';
|
||||
|
||||
-- +goose Down
|
||||
SELECT 1;
|
||||
@@ -1,113 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 1. distribution_sites 表增加品牌形象与前台装修字段
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'logo_url') = 0,
|
||||
'ALTER TABLE distribution_sites ADD COLUMN logo_url VARCHAR(512) NOT NULL DEFAULT \'\' AFTER display_account_enabled',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'favicon_url') = 0,
|
||||
'ALTER TABLE distribution_sites ADD COLUMN favicon_url VARCHAR(512) NOT NULL DEFAULT \'\' AFTER logo_url',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'banners_json') = 0,
|
||||
'ALTER TABLE distribution_sites ADD COLUMN banners_json TEXT NULL AFTER favicon_url',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'announcements_json') = 0,
|
||||
'ALTER TABLE distribution_sites ADD COLUMN announcements_json TEXT NULL AFTER banners_json',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'footer_info_json') = 0,
|
||||
'ALTER TABLE distribution_sites ADD COLUMN footer_info_json TEXT NULL AFTER announcements_json',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 2. distribution_site_accounts 增加自主加价字段
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_site_accounts' AND COLUMN_NAME = 'markup_amount_cent') = 0,
|
||||
'ALTER TABLE distribution_site_accounts ADD COLUMN markup_amount_cent BIGINT NOT NULL DEFAULT 0 AFTER display_status',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_site_accounts' AND COLUMN_NAME = 'markup_amount_cent') > 0,
|
||||
'ALTER TABLE distribution_site_accounts DROP COLUMN markup_amount_cent',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'footer_info_json') > 0,
|
||||
'ALTER TABLE distribution_sites DROP COLUMN footer_info_json',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'announcements_json') > 0,
|
||||
'ALTER TABLE distribution_sites DROP COLUMN announcements_json',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'banners_json') > 0,
|
||||
'ALTER TABLE distribution_sites DROP COLUMN banners_json',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'favicon_url') > 0,
|
||||
'ALTER TABLE distribution_sites DROP COLUMN favicon_url',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'distribution_sites' AND COLUMN_NAME = 'logo_url') > 0,
|
||||
'ALTER TABLE distribution_sites DROP COLUMN logo_url',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@@ -1,58 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 下单时冻结货架加价,后续改价不影响历史订单。
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_orders' AND COLUMN_NAME = 'base_price_cent') = 0,
|
||||
'ALTER TABLE rental_orders ADD COLUMN base_price_cent BIGINT NOT NULL DEFAULT 0 AFTER platform_fee_cent',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_orders' AND COLUMN_NAME = 'shelf_markup_cent') = 0,
|
||||
'ALTER TABLE rental_orders ADD COLUMN shelf_markup_cent BIGINT NOT NULL DEFAULT 0 AFTER base_price_cent',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_orders' AND COLUMN_NAME = 'markup_site_id') = 0,
|
||||
'ALTER TABLE rental_orders ADD COLUMN markup_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER shelf_markup_cent',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_orders' AND COLUMN_NAME = 'markup_site_id') > 0,
|
||||
'ALTER TABLE rental_orders DROP COLUMN markup_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_orders' AND COLUMN_NAME = 'shelf_markup_cent') > 0,
|
||||
'ALTER TABLE rental_orders DROP COLUMN shelf_markup_cent',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_orders' AND COLUMN_NAME = 'base_price_cent') > 0,
|
||||
'ALTER TABLE rental_orders DROP COLUMN base_price_cent',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@@ -1,71 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 补偿 000072 / 000073:货架展示不等于产地,禁止用绝对时间回滚。
|
||||
-- 本站上传特征:无 listing_uploads(外部导入走该表,source_channel=咸鱼/淘宝等),
|
||||
-- 且 distribution_site_accounts 与商品在同一发布事务内写入(created_at 相差不超过 5 秒)。
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_listings' AND COLUMN_NAME = 'source_channel') = 0,
|
||||
'ALTER TABLE rental_listings ADD COLUMN source_channel VARCHAR(64) NOT NULL DEFAULT '''' AFTER distribution_site_id',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 1. 恢复被 000073 时间戳误清空的本站上传归属
|
||||
UPDATE rental_listings l
|
||||
INNER JOIN (
|
||||
SELECT dsa.listing_id, MIN(dsa.site_id) AS site_id
|
||||
FROM distribution_site_accounts dsa
|
||||
INNER JOIN rental_listings l2 ON l2.id = dsa.listing_id
|
||||
LEFT JOIN listing_uploads lu ON lu.listing_id = dsa.listing_id
|
||||
WHERE lu.id IS NULL
|
||||
AND TIMESTAMPDIFF(SECOND, l2.created_at, dsa.created_at) BETWEEN 0 AND 5
|
||||
GROUP BY dsa.listing_id
|
||||
) origin ON origin.listing_id = l.id
|
||||
SET l.distribution_site_id = origin.site_id
|
||||
WHERE l.distribution_site_id IS NULL OR l.distribution_site_id = 0;
|
||||
|
||||
-- 2. 清掉仅因货架回填产生的归属(外部导入,或 DSA 明显晚于商品创建)
|
||||
UPDATE rental_listings l
|
||||
LEFT JOIN listing_uploads lu ON lu.listing_id = l.id
|
||||
LEFT JOIN distribution_site_accounts origin_dsa
|
||||
ON origin_dsa.listing_id = l.id
|
||||
AND origin_dsa.site_id = l.distribution_site_id
|
||||
AND TIMESTAMPDIFF(SECOND, l.created_at, origin_dsa.created_at) BETWEEN 0 AND 5
|
||||
SET l.distribution_site_id = NULL
|
||||
WHERE l.distribution_site_id IS NOT NULL
|
||||
AND l.distribution_site_id <> 0
|
||||
AND (lu.id IS NOT NULL OR origin_dsa.id IS NULL);
|
||||
|
||||
-- 3. 回填来源渠道快照:分销站用 site_code,外部导入用 listing_uploads.source_channel
|
||||
UPDATE rental_listings l
|
||||
INNER JOIN distribution_sites s ON s.id = l.distribution_site_id
|
||||
SET l.source_channel = s.site_code
|
||||
WHERE l.distribution_site_id IS NOT NULL
|
||||
AND l.distribution_site_id <> 0
|
||||
AND l.source_channel = '';
|
||||
|
||||
UPDATE rental_listings l
|
||||
INNER JOIN (
|
||||
SELECT lu.listing_id, MAX(NULLIF(lu.source_channel, '')) AS source_channel
|
||||
FROM listing_uploads lu
|
||||
WHERE lu.listing_id IS NOT NULL
|
||||
GROUP BY lu.listing_id
|
||||
) upload ON upload.listing_id = l.id
|
||||
SET l.source_channel = COALESCE(upload.source_channel, '')
|
||||
WHERE (l.distribution_site_id IS NULL OR l.distribution_site_id = 0)
|
||||
AND l.source_channel = ''
|
||||
AND COALESCE(upload.source_channel, '') <> '';
|
||||
|
||||
-- +goose Down
|
||||
|
||||
SET @sql = IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rental_listings' AND COLUMN_NAME = 'source_channel') > 0,
|
||||
'ALTER TABLE rental_listings DROP COLUMN source_channel',
|
||||
'SELECT 1'
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@@ -1,24 +0,0 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 跨站点查看/管理分销数据的显式权限。无站点成员不再视为超管。
|
||||
INSERT INTO permissions (code, name, resource, action) VALUES
|
||||
('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
|
||||
JOIN permissions p ON p.code = 'distribution:all_view'
|
||||
WHERE r.code = 'super_admin';
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DELETE rp
|
||||
FROM role_permissions rp
|
||||
JOIN permissions p ON p.id = rp.permission_id
|
||||
WHERE p.code = 'distribution:all_view';
|
||||
|
||||
DELETE FROM permissions WHERE code = 'distribution:all_view';
|
||||
Reference in New Issue
Block a user