/*!40101 SET NAMES utf8mb4 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE IF NOT EXISTS mall4cloud_payment DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

USE mall4cloud_payment;

/*Table structure for table `leaf_alloc` */

CREATE TABLE IF NOT EXISTS `leaf_alloc` (
  `biz_tag` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '区分业务',
  `max_id` bigint unsigned NOT NULL DEFAULT '1' COMMENT '该biz_tag目前所被分配的ID号段的最大值',
  `step` int NOT NULL COMMENT '每次分配的号段长度',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `description` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '描述',
  PRIMARY KEY (`biz_tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

/*Data for the table `leaf_alloc` */


/*Table structure for table `undo_log` */

DROP TABLE IF EXISTS `undo_log`;

CREATE TABLE `undo_log` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `branch_id` bigint NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int NOT NULL,
  `log_created` datetime NOT NULL,
  `log_modified` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

/*Data for the table `undo_log` */
USE mall4cloud_payment;

DELIMITER //
DROP PROCEDURE IF EXISTS createTable//
CREATE PROCEDURE createTable()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i<50 DO
        SET @create_pay_info_table = CONCAT(
                'CREATE TABLE IF NOT EXISTS pay_info_', i,
                "
                (
                  `pay_id` bigint unsigned NOT NULL COMMENT '支付单号',
                  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                  `user_id` bigint DEFAULT NULL COMMENT '用户id',
                  `order_ids` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '本次支付关联的多个订单号',
                  `biz_pay_no` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '外部订单流水号',
                  `sys_type` tinyint DEFAULT NULL COMMENT '系统类型 见SysTypeEnum',
                  `pay_entry` tinyint DEFAULT NULL COMMENT '支付入口 0下单 1余额充值',
                  `pay_type` tinyint DEFAULT NULL COMMENT '支付方式 1 微信支付 2 支付宝',
                  `pay_status` tinyint DEFAULT NULL COMMENT '支付状态',
                  `pay_score` bigint DEFAULT NULL COMMENT '支付积分',
                  `pay_amount` bigint DEFAULT NULL COMMENT '支付金额',
                  `version` int DEFAULT NULL COMMENT '版本号',
                  `callback_content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '回调内容',
                  `callback_time` datetime DEFAULT NULL COMMENT '回调时间',
                  `confirm_time` datetime DEFAULT NULL COMMENT '确认时间',
                  `refund_id` bigint DEFAULT NULL COMMENT '本次支付关联的退款号',
                  `pay_sys_type` int DEFAULT NULL COMMENT '系统支付类型 0.默认支付系统 1.通联支付',
                  `to_user_id` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '支付给商城的用户id，通联支付独有',
                  `system_model` tinyint DEFAULT NULL COMMENT '支付的系统类型 1:pc  2:h5  3:小程序 4:安卓  5:ios',
                  `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '第三方系统userid',
                  PRIMARY KEY (`pay_id`),
                  KEY `idx_biz_pay_no` (`biz_pay_no`),
                  KEY `idx_user_id` (`user_id`),
                  KEY `idx_order_ids` (`order_ids`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='订单支付记录'
                "
            );
        PREPARE create_pay_info_table FROM @create_pay_info_table;
        EXECUTE create_pay_info_table;




        SET @create_refund_info_table = CONCAT(
                'CREATE TABLE IF NOT EXISTS refund_info_', i,
                "
                (
                  `refund_id` bigint unsigned NOT NULL COMMENT '退款单号',
                  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                  `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
                  `order_id` bigint DEFAULT NULL COMMENT '关联的支付订单id',
                  `pay_id` bigint NOT NULL DEFAULT '0' COMMENT '关联的支付单id',
                  `pay_type` tinyint DEFAULT NULL COMMENT '支付方式',
                  `user_id` bigint DEFAULT NULL COMMENT '用户id',
                  `refund_status` tinyint DEFAULT NULL COMMENT '退款状态',
                  `refund_amount` bigint DEFAULT NULL COMMENT '退款金额',
                  `callback_content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '回调内容',
                  `callback_time` datetime DEFAULT NULL COMMENT '回调时间',
                  PRIMARY KEY (`refund_id`,`pay_id`),
                  KEY `idx_order_id` (`order_id`),
                  KEY `idx_pay_id` (`pay_id`),
                  KEY `idx_userid` (`user_id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='退款信息'
                "
            );
        PREPARE create_refund_info_table FROM @create_refund_info_table;
        EXECUTE create_refund_info_table;

        SET i = i+1;
    END WHILE;


END//
CALL createTable()//
DELIMITER ;