Files
qitongxue-api/sql/member_pay.sql
2026-09-17 17:38:45 +08:00

80 lines
4.7 KiB
Go
Raw Permalink 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.
-- ============================================================================
-- 会员购买微信小程序虚拟支付 · 道具直购
-- 适用个人主体小程序服务类目含工具月支付限额 10 万元
-- 执行前请确认已备份本脚本幂等性由 `IF NOT EXISTS` / 存在性判断保证
-- ============================================================================
-- 1. users等级有效期 + session_key ----------------------------------------
-- level_expire_at会员到期时间NULL = 永久管理员手动分配的等级保持 NULL
-- session_key 微信 session_key用户态签名 signature 需要
SET @db := DATABASE();
SET @sql := (
SELECT IF(COUNT(*) = 0,
'ALTER TABLE `users` ADD COLUMN `level_expire_at` datetime DEFAULT NULL COMMENT ''等级/会员到期时间NULL=永久'' AFTER `level_key`',
'SELECT ''users.level_expire_at already exists'' AS msg')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'users' AND COLUMN_NAME = 'level_expire_at'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @sql := (
SELECT IF(COUNT(*) = 0,
'ALTER TABLE `users` ADD COLUMN `session_key` varchar(128) DEFAULT NULL COMMENT ''微信 session_key虚拟支付签名用'' AFTER `openid`',
'SELECT ''users.session_key already exists'' AS msg')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'users' AND COLUMN_NAME = 'session_key'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 2. member_plans会员套餐 --------------------------------------------------
CREATE TABLE IF NOT EXISTS `member_plans` (
`id` bigint NOT NULL AUTO_INCREMENT,
`plan_key` varchar(32) NOT NULL COMMENT '套餐标识 vip-month',
`name` varchar(64) NOT NULL COMMENT '展示名 月卡',
`subtitle` varchar(128) DEFAULT NULL COMMENT '副标题',
`level_key` varchar(32) NOT NULL COMMENT '购买后生效的等级',
`duration_days` int NOT NULL DEFAULT 30 COMMENT '有效天数',
`price_cents` bigint NOT NULL DEFAULT 0 COMMENT '价格须与微信后台道具价格一致',
`product_id` varchar(64) NOT NULL COMMENT '微信虚拟支付道具 ID',
`badge` varchar(32) DEFAULT NULL COMMENT '角标文案',
`sort` int NOT NULL DEFAULT 0,
`is_enabled` tinyint NOT NULL DEFAULT 1 COMMENT '1 上架 / 0 下架',
`remark` varchar(255) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_plan_key` (`plan_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员套餐道具直购';
-- 3. member_orders会员订单 -------------------------------------------------
CREATE TABLE IF NOT EXISTS `member_orders` (
`id` bigint NOT NULL AUTO_INCREMENT,
`out_trade_no` varchar(64) NOT NULL COMMENT '业务单号唯一8-32 位且不能以下划线开头',
`wx_order_id` varchar(64) DEFAULT NULL COMMENT '平台单号发货/对账以此为准用于幂等去重',
`user_id` bigint NOT NULL,
`openid` varchar(64) NOT NULL COMMENT '支付时使用的 openid主动查单需要',
`plan_key` varchar(32) NOT NULL,
`product_id` varchar(64) NOT NULL,
`price_cents` bigint NOT NULL DEFAULT 0,
`level_key` varchar(32) NOT NULL COMMENT '等级快照',
`duration_days` int NOT NULL DEFAULT 0 COMMENT '天数快照',
`status` tinyint NOT NULL DEFAULT 0 COMMENT '0 待支付 / 1 已发货 / 2 已退款 / 3 已关闭',
`pay_channel` varchar(16) DEFAULT NULL COMMENT 'wxpay / apple',
`attach` varchar(128) DEFAULT NULL COMMENT '透传数据',
`paid_at` datetime DEFAULT NULL,
`delivered_at` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_out_trade_no` (`out_trade_no`),
UNIQUE KEY `uk_wx_order_id` (`wx_order_id`),
KEY `idx_user_created` (`user_id`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员订单虚拟支付道具直购';
-- 4. 套餐数据 ----------------------------------------------------------------
-- 套餐种子由服务端 internal/logic/seed.go 在启动时幂等写入与其他种子数据一致
-- 请在那里修改 product_id / 价格后重启服务即可
-- 注意product_id 必须先在 MP 后台虚拟支付 道具管理创建并发布
-- 价格也要与后台道具价格严格一致否则支付会被平台拒绝