-- 支付模块基础表
CREATE TABLE IF NOT EXISTS `pay_app`
(
    `id`
    BIGINT
    UNSIGNED
    NOT
    NULL
    AUTO_INCREMENT
    COMMENT
    '应用编号',
    `app_key`
    VARCHAR
(
    64
) NOT NULL COMMENT '应用标识',
    `name` VARCHAR
(
    100
) NOT NULL COMMENT '应用名称',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
    `remark` VARCHAR
(
    255
) DEFAULT NULL COMMENT '备注',
    `order_notify_url` VARCHAR
(
    255
) DEFAULT NULL COMMENT '业务回调地址',
    `create_by` BIGINT DEFAULT NULL,
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `update_by` BIGINT DEFAULT NULL,
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT '0正常 1删除',
    PRIMARY KEY
(
    `id`
),
    UNIQUE KEY `uk_pay_app_key`
(
    `app_key`
)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT ='支付应用';

CREATE TABLE IF NOT EXISTS `pay_channel`
(
    `id`
    BIGINT
    UNSIGNED
    NOT
    NULL
    AUTO_INCREMENT
    COMMENT
    '渠道编号',
    `code`
    VARCHAR
(
    64
) NOT NULL COMMENT '渠道编码',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
    `remark` VARCHAR
(
    255
) DEFAULT NULL COMMENT '备注',
    `app_id` BIGINT UNSIGNED NOT NULL COMMENT '应用编号',
    `config` LONGTEXT NOT NULL COMMENT '渠道配置(JSON)',
    `create_by` BIGINT DEFAULT NULL,
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `update_by` BIGINT DEFAULT NULL,
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT '0正常 1删除',
    PRIMARY KEY
(
    `id`
),
    UNIQUE KEY `uk_pay_channel_app_code`
(
    `app_id`,
    `code`
)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT ='支付渠道';

CREATE TABLE IF NOT EXISTS `pay_order`
(
    `id`
    BIGINT
    UNSIGNED
    NOT
    NULL
    AUTO_INCREMENT
    COMMENT
    '支付订单编号',
    `app_id`
    BIGINT
    UNSIGNED
    NOT
    NULL
    COMMENT
    '应用编号',
    `merchant_order_no`
    VARCHAR
(
    64
) DEFAULT NULL COMMENT '业务订单号',
    `biz_order_type` VARCHAR
(
    64
) DEFAULT NULL COMMENT '业务类型',
    `biz_order_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '业务主键',
    `subject` VARCHAR
(
    128
) DEFAULT NULL COMMENT '订单标题',
    `body` VARCHAR
(
    255
) DEFAULT NULL COMMENT '订单描述',
    `channel_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '渠道编号',
    `channel_code` VARCHAR
(
    64
) DEFAULT NULL COMMENT '渠道编码',
    `notify_url` VARCHAR
(
    255
) DEFAULT NULL COMMENT '异步回调地址',
    `return_url` VARCHAR
(
    255
) DEFAULT NULL COMMENT '同步跳转地址',
    `price` DECIMAL
(
    18,
    2
) NOT NULL COMMENT '支付金额（元）',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态：1待支付 2支付中 3成功 4失败 5关闭',
    `user_ip` VARCHAR
(
    64
) DEFAULT NULL COMMENT '用户IP',
    `expire_time` DATETIME DEFAULT NULL COMMENT '失效时间',
    `success_time` DATETIME DEFAULT NULL COMMENT '支付成功时间',
    `extension_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '支付成功拓展单编号',
    `no` VARCHAR
(
    64
) DEFAULT NULL COMMENT '平台支付订单号',
    `channel_user_id` VARCHAR
(
    64
) DEFAULT NULL COMMENT '渠道用户编号',
    `channel_order_no` VARCHAR
(
    64
) DEFAULT NULL COMMENT '渠道订单号',
    `create_by` BIGINT DEFAULT NULL,
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `update_by` BIGINT DEFAULT NULL,
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT '0正常 1删除',
    PRIMARY KEY
(
    `id`
),
    UNIQUE KEY `uk_pay_order_no`
(
    `no`
),
    KEY `idx_pay_order_biz`
(
    `biz_order_type`,
    `biz_order_id`
)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT ='支付订单';

CREATE TABLE IF NOT EXISTS `pay_order_extension`
(
    `id`
    BIGINT
    UNSIGNED
    NOT
    NULL
    AUTO_INCREMENT
    COMMENT
    '拓展单编号',
    `no`
    VARCHAR
(
    64
) NOT NULL COMMENT '三方侧商户单号',
    `order_id` BIGINT UNSIGNED NOT NULL COMMENT '支付订单编号',
    `channel_id` BIGINT UNSIGNED NOT NULL COMMENT '渠道编号',
    `channel_code` VARCHAR
(
    64
) NOT NULL COMMENT '渠道编码',
    `user_ip` VARCHAR
(
    64
) DEFAULT NULL COMMENT '用户IP',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态：1待支付 2支付中 3成功 4失败 5关闭',
    `channel_extras` LONGTEXT DEFAULT NULL COMMENT '渠道扩展信息',
    `channel_error_code` VARCHAR
(
    64
) DEFAULT NULL COMMENT '渠道错误码',
    `channel_error_msg` VARCHAR
(
    255
) DEFAULT NULL COMMENT '渠道错误信息',
    `channel_notify_data` LONGTEXT DEFAULT NULL COMMENT '渠道回调原文',
    `create_by` BIGINT DEFAULT NULL,
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `update_by` BIGINT DEFAULT NULL,
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT '0正常 1删除',
    PRIMARY KEY
(
    `id`
),
    UNIQUE KEY `uk_pay_order_extension_no`
(
    `no`
),
    KEY `idx_pay_order_extension_order`
(
    `order_id`
)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT ='支付订单拓展单';

ALTER TABLE `hot_fund_recharge_order`
    MODIFY COLUMN `recharge_amount` DECIMAL (18, 2) NULL COMMENT '充值金额（元）',
    MODIFY COLUMN `pay_status` TINYINT NULL DEFAULT 1 COMMENT '支付状态：1待支付 2支付中 3支付成功 4支付失败 5已关闭 6已退款';

-- 初始化建议
-- INSERT INTO pay_app(app_key, name, status, remark) VALUES ('wallet-recharge', '钱包充值', 1, '钱包充值支付应用');
-- 渠道编码建议：
-- WECHAT_QR / WECHAT_H5 / WECHAT_APP / ALIPAY_QR / ALIPAY_H5 / ALIPAY_APP
-- 支付渠道 config 示例：
-- 支付宝：{"appId":"xxx","privateKey":"-----BEGIN PRIVATE KEY-----...","alipayPublicKey":"-----BEGIN PUBLIC KEY-----...","gateway":"https://openapi.alipay.com/gateway.do"}
-- 微信：{"appId":"wxxxx","merchantId":"1xxxx","merchantSerialNo":"xxxx","privateKey":"-----BEGIN PRIVATE KEY-----...","apiV3Key":"32位密钥","platformPublicKey":"-----BEGIN PUBLIC KEY-----...","gateway":"https://api.mch.weixin.qq.com"}
