-- =====================================================
-- 钱包系统数据库表创建脚本
-- 创建日期: 2026-01-23
-- 数据库: peixue
-- 说明: 创建钱包和钱包交易记录表
-- =====================================================

USE peixue;

-- =====================================================
-- 1. 创建钱包表
-- =====================================================
CREATE TABLE IF NOT EXISTS `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='钱包表';

-- =====================================================
-- 2. 创建钱包交易记录表
-- =====================================================
CREATE TABLE IF NOT EXISTS `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='钱包交易记录表';

-- =====================================================
-- 3. 插入测试数据
-- =====================================================

-- 为测试用户创建钱包（假设用户ID为1）
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)
ON DUPLICATE KEY UPDATE `updated_time` = CURRENT_TIMESTAMP;

-- =====================================================
-- 4. 验证表创建
-- =====================================================
SELECT '钱包表创建完成' AS message;
SELECT COUNT(*) AS wallet_count FROM `wallet`;

SELECT '钱包交易记录表创建完成' AS message;
SELECT COUNT(*) AS transaction_count FROM `wallet_transaction`;

-- =====================================================
-- 5. 显示表结构
-- =====================================================
SHOW CREATE TABLE `wallet`;
SHOW CREATE TABLE `wallet_transaction`;

-- =====================================================
-- 执行完成
-- =====================================================
SELECT '✅ 钱包系统数据库表创建完成！' AS result;
