%%%--------------------------------------
%%% @Module  : supplication.hrl
%%% @Author  : WJQ
%%% @Created : 2019.04.01
%%% @Description:  祈愿
%%%--------------------------------------
-define(SUPPLICATION_BRO_NUM,			50).		%% 读取最新广播记录数
-define(SUPPLICATION_REWARD_LENGTH,		10).		%% 奖励选择数量
-define(SUPPLICATION_REWARD_LV_LIST,	[0,1,2,3]).	%% 奖励等级列表

%% 奖励限制类型
-define(LIMIT_TYPE_PERSONAL,			1).			%% 个人限制
-define(LIMIT_TYPE_GLOBAL,				2).			%% 全服限制（本服）

%% 数据库写入状态
-define(WSTATUS_SUCCESSS,				1).			%% 写入成功
-define(WSTATUS_WAIT,					2).			%% 待写入

-define(SAVE_DB_CD,						10).		%% 写入数据库CD时间（秒）

%% 任务完成状态
-define(UNFINISH,						0).			%% 未完成
-define(FINISH,							1).			%% 已完成但未领取奖励
-define(HAS_RECEIVE,					2).			%% 已领取奖励

%% 广播设置
-define(BROADCAST,						1).			%% 广播
-define(NO_BROADCAST,					0).			%% 不广播

%% 玩家祈愿数据
-record(supplication, {
	sub_type		= 0,		%% 活动子类型
	self_log 		= [],		%% 个人抽奖记录 [reward_id,...]
	task_list 		= [],		%% 任务列表 [#task_progress{}...]
	select_reward	= [],		%% 已选择奖励列表 [{RewardLv, [{Weight, {PosId, RewardId}},...]},...]
	count			= 0,		%% 已抽奖次数
	counter 		= [],		%% 抽中该奖励等级后抽奖次数 [{RewardLv, Count, GetTimes},...]
	time 			= 0			%% 数据记录时间
}).

%% 玩家任务数据
-record(task_progress, {
	task_id 		= 0,		%% 任务ID
	sub_type		= 0,		%% 活动子类型
	progress 		= 0,		%% 任务进度
	extra			= [],		%% 额外数据
	task_status		= 0, 		%% 任务状态
	write_time		= 0,		%% 最近一次更新写入数据库的时间
	write_status	= 0,		%% 写入状态 1:写入成功 2:待写入
	time 			= 0			%% 数据记录时间
}).

%% 祈愿广播记录
-record(supplication_kf, {
	broadcast_list	= []		%% 广播列表 [#broadcast{}]
}).

-record(supplication_local, {
	status			= 0,		%% 0未同步 1已同步
	broadcast_list  = [],		%% 广播列表 [#broadcast{}]
	counter 		= []		%% 全服玩家抽中该奖励等级后抽奖次数 [{RewardLv, Count, GetTimes},...]
}).

%% 广播记录
-record(broadcast, {
	role_id 		= 0,		%% 角色id
	role_name		= "",		%% 玩家名
	server_num		= 0,		%% 服务器标识
	reward_id 		= 0			%% 奖励ID
}).

%% 祈愿任务配置
-record(supplication_task_cfg, {
	task_id 		= 0,		%% 任务ID
	module_id		= 0,		%% 模块ID
	sub_type		= 0,		%% 子类型
	desc			= "",		%% 任务描述
	condition       = [],		%% 任务完成条件
	reward_list		= [],		%% 奖励
	jump_cfg 		= []		%% 跳转配置
}).

%% 祈愿奖励配置
-record(supplication_reward_cfg, {
	reward_id		= 0,		%% 奖励ID
	reward_lv		= 0,		%% 奖励等级
	weight			= 0,		%% 权重
	lv_min			= 0,		%% 等级下限
	lv_max			= 0,		%% 等级上限
	reward_list 	= [],		%% 奖励
	effect			= 0,		%% 特效
	broadcast 		= 0			%% 广播
}).

%% 祈愿常量配置
-record(supplication_constant_cfg, {
	key				= 0,		%% 键
	value			= [],		%% 值
	desc 			= ""		%% 描述
}).

%% ========================
%%			SQL
%% ========================
-define(SELECT_SUPPLICATION, <<"SELECT `sub_type`, `self_log`, `select_reward`, `count`, `counter`, `time` FROM `supplication` WHERE `role_id` = ~p">>).
-define(SELECT_SUPPLICATION_TASK, <<"SELECT `task_id`, `sub_type`, `progress`, `extra`, `task_status`, `time` FROM `supplication_task` WHERE `role_id` = ~p">>).
-define(SELECT_LATEST_BROADCAST, <<"SELECT `role_id`, `role_name`, `server_num`, `reward_id` FROM `supplication_broadcast` ORDER BY `id` LIMIT ~p ">>).
-define(SELECT_SUPPLICATION_COUNTER, <<"SELECT `reward_lv`, `count`, `reward_count` FROM `supplication_counter`">>).

-define(REPLACE_SUPPLICATION, <<"REPLACE INTO `supplication` (`role_id`, `sub_type`, `self_log`, `select_reward`, `count`, `counter`, `time`) VALUES (~p, ~p, '~s', '~s', ~p, '~s', ~p)">>).
-define(REPLACE_SUPPLICATION_TASK, <<"REPLACE INTO `supplication_task` (`role_id`, `task_id`, `sub_type`, `progress`, `extra`, `task_status`, `time`) VALUES (~p, ~p, ~p, ~p, '~s', ~p, ~p)">>).
-define(REPLACE_SUPPLICATION_COUNTER, <<"REPLACE INTO `supplication_counter` (`reward_lv`, `count`, `reward_count`) VALUES (~p, ~p, ~p)">>).

-define(INSERT_BROADCAST, <<"INSERT INTO `supplication_broadcast` (`role_id`, `role_name`, `server_num`, `reward_id`) VALUES (~p, '~ts', ~p, ~p)">>).

-define(DELETE_ALL_SUPPLICATION_TASK, <<"DELETE FROM `supplication_task` WHERE `role_id` = ~p AND `task_id` = ~p">>).
-define(DELETE_OLD_BROADCAST,
		<<"DELETE FROM `supplication_broadcast`
		WHERE `id` NOT IN (SELECT * FROM (SELECT `id` FROM supplication_broadcast ORDER BY `id` DESC LIMIT ~p) AS `a`)">>).
-define(DELETE_SUPPLICATION_COUNTER, <<"DELETE FROM `supplication_counter` WHERE `reward_lv` = ~p">>).

-define(UPDATE_SUPPLICATION_TASK, <<"UPDATE `supplication_task` SET `progress` = ~p, `extra` = '~s', `task_status` = ~p WHERE `role_id` = ~p AND `task_id` = ~p">>).
