feat(M07-A): 建立双余额账本
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS qipai_wallet_ledger_entries;
|
||||
DROP TABLE IF EXISTS qipai_wallet_accounts;
|
||||
DROP TABLE IF EXISTS qipai_wallet_scope_policies;
|
||||
|
||||
DELETE FROM qipai_schema_migrations WHERE version = '2026062421';
|
||||
@@ -0,0 +1,76 @@
|
||||
CREATE TABLE IF NOT EXISTS qipai_wallet_scope_policies (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
tenant_id BIGINT UNSIGNED NOT NULL,
|
||||
store_id BIGINT UNSIGNED NULL,
|
||||
scope_type VARCHAR(16) NOT NULL DEFAULT 'TENANT',
|
||||
enabled TINYINT(1) NOT NULL DEFAULT 1,
|
||||
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
|
||||
ON UPDATE CURRENT_TIMESTAMP(3),
|
||||
CONSTRAINT fk_qipai_wallet_policy_tenant
|
||||
FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
|
||||
CONSTRAINT fk_qipai_wallet_policy_store
|
||||
FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
|
||||
UNIQUE KEY uq_qipai_wallet_policy_store (tenant_id, store_id),
|
||||
KEY idx_qipai_wallet_policy_tenant (tenant_id, enabled)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS qipai_wallet_accounts (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
tenant_id BIGINT UNSIGNED NOT NULL,
|
||||
user_id BIGINT UNSIGNED NOT NULL,
|
||||
scope_type VARCHAR(16) NOT NULL,
|
||||
store_id BIGINT UNSIGNED NULL,
|
||||
cash_balance_cents INT NOT NULL DEFAULT 0,
|
||||
gift_balance_cents INT NOT NULL DEFAULT 0,
|
||||
status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
|
||||
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
|
||||
ON UPDATE CURRENT_TIMESTAMP(3),
|
||||
CONSTRAINT fk_qipai_wallet_account_tenant
|
||||
FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
|
||||
CONSTRAINT fk_qipai_wallet_account_user
|
||||
FOREIGN KEY (user_id) REFERENCES qipai_users(id),
|
||||
CONSTRAINT fk_qipai_wallet_account_store
|
||||
FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
|
||||
UNIQUE KEY uq_qipai_wallet_account_scope
|
||||
(tenant_id, user_id, scope_type, store_id),
|
||||
KEY idx_qipai_wallet_account_user (tenant_id, user_id, status)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS qipai_wallet_ledger_entries (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
tenant_id BIGINT UNSIGNED NOT NULL,
|
||||
account_id BIGINT UNSIGNED NOT NULL,
|
||||
user_id BIGINT UNSIGNED NOT NULL,
|
||||
store_id BIGINT UNSIGNED NULL,
|
||||
business_type VARCHAR(64) NOT NULL,
|
||||
business_id VARCHAR(128) NOT NULL,
|
||||
entry_type VARCHAR(32) NOT NULL,
|
||||
cash_delta_cents INT NOT NULL DEFAULT 0,
|
||||
gift_delta_cents INT NOT NULL DEFAULT 0,
|
||||
cash_balance_after_cents INT NOT NULL,
|
||||
gift_balance_after_cents INT NOT NULL,
|
||||
operator_id BIGINT UNSIGNED NULL,
|
||||
trace_id VARCHAR(128) NOT NULL,
|
||||
note VARCHAR(512) NOT NULL DEFAULT '',
|
||||
metadata JSON NULL,
|
||||
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
CONSTRAINT fk_qipai_wallet_ledger_tenant
|
||||
FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
|
||||
CONSTRAINT fk_qipai_wallet_ledger_account
|
||||
FOREIGN KEY (account_id) REFERENCES qipai_wallet_accounts(id),
|
||||
CONSTRAINT fk_qipai_wallet_ledger_user
|
||||
FOREIGN KEY (user_id) REFERENCES qipai_users(id),
|
||||
CONSTRAINT fk_qipai_wallet_ledger_store
|
||||
FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
|
||||
CONSTRAINT fk_qipai_wallet_ledger_operator
|
||||
FOREIGN KEY (operator_id) REFERENCES qipai_users(id),
|
||||
UNIQUE KEY uq_qipai_wallet_ledger_business
|
||||
(tenant_id, account_id, business_type, business_id),
|
||||
KEY idx_qipai_wallet_ledger_user (tenant_id, user_id, created_at),
|
||||
KEY idx_qipai_wallet_ledger_trace (tenant_id, trace_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
INSERT IGNORE INTO qipai_schema_migrations (version, name)
|
||||
VALUES ('2026062421', 'm07a_wallet_ledger');
|
||||
@@ -0,0 +1,30 @@
|
||||
SELECT 'qipai_wallet_scope_policies' AS table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = DATABASE() AND table_name = 'qipai_wallet_scope_policies';
|
||||
|
||||
SELECT 'qipai_wallet_accounts' AS table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = DATABASE() AND table_name = 'qipai_wallet_accounts';
|
||||
|
||||
SELECT 'qipai_wallet_ledger_entries' AS table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = DATABASE() AND table_name = 'qipai_wallet_ledger_entries';
|
||||
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'qipai_wallet_accounts'
|
||||
AND column_name IN ('cash_balance_cents', 'gift_balance_cents', 'scope_type', 'store_id');
|
||||
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'qipai_wallet_ledger_entries'
|
||||
AND column_name IN (
|
||||
'business_type', 'business_id', 'cash_delta_cents', 'gift_delta_cents',
|
||||
'cash_balance_after_cents', 'gift_balance_after_cents', 'trace_id'
|
||||
);
|
||||
|
||||
SELECT '2026062421' AS version
|
||||
FROM qipai_schema_migrations
|
||||
WHERE version = '2026062421';
|
||||
Reference in New Issue
Block a user