feat: 钱包系统分离 - 代理钱包与卡钱包完全隔离
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m17s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m17s
## 变更概述 将统一钱包系统拆分为代理钱包和卡钱包两个独立系统,实现数据表和代码层面的完全隔离。 ## 数据库变更 - 新增 6 张表:tb_agent_wallet、tb_agent_wallet_transaction、tb_agent_recharge_record、tb_card_wallet、tb_card_wallet_transaction、tb_card_recharge_record - 删除 3 张旧表:tb_wallet、tb_wallet_transaction、tb_recharge_record - 代理钱包:按 (shop_id, wallet_type) 唯一标识,支持主钱包和分佣钱包 - 卡钱包:按 (resource_type, resource_id) 唯一标识,支持物联网卡和设备 ## 代码变更 - Model 层:新增 AgentWallet、AgentWalletTransaction、AgentRechargeRecord、CardWallet、CardWalletTransaction、CardRechargeRecord 模型 - Store 层:新增 6 个独立 Store,支持事务、乐观锁、Redis 缓存 - Service 层:重构 commission_calculation、commission_withdrawal、order、recharge 等 8 个服务 - Bootstrap 层:更新 Store 和 Service 依赖注入 - 常量层:按钱包类型重新组织常量和 Redis Key 生成函数 ## 技术特性 - 乐观锁:使用 version 字段防止并发冲突 - 多租户:支持 shop_id_tag 和 enterprise_id_tag 过滤 - 事务管理:所有余额变动使用事务保证 ACID - 缓存策略:Cache-Aside 模式,余额变动后删除缓存 ## 业务影响 - 代理钱包和卡钱包业务完全隔离,互不影响 - 为独立监控、优化、扩展打下基础 - 提升代理钱包的稳定性和独立性 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2
migrations/000059_create_tb_agent_wallet.down.sql
Normal file
2
migrations/000059_create_tb_agent_wallet.down.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
-- 删除代理钱包主表
|
||||
DROP TABLE IF EXISTS tb_agent_wallet;
|
||||
48
migrations/000059_create_tb_agent_wallet.up.sql
Normal file
48
migrations/000059_create_tb_agent_wallet.up.sql
Normal file
@@ -0,0 +1,48 @@
|
||||
-- 创建代理钱包主表
|
||||
-- 用于管理店铺级别的主钱包和分佣钱包
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tb_agent_wallet (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
shop_id BIGINT NOT NULL,
|
||||
wallet_type VARCHAR(20) NOT NULL,
|
||||
balance BIGINT NOT NULL DEFAULT 0,
|
||||
frozen_balance BIGINT NOT NULL DEFAULT 0,
|
||||
currency VARCHAR(10) NOT NULL DEFAULT 'CNY',
|
||||
status INT NOT NULL DEFAULT 1,
|
||||
version INT NOT NULL DEFAULT 0,
|
||||
shop_id_tag BIGINT NOT NULL,
|
||||
enterprise_id_tag BIGINT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP,
|
||||
|
||||
-- 约束
|
||||
CONSTRAINT chk_agent_wallet_balance CHECK (balance >= 0),
|
||||
CONSTRAINT chk_agent_wallet_frozen_balance CHECK (frozen_balance >= 0 AND frozen_balance <= balance),
|
||||
CONSTRAINT chk_agent_wallet_status CHECK (status IN (1, 2, 3)),
|
||||
CONSTRAINT chk_agent_wallet_type CHECK (wallet_type IN ('main', 'commission'))
|
||||
);
|
||||
|
||||
-- 唯一索引:shop_id + wallet_type 在未删除时唯一
|
||||
CREATE UNIQUE INDEX idx_agent_wallet_shop_type ON tb_agent_wallet (shop_id, wallet_type) WHERE deleted_at IS NULL;
|
||||
|
||||
-- 状态索引
|
||||
CREATE INDEX idx_agent_wallet_status ON tb_agent_wallet (status);
|
||||
|
||||
-- 多租户过滤索引
|
||||
CREATE INDEX idx_agent_wallet_shop_tag ON tb_agent_wallet (shop_id_tag);
|
||||
|
||||
-- 企业标签索引(用于多租户过滤)
|
||||
CREATE INDEX idx_agent_wallet_enterprise_tag ON tb_agent_wallet (enterprise_id_tag) WHERE enterprise_id_tag IS NOT NULL;
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE tb_agent_wallet IS '代理钱包主表';
|
||||
COMMENT ON COLUMN tb_agent_wallet.shop_id IS '店铺 ID';
|
||||
COMMENT ON COLUMN tb_agent_wallet.wallet_type IS '钱包类型:main-主钱包 | commission-分佣钱包';
|
||||
COMMENT ON COLUMN tb_agent_wallet.balance IS '余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_agent_wallet.frozen_balance IS '冻结余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_agent_wallet.currency IS '币种';
|
||||
COMMENT ON COLUMN tb_agent_wallet.status IS '钱包状态:1-正常 2-冻结 3-关闭';
|
||||
COMMENT ON COLUMN tb_agent_wallet.version IS '版本号(乐观锁)';
|
||||
COMMENT ON COLUMN tb_agent_wallet.shop_id_tag IS '店铺 ID 标签(多租户过滤)';
|
||||
COMMENT ON COLUMN tb_agent_wallet.enterprise_id_tag IS '企业 ID 标签(多租户过滤)';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- 删除代理钱包交易记录表
|
||||
DROP TABLE IF EXISTS tb_agent_wallet_transaction;
|
||||
60
migrations/000060_create_tb_agent_wallet_transaction.up.sql
Normal file
60
migrations/000060_create_tb_agent_wallet_transaction.up.sql
Normal file
@@ -0,0 +1,60 @@
|
||||
-- 创建代理钱包交易记录表
|
||||
-- 记录所有代理钱包余额变动
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tb_agent_wallet_transaction (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
agent_wallet_id BIGINT NOT NULL,
|
||||
shop_id BIGINT NOT NULL,
|
||||
user_id BIGINT NOT NULL,
|
||||
transaction_type VARCHAR(20) NOT NULL,
|
||||
amount BIGINT NOT NULL,
|
||||
balance_before BIGINT NOT NULL,
|
||||
balance_after BIGINT NOT NULL,
|
||||
status INT NOT NULL DEFAULT 1,
|
||||
reference_type VARCHAR(50),
|
||||
reference_id BIGINT,
|
||||
remark TEXT,
|
||||
metadata JSONB,
|
||||
creator BIGINT NOT NULL,
|
||||
shop_id_tag BIGINT NOT NULL,
|
||||
enterprise_id_tag BIGINT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP,
|
||||
|
||||
-- 约束
|
||||
CONSTRAINT chk_agent_tx_status CHECK (status IN (1, 2, 3)),
|
||||
CONSTRAINT chk_agent_tx_type CHECK (transaction_type IN ('recharge', 'deduct', 'refund', 'commission', 'withdrawal'))
|
||||
);
|
||||
|
||||
-- 按钱包查询交易历史索引
|
||||
CREATE INDEX idx_agent_tx_wallet ON tb_agent_wallet_transaction (agent_wallet_id, created_at DESC);
|
||||
|
||||
-- 按店铺汇总交易索引
|
||||
CREATE INDEX idx_agent_tx_shop ON tb_agent_wallet_transaction (shop_id, created_at DESC);
|
||||
|
||||
-- 按关联业务查询索引
|
||||
CREATE INDEX idx_agent_tx_ref ON tb_agent_wallet_transaction (reference_type, reference_id) WHERE reference_type IS NOT NULL;
|
||||
|
||||
-- 按交易类型统计索引
|
||||
CREATE INDEX idx_agent_tx_type ON tb_agent_wallet_transaction (transaction_type, created_at DESC);
|
||||
|
||||
-- 多租户过滤索引
|
||||
CREATE INDEX idx_agent_tx_shop_tag ON tb_agent_wallet_transaction (shop_id_tag);
|
||||
|
||||
-- 企业标签索引
|
||||
CREATE INDEX idx_agent_tx_enterprise_tag ON tb_agent_wallet_transaction (enterprise_id_tag) WHERE enterprise_id_tag IS NOT NULL;
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE tb_agent_wallet_transaction IS '代理钱包交易记录表';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.agent_wallet_id IS '代理钱包 ID';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.shop_id IS '店铺 ID(冗余字段,便于查询)';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.user_id IS '操作人用户 ID';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.transaction_type IS '交易类型:recharge-充值 | deduct-扣款 | refund-退款 | commission-分佣 | withdrawal-提现';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.amount IS '变动金额(单位:分,正数为增加,负数为减少)';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.balance_before IS '变动前余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.balance_after IS '变动后余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.status IS '交易状态:1-成功 2-失败 3-处理中';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.reference_type IS '关联业务类型(如 order | commission | withdrawal | topup)';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.reference_id IS '关联业务 ID';
|
||||
COMMENT ON COLUMN tb_agent_wallet_transaction.metadata IS '扩展信息(如手续费、支付方式等)';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- 删除代理充值记录表
|
||||
DROP TABLE IF EXISTS tb_agent_recharge_record;
|
||||
59
migrations/000061_create_tb_agent_recharge_record.up.sql
Normal file
59
migrations/000061_create_tb_agent_recharge_record.up.sql
Normal file
@@ -0,0 +1,59 @@
|
||||
-- 创建代理充值记录表
|
||||
-- 记录所有代理充值操作
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tb_agent_recharge_record (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
agent_wallet_id BIGINT NOT NULL,
|
||||
shop_id BIGINT NOT NULL,
|
||||
recharge_no VARCHAR(50) NOT NULL UNIQUE,
|
||||
amount BIGINT NOT NULL,
|
||||
payment_method VARCHAR(20) NOT NULL,
|
||||
payment_channel VARCHAR(50),
|
||||
payment_transaction_id VARCHAR(100),
|
||||
status INT NOT NULL DEFAULT 1,
|
||||
paid_at TIMESTAMP,
|
||||
completed_at TIMESTAMP,
|
||||
shop_id_tag BIGINT NOT NULL,
|
||||
enterprise_id_tag BIGINT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP,
|
||||
|
||||
-- 约束
|
||||
CONSTRAINT chk_agent_recharge_amount CHECK (amount >= 10000),
|
||||
CONSTRAINT chk_agent_recharge_status CHECK (status IN (1, 2, 3, 4, 5)),
|
||||
CONSTRAINT chk_agent_recharge_method CHECK (payment_method IN ('alipay', 'wechat', 'bank', 'offline'))
|
||||
);
|
||||
|
||||
-- 按用户查询充值记录索引
|
||||
CREATE INDEX idx_agent_recharge_user ON tb_agent_recharge_record (user_id, created_at DESC);
|
||||
|
||||
-- 按店铺查询充值记录索引
|
||||
CREATE INDEX idx_agent_recharge_shop ON tb_agent_recharge_record (shop_id, created_at DESC);
|
||||
|
||||
-- 按状态过滤充值记录索引
|
||||
CREATE INDEX idx_agent_recharge_status ON tb_agent_recharge_record (status, created_at DESC);
|
||||
|
||||
-- 按订单号查询索引
|
||||
CREATE UNIQUE INDEX idx_agent_recharge_no ON tb_agent_recharge_record (recharge_no);
|
||||
|
||||
-- 多租户过滤索引
|
||||
CREATE INDEX idx_agent_recharge_shop_tag ON tb_agent_recharge_record (shop_id_tag);
|
||||
|
||||
-- 企业标签索引
|
||||
CREATE INDEX idx_agent_recharge_enterprise_tag ON tb_agent_recharge_record (enterprise_id_tag) WHERE enterprise_id_tag IS NOT NULL;
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE tb_agent_recharge_record IS '代理充值记录表';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.user_id IS '操作人用户 ID';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.agent_wallet_id IS '代理钱包 ID';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.shop_id IS '店铺 ID(冗余字段,便于查询)';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.recharge_no IS '充值订单号(格式:ARCH+时间戳+随机数)';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.amount IS '充值金额(单位:分,最小 10000 分 = 100 元)';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.payment_method IS '支付方式:alipay-支付宝 | wechat-微信 | bank-银行转账 | offline-线下';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.payment_channel IS '支付渠道';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.payment_transaction_id IS '第三方支付交易号';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.status IS '充值状态:1-待支付 2-已支付 3-已完成 4-已关闭 5-已退款';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.paid_at IS '支付时间';
|
||||
COMMENT ON COLUMN tb_agent_recharge_record.completed_at IS '完成时间';
|
||||
2
migrations/000062_create_tb_card_wallet.down.sql
Normal file
2
migrations/000062_create_tb_card_wallet.down.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
-- 删除卡钱包主表
|
||||
DROP TABLE IF EXISTS tb_card_wallet;
|
||||
48
migrations/000062_create_tb_card_wallet.up.sql
Normal file
48
migrations/000062_create_tb_card_wallet.up.sql
Normal file
@@ -0,0 +1,48 @@
|
||||
-- 创建卡钱包主表
|
||||
-- 用于管理物联网卡和设备级别的钱包
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tb_card_wallet (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
resource_type VARCHAR(20) NOT NULL,
|
||||
resource_id BIGINT NOT NULL,
|
||||
balance BIGINT NOT NULL DEFAULT 0,
|
||||
frozen_balance BIGINT NOT NULL DEFAULT 0,
|
||||
currency VARCHAR(10) NOT NULL DEFAULT 'CNY',
|
||||
status INT NOT NULL DEFAULT 1,
|
||||
version INT NOT NULL DEFAULT 0,
|
||||
shop_id_tag BIGINT NOT NULL,
|
||||
enterprise_id_tag BIGINT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP,
|
||||
|
||||
-- 约束
|
||||
CONSTRAINT chk_card_wallet_balance CHECK (balance >= 0),
|
||||
CONSTRAINT chk_card_wallet_frozen_balance CHECK (frozen_balance >= 0 AND frozen_balance <= balance),
|
||||
CONSTRAINT chk_card_wallet_status CHECK (status IN (1, 2, 3)),
|
||||
CONSTRAINT chk_card_wallet_type CHECK (resource_type IN ('iot_card', 'device'))
|
||||
);
|
||||
|
||||
-- 唯一索引:resource_type + resource_id 在未删除时唯一
|
||||
CREATE UNIQUE INDEX idx_card_wallet_resource ON tb_card_wallet (resource_type, resource_id) WHERE deleted_at IS NULL;
|
||||
|
||||
-- 状态索引
|
||||
CREATE INDEX idx_card_wallet_status ON tb_card_wallet (status);
|
||||
|
||||
-- 多租户过滤索引
|
||||
CREATE INDEX idx_card_wallet_shop_tag ON tb_card_wallet (shop_id_tag);
|
||||
|
||||
-- 企业标签索引
|
||||
CREATE INDEX idx_card_wallet_enterprise_tag ON tb_card_wallet (enterprise_id_tag) WHERE enterprise_id_tag IS NOT NULL;
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE tb_card_wallet IS '卡钱包主表';
|
||||
COMMENT ON COLUMN tb_card_wallet.resource_type IS '资源类型:iot_card-物联网卡 | device-设备';
|
||||
COMMENT ON COLUMN tb_card_wallet.resource_id IS '资源 ID(关联 tb_iot_card.id 或 tb_device.id)';
|
||||
COMMENT ON COLUMN tb_card_wallet.balance IS '余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_card_wallet.frozen_balance IS '冻结余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_card_wallet.currency IS '币种';
|
||||
COMMENT ON COLUMN tb_card_wallet.status IS '钱包状态:1-正常 2-冻结 3-关闭';
|
||||
COMMENT ON COLUMN tb_card_wallet.version IS '版本号(乐观锁)';
|
||||
COMMENT ON COLUMN tb_card_wallet.shop_id_tag IS '店铺 ID 标签(多租户过滤)';
|
||||
COMMENT ON COLUMN tb_card_wallet.enterprise_id_tag IS '企业 ID 标签(多租户过滤)';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- 删除卡钱包交易记录表
|
||||
DROP TABLE IF EXISTS tb_card_wallet_transaction;
|
||||
62
migrations/000063_create_tb_card_wallet_transaction.up.sql
Normal file
62
migrations/000063_create_tb_card_wallet_transaction.up.sql
Normal file
@@ -0,0 +1,62 @@
|
||||
-- 创建卡钱包交易记录表
|
||||
-- 记录所有卡钱包余额变动
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tb_card_wallet_transaction (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
card_wallet_id BIGINT NOT NULL,
|
||||
resource_type VARCHAR(20) NOT NULL,
|
||||
resource_id BIGINT NOT NULL,
|
||||
user_id BIGINT NOT NULL,
|
||||
transaction_type VARCHAR(20) NOT NULL,
|
||||
amount BIGINT NOT NULL,
|
||||
balance_before BIGINT NOT NULL,
|
||||
balance_after BIGINT NOT NULL,
|
||||
status INT NOT NULL DEFAULT 1,
|
||||
reference_type VARCHAR(50),
|
||||
reference_id BIGINT,
|
||||
remark TEXT,
|
||||
metadata JSONB,
|
||||
creator BIGINT NOT NULL,
|
||||
shop_id_tag BIGINT NOT NULL,
|
||||
enterprise_id_tag BIGINT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP,
|
||||
|
||||
-- 约束
|
||||
CONSTRAINT chk_card_tx_status CHECK (status IN (1, 2, 3)),
|
||||
CONSTRAINT chk_card_tx_type CHECK (transaction_type IN ('recharge', 'deduct', 'refund'))
|
||||
);
|
||||
|
||||
-- 按钱包查询交易历史索引
|
||||
CREATE INDEX idx_card_tx_wallet ON tb_card_wallet_transaction (card_wallet_id, created_at DESC);
|
||||
|
||||
-- 按资源查询交易索引
|
||||
CREATE INDEX idx_card_tx_resource ON tb_card_wallet_transaction (resource_type, resource_id, created_at DESC);
|
||||
|
||||
-- 按关联业务查询索引
|
||||
CREATE INDEX idx_card_tx_ref ON tb_card_wallet_transaction (reference_type, reference_id) WHERE reference_type IS NOT NULL;
|
||||
|
||||
-- 按交易类型统计索引
|
||||
CREATE INDEX idx_card_tx_type ON tb_card_wallet_transaction (transaction_type, created_at DESC);
|
||||
|
||||
-- 多租户过滤索引
|
||||
CREATE INDEX idx_card_tx_shop_tag ON tb_card_wallet_transaction (shop_id_tag);
|
||||
|
||||
-- 企业标签索引
|
||||
CREATE INDEX idx_card_tx_enterprise_tag ON tb_card_wallet_transaction (enterprise_id_tag) WHERE enterprise_id_tag IS NOT NULL;
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE tb_card_wallet_transaction IS '卡钱包交易记录表';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.card_wallet_id IS '卡钱包 ID';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.resource_type IS '资源类型(冗余字段,便于查询)';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.resource_id IS '资源 ID(冗余字段,便于查询)';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.user_id IS '操作人用户 ID';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.transaction_type IS '交易类型:recharge-充值 | deduct-扣款 | refund-退款';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.amount IS '变动金额(单位:分,正数为增加,负数为减少)';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.balance_before IS '变动前余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.balance_after IS '变动后余额(单位:分)';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.status IS '交易状态:1-成功 2-失败 3-处理中';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.reference_type IS '关联业务类型(如 order | topup)';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.reference_id IS '关联业务 ID';
|
||||
COMMENT ON COLUMN tb_card_wallet_transaction.metadata IS '扩展信息(如套餐信息、支付方式等)';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- 删除卡充值记录表
|
||||
DROP TABLE IF EXISTS tb_card_recharge_record;
|
||||
61
migrations/000064_create_tb_card_recharge_record.up.sql
Normal file
61
migrations/000064_create_tb_card_recharge_record.up.sql
Normal file
@@ -0,0 +1,61 @@
|
||||
-- 创建卡充值记录表
|
||||
-- 记录所有卡充值操作
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tb_card_recharge_record (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
card_wallet_id BIGINT NOT NULL,
|
||||
resource_type VARCHAR(20) NOT NULL,
|
||||
resource_id BIGINT NOT NULL,
|
||||
recharge_no VARCHAR(50) NOT NULL UNIQUE,
|
||||
amount BIGINT NOT NULL,
|
||||
payment_method VARCHAR(20) NOT NULL,
|
||||
payment_channel VARCHAR(50),
|
||||
payment_transaction_id VARCHAR(100),
|
||||
status INT NOT NULL DEFAULT 1,
|
||||
paid_at TIMESTAMP,
|
||||
completed_at TIMESTAMP,
|
||||
shop_id_tag BIGINT NOT NULL,
|
||||
enterprise_id_tag BIGINT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP,
|
||||
|
||||
-- 约束
|
||||
CONSTRAINT chk_card_recharge_amount CHECK (amount >= 100),
|
||||
CONSTRAINT chk_card_recharge_status CHECK (status IN (1, 2, 3, 4, 5)),
|
||||
CONSTRAINT chk_card_recharge_method CHECK (payment_method IN ('alipay', 'wechat'))
|
||||
);
|
||||
|
||||
-- 按用户查询充值记录索引
|
||||
CREATE INDEX idx_card_recharge_user ON tb_card_recharge_record (user_id, created_at DESC);
|
||||
|
||||
-- 按资源查询充值记录索引
|
||||
CREATE INDEX idx_card_recharge_resource ON tb_card_recharge_record (resource_type, resource_id, created_at DESC);
|
||||
|
||||
-- 按状态过滤充值记录索引
|
||||
CREATE INDEX idx_card_recharge_status ON tb_card_recharge_record (status, created_at DESC);
|
||||
|
||||
-- 按订单号查询索引
|
||||
CREATE UNIQUE INDEX idx_card_recharge_no ON tb_card_recharge_record (recharge_no);
|
||||
|
||||
-- 多租户过滤索引
|
||||
CREATE INDEX idx_card_recharge_shop_tag ON tb_card_recharge_record (shop_id_tag);
|
||||
|
||||
-- 企业标签索引
|
||||
CREATE INDEX idx_card_recharge_enterprise_tag ON tb_card_recharge_record (enterprise_id_tag) WHERE enterprise_id_tag IS NOT NULL;
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE tb_card_recharge_record IS '卡充值记录表';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.user_id IS '操作人用户 ID';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.card_wallet_id IS '卡钱包 ID';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.resource_type IS '资源类型(冗余字段)';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.resource_id IS '资源 ID(冗余字段)';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.recharge_no IS '充值订单号(格式:CRCH+时间戳+随机数)';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.amount IS '充值金额(单位:分,最小 100 分 = 1 元)';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.payment_method IS '支付方式:alipay-支付宝 | wechat-微信';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.payment_channel IS '支付渠道';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.payment_transaction_id IS '第三方支付交易号';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.status IS '充值状态:1-待支付 2-已支付 3-已完成 4-已关闭 5-已退款';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.paid_at IS '支付时间';
|
||||
COMMENT ON COLUMN tb_card_recharge_record.completed_at IS '完成时间';
|
||||
6
migrations/000065_drop_old_wallet_tables.down.sql
Normal file
6
migrations/000065_drop_old_wallet_tables.down.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
-- 回滚:恢复旧的钱包表结构
|
||||
-- 注意:此回滚脚本仅用于迁移失败的情况,实际使用中应避免回滚
|
||||
-- 旧系统已废弃,不建议回滚到旧表结构
|
||||
|
||||
-- 此处故意留空,因为钱包系统已完全重构
|
||||
-- 如需回滚,请手动参考旧版本的迁移文件重建表结构
|
||||
6
migrations/000065_drop_old_wallet_tables.up.sql
Normal file
6
migrations/000065_drop_old_wallet_tables.up.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
-- 删除旧的钱包相关表
|
||||
-- 新的钱包系统已完全迁移至 tb_agent_wallet、tb_card_wallet 等 6 张新表
|
||||
|
||||
DROP TABLE IF EXISTS tb_recharge_record;
|
||||
DROP TABLE IF EXISTS tb_wallet_transaction;
|
||||
DROP TABLE IF EXISTS tb_wallet;
|
||||
Reference in New Issue
Block a user