DROP TABLE IF EXISTS `pay_channel`;
CREATE TABLE `pay_channel`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `channel_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '渠道编码（WECHAT,ALIPAY）',
  `channel_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '渠道名称（微信、支付宝）',
  `status` int NULL DEFAULT NULL COMMENT '是否启用(0-禁用，1-启用)',
  `config_json` JSON NOT NULL COMMENT '支付配置（JSON格式）',
  `sort` int NULL DEFAULT NULL COMMENT '排序',
  `deleted` tinyint(1) NOT NULL default 0 COMMENT '逻辑删除',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uk_channel_code`(`channel_code`),
  INDEX `idx_status`(`status`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci ;

INSERT INTO pay_channel (channel_code,channel_name,status,config_json,sort) VALUES
	 ('WECHAT_JSAPI','微信支付',1,'{"appId": "Ja3IAKHBlHvYRvmSlT3FEtarmjKSKNo8YpuRTpNX5Fo%3D", "mchId": "7rj3bZfpyZ0aOjRQK0%2FYeQ%3D%3D", "mchKey": "9keMBYSBHgakxLFNpkyUM9xgiMd7ih09%2FDR3yp%2FP830SnzHMN7Z1Stn0%2FOUWghc8", "secret": "2O2yvq30JsVmA4yVYTB%2F2safeZ6zDX828LU31tsAY4dMcIFNHcEwMH93PSMDZBrm", "apiV3Key": "flAa9xheWEhZ5akZmFNizF%2BhKVFMVIH573k0vSoIWmmMTN8yKCvcRiGwuYT8kxpr", "wxLoginUrl": "https://api.weixin.qq.com/sns/jscode2session", "publicKeyId": "iHPdjJNcA8T9PZ92Psqz8DFDPRRu5gcmggueHPyBw0Lmt1HQrffQMIyllT7u49h9", "getQrCodeUrl": "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=", "publicKeyPem": "80meMdjDCc5a5%2BdtILL8BOcocGczBiCgpEE8w0dy5ehxxzGngvxRnvzvfbQJNHA9svGXqQsBfz1g%2FYfUM12qy9wtWSGH5XdqTUPGXjSIaBXp27gTzl8jN48%2B%2FA%2FQyVdUS9RiGkemeFMXpfDCji7H%2B%2FBWVBAO5Y9VKFlcFyRfpZdMPgB9tcJ9Z13SmbxNN5sNh2%2B0MlbZcCnwywuHWKiGugulARb8dXcEvsxwkh4VOabN2gHob9dEI32TQXnZxfL3f5%2FL4oEuQobJxdjpGaaFqwM03PszyZzoqCIZ9Y5bU3cCL5YjXjwGiapBD4pyhzCvSBZFasH%2BNeu1MQNJqA7aR0iz3hG1uQgx8a2e6LoToegHof6DbUvuSWhRa8mL3nj3KCRZRTLzt5qftDui5Mrv4zlS8Dd6kan4HGiSf3ZDx1SdujczjZvcEvAoQZwro1MywMSrFpVxcaG4OzKBLOnvIUtSvjTgXCrPVqWc6%2BwS1eY9MkLHeMxMnGSAOYKp1ezjPiLz2%2F%2BMGJVWl2q236Dw6MMNcZi1RGcL0SCTfQYm2AborzzD7srSuyufocNxEFYPwh2SRs3We5sGJ0yB255TZA%3D%3D", "privateKeyPem": "%2BB3MZV%2B1DJ%2BMzXRrBDkycUc43ZBU9ZXYgCIovE7diVD983puTDi3tvEnbl48TRQi61D8ChYpsiYCYAafuq83zaJyEznsvHa%2BI7zY9T8%2B7dPQkDKUzNq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7DdobKSoZlzXckFD3cwJ3uxrdggN8LWwAklt%2F67zVQD6%2FGThBwHq1CoBMON6VvyFa%2Fh9uX%2BDbojQ0KXJLPLCxvd21DjGTBOZHN", "jsPayNotifyUrl": "https://zs.seerteach.net/seer/pay/app/pay/callback/WECHAT_JSAPI", "redisAccessKey": "wxAccessToken", "getAccessTokenUrl": "https://api.weixin.qq.com/cgi-bin/token", "jsRefundNotifyUrl": "https://zs.seerteach.cn/wechat/wallet/refund/notify", "merchantSerialNumber": "LQjQEadgqSHuwkPFBkuVFkLJzw2HRP8AITYSZXPln6EQ0KyeZqgGo53lfg0OfORJ"}',1),
	 ('COIN','学豆支付',1,'{}',2),
	 ('ALIPAY','支付宝支付',0,'{}',3);

DROP TABLE IF EXISTS `pay_order`;
CREATE TABLE `pay_order`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '支付订单编号，数据库自增',
  `order_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '支付内部订单号,对应微信和支付宝的商户订单号（out_trade_no）字段，要求6-32个字符内，只能是数字、大小写字母_-|* 且在同一个商户号下唯一',
  `channel_id` bigint NULL DEFAULT NULL COMMENT '渠道编号',
  `channel_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '渠道编码',
  `user_id` bigint NULL DEFAULT NULL COMMENT '用户ID',
  `order_source` tinyint NULL DEFAULT NULL COMMENT '订单来源',
  `merchant_order_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '商户订单编号',
  `merchant_order_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '渠道订单号',
  `subject` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '商品标题',
  `body` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '商品描述',
  `notify_url` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '异步通知地址',
  `total_amount` bigint NULL COMMENT '实际支付订单总金额，单位为分，整型，必须大于0',
  `currency` varchar(6) NULL DEFAULT 'CNY' COMMENT '币种,默认为CNY，代表人民币',
  `actual_coin_price` DECIMAL(12,2) NULL COMMENT '实际支付的学豆',
  `coin_config_id` bigint NULL DEFAULT NULL COMMENT '学豆兑换比例配置ID',
  `exchange_ratio` DECIMAL(18, 2) NOT NULL COMMENT '兑换比例',
  `channel_fee_rate` DECIMAL(12,2) NULL DEFAULT 0 COMMENT '渠道手续费，单位：百分比',
  `channel_fee_price` bigint NULL DEFAULT 0 COMMENT '渠道手续金额，单位：分',
  `status` tinyint NOT NULL COMMENT '支付状态(0-待支付，1-支付成功，2-支付失败，3-申请退款，4-取消支付)',
  `transaction_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL,
  `user_ip` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '用户 IP',
  `expire_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单失效时间',
  `success_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单支付成功时间',
  `refund_price` bigint NULL DEFAULT NULL COMMENT '退款总金额，单位：分',
  `deleted` bit(1) NULL DEFAULT b'0' COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE,
  INDEX `idx_merchant_order_id`(`merchant_order_id` ASC) USING BTREE,
  INDEX `idx_merchant_order_sn`(`merchant_order_sn` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '支付订单';

DROP TABLE IF EXISTS `pay_order_extension`;
CREATE TABLE `pay_order_extension`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '订单拓展编号，数据库自增',
  `merchant_order_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '商户系统内部订单号，要求6-32个字符内，只能是数字、大小写字母_-|* 且在同一个商户号下唯一',
  `order_id` bigint NOT NULL COMMENT '订单号,pay_order表主键',
  `channel_extras` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '支付渠道的额外参数',
  `channel_error_code` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '渠道调用报错时，错误码',
  `channel_error_msg` varchar(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '渠道调用报错时，错误信息',
  `channel_notify_data` varchar(4096) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '支付渠道异步通知的内容',
  `deleted` bit(1) NOT NULL default 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '支付订单';



DROP TABLE IF EXISTS `pay_refund_order`;
CREATE TABLE `pay_refund_order`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NULL DEFAULT NULL COMMENT '用户ID',
  `refund_id` varchar(64) NULL DEFAULT NULL COMMENT '渠道商的退款成功全局唯一标识（如微信的退款编号refund_id，支付宝的notify_id）',
  `refund_sn` varchar(50) NULL DEFAULT NULL COMMENT '渠道商的退款号（如微信的退款编号，支付宝的退款编号）',
  `pay_order_sn` varchar(50) NULL DEFAULT NULL COMMENT '支付模块的原支付订单号',
  `merchant_order_sn` varchar(50) NOT NULL COMMENT '外部系统的商户原订单SN',
  `merchant_refund_sn` varchar(50) NOT NULL  COMMENT '外部系统的商户订原退款单号',
  `channel_code` varchar(20) NULL DEFAULT NULL COMMENT '支付渠道编码',
  `refund_reason` varchar(200) NULL DEFAULT NULL COMMENT '退款原因',
  `refund_amount` bigint NULL DEFAULT NULL COMMENT '退款金额（分）',
  `total_amount` bigint NULL DEFAULT NULL COMMENT '原订单总金额（分）',
  `transaction_id` varchar(255) DEFAULT NULL COMMENT '渠道方的交易流水号,微信支付返回的交易流水号',
  `status` TINYINT(1) NULL DEFAULT 0 COMMENT '退款状态（0处理中，1成功，2失败，3关闭）',
  `refund_type` int NULL DEFAULT NULL COMMENT '退款类型（1-售后退款，2-订单取消退款，3-学豆充值退款））',
  `success_time` datetime NULL DEFAULT NULL COMMENT '退款成功时间',
  `remark` varchar(200) NULL DEFAULT NULL COMMENT '备注',
  `response_json` text COMMENT '响应原始数据',
  `deleted` tinyint(1) NOT NULL COMMENT '逻辑删除标记（0-正常，1-已删除）',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_by` varchar(20) NULL DEFAULT NULL COMMENT '创建人',
  `update_by` varchar(20) NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) NULL DEFAULT NULL COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE COMMENT '用户ID',
  INDEX `idx_refund_sn`(`refund_sn` ASC) USING BTREE COMMENT '退款订单号',
  INDEX `idx_pay_order_sn`(`pay_order_sn` ASC) USING BTREE COMMENT '原支付订单号',
  INDEX `idx_merchant_order_sn` (`merchant_order_sn`),
  INDEX `idx_merchant_refund_sn` (`merchant_refund_sn`)
) ENGINE = InnoDB COMMENT='退款订单';

-- ----------------------------
-- Table structure for pay_notify_log
-- ----------------------------
DROP TABLE IF EXISTS `pay_notify_log`;
CREATE TABLE `pay_notify_log`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '日志编号',
  `task_id` bigint NOT NULL COMMENT '通知任务编号',
  `notify_times` tinyint NOT NULL COMMENT '第几次被通知',
  `response` varchar(2048) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '请求参数',
  `status` tinyint NOT NULL COMMENT '通知状态',
  `deleted` bit(1) NOT NULL default 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '支付通知 App 的日志';

-- ----------------------------
-- Table structure for pay_notify_task
-- ----------------------------
DROP TABLE IF EXISTS `pay_notify_task`;
CREATE TABLE `pay_notify_task`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '任务编号',
  `type` tinyint NOT NULL COMMENT '通知类型',
  `data_id` bigint NOT NULL COMMENT '数据编号',
  `merchant_order_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '商户订单编号（商户系统生成）',
  `merchant_refund_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '商户退款编号（商户系统生成）',
  `merchant_transfer_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '商户转账编号（商户系统生成）',
  `status` tinyint NOT NULL COMMENT '通知状态',
  `next_notify_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '下一次通知时间',
  `last_execute_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次执行时间',
  `notify_times` tinyint NOT NULL COMMENT '当前通知次数',
  `max_notify_times` tinyint NOT NULL COMMENT '最大可通知次数',
  `notify_url` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '异步通知商户地址',
  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL DEFAULT 'Default' COMMENT '租户编号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '商户支付、退款等的通知';


DROP TABLE IF EXISTS `pay_coin_config`;
CREATE TABLE `pay_coin_config`  (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
  `coin_name` varchar(255)  NOT NULL COMMENT '名称',
  `exchange_ratio` DECIMAL(18, 2) NOT NULL COMMENT '兑换比例',
  `is_used` tinyint NOT NULL DEFAULT 1 COMMENT '是否启用：0-禁用，1-启用',
  `deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB COMMENT = '账户配置表' ROW_FORMAT = DYNAMIC;

INSERT INTO pay_coin_config(coin_name, exchange_ratio, is_used, deleted)VALUES('学豆', 12.00, 1, 0);

DROP TABLE IF EXISTS `pay_coin_freeze_log`;
CREATE TABLE `pay_coin_freeze_log`  (
  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
  `user_id` bigint UNSIGNED NOT NULL COMMENT '用户ID',
  `account_id` bigint UNSIGNED NOT NULL COMMENT '账户ID',
  `order_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '关联订单编号',
  `freeze_amount` decimal(18, 2) NOT NULL COMMENT '冻结金额（单位：分）',
  `status` tinyint NOT NULL DEFAULT 0 COMMENT '状态：0-已冻结，1-已解冻，2-已扣除',
  `freeze_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '冻结时间',
  `unfreeze_time` datetime NULL DEFAULT NULL COMMENT '解冻时间',
  `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_order_sn`(`order_sn` ASC) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '账户冻结记录表' ROW_FORMAT = DYNAMIC;


DROP TABLE IF EXISTS `pay_coin_recharge`;
CREATE TABLE `pay_coin_recharge`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL COMMENT '用户ID',
  `recharge_amount` decimal(18, 2) NULL DEFAULT NULL COMMENT '充值金额,通过兑换比例计算获得，充值学豆 * 兑换比例',
  `recharge_coin` decimal(18, 2) NULL DEFAULT NULL COMMENT '充值学豆',
  `recharge_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '充值订单号',
  `coin_config_id` int NULL DEFAULT NULL COMMENT '学豆配置id',
  `recharge_time` datetime NULL DEFAULT NULL COMMENT '充值时间',
  `pay_status` tinyint NULL DEFAULT NULL COMMENT '支付状态{0-付款中;1-已付款;2-付款失败;3-退款}',
  `pay_success_time` datetime NULL DEFAULT NULL COMMENT '充值时间',
  `deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE,
  INDEX `idx_recharge_sn`(`recharge_sn` ASC) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb4 COMMENT = '学豆充值表' ROW_FORMAT = DYNAMIC;


DROP TABLE IF EXISTS `pay_coin_refund`;
CREATE TABLE `pay_coin_refund`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL COMMENT '用户ID',
  `refund_sn` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '退款编号',
  `recharge_id` int NOT NULL,
  `refund_reason` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '退款原因',
  `actual_refund_amount` int NULL DEFAULT NULL COMMENT '实际退款金额',
  `refund_success_time` datetime NULL DEFAULT NULL COMMENT '退款成功作时间',
  `status` tinyint(1) NULL DEFAULT NULL COMMENT '状态（0待审核，1审核通过，2审核拒绝，3：待微信退款，4：微信退款成功，5微信退款失败)',
  `refund_amount` decimal(18, 2) NULL DEFAULT NULL COMMENT '退款金额',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL,
  `deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci ROW_FORMAT = DYNAMIC;


DROP TABLE IF EXISTS `pay_coin_refund_log`;
CREATE TABLE `pay_coin_refund_log`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `refund_id` int NOT NULL COMMENT '退款id',
  `operation_name` varchar(500) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '操作名称',
  `operation_time` datetime NULL DEFAULT NULL COMMENT '操作时间',
  `status` tinyint(1) NULL DEFAULT NULL COMMENT '状态（0待审核，1审核通过，2审核拒绝，3：待微信退款，4：微信退款成功，5微信退款失败)',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL,
  `request_json` json NULL COMMENT '微信接口请求json',
  `response_json` json NULL COMMENT '微信接口响应json',
  `deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_refund_id`(`refund_id` ASC) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '退款操作日志表' ROW_FORMAT = DYNAMIC;


DROP TABLE IF EXISTS `pay_coin_account`;
CREATE TABLE `pay_coin_account`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL COMMENT '用户ID',
  `freeze_balance` decimal(18, 2) NULL DEFAULT NULL COMMENT '冻结余额',
  `available_balance` decimal(18, 2) NULL DEFAULT NULL COMMENT '可用余额',
  `total_amount` decimal(18, 2) NULL DEFAULT NULL COMMENT '冻结余额',
  `is_block` tinyint(1) NULL DEFAULT NULL COMMENT '交易状态（0正常，1锁定）',
  `deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci COMMENT = '用户账户表' ROW_FORMAT = DYNAMIC;


DROP TABLE IF EXISTS `pay_coin_account_trade_log`;
CREATE TABLE `pay_coin_account_trade_log`  (
  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` bigint UNSIGNED NOT NULL COMMENT '用户ID',
  `trade_type` tinyint NOT NULL COMMENT '交易类型: 1-充值，2-微信退款, 3-兑换, 4-购买, 5-退货',
  `trade_name` varchar(500) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '操作名称',
  `trade_time` datetime NULL DEFAULT NULL COMMENT '操作时间',
  `related_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '关联业务ID（如课程ID、订单ID）',
  `before_balance` decimal(18, 2) NOT NULL COMMENT '交易前余额',
  `trade_amount` decimal(18, 2) NOT NULL COMMENT '交易金额',
  `after_balance` decimal(18, 2) NOT NULL COMMENT '交易后余额',
  `remark` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '备注信息',
  `deleted` bit(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default'  COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB  CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '用户账户交易明细表' ROW_FORMAT = Dynamic;