%%%--------------------------------------
%%% @Module  : lib_activitycalen
%%% @Author  : xiaoxiang
%%% @Created : 2017-03-01
%%% @Description:  lib_activitycalen
%%%--------------------------------------
-module(lib_activitycalen).
-export([
        daily_clear/1       %%清掉ets过期活跃度玩家
    ]).
-compile(export_all).
-include("server.hrl").
-include("figure.hrl").
-include("role.hrl").
-include("errcode.hrl").
-include("activitycalen.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("def_daily.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-include("goods.hrl").
-include("guild.hrl").
-include("def_fun.hrl").
-include("sql_guild.hrl").
-include("vip.hrl").

%%玩家登陆
login(PS) ->
    #player_status{id = RoleId, reg_time = CreateTime} = PS,
    case db_select_daily_live(RoleId) of
        [] ->
            db_update_daily_live(RoleId, CreateTime, []),
            EtsAct = #ets_activitycalen{id = RoleId, create_time = CreateTime, liveness_list = []};
        [_, LiveNessList] ->
            TermNessList = util:bitstring_to_term(LiveNessList),
            EtsAct = #ets_activitycalen{id = RoleId, create_time = CreateTime, liveness_list = TermNessList}
    end,
    ets:insert(?ETS_ACTIVITYCALEN, EtsAct).

%% 查询活动状态
%% 0未开启 1开启中 2结束
ask_activity_status(Player) ->
    #player_status{id = RoleId, figure = #figure{lv = _Lv}} = Player,
    mod_activitycalen:ask_activity_status(RoleId).


%% 查询活跃度奖励
ask_live_reward(Player) ->
    #player_status{id = RoleId} = Player,
    Live      = lib_daily:get_count(RoleId, ?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY),
    %% 每日最大活跃度
    LiveMax   = lib_daily:get_count_limit(?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY),
    IdList    = data_activitycalen:get_reward_id(),
    CountList = lib_daily:get_count(RoleId, [{?MOD_ACTIVITY, ?MOD_ACTIVITY_LIVE_REWARD, Id} || Id <- IdList]),
    Info      = [{Id, Value} ||{{_, _, Id}, Value} <- CountList],
    %?PRINT("Info:~p,~p,~p ~n", [Live, LiveMax, Info]),
    lib_server_send:send_to_uid(RoleId, pt_157, 15703, [Live, LiveMax, Info]).

% 兑换奖励
get_live_reward(Player, Id) ->
    #player_status{id = RoleId, figure = #figure{lv = Lv}}= Player,
    case check_get_reward(Player, Id) of
        {true, NewPlayerTmp} ->
            #ac_reward{reward = Reward} = data_activitycalen:get_reward_config(Id, Lv),
            Produce = #produce{type = activity_live_reward, show_tips= 1, reward = Reward, remark=""},
            {ok, NewPlayer} = lib_goods_api:send_reward_with_mail(NewPlayerTmp, Produce, ?MOD_ACTIVITY),
            %% 置为已领状态
            lib_daily:increment(RoleId, ?MOD_ACTIVITY, ?MOD_ACTIVITY_LIVE_REWARD, Id),
            lib_server_send:send_to_uid(RoleId, pt_157, 15705, [?SUCCESS ,Id]);
        {false, ErrCode, NewPlayer} ->
            lib_server_send:send_to_uid(RoleId, pt_157, 15700, [ErrCode])
    end,
    {ok, NewPlayer}.

refresh_onhook_time(RoleId, OnhookTime) ->
    %?PRINT("~p~n", [OnhookTime]),
    lib_server_send:send_to_uid(RoleId, pt_157, 15714, [OnhookTime]).


%% -------------------------------------
% 玩家完成活动用于记录玩家活跃度
%% -------------------------------------
%% return #player_status{}
%% 玩家完成活动

%% 活跃度用4，总表用1,2
role_success_end_activity(Player, TempModule, TempModuleSub, Count) -> %Player.
    {Module, ModuleSub} = get_extra_ac(TempModule, TempModuleSub),
    #player_status{id = RoleId, combat_power = ComPower, figure = #figure{lv = Lv}} = Player,
    IdList = data_activitycalen:get_ac_sub(TempModule, TempModuleSub),
    CheckList = [time, week, month, open_day, merge_day],
    #ac_liveness{name = AcName, max = Max0, act_type = ActType, start_lv = StartLv, end_lv = EndLv,live=Live} = data_activitycalen:get_live_config(Module, ModuleSub),
    CheckRes = [1 || AcSub <- IdList, lib_activitycalen_util:do_check_ac_sub(data_activitycalen:get_ac(TempModule, TempModuleSub, AcSub), CheckList)],
    Max01 = get_vip_max(Module, ModuleSub,Player,Max0),
    Max02 = get_main_line_dun_max(Module, ModuleSub, Max01),
    Max   = get_sup_vip_max(Module, ModuleSub,Player, Max02),
    case lists:member(1, CheckRes) of
        true ->
            %% 资源找回的处理
            Data1 = #act_data{act_id = TempModule, act_sub = TempModuleSub, num = Count},
            lib_player_event:async_dispatch(RoleId, ?EVENT_PARTICIPATE_ACT, Data1),
            Value = lib_daily:get_count(RoleId, ?MOD_ACTIVITY, ?MOD_ACTIVITY_NUM, Module * ?AC_LIVE_ADD + ModuleSub),
            case Value < Max andalso StartLv =< Lv andalso EndLv >= Lv  of
                true ->
                     %% 日常次数增加
                    lib_daily:plus_count(RoleId, ?MOD_ACTIVITY, ?MOD_ACTIVITY_NUM, Module * ?AC_LIVE_ADD + ModuleSub, Count),
                     %% 每日必做的完成了通知客户端红点状态
                     if
                         Value + Count >= Max andalso ActType == ?ACTIVITY_TYPE_DAILY ->
                            lib_activitycalen_mod:send_15707(Module, ModuleSub, 1, RoleId, Lv);
                         true -> skip
                     end,
                    %% 活跃度增加
                    AddLive = min(Max-Value,Count) * Live,
                    OldLive = lib_daily:get_count(RoleId, ?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY),
                    LiveLimit = lib_daily:get_count_limit(?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY),
                    %% ?PRINT("~p, ~p, ~p~n", [AddLive, OldLive, LiveLimit]),
                    %%活跃度
                    case OldLive < LiveLimit of
                        true ->
                            Add = min(AddLive, LiveLimit - OldLive),
                            NewLive = OldLive + Add,
                            % NewPlayer = ?IF(GuildId > 0, add_guild_liveness(Player, NewLive, Add), Player),
                            lib_task_api:finish_day_activity(Player, NewLive),
                            lib_achievement_api:activity_event(Player, Add),
                            contract_task_api:act_live_trigger(RoleId, Add),
                            Player2 = offline_hosting_api:check_add_value(Player, Add),
                            NewPlayer = Player2,
                            lib_daily:plus_count(RoleId, ?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY, Add),
                            lib_log_api:log_activity_live(RoleId, Lv, ComPower, TempModule, TempModuleSub, util:make_sure_binary(AcName), Live, OldLive, NewLive, utime:unixtime()),
                            % 活跃事件派发
                            Data = #callback_activity_live{activity_live = NewLive, add_live = Add},
                            lib_player_event:async_dispatch(RoleId, ?EVENT_ADD_LIVENESS, Data),
                            %% 更新ps的活跃度
                            lib_player:apply_cast(RoleId, ?APPLY_CAST_SAVE, lib_liveness, add_liveness, [Add]),
                            update_ets_live(NewPlayer, Add);
                        false -> NewPlayer = Player
                    end;
                false -> NewPlayer = Player
            end;
        _ -> NewPlayer = Player
    end,
    NewPlayer.

% %%增加公会活跃度
% add_guild_liveness(Player, Live, AddLiveness) ->
%     #player_status{id = RoleId, guild = #status_guild{live_reward_ids = LiveRewardIds} = StatusGuild} = Player,
%     AllReward = data_guild:get_all_extra_live_reward_id(),
%     Fun = fun(Id, {Sum, List})->
%         #base_extra_live_reward{extra_add = Add, live = NeedLive} = data_guild:get_extra_live_reward(Id),
%         case lists:member(Id, LiveRewardIds) == false andalso Live>=NeedLive of
%             true -> {Sum + Add, [Id | List]};
%             _ -> {Sum, List}
%         end
%     end,
%     {ExtraAdd, AddRewardIds} = lists:foldl(Fun, {0, []}, AllReward),
%     case ExtraAdd > 0 of
%         true ->
%             NewLiveRewardIds = AddRewardIds ++ LiveRewardIds,
%             Sql = io_lib:format(?sql_guild_update_live_reward, [util:term_to_bitstring(NewLiveRewardIds), RoleId]),
%             db:execute(Sql),
%             NewStatusGuild = StatusGuild#status_guild{live_reward_ids = NewLiveRewardIds},
%             mod_guild:add_liveness(RoleId, AddLiveness, ExtraAdd),
%             lib_log_api:log_guild_liveness_extra_reward(RoleId, Live, NewLiveRewardIds, ExtraAdd),
%             Player#player_status{guild = NewStatusGuild};
%         false ->
%             mod_guild:add_liveness(RoleId, AddLiveness, 0),
%             Player
%     end.

% %%离线增加公会活跃度
% add_guild_liveness_offline(#player_status{id = RoleId, guild = #status_guild{id = GuildId} = StatusGuild} = Player, Live, AddLiveness) when GuildId > 0->
%     NewPlayer = add_guild_liveness(Player, Live, AddLiveness),
%     NewStatusGuild = NewPlayer#player_status.guild,
%     case NewStatusGuild == StatusGuild of
%         true -> ok;
%         false -> lib_offline_api:update_offline_ps(RoleId, [{guild, NewStatusGuild}])
%     end,
%     NewPlayer;
% add_guild_liveness_offline(Player, _Live, _AddLiveness) ->
%     Player.

role_success_end_activity_offline(RoleId, TempModule, TempModuleSub, Count) ->
    {Module, ModuleSub} = get_extra_ac(TempModule, TempModuleSub),
    case lib_role:get_role_show(RoleId) of
        #ets_role_show{figure = #figure{lv = Lv}, combat_power = ComPower} ->
            IdList = data_activitycalen:get_ac_sub(TempModule, TempModuleSub),
            CheckList = [time, week, month, open_day, merge_day],
            #ac_liveness{name = AcName, max = Max, start_lv = StartLv, end_lv = EndLv,live=Live} = data_activitycalen:get_live_config(Module, ModuleSub),
            CheckRes=[1 || AcSub <- IdList, lib_activitycalen_util:do_check_ac_sub(data_activitycalen:get_ac(TempModule, TempModuleSub, AcSub), CheckList)],
            case lists:member(1, CheckRes)of
                true ->
                    F = fun() ->
                            %% 资源找回处理
                            Data1 = #act_data{act_id = TempModule, act_sub = TempModuleSub, num = Count},
                            lib_player_event:async_dispatch(RoleId, ?HAND_OFFLINE, ?EVENT_PARTICIPATE_ACT, Data1),
                            Value = lib_daily:get_count_offline(RoleId, ?MOD_ACTIVITY, ?MOD_ACTIVITY_NUM, Module * ?AC_LIVE_ADD + ModuleSub),
                            case Value < Max andalso StartLv =< Lv andalso EndLv >= Lv  of
                                true ->
                                    lib_daily:plus_count_offline(RoleId, ?MOD_ACTIVITY, ?MOD_ACTIVITY_NUM, Module * ?AC_LIVE_ADD + ModuleSub, Count),
                                    AddLive = Count * Live,
                                    OldLive = lib_daily:get_count_offline(RoleId, ?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY),
                                    LiveLimit = lib_daily:get_count_limit(?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY),
                                    case OldLive < LiveLimit of
                                        true ->
                                            Add = min(AddLive, LiveLimit - OldLive),
                                            NewLive = OldLive + Add,
                                            lib_daily:plus_count_offline(RoleId, ?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY, Add),
                                            lib_log_api:log_activity_live(RoleId, Lv, ComPower, TempModule, TempModuleSub, util:make_sure_binary(AcName), Live, OldLive, NewLive, utime:unixtime()),
                                            %% 触发成就
                                            lib_achievement_api:async_event(RoleId, lib_achievement_api, activity_event, Add),
                                            % 活跃派发
                                            Data = #callback_activity_live{activity_live = NewLive, add_live = Add},
                                            lib_player_event:async_dispatch(RoleId, ?HAND_OFFLINE, ?EVENT_ADD_LIVENESS, Data),
                                            lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?HAND_OFFLINE, ?MODULE, add_guild_liveness_offline, [NewLive, Add]),
                                            %% 更新ps数据
                                            lib_player:apply_cast(RoleId, ?APPLY_CAST_SAVE, ?HAND_OFFLINE, lib_liveness, add_liveness, [Add]),
                                            update_ets_live(RoleId, Add),
                                            ok;
                                        false ->
                                            ok
                                    end;
                                false ->
                                    ok
                            end
                    end,
                    case catch F() of
                        ok -> ok;
                        _R ->
                            ?ERR("_R:~p ~n", [_R]),
                            skip
                    end;
                _ ->
                    %?PRINT("FALSE ~n", []),
                    skip
            end;
        _ ->
            skip
    end.

get_extra_ac(TempModule, TempModuleSub) ->
    if
        true -> {TempModule, TempModuleSub}
    end.

%% 判断当天是否会开启
check_ac_live_day(Module, ModuleSub) ->
    CheckList = [time, week, month, open_day, merge_day],
    CheckRes = case {Module, ModuleSub} of
        _ ->
            IdList = data_activitycalen:get_ac_sub(Module, ModuleSub),
            [1 ||AcSub <- IdList, lib_activitycalen_util:do_check_ac_sub(data_activitycalen:get_ac(Module, ModuleSub, AcSub), CheckList)]
    end,
    lists:member(1, CheckRes).

%% ----------------------------------------------
%% 每日活跃度相关
%%-----------------------------------------------
%%ets更新每日活跃度
update_ets_live(RoleId, Live) when is_integer(RoleId)->
    ZeroTime = utime:unixdate(),
    Date = erlang:date(),
    FiveDay = utime:get_before_day(Date, 5, 0),
    Last5Time = utime:unixtime({FiveDay, {0, 0, 0}}),
    case db_select_daily_live(RoleId) of
        [] ->
            Sql = io_lib:format("select reg_time from `player_login` where id=~w limit 1", [RoleId]),
            case db:get_row(Sql) of
                [CreateTime] -> ok;
                _ -> CreateTime = ZeroTime
            end,
            NewLiveNessList = [{ZeroTime, Live}];
        [CreateTime, LiveNessList] ->
            TermNessList = util:bitstring_to_term(LiveNessList),
            case lists:keyfind(ZeroTime, 1, TermNessList) of
                {_, OldLive} -> NewLive = OldLive + Live;
                _ -> NewLive = Live
            end,
            TempLiveNessList = lists:keystore(ZeroTime, 1, TermNessList, {ZeroTime, NewLive}),
            NewLiveNessList = [{Time, Val} || {Time, Val} <- TempLiveNessList, Time>=Last5Time, Time =< ZeroTime]
    end,
    db_update_daily_live(RoleId, CreateTime, NewLiveNessList),
    EtsAct = #ets_activitycalen{id = RoleId, create_time = CreateTime, liveness_list = NewLiveNessList},
    ets:insert(?ETS_ACTIVITYCALEN, EtsAct),
    ok;
update_ets_live(PS, Live) ->
    #player_status{id = RoleId, reg_time = CreateTime} = PS,
    ZeroTime = utime:unixdate(),
    Date = erlang:date(),
    FiveDay = utime:get_before_day(Date, 5, 0),
    Last5Time = utime:unixtime({FiveDay, {0, 0, 0}}),
    case ets:lookup(?ETS_ACTIVITYCALEN, RoleId) of
        [#ets_activitycalen{liveness_list = LiveNessList}] ->
            case lists:keyfind(ZeroTime, 1, LiveNessList) of
                {_, OldLive} -> NewLive = OldLive + Live;
                _ -> NewLive = Live
            end,
            TempLiveNessList = lists:keystore(ZeroTime, 1, LiveNessList, {ZeroTime, NewLive}),
            NewLiveNessList = [{Time, Val} || {Time, Val} <- TempLiveNessList, Time>=Last5Time, Time =< ZeroTime];
        _ ->
            NewLiveNessList = [{ZeroTime, Live}]
    end,
    db_update_daily_live(RoleId, CreateTime, NewLiveNessList),
    EtsAct = #ets_activitycalen{id = RoleId, create_time = CreateTime, liveness_list = NewLiveNessList},
    ets:insert(?ETS_ACTIVITYCALEN, EtsAct).

%%玩家每日活跃度db更新
db_update_daily_live(RoleId, CreateTime, LiveNessList) ->
    BitNessList = util:term_to_bitstring(LiveNessList),
    Sql = io_lib:format(?sql_replace_role_live, [RoleId, CreateTime, BitNessList]),
    db:execute(Sql).

%%玩家每日活跃度db查询
db_select_daily_live() ->
    Sql = io_lib:format(?sql_select_role_live, []),
    db:get_all(Sql).

db_select_daily_live(RoleId) ->
    Sql = io_lib:format("select create_time, livess_list from `player_daily_liveness` where role_id = ~p", [RoleId]),
    db:get_row(Sql).

%%清掉过期ets记录
daily_clear(0) ->
    OnlineList = ets:tab2list(?ETS_ONLINE),
    LivenessList = ets:tab2list(?ETS_ACTIVITYCALEN),
    spawn(fun()-> clear_out_time_ets_live(OnlineList, LivenessList) end);
daily_clear(_) -> ok.

clear_out_time_ets_live(_OnlineList, []) -> ok;
clear_out_time_ets_live(OnlineList, [EtsAct | T]) ->
    #ets_activitycalen{id = RoleId} = EtsAct,
    case lists:keyfind(RoleId, #ets_online.id, OnlineList) of
        #ets_online{} ->
            lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?MODULE, clear_ets_live, []);
        _ ->
            clear_ets_live(RoleId, EtsAct)
    end,
    clear_out_time_ets_live(OnlineList, T).

clear_ets_live(PS) ->
    ?PRINT("aaaaaaaaa~n",[]),
    #player_status{id = RoleId} = PS,
    case ets:lookup(?ETS_ACTIVITYCALEN, RoleId) of
        [#ets_activitycalen{} = EtsAct] ->
            clear_ets_live(RoleId, EtsAct);
        _ -> ok
    end,
    PS.
clear_ets_live(RoleId, EtsAct) ->
    #ets_activitycalen{liveness_list = LivenessList} = EtsAct,
    Date = erlang:date(),
    FiveDay = utime:get_before_day(Date, 5, 0),
    Last5Time = utime:unixtime({FiveDay, {0, 0, 0}}),
    FilterLivenessList = [{Time, Val} || {Time, Val} <- LivenessList, Time >= Last5Time],
    case FilterLivenessList == [] of
        true -> ets:delete(?ETS_ACTIVITYCALEN, RoleId);
        false -> ok
    end.

%%购买增加昨日活跃度
gm_update_ets_live(RoleId, Num) ->
    case ets:lookup(?ETS_ACTIVITYCALEN, RoleId) of
        [#ets_activitycalen{liveness_list = LiveNessList} = EtsAct] ->
            Date = erlang:date(),
            FiveDay = utime:get_before_day(Date, 1, 0),
            Time = utime:unixtime({FiveDay, {0, 0, 0}}),
            NewLive = case lists:keyfind(Time, 1, LiveNessList) of
                false -> Num;
                {_, OldLive} -> OldLive + Num
            end,
            NewLiveNessList = lists:keystore(Time, 1, LiveNessList, {Time, NewLive}),
            NewEtsAct = EtsAct#ets_activitycalen{liveness_list = NewLiveNessList},
            BitLiveNessList = util:term_to_bitstring(NewLiveNessList),
            Sql = io_lib:format("update `player_daily_liveness` set livess_list = '~s' where role_id = ~p", [BitLiveNessList, RoleId]),
            db:execute(Sql),
            ets:insert(?ETS_ACTIVITYCALEN, NewEtsAct);
        _ ->
            ok
    end.


%% ----------------------------------------------
%% 查询活动的当日的活动时间
%%-----------------------------------------------
% return [{start_hour,start_min}|...] || []
get_time(Module, ModuleSub) ->
    IdList = data_activitycalen:get_ac_sub(Module, ModuleSub),
    CheckList = [time, week, month, open_day, merge_day],
    TimeList=[begin
        #base_ac{time_region=TimeRegion}=data_activitycalen:get_ac(Module, ModuleSub, AcSub),
        [{StartH,StartM} || {{StartH,StartM}, _} <- TimeRegion]
    end || AcSub <- IdList, lib_activitycalen_util:do_check_ac_sub(data_activitycalen:get_ac(Module, ModuleSub, AcSub), CheckList)],
    case TimeList of
        [H | _T] ->
            H;
        _ ->
            []
    end.

%% ----------------------------------------------
%% 检查活动是否符合开启条件
%%----------------------------------------------
% return true|false
check_ac_start(Module, ModuleSub) ->
    IdList = data_activitycalen:get_ac_sub(Module, ModuleSub),
    CheckList = [time, week, month, open_day, merge_day, time_region],
    AcList = [true || AcSub <- IdList, lib_activitycalen_util:do_check_ac_sub(data_activitycalen:get_ac(Module, ModuleSub, AcSub), CheckList)],
    lists:member(true, AcList).

check_mod_ac_start(Module, ModuleSub,Act)->
    IdList = [Act],
    CheckList = [time, week, month, open_day, merge_day, time_region],
    AcList = [true || AcSub <- IdList, lib_activitycalen_util:do_check_ac_sub(data_activitycalen:get_ac(Module, ModuleSub, AcSub), CheckList)],
    lists:member(true, AcList).
%% ----------------------------------------check-------------------------------------------------
check_get_reward(Player, Id) ->
    CheckList = [
        'live_cfg'
        ,'bag_enough'
        , 'live_enough'
        , 'already_get'
    ],
    do_check_get_reward(CheckList, Player, Id).

do_check_get_reward([], Player, _Id) -> {true, Player};
do_check_get_reward([H|T], Player, Id) ->
    case do_check_get_reward_help(H, Player, Id) of
        {true, NewPlayer} ->
            do_check_get_reward(T, NewPlayer, Id);
        {false, ErrCode, NewPlayer} ->
            {false, ErrCode, NewPlayer}
    end.

do_check_get_reward_help('live_cfg', Player, Id) ->
    #player_status{figure = #figure{lv = Lv}} = Player,
    case data_activitycalen:get_reward_config(Id, Lv) of
        #ac_reward{} -> {true, Player};
        _ -> {false, ?ERRCODE(err157_5_cfg_err), Player}
    end;

do_check_get_reward_help('already_get', Player, Id) ->
    #player_status{id = RoleId} = Player,
    Value = lib_daily:get_count(RoleId, ?MOD_ACTIVITY, ?MOD_ACTIVITY_LIVE_REWARD, Id),
    case Value < ?FINISH_REWARD of
        true ->
            {true, Player};
        false ->
            {false, ?ERRCODE(err157_2_live_aready_get), Player}
    end;

do_check_get_reward_help('live_enough', Player, Id) ->
    #player_status{id = RoleId, figure = #figure{lv = Lv}} = Player,
    #ac_reward{live = NeedLive} = data_activitycalen:get_reward_config(Id, Lv),
    Value = lib_daily:get_count(RoleId, ?MOD_ACTIVITY, ?ACTIVITY_LIVE_DAILY),
    RewardState = lib_daily:get_count(RoleId, ?MOD_ACTIVITY, ?MOD_ACTIVITY_LIVE_REWARD, Id),
    if
        Value < NeedLive orelse RewardState =:= ?NOT_REWARD ->
            {false, ?ERRCODE(err157_1_live_not_enough), Player};
        true ->
            {true, Player}
    end;

do_check_get_reward_help('bag_enough', Player, Id) ->
    #player_status{figure = #figure{lv = Lv}} = Player,
    #ac_reward{reward = RewardList} = data_activitycalen:get_reward_config(Id, Lv),
    %Reward = get_reward_by_lv(RoleLv, RewardList),
    % GoodsTypeList = [{TypeId, Num}||{Type, TypeId, Num}<-Reward, Type == ?TYPE_GOODS],
    case lib_goods_api:can_give_goods(Player, RewardList) of
        true ->
            {true, Player};
        false ->
            {false, ?ERRCODE(err157_3_bag_not_enough), Player}
    end;

do_check_get_reward_help(_Msg, Player, _Id) -> {true, Player}.


%%　获取活动子类(包含结束时间)
get_ac_sub_with_end(Module, ModuleSub) ->
    IdList = data_activitycalen:get_ac_sub(Module, ModuleSub),
    CheckList = [time, time_region_loose, week, month, open_day, merge_day],
    case check_ac_sub(Module, ModuleSub, IdList, CheckList) of
        _Other when IdList == [] ->
            1;
        AcSub when is_integer(AcSub) ->
            AcSub;
        false ->
            case check_ac_sub_priority(Module, ModuleSub, IdList) of
                Value when is_integer(Value) ->
                    Value;
                _ ->
                    1
            end;
        _ ->
            1
    end.

%% 检测所有时间
check_ac_sub(Module, ModuleSub, [Id|T], CheckList) ->
    case lib_activitycalen_util:do_check_ac_sub(data_activitycalen:get_ac(Module, ModuleSub, Id), CheckList) of
        true -> Id;
        _ ->
            check_ac_sub(Module, ModuleSub, T, CheckList)
    end;
check_ac_sub(_, _, [], _) -> false.

% 1.先测试除了北京时间外的时间
% 2.没有符合的则只检测开服和合服时间
check_ac_sub_priority(Module, ModuleSubAc, IdList) ->
    CheckList = [time, week, month, open_day, merge_day],
    CheckRes = [AcSub||
                    AcSub<-IdList,
                    lib_activitycalen_util:do_check_ac_sub(
                        data_activitycalen:get_ac(Module, ModuleSubAc, AcSub),
                        CheckList)],
    case CheckRes == [] of
        true ->
            LastCheckList = [open_day, merge_day],
            LastCheckRes = [AcSub||
                            AcSub<-IdList,
                            lib_activitycalen_util:do_check_ac_sub(
                                data_activitycalen:get_ac(Module, ModuleSubAc, AcSub),
                                LastCheckList)],
            case LastCheckRes of
                [] ->
                    false;
                _ ->
                    lists:min(LastCheckRes)
            end;
        _ ->
            lists:min(CheckRes)
    end.

get_reward_by_lv(RoleLv, RewardList) ->
    List = [{Lv, Reward} || {Lv, Reward} <- RewardList, RoleLv >= Lv],
    LastList = lists:sort(fun({Lv1, _}, {Lv2, _}) -> Lv1 >= Lv2 end, List),
    case LastList of
        [] ->
            [];
        [{_LastLv, LastReward} | _T] ->
            [{T, TI, N}||{T,TI,N} <- LastReward]
    end.

get_exp_by_lv(RoleLv, BaseExp) ->
    List = [{Lv, Exp} || {Lv, Exp} <- BaseExp, RoleLv >= Lv],
    LastList = lists:sort(fun({Lv1, _}, {Lv2, _}) -> Lv1 >= Lv2 end, List),
    case LastList of
        [] ->
            0;
        [{_LastLv, LastExp} | _T] ->
            LastExp * 12 * (RoleLv div 10 + 1)
    end.

get_next_act_lv(Lv, Type) ->
    LvList = data_activitycalen:get_start_lv_list(Type),
    do_get_next_act_lv(Lv, LvList).
do_get_next_act_lv(_Lv, []) -> 0;
do_get_next_act_lv(Lv, [H|T]) ->
    case Lv < H of
        true -> H;
        _ -> do_get_next_act_lv(Lv, T)
    end.

%% 通过配置获取活动当前的开启时间段
%% 活动已开启 return {ok, [开启时间戳, 结束时间戳]}
%% 活动未开启 return false
get_act_open_time_region(Module, SubModule, ActId) ->
    Act = data_activitycalen:get_ac(Module, SubModule, ActId),
    #base_ac{time_region = TimeRegion} = Act,
    {Date, {NowH,NowM,_}} = calendar:local_time(),
    Now = NowH * 60 + NowM,
    case ulists:find(fun
        ({{SH, SM}, {EH, EM}}) ->
            SH * 60 + SM =< Now andalso Now =< EH * 60 + EM
    end, TimeRegion) of
        {ok, {{SH, SM}, {EH, EM}}} ->
            Stime = utime:unixtime({Date, {SH, SM, 0}}),
            Etime = utime:unixtime({Date, {EH, EM, 0}}),
            {ok, [Stime, Etime]};
        _ -> false
    end.


get_vip_max(Module, ModuleSub,Player,Max0)->
    case lists:member(Module,[?MOD_CGWAR,?MOD_ARENA,?MOD_DUNGEON]) of
        true->
            case {Module, ModuleSub} == {610, 8} of
                true -> Max0;
                _ ->
                    Value = lib_vip_api:get_vip_privilege(Player,Module,ModuleSub),
                    Max0+Value
            end;
        _->
            Max0
    end.

%% 主线副本上限需要特殊处理
get_main_line_dun_max(Module, ModuleSub, Max) ->
    case lists:member({Module, ModuleSub}, [{?MOD_DUNGEON, 20}]) of
        true ->
            999;
        _ ->
            Max
    end.

%% 贵族加3次
get_sup_vip_max(Module, ModuleSub, Player, Max) ->
    case lists:member({Module, ModuleSub}, [{?MOD_DUNGEON, 15}]) of
        true ->
            case lib_sup_vip:is_sup_vip(Player) of
                true -> Max + 3;
                _ -> Max
            end;
        _ ->
            Max
    end.

