-- 创建数据库

drop database if exists anhei_account;
create database anhei_account DEFAULT CHARACTER SET utf8mb4;
use anhei_account;


-- 用于生成新的玩家 uid(游戏里的帐号uid，对应一个角色),考虑到分表，一个分表一条记录，每次加分表数量
drop table if exists tbuid;
create table tbuid (
	subIndex int not null,
	uid	   bigint not null,
	primary key (uid),
	index (`subIndex`)
) engine=InnoDB default charset=utf8mb4;


-- http pay
drop table if exists tbpay_http;
CREATE TABLE tbpay_http (
    orderId varchar(200) not null comment '订单号',
    uid bigint not null,
    isTestPay int(11) not null,
    method int(11) not null comment 'get 1, post 2',
    url varchar(200) not null comment 'http url',
    content varchar(10240) not null comment 'http content',
 
    PRIMARY KEY (orderId)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;


-- 战斗记录
drop table if exists tbAbyssRecording;
CREATE TABLE `tbAbyssRecording` (
  `abyssId` int(11) NOT NULL, 				-- id
  `recording` mediumblob,							-- bolb数据
  PRIMARY KEY (`abyssId`)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;

-- 已进入realm的玩家uid数据
drop table if exists tb_realm_uid_data;
CREATE TABLE `tb_realm_uid_data` (
  `realmId` int NOT NULL,
  `openTime` int not null default 0,
  `visibleTime` int not null default 0,
  `playerCount` int not null default 0,  -- 进入人数
  `isFull` int not null default 0,      
  `uidListCount` int not null default 0, -- uidList中的人数
  `uidData` blob,
  PRIMARY KEY (`realmId`)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;

-- realcfg 存储
DROP TABLE IF EXISTS `tb_realmcfg`;
create table tb_realmcfg
(
	areaName VARCHAR(100) NOT NULL  COMMENT '大区名称',
	worldId INT  NOT NULL COMMENT 'WORLDID',
	realmId INT  PRIMARY KEY NOT NULL COMMENT 'realmId',
	logicWorldId INT NOT NULL COMMENT '虚拟世界ID',
	realmName VARCHAR(100) NOT NULL COMMENT '区服名称',
	showSeq	INT NOT NULL COMMENT '展示',
	status INT NOT NULL COMMENT '状态',
	visibleOnlyWhiteList INT NOT NULL COMMENT '白名单',
	bdcName VARCHAR(100) NOT NULL COMMENT '上报给bdc的名称',
	openTime DATETIME NOT NULL COMMENT '开服时间',
	visibleTime DATETIME NOT NULL COMMENT '显示时间',
	bigRealmId INT NOT NULL COMMENT '跨服大区ID',
	regMax	INT NOT NULL COMMENT '注册上限',
	regNum	INT NOT NULL COMMENT '当前人数',
	index (realmId)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;

-- ----------------------------
-- Table structure for exchange
-- ----------------------------
DROP TABLE IF EXISTS `tbexchange_code`;
CREATE TABLE `tbexchange_code` (
`exchangeCodeId`  int NOT NULL ,
`exchangeCode`  varchar(64) NOT NULL UNIQUE,
`maxExchangeNum`  int ZEROFILL NOT NULL ,
`exchangeNum`  int ZEROFILL NOT NULL ,
`mainline`  int NOT NULL ,
`status`  int NOT NULL ,
`beginTime`  bigint NOT NULL ,
`endTime`  bigint NOT NULL ,
`content`  blob NOT NULL ,
`type`  int NOT NULL ,
PRIMARY KEY (`exchangeCodeId`)
)ENGINE=InnoDB DEFAULT charset=utf8mb4;



-- 替换结束符
delimiter //

-- 支付
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 10 DO
		SET @sqlstr = CONCAT(
		"create table tbpay_",
		`@i`,
		"(
			orderId 		varchar(200) not null,	-- 订单号,服务器生成
			payType 		int not null,			-- 支付类型
			productId 		varchar(100) not null,	-- 商品ID
			purchaseTime 	bigint not null, 		-- 购买时间
			uid				bigint not null,		-- 玩家uid
			status 			int not null, 			-- 订单状态 0=建立订单  1=支付成功  2=加钻石成功
			diamond			int not null default 0,			-- 钻石
			money			int not null,			-- 配置的金额
			orderId3rd 		varchar(200) not null default '',	-- 订单号,第三方支付平台
			diamondGift     int not null default 0,			-- 赠送钻石
			isTestPay       int not null default 0,			-- 测试支付
			refundTime		bigint not null default 0, 		-- 退款时间
			amount			int not null default 0,			-- 实际支付的金额
			currency 		varchar(16) not null default '',	-- 币种
			amountExchange  int not null default 0,			-- 兑换金额
			diamondExchange int not null default 0,			-- 兑换钻石数量
			subPayType 		int not null default 0,			-- 支付子类型, payType下的详细分类
			exData 	        tinyblob comment '额外数据',     -- 保存自选商品信息

			primary key (orderId),
			index (payType),
			index (productId),
			index (uid),
			index (status),
			index (orderId3rd)
		) engine=InnoDB default charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
//

-- 账号表
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 100 DO
		-- 创建账号表
		SET @sqlstr = CONCAT(
		"create table tbaccount_",
		`@i`,
		"(
			`accountType`			int not null,	
			`accountId`				varchar(256) not null,
			`accountToken`			varchar(8192) not null comment '账号的token, accountType=0时这里就是密码',
			`createTime`			bigint not null,
			`createDeviceId`		varchar(100) not null default '' comment '创建时的设备id，用于防刷',
			`createIpAddr`			int unsigned not null default 0 comment '创建时IP',
			`nick` 					varchar(64) not null default '',
			`gender` 				int not null default 0,
			`icon` 					varchar(200) not null default '',
			`money`				bigint not null default 0 comment '钱，平台币',
			`recharge`			bigint not null default 0 comment '充值了多少现金',
			`lastLoginRealm` 	int not null default 0,
			`grade` 	int not null default 0 comment '玩家评分',
			`exData` 	blob comment '其它数据',
			primary key ( `accountId`, `accountType` ),
		    index (`createTime` )
		) engine=InnoDB default charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;
		
		-- 创建账号关联uid和realm表
		SET @sqlstr = CONCAT(
		"create table tbacc_realmuid_link_",
		`@i`,
		"(
			`accountType`			int not null,	
			`accountId`				varchar(256) not null,
			`realm`					int not null,
			`uid`				    bigint not null,
			`nick`					varchar(64) not null default '',
			`icon`					varchar(200) not null default '',
			`level`					int not null default 0,
			`iconFrameId`			int not null default 0,
			`lastLoginTime` 		bigint(20) NOT NULL DEFAULT '0',
			primary key ( `accountType`,`accountId`,`realm`,`uid`)			
		) engine=InnoDB default charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
//


-- name表
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 10 DO
		SET @sqlstr = CONCAT(
		"create table tbname_",
		`@i`,
		"(
			`hashName`				varchar(200) not null,
			`strName`				varchar(200) not null,
			`realmId`				int not null,
			`accountId`				varchar(256) not null,
			PRIMARY KEY (`hashName`)
			) ENGINE=InnoDB DEFAULT charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
//

-- 恢复结束符;
delimiter ;
