%% ---------------------------------------------------------------------------
%% @doc mod_kf_supplication
%% @author WJQ
%% @since  2019-04-02
%% @deprecated 祈愿跨服进程
%% ---------------------------------------------------------------------------
-module(mod_kf_supplication).
-behaviour(gen_server).
-include("common.hrl").
-include("def_gen_server.hrl").
-include("supplication.hrl").

%% API
-export([start_link/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([
	sync_broadcast/1			%% 通知节点同步数据
	,add_broadcast/6			%% 添加广播
]).

start_link() ->
    gen_server:start_link({local,?MODULE}, ?MODULE, [], []).

sync_broadcast(Node) ->
	gen_server:cast(?MODULE, {'sync_broadcast', Node}).

add_broadcast(RoleId, RoleName, ServerNum, RewardId, RewardLvStr, RewardList) ->
	gen_server:cast(?MODULE, {'add_broadcast', RoleId, RoleName, ServerNum, RewardId, RewardLvStr, RewardList}).

init(Args) ->
    ?DO_INIT(Args).
handle_call(Request, From, State) ->
    ?DO_HANDLE_CALL(Request, From, State).
handle_cast(Msg, State) ->
    ?DO_HANDLE_CAST(Msg, State).
handle_info(Info, State) ->
    ?DO_HANDLE_INFO(Info, State).
terminate(Reason, State) ->
    ?DO_TERMINATE(Reason, State).
code_change(OldVsn, State, Extra) ->
    ?DO_CODE_CHANGE(OldVsn, State, Extra).

do_init(_Args) ->
	db_delete_old_broadcast(?SUPPLICATION_BRO_NUM),
	case db_get_latest_broadcast() of
		[]   -> BroadcastList = [];
		List -> BroadcastList = make_broadcast_list(List, [])
	end,
	gen_server:cast(?MODULE, {'sync_broadcast'}),
	State = #supplication_kf{broadcast_list = BroadcastList},
    {ok, State}.

%% =================================================== CALL ===================================================
do_handle_call(_Request, _From, State) ->
    {reply, ok, State}.

%% =================================================== CAST ===================================================
%% 同步祈愿记录到所有节点
do_handle_cast({'sync_broadcast'}, State) ->
	#supplication_kf{broadcast_list = BroadcastList} = State,
	clusters_center:apply_to_all_node(mod_local_supplication, sync_broadcast, [BroadcastList], 50),
	{noreply, State};

%% 同步祈愿记录到指定节点
do_handle_cast({'sync_broadcast', Node}, State) ->
	#supplication_kf{broadcast_list = BroadcastList} = State,
	clusters_center:apply_cast(Node, mod_local_supplication, sync_broadcast, [BroadcastList]),
	{noreply, State};

%% 增加一条祈愿记录
do_handle_cast({'add_broadcast', RoleId, RoleName, ServerNum, RewardId, RewardLvStr, RewardList}, State) ->
	#supplication_kf{broadcast_list = BroadcastList} = State,
	Broadcast = #broadcast{role_id = RoleId, role_name = RoleName, server_num = ServerNum, reward_id = RewardId},
	TempBroadcastList = [Broadcast|BroadcastList],
	NewBroadcastList = lists:sublist(TempBroadcastList, ?SUPPLICATION_BRO_NUM),
	NewState = State#supplication_kf{broadcast_list = NewBroadcastList},
	db_insert_broadcast(RoleId, RoleName, ServerNum, RewardId),
	clusters_center:apply_to_all_node(lib_supplication, send_broadcast, [RoleName, ServerNum, RewardLvStr, RewardList], 50),
	clusters_center:apply_to_all_node(mod_local_supplication, add_broadcast, [Broadcast], 50),
	gen_server:cast(?MODULE, {'sync_broadcast'}),
	{noreply, NewState};

do_handle_cast(_Msg, State) ->
    {noreply, State}.

%% =================================================== INFO ===================================================
do_handle_info(_Info, State) ->
    {noreply, State}.
do_terminate(_Reason, _State) ->
    ok.
do_code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%% --------------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------------
%% 格式化广播列表
make_broadcast_list([], AccList) -> AccList;
make_broadcast_list([[RoleId, RoleName, ServerNum, RewardId]|T], AccList) ->
	NewAccList = [#broadcast{role_id = RoleId, role_name = RoleName, server_num = ServerNum, reward_id = RewardId}|AccList],
	make_broadcast_list(T, NewAccList);
make_broadcast_list([_H|T], AccList) ->
	make_broadcast_list(T, AccList).

db_get_latest_broadcast() ->
	ReSql = io_lib:format(?SELECT_LATEST_BROADCAST, [?SUPPLICATION_BRO_NUM]),
	db:get_all(ReSql).

db_insert_broadcast(RoleId, RoleName, ServerNum, RewardId) ->
	ReSql = io_lib:format(?INSERT_BROADCAST, [RoleId, RoleName, ServerNum, RewardId]),
	db:execute(ReSql).

%% Num :: integer()  保留记录数量
db_delete_old_broadcast(Num) ->
	ReSql = io_lib:format(?DELETE_OLD_BROADCAST, [Num]),
	db:execute(ReSql).
