peixue-dev/Archive/peidu-temp-files/sql/create_wallet_tables_fixed_2026-01-23.sql

98 lines
4.5 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.

-- =====================================================
-- 钱包系统数据库表创建脚本(修复版)
-- 创建日期: 2026-01-23
-- 数据库: peixue
-- 说明: 创建钱包和钱包交易记录表
-- =====================================================
USE peixue;
-- =====================================================
-- 1. 删除旧表(如果存在)
-- =====================================================
DROP TABLE IF EXISTS `wallet_transaction`;
DROP TABLE IF EXISTS `wallet`;
-- =====================================================
-- 2. 创建钱包表
-- =====================================================
CREATE TABLE `wallet` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` BIGINT NOT NULL COMMENT '用户ID',
`balance` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '余额',
`frozen_amount` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '冻结金额',
`total_recharge` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '累计充值',
`total_consume` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '累计消费',
`total_withdraw` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '累计提现',
`points` INT DEFAULT 0 COMMENT '积分余额',
`status` TINYINT DEFAULT 1 COMMENT '状态0-停用1-启用',
`version` INT NOT NULL DEFAULT 0 COMMENT '乐观锁版本号',
`tenant_id` BIGINT COMMENT '租户ID',
`created_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` TINYINT DEFAULT 0 COMMENT '删除标记',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_id` (`user_id`),
KEY `idx_tenant_id` (`tenant_id`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='钱包表';
-- =====================================================
-- 3. 创建钱包交易记录表
-- =====================================================
CREATE TABLE `wallet_transaction` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` BIGINT NOT NULL COMMENT '用户ID',
`wallet_id` BIGINT NOT NULL COMMENT '钱包ID',
`order_id` BIGINT COMMENT '关联订单ID',
`transaction_type` VARCHAR(50) NOT NULL COMMENT '交易类型recharge-充值consume-消费refund-退款withdraw-提现income-收益',
`amount` DECIMAL(10,2) NOT NULL COMMENT '交易金额',
`balance_before` DECIMAL(10,2) NOT NULL COMMENT '交易前余额',
`balance_after` DECIMAL(10,2) NOT NULL COMMENT '交易后余额',
`transaction_no` VARCHAR(100) COMMENT '交易流水号',
`payment_channel` VARCHAR(50) COMMENT '支付渠道wechat,alipay',
`status` TINYINT DEFAULT 1 COMMENT '状态0-失败1-成功2-处理中',
`remark` VARCHAR(500) COMMENT '备注',
`tenant_id` BIGINT COMMENT '租户ID',
`created_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` TINYINT DEFAULT 0 COMMENT '删除标记',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_wallet_id` (`wallet_id`),
KEY `idx_order_id` (`order_id`),
KEY `idx_transaction_type` (`transaction_type`),
KEY `idx_transaction_no` (`transaction_no`),
KEY `idx_created_time` (`created_time`),
KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='钱包交易记录表';
-- =====================================================
-- 4. 插入测试数据
-- =====================================================
INSERT INTO `wallet` (user_id, balance, frozen_amount, total_recharge, total_consume, total_withdraw, points, status, version, tenant_id)
VALUES
(1, 0.00, 0.00, 0.00, 0.00, 0.00, 0, 1, 0, 1),
(2, 0.00, 0.00, 0.00, 0.00, 0.00, 0, 1, 0, 1),
(3, 0.00, 0.00, 0.00, 0.00, 0.00, 0, 1, 0, 1);
-- =====================================================
-- 5. 验证表创建
-- =====================================================
SELECT '✅ 钱包表创建完成' AS result;
SELECT COUNT(*) AS wallet_count FROM `wallet`;
SELECT '✅ 钱包交易记录表创建完成' AS result;
SELECT COUNT(*) AS transaction_count FROM `wallet_transaction`;
-- =====================================================
-- 6. 显示表结构
-- =====================================================
DESC `wallet`;
DESC `wallet_transaction`;
-- =====================================================
-- 执行完成
-- =====================================================
SELECT '✅✅✅ 钱包系统数据库表创建完成!' AS final_result;