/*
 Navicat Premium Dump SQL

 Source Server         : agent
 Source Server Type    : MySQL
 Source Server Version : 80036 (8.0.36)
 Source Host           : rm-bp1btyuwq77591x0jpo.mysql.rds.aliyuncs.com:3306
 Source Schema         : agent_db_temp

 Target Server Type    : MySQL
 Target Server Version : 80036 (8.0.36)
 File Encoding         : 65001

 Date: 15/03/2026 19:31:42
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for gold_account
-- ----------------------------
DROP TABLE IF EXISTS `gold_account`;
CREATE TABLE `gold_account`  (
  `idx` int NOT NULL AUTO_INCREMENT,
  `goac_id` varchar(23) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT 'id，时间戳+房号+4位随机码',
  `goac_agentid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '代理商id',
  `goac_gameid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '游戏id',
  `goac_roomcode` int NOT NULL COMMENT '房间号',
  `goac_roommode` tinyint(1) NOT NULL COMMENT '0-星星场玩家房间 1-星星场系统房间 2-非星星场(星星限进)',
  `goac_createtime` datetime NOT NULL COMMENT '房间的创建时间',
  `goac_beanmult` int NOT NULL COMMENT '倍数',
  `goac_ownerid` int NULL DEFAULT NULL COMMENT '房主的玩家id',
  `goac_shortcode` int NULL DEFAULT NULL COMMENT '短号',
  `goac_deduct` tinyint(1) NULL DEFAULT NULL COMMENT '0-房主扣卡 1-每人扣卡 2-大赢家扣卡',
  `goac_roomcard` int NULL DEFAULT NULL COMMENT '扣的房卡数量',
  `goac_datatype` tinyint(1) NOT NULL COMMENT '0-大局结算数据 1-小局结算数据',
  `goac_asetcount` int NULL DEFAULT NULL COMMENT '总局数',
  `goac_asetnum` int NULL DEFAULT NULL COMMENT '当前小局号，从1开始计数',
  `goac_playerid` int NULL DEFAULT NULL COMMENT '玩家id',
  `goac_isrobot` tinyint(1) NULL DEFAULT 0 COMMENT '0-真实玩家 1-机器人',
  `goac_grade` int NOT NULL COMMENT '玩家得分',
  `goac_rebatefrom` tinyint(1) NOT NULL COMMENT '0-不抽成 1-对大赢家进行抽成 2-对所有赢家抽成 3-对所有玩家抽成',
  `goac_rebateto` tinyint(1) NULL DEFAULT NULL COMMENT '0-抽成给系统 1-抽成给房主 2-抽成给指定玩家 3-抽成给指定玩家的代理账号',
  `goac_toplayerid` int NULL DEFAULT NULL COMMENT '抽成给到的玩家id',
  `goac_rebatemode` tinyint(1) NULL DEFAULT NULL COMMENT '1-按得分的百分比抽成 2-按倍数的百分比抽成 3-按固定值抽成',
  `goac_setvalue` int NULL DEFAULT NULL COMMENT '抽成设置的值',
  `goac_rebatevalue` int NULL DEFAULT NULL COMMENT '实际抽成数量',
  `goac_time` datetime NOT NULL COMMENT '发生时间',
  `goac_state` tinyint(1) NULL DEFAULT 0 COMMENT '代理后台处理状态 0:未处理 1:已处理',
  `goac_statetime` datetime NULL DEFAULT NULL COMMENT '代理后台处理时间',
  `goac_invitecode` int NULL DEFAULT NULL,
  `goac_inviteid` int NULL DEFAULT NULL,
  `goac_marketid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `goac_i1` int NULL DEFAULT NULL,
  `goac_i2` int NULL DEFAULT NULL,
  `goac_i3` int NULL DEFAULT NULL,
  PRIMARY KEY (`idx`) USING BTREE,
  INDEX `gold_account_index2`(`goac_agentid` ASC, `goac_playerid` ASC) USING BTREE,
  INDEX `gold_account_index1`(`goac_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 18149691 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '金币变动表' ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Table structure for player
-- ----------------------------
DROP TABLE IF EXISTS `player`;
CREATE TABLE `player`  (
  `idx` int NOT NULL AUTO_INCREMENT,
  `play_agentid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '代理商id',
  `play_playerid` int NOT NULL COMMENT '玩家id',
  `play_channelid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_openid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '微信openid',
  `play_unionid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '微信unionid',
  `play_nickname` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '玩家昵称',
  `play_avatar` varchar(200) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '头像',
  `play_sex` tinyint(1) NULL DEFAULT NULL COMMENT '性别',
  `play_province` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '省',
  `play_city` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '城市',
  `play_roomcard` int NULL DEFAULT 8 COMMENT '房卡数量',
  `play_bean` int NULL DEFAULT 0 COMMENT '金币',
  `play_regtime` datetime NOT NULL COMMENT '注册时间',
  `play_lasttime` datetime NOT NULL COMMENT '最后登录时间',
  `play_logindate` int NULL DEFAULT 1 COMMENT '累积登录天数',
  `play_usecard` int NULL DEFAULT 0 COMMENT '累积使用房卡数量',
  `play_taskaward` int NULL DEFAULT 0 COMMENT '累积任务奖励房卡数量',
  `play_type` tinyint(1) NULL DEFAULT 0 COMMENT '用户类型 0:普通用户 1:vip开房不扣房卡',
  `play_score` int NULL DEFAULT 0,
  `play_a_country` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_a_province` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_a_city` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_a_citycode` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_a_district` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_a_street` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_a_address` varchar(200) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_longitude` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_latitude` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_invitecode` int NULL DEFAULT NULL,
  `play_inviteid` int NULL DEFAULT NULL,
  `play_state` tinyint(1) NULL DEFAULT 0,
  `play_advanced` tinyint NULL DEFAULT 0,
  `play_shortcode` int NULL DEFAULT NULL,
  `play_roomcodes` varchar(4000) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_desone` varchar(400) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_destwo` varchar(400) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_whitelist` varchar(6000) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_limit` int NULL DEFAULT NULL,
  `play_notice` varchar(400) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_bankpower` tinyint(1) NULL DEFAULT 1,
  `play_bank` int NULL DEFAULT 0,
  `play_bankpwd` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_tel` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_wechat` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_marketid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_phoneinfo` varchar(8000) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `play_sign` varchar(400) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`idx`) USING BTREE,
  UNIQUE INDEX `player_index2`(`play_agentid` ASC, `play_unionid` ASC) USING BTREE,
  UNIQUE INDEX `player_index1`(`play_agentid` ASC, `play_playerid` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 136111 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '玩家表' ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Table structure for player_grade
-- ----------------------------
DROP TABLE IF EXISTS `player_grade`;
CREATE TABLE `player_grade`  (
  `idx` int NOT NULL AUTO_INCREMENT,
  `plgr_agentid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '代理商id',
  `plgr_playerid` int NOT NULL COMMENT '玩家id',
  `plgr_gameid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '游戏id',
  `plgr_roomcode` int NOT NULL COMMENT '房号',
  `plgr_ownerid` int NULL DEFAULT NULL COMMENT '房主id',
  `plgr_roomtype` varchar(200) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '房间类型',
  `plgr_createtime` datetime NOT NULL COMMENT '开房时间',
  `plgr_makewartime` datetime NOT NULL COMMENT '开战时间',
  `plgr_overtime` datetime NOT NULL COMMENT '结束时间',
  `plgr_roomcard` int NULL DEFAULT NULL,
  `plgr_ownercard` int NULL DEFAULT NULL,
  `plgr_gameinfo1` varchar(10000) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT '' COMMENT '一局游戏的完整数据 json格式',
  `plgr_gameinfo2` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL COMMENT '一局游戏的完整数据 json格式',
  `plgr_shortcode` int NULL DEFAULT NULL,
  `plgr_deducttype` tinyint(1) NULL DEFAULT NULL,
  `plgr_deductcard` int NULL DEFAULT NULL,
  `plgr_asetcount` int NULL DEFAULT NULL,
  `plgr_roommode` tinyint(1) NULL DEFAULT NULL,
  `plgr_winner` tinyint(1) NOT NULL DEFAULT 0 COMMENT '大赢家',
  `plgr_score` int NOT NULL DEFAULT 0 COMMENT '得分',
  PRIMARY KEY (`idx`) USING BTREE,
  INDEX `player_grade2_index`(`plgr_agentid` ASC, `plgr_playerid` ASC, `plgr_gameid` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 33678017 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '玩家战绩表' ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Table structure for sales_transferbill
-- ----------------------------
DROP TABLE IF EXISTS `sales_transferbill`;
CREATE TABLE `sales_transferbill`  (
  `idx` int NOT NULL AUTO_INCREMENT,
  `satr_agentid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '代理商id',
  `satr_openid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '微信openid',
  `channel_id` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '渠道id',
  `from_sales` int NULL DEFAULT NULL COMMENT '来源代理号',
  `satr_salesid` int NOT NULL COMMENT '个人代理id',
  `satr_amount` int NOT NULL COMMENT '转卡数量',
  `satr_transfertime` datetime NOT NULL COMMENT '转卡时间',
  PRIMARY KEY (`idx`) USING BTREE,
  INDEX `nk_transferbill_agent`(`satr_agentid` ASC, `channel_id` ASC) USING BTREE,
  INDEX `nk_transferbill_openid`(`satr_agentid` ASC, `channel_id` ASC, `satr_openid` ASC) USING BTREE,
  INDEX `nk_transferbill_salesid`(`satr_agentid` ASC, `channel_id` ASC, `satr_salesid` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 18554 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '个人代理转卡记录表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for sales_user
-- ----------------------------
DROP TABLE IF EXISTS `sales_user`;
CREATE TABLE `sales_user`  (
  `idx` int NOT NULL AUTO_INCREMENT,
  `user_id` int NULL DEFAULT NULL COMMENT '用户号',
  `saus_agentid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '代理商id',
  `saus_channelid` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `saus_openid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '微信openid',
  `saus_unionid` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '微信unionid',
  `saus_nickname` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '昵称',
  `saus_avatar` varchar(200) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '头像',
  `saus_sex` tinyint(1) NULL DEFAULT NULL COMMENT '性别',
  `saus_province` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '省',
  `saus_city` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '城市',
  `saus_firsttime` datetime NOT NULL COMMENT '第一次登登录时间',
  `saus_lasttime` datetime NOT NULL COMMENT '最后一次登录时间',
  `saus_salesman` tinyint(1) NULL DEFAULT 0 COMMENT '0:普通用户 1:个人代理',
  `saus_salesid` int NULL DEFAULT NULL COMMENT '个人代理id',
  `saus_level` int NULL DEFAULT 0,
  `saus_parentid` int NULL DEFAULT NULL COMMENT '上级个人代理id',
  `saus_salestype` tinyint(1) NULL DEFAULT 1,
  `saus_roomcard` int NULL DEFAULT 0 COMMENT '房卡数量',
  `saus_bean` decimal(18, 4) NULL DEFAULT 0.0000 COMMENT '金币数',
  `saus_saletime` datetime NULL DEFAULT NULL COMMENT '成为个人代理时间',
  `saus_tel` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `saus_wechat` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `saus_invitecode` int NULL DEFAULT NULL,
  `saus_power` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `saus_pushrate1` int NULL DEFAULT 33,
  `saus_pushrate2` int NULL DEFAULT 8,
  `saus_pushmoney1` decimal(10, 2) NULL DEFAULT 0.00,
  `saus_pushmoney2` decimal(10, 2) NULL DEFAULT 0.00,
  `saus_status` int NULL DEFAULT 0 COMMENT '0、正常，1、封禁',
  `password` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '用户密码',
  `global_power` tinyint(1) NULL DEFAULT 0 COMMENT '0、没有权限，1、有封禁权限',
  `club_precent` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '俱乐部抽成',
  `is_send_star` tinyint(1) NULL DEFAULT 0 COMMENT '0、未赠送，1、已赠送',
  `currency_total` int UNSIGNED NULL DEFAULT 0 COMMENT '总计分成游戏币',
  `currency_withdraw` int UNSIGNED NULL DEFAULT 0 COMMENT '可提现游戏币',
  `currency_freeze` int UNSIGNED NULL DEFAULT 0 COMMENT '被冻结的游戏币',
  `saus_realname` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '真实姓名',
  `saus_alipay` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '支付宝账户',
  `saus_kaihu_name` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '开户人姓名',
  `saus_kaihu_bank` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '开户银行',
  `saus_kaihu_zhihang` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '开户支行',
  `saus_kaihu_count` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '银行账号',
  `saus_commit_auth` int NULL DEFAULT 2 COMMENT '提现权限（1、开启 2、关闭）',
  `statistic_type` int NULL DEFAULT 0 COMMENT '用户类型（-1：不计入统计口径；0：统计为普通用户口径；1：统计为平台用户口径）',
  `player_id` int NULL DEFAULT NULL COMMENT '关联的玩家编号',
  PRIMARY KEY (`idx`) USING BTREE,
  UNIQUE INDEX `uk_sales_user_id`(`idx` ASC) USING BTREE,
  UNIQUE INDEX `uk_sales_user_unionid`(`saus_agentid` ASC, `saus_channelid` ASC, `saus_unionid` ASC) USING BTREE,
  UNIQUE INDEX `uk_sales_user_salesid`(`saus_agentid` ASC, `saus_channelid` ASC, `saus_salesid` ASC) USING BTREE,
  UNIQUE INDEX `uk_sales_user_playerid`(`saus_agentid` ASC, `saus_channelid` ASC, `player_id` ASC) USING BTREE,
  UNIQUE INDEX `uk_sales_user_openid`(`saus_agentid` ASC, `saus_channelid` ASC, `saus_openid` ASC) USING BTREE,
  UNIQUE INDEX `uk_sales_user_userid`(`user_id` ASC) USING BTREE,
  INDEX `nk_sales_user_salesid`(`saus_agentid` ASC, `saus_channelid` ASC, `saus_salesid` ASC, `saus_openid` ASC, `saus_unionid` ASC, `saus_status` ASC, `password` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 25297 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '公众号用户表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for trans_star_record
-- ----------------------------
DROP TABLE IF EXISTS `trans_star_record`;
CREATE TABLE `trans_star_record`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `agent_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '代理商id',
  `channel_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '渠道id',
  `union_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '微信id',
  `open_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `send_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '发送者id',
  `get_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '获得者id',
  `type` tinyint(1) NULL DEFAULT 0 COMMENT '0、转让星星',
  `amount` int NULL DEFAULT 0 COMMENT '数量',
  `op_time` datetime NULL DEFAULT NULL COMMENT '操作时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `[REDACTED](`id` ASC) USING BTREE,
  INDEX `nk_trans_star_record_sales_to`(`agent_id` ASC, `channel_id` ASC, `get_id` ASC) USING BTREE,
  INDEX `nk_trans_star_record_sales_from`(`agent_id` ASC, `channel_id` ASC, `send_id` ASC) USING BTREE,
  INDEX `nk_trans_star_record_agent`(`agent_id` ASC, `channel_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 696 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Procedure structure for cp_SynchronizeSettle
-- ----------------------------
DROP PROCEDURE IF EXISTS `cp_SynchronizeSettle`;
delimiter ;;
CREATE PROCEDURE `cp_SynchronizeSettle`()
  SQL SECURITY INVOKER
begin
	declare nErrorCode integer default 0;
	declare continue handler for sqlexception set nErrorCode = 1;

	start transaction;

	-- 同步战绩数据
	insert into
		ct_grade_temp
		(
			agent_id,
			channel_id,
			game_id,
			player_id,
			room_code,
			room_type,
			owner_id,
			room_card,
			owner_card,
			create_time,
			makewar_time,
			over_time,
			short_number,
			deduct_type,
			deduct_card,
			aset_count,
			room_mode,
			out_id
		)
	select
		plgr_agentid,
		null,
		plgr_gameid,
		plgr_playerid,
		plgr_roomcode,
		plgr_roomtype,
		plgr_ownerid,
		plgr_roomcard,
		plgr_ownercard,
		plgr_createtime,
		plgr_makewartime,
		plgr_overtime,
		plgr_shortcode,
		plgr_deducttype,
		plgr_deductcard,
		plgr_asetcount,
		plgr_roommode,
		idx
	from
		game_db_temp.player_grade
	where
		idx not in (select out_id from ct_grade_temp);


	-- 同步并计算分成数据
	insert into
		ct_user_commission
		(
			agent_id,
			channel_id,
			game_id,
			room_id,
			player_id,
			user_id,
			parent_user_id,
			room_code,
			short_number,
			used_card,
			owner_id,
			room_type,
			room_mode,
			commission_type,
			grade,
			source_mode,
			target_mode,
			target_id,
			mode,
			type,
			commission_total,
			commission_system,
			commission_user,
			commission_parent_user,
			commission_user_rate,
			commission_parent_user_rate,
			create_time,
			is_robot,
			out_id
		)
	select
		agent_id,
		channel_id,
		game_id,
		room_id,
		player_id,
		user_id,
		parent_user_id,
		room_code,
		short_number,
		used_card,
		owner_id,
		room_type,
		room_mode,
		commission_type,
		grade,
		source_mode,
		target_mode,
		target_id,
		mode,
		type,
		ifnull(commission_total, 0) commission_total,
		ifnull(commission_total, 0) - floor(ifnull(commission_total, 0) * ifnull(user_rate, 0)) - floor(ifnull(commission_total, 0) * ifnull(parent_user_rate, 0)) commission_system,
		floor(ifnull(commission_total, 0) * ifnull(user_rate, 0)) commission_user,
		floor(ifnull(commission_total, 0) * ifnull(parent_user_rate, 0)) commission_parent_user,
		ifnull(user_rate, 0) commission_user_rate,
		ifnull(parent_user_rate, 0) commission_parent_user_rate,
		create_time,
		is_robot,
		out_id
	from
	(
		select
			a.agent_id,
			a.channel_id,
			a.game_id,
			a.room_id,
			a.player_id,
			a.user_id,
			a.parent_user_id,
			a.room_code,
			a.short_number,
			a.used_card,
			a.owner_id,
			a.room_type,
			a.room_mode,
			a.commission_type,
			a.grade,
			a.source_mode,
			a.target_mode,
			a.target_id,
			a.mode,
			a.type,
			a.commission_total,
			case ifnull(target_mode, 0)
				when 0 then
					case
						when (0 = a.user_id) or (0 != a.user_is_admin) then 0
						else case ifnull(c.is_custom, 0)
							when 0 then b.user_commission_rate
							else c.commission_rate
						end
					end
					+
					case
						when ((0 != a.user_id) and (0 = a.user_is_admin)) and ((0 = a.parent_user_id) or (0 != a.parent_user_is_admin)) then case ifnull(c.is_custom, 0)
							when 0 then b.parent_user_commission_rate
							else c.parent_commission_rate
						end
						else 0
					end
				else 0
			end user_rate,
			case ifnull(target_mode, 0)
				when 0 then
					case
						when (0 = a.parent_user_id) or (0 != a.parent_user_is_admin) then 0
						else case ifnull(c.is_custom, 0)
							when 0 then b.parent_user_commission_rate
							else c.parent_commission_rate
						end
					end
				else 0
			end parent_user_rate,
			a.user_is_admin,
			a.parent_user_is_admin,
			a.create_time,
			a.is_robot,
			a.out_id
		from
		(
			select
				a.goac_agentid agent_id,
				b.play_channelid channel_id,
				a.goac_gameid game_id,
				a.goac_id room_id,
				a.goac_playerid player_id,
				ifnull(b.play_invitecode, 0) user_id,
				ifnull(c.global_power, 0) user_is_admin,
				ifnull(c.saus_parentid, 0) parent_user_id,
				ifnull(d.global_power, 0) parent_user_is_admin,
				a.goac_roomcode room_code,
				a.goac_shortcode short_number,
				a.goac_roomcard used_card,
				a.goac_ownerid owner_id,
				null room_type,
				a.goac_roommode room_mode,
				case a.goac_roommode
					when 0 then '001'
					when 1 then '002'
					else '001'
				end commission_type,
				a.goac_grade grade,
				a.goac_rebatefrom source_mode,
				a.goac_rebateto target_mode,
				a.goac_toplayerid target_id,
				a.goac_rebatemode mode,
				a.goac_datatype type,
				a.goac_rebatevalue commission_total,
				a.goac_time create_time,
				a.goac_isrobot is_robot,
				a.idx out_id
			from
				game_db_temp.gold_account a left join
				player b on a.goac_agentid = b.play_agentid and a.goac_playerid = b.play_playerid left join
				sales_user c on b.play_agentid = c.saus_agentid and b.play_channelid = c.saus_channelid and b.play_invitecode = c.saus_salesid left join
				sales_user d on c.saus_agentid = d.saus_agentid and c.saus_channelid = d.saus_channelid and c.saus_parentid = d.saus_salesid
		) a left join
		ct_agent_commission b on a.agent_id = b.agent_id and a.channel_id = b.channel_id and a.commission_type = b.type_id left join
		ct_agent_commission_user c on a.agent_id = c.agent_id and a.channel_id = c.channel_id and b.type_id = c.type_id and a.user_id = c.user_id
	) t
	where
		t.out_id not in (select out_id from ct_user_commission);

	-- 转移指定了接收代理编号的抽水金币数
	update
		sales_user a,
		(
			select
				a.agent_id,
				a.channel_id,
				b.saus_salesid sales_id,
				sum(a.commission_total) total
			from
				ct_user_commission a inner join
				ct_sales_player b on a.agent_id = b.agent_id and a.channel_id = b.channel_id and a.target_id = b.play_playerid
			where
				ifnull(a.target_mode, 0) = 3 and a.is_settle = 0
			group by
				a.agent_id,
				a.channel_id,
				b.saus_salesid
		) b
	set
		a.saus_bean = a.saus_bean + b.total
	where
		a.saus_agentid = b.agent_id and
		a.saus_channelid = b.channel_id and
		a.saus_salesid = b.sales_id;

	-- 增加玩家绑定的代理的总计抽水金币数
	update
		sales_user a,
		(
			select
				agent_id, channel_id, user_id, sum(ifnull(commission_user, 0)) commission_user
			from
				ct_user_commission
			where
				is_settle = 0
			group by
				agent_id, channel_id, user_id
		) b
	set
		a.currency_total = ifnull(a.currency_total, 0) + b.commission_user
	where
		a.saus_agentid = b.agent_id and
		a.saus_channelid = b.channel_id and
		a.saus_salesid = b.user_id;

	-- 增加玩家绑定的代理的上级代理的总计抽水金币数
	update
		sales_user a,
		(
			select
				agent_id, channel_id, parent_user_id user_id, sum(ifnull(commission_parent_user, 0)) commission_user
			from
				ct_user_commission
			where
				is_settle = 0
			group by
				agent_id, channel_id, parent_user_id
		) b
	set
		a.currency_total = ifnull(a.currency_total, 0) + b.commission_user
	where
		a.saus_agentid = b.agent_id and
		a.saus_channelid = b.channel_id and
		a.saus_salesid = b.user_id;

	-- 计算可提现金币数
	update
		sales_user a,
		(
			select
				agent_id, channel_id, user_id
			from
				ct_user_commission
			where
				is_settle = 0
			union all
			select
				agent_id, channel_id, parent_user_id
			from
				ct_user_commission
		) b
	set
		a.currency_withdraw = ifnull(a.currency_total, 0) - ifnull(a.currency_freeze, 0)
	where
		a.saus_agentid = b.agent_id and
		a.saus_channelid = b.channel_id and
		a.saus_salesid = b.user_id;

	-- 变更状态
	update
		ct_user_commission
	set
		is_settle = 1
	where
		ifnull(is_settle, 0) = 0;

	-- 更新表(player)
	update
		player a,
		game_db_temp.player b
	set
		a.play_nickname = b.play_nickname,
		a.play_bean = b.play_bean,
		a.play_roomcard = b.play_roomcard
	where
		a.play_agentid = b.play_agentid and
		a.play_channelid = b.play_channelid and
		a.play_playerid = b.play_playerid;

	-- 重建索引(ct_grade_temp)
	alter table ct_grade_temp
	drop index pk_grade_temp,
	drop index uk_grade_temp,
	drop index nk_grade_temp_player_id,
	drop index nk_grade_temp_create_time,
	drop index nk_grade_temp_group_data,
	add unique index pk_grade_temp (id) using hash,
	add unique index uk_grade_temp (out_id) using hash,
	add index nk_grade_temp_player_id (agent_id,channel_id,game_id,player_id) using btree,
	add index nk_grade_temp_create_time (agent_id,channel_id,game_id,create_time,short_number) using btree,
	add index nk_grade_temp_group_data (agent_id,channel_id,player_id,create_time,short_number) using btree;

	-- 重建索引(ct_user_commission)
	alter table ct_user_commission
	drop index pk_user_commission,
	drop index uk_user_commission_out_id,
	drop index nk_user_commission,
	add unique index pk_user_commission (id) using hash,
	add unique index uk_user_commission_out_id (out_id) using hash,
	add index nk_user_commission (agent_id,channel_id,game_id,player_id,user_id) using btree;

	-- 重建索引(sales_user)
	alter table sales_user
	drop index uk_sales_user_openid,
	drop index uk_sales_user_unionid,
	drop index nk_sales_user_salesid,
	add unique index uk_sales_user_openid (saus_agentid,saus_channelid,saus_openid) using hash,
	add unique index uk_sales_user_unionid (saus_agentid,saus_channelid,saus_unionid) using hash,
	add index nk_sales_user_salesid (saus_agentid,saus_channelid,saus_salesid,saus_openid,saus_unionid,saus_status,password) using btree;

	-- 重建索引(player)
	alter table player
	drop index player_index1,
	drop index player_index2,
	drop index nk_player,
	add unique index player_index1 (play_agentid,play_channelid,play_playerid) using hash,
	add unique index player_index2 (play_agentid,play_channelid,play_unionid) using hash,
	add index nk_player (play_agentid,play_playerid,play_channelid,play_openid,play_unionid,play_status) using btree;

	-- 判断是否成功
	if 1 = nErrorCode then
		rollback;
		select 'error';
	else
		commit;
		select 'success';
	end if;
end
;;
delimiter ;

SET FOREIGN_KEY_CHECKS = 1;
