Files
junhong_cmp_fiber/migrations/000060_create_tb_agent_wallet_transaction.up.sql
huang 18daeae65a
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m17s
feat: 钱包系统分离 - 代理钱包与卡钱包完全隔离
## 变更概述
将统一钱包系统拆分为代理钱包和卡钱包两个独立系统,实现数据表和代码层面的完全隔离。

## 数据库变更
- 新增 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>
2026-02-25 09:51:00 +08:00

61 lines
3.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 创建代理钱包交易记录表
-- 记录所有代理钱包余额变动
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 '扩展信息(如手续费、支付方式等)';