%%%-------------------------------------------------------------------
%%% @doc     超能大奖-累计充值/累计消费奖励
%%% Module  : lib_great_rewards.erl
%%% Created : 2020-12-22
%%% @author : tyl
%%% @end
%%%-------------------------------------------------------------------
-module(lib_great_rewards).

-include("greate_rewards.hrl").
-include("custom_act.hrl").
-include("server.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("predefine.hrl").
-include("def_goods.hrl").
-include("goods.hrl").
-include("figure.hrl").
-include("rec_event.hrl").
-include("def_event.hrl").
-include("errcode.hrl").

%% API
-export([
        ask_reward_status/3             %% 奖励信息
        ,receive_reward/4               %% 领取奖励
        ,handle_event/2                 %% 充值/消费回调
        ,act_end/1                      %% 活动结束清理数据 & 邮件发放奖励
        ,send_not_receive_reward/3

        ,pack_cfg_reward/4
    ]).


%% ---------------------------------------------------------------
%% @doc 奖励信息 @end
%% ---------------------------------------------------------------
ask_reward_status(#player_status{id = RoleId} = Player, Type, SubType) when Type == ?CUSTOM_ACT_TYPE_GREAT_REWARD ->
    case lib_custom_act_api:is_open_act(Type, SubType) of
        true ->
            #custom_act_data{act_data = ActData} =
            lib_custom_act_data:custom_act_data(Type,SubType),
            %% 已领取奖励信息
            case trans_great_reward(ActData) of
                #great_reward{reward_map = RewardMap} ->
                    RewardList = lists:flatten(maps:values(RewardMap));
                _  ->
                    RewardList = []
            end,
            %% 总充值/消费金额
            case lib_custom_act_util:get_custom_act_open_info(Type, SubType) of
                #act_info{stime = STime, etime = ETime} -> ok;
                _ -> STime = 0, ETime = 0
            end,
            {SumRecharge, SumConsume} = get_sum_money(RoleId, ?GREAT_REWARD_TYPE_BIG_REWARD, STime, ETime),
            %% 奖励配置信息
            PackList = pack_cfg_reward(SubType, SumRecharge, SumConsume, RewardList),
            lib_server_send:send_to_sid(
                Player#player_status.sid, pt_331, 33104, [Type, SubType, PackList]
            );
        false ->
            lib_server_send:send_to_sid(
                Player#player_status.sid, pt_331, 33104, [Type, SubType, []]
            )
    end,
    ok;
ask_reward_status(_, _, _) -> ok.

pack_cfg_reward(SubType, SumRecharge, SumConsume, RewardList) ->
    Type = ?CUSTOM_ACT_TYPE_GREAT_REWARD,
    GradeIdList = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    {RechargeGradeL, ConsumeGradeL, BigRewardGradeL} = split_reward_type(GradeIdList, Type, SubType, {[], [], []}),
    PackList1 = do_pack_cfg_reward(RechargeGradeL, Type, SubType, ?GREAT_REWARD_TYPE_RECHARGE, SumRecharge, SumConsume, RewardList, []),
    PackList2 = do_pack_cfg_reward(ConsumeGradeL, Type, SubType, ?GREAT_REWARD_TYPE_CONSUME, SumRecharge, SumConsume, RewardList, []),
    PackList3 = do_pack_cfg_reward(BigRewardGradeL, Type, SubType, ?GREAT_REWARD_TYPE_BIG_REWARD, SumRecharge, SumConsume, RewardList, []),
    PackList1 ++ PackList2 ++ PackList3.

do_pack_cfg_reward([], _Type, _SubType, _RewardType, _SumRecharge, _SumConsume, _RewardList, PackList) -> PackList;
do_pack_cfg_reward([GradeId|T], Type, SubType, RewardType, SumRecharge, SumConsume, RewardList, PackList) ->
    case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
        #custom_act_reward_cfg{name = Name, desc = Desc, format = Format, condition = Condition, reward = Reward} ->
            case RewardType == ?GREAT_REWARD_TYPE_RECHARGE orelse RewardType == ?GREAT_REWARD_TYPE_CONSUME of
                true ->
                    Key = trans_reward_type(RewardType),
                    case lists:keyfind(Key, 1, Condition) of
                        {_, Money} ->
                            case RewardType of
                                ?GREAT_REWARD_TYPE_RECHARGE when SumRecharge >= Money ->
                                    case lists:member(GradeId, RewardList) of
                                        true -> RewardStatus = ?GREAT_REWARD_HAS_RECEIVED;
                                        _ -> RewardStatus = ?GREAT_REWARD_CAN_RECEIVE
                                    end;
                                ?GREAT_REWARD_TYPE_CONSUME when SumConsume >= Money ->
                                    case lists:member(GradeId, RewardList) of
                                        true -> RewardStatus = ?GREAT_REWARD_HAS_RECEIVED;
                                        _ -> RewardStatus = ?GREAT_REWARD_CAN_RECEIVE
                                    end;
                                _ -> RewardStatus = ?GREAT_REWARD_CANNOT_RECEIVE
                            end,
                            Progress = case RewardType of
                                ?GREAT_REWARD_TYPE_RECHARGE -> SumRecharge;
                                _ -> SumConsume
                            end,
                            ConditionStr = util:term_to_string(Condition),
                            RewardStr = util:term_to_string(Reward),
                            PackList1 = [{GradeId, Format, Progress, RewardStatus, 0, Name, Desc, ConditionStr, RewardStr}|PackList];
                        _ -> PackList1 = PackList
                    end;
                _ ->
                    case Condition of
                        [{recharge, Recharge}, {consume, Consume}] ->
                            case SumRecharge >= Recharge andalso SumConsume >= Consume of
                                true ->
                                    case lists:member(GradeId, RewardList) of
                                        true -> RewardStatus = ?GREAT_REWARD_HAS_RECEIVED;
                                        _ -> RewardStatus = ?GREAT_REWARD_CAN_RECEIVE
                                    end;
                                _ -> RewardStatus = ?GREAT_REWARD_CANNOT_RECEIVE
                            end,
                            ConditionStr = util:term_to_string(Condition),
                            RewardStr = util:term_to_string(Reward),
                            PackList1 = [{GradeId, Format, 0, RewardStatus, 0, Name, Desc, ConditionStr, RewardStr}|PackList];
                        _ -> PackList1 = PackList
                    end
            end;
        _ -> PackList1 = PackList
    end,
    do_pack_cfg_reward(T, Type, SubType, RewardType, SumRecharge, SumConsume, RewardList, PackList1).

split_reward_type([], _Type, _SubType, GradeTuple) -> GradeTuple;
split_reward_type([GradeId|T], Type, SubType, {GradeList1, GradeList2, GradeList3}) ->
    case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
        #custom_act_reward_cfg{condition = Condition} ->
            Tuple1 = case Condition of
                [{recharge, _}] -> {[GradeId|GradeList1], GradeList2, GradeList3};
                [{consume, _}]  -> {GradeList1, [GradeId|GradeList2], GradeList3};
                [{recharge, _}, {consume, _}] -> {GradeList1, GradeList2, [GradeId|GradeList3]};
                _ -> {GradeList1, GradeList2, GradeList3}
            end;
        _ ->
            Tuple1 = {GradeList1, GradeList2, GradeList3}
    end,
    split_reward_type(T, Type, SubType, Tuple1).

get_sum_money(RoleId, RewardType, STime, ETime) ->
    case RewardType of
        ?GREAT_REWARD_TYPE_RECHARGE ->
            Recharge = lib_recharge_data:get_my_recharge_between(RoleId, STime, ETime),
            {Recharge, 0};
        ?GREAT_REWARD_TYPE_CONSUME  ->
            Consume = lib_consume_data:get_gold_consume_between(RoleId, STime, ETime),
            {0, Consume};
        ?GREAT_REWARD_TYPE_BIG_REWARD ->
            Recharge = lib_recharge_data:get_my_recharge_between(RoleId, STime, ETime),
            Consume = lib_consume_data:get_gold_consume_between(RoleId, STime, ETime),
            {Recharge, Consume};
        _ ->
            {0, 0}
    end.

trans_reward_type(RewardType) ->
    case RewardType of
        ?GREAT_REWARD_TYPE_RECHARGE -> recharge;
        ?GREAT_REWARD_TYPE_CONSUME  -> consume;
        _ -> none
    end.

trans_great_reward(ActData) when is_record(ActData, great_reward) -> ActData;
trans_great_reward(_) ->
    #great_reward{}.

%% ---------------------------------------------------------------
%% @doc 领取奖励 @end
%% ---------------------------------------------------------------
receive_reward(#player_status{id = RoleId, figure = Figure} = Player, Type, SubType, GradeId) ->
    RewardType = get_reward_type(SubType, GradeId),
    case lib_custom_act_api:is_open_act(Type, SubType) of
        true when RewardType > 0 ->
            #custom_act_data{act_data = ActData} = CustomActData =
            lib_custom_act_data:custom_act_data(Type,SubType),
            %% 已领取奖励信息
            ActData1 = trans_great_reward(ActData),
            case ActData1 of
                #great_reward{reward_map = RewardMap} ->
                    RewardList = maps:get(RewardType, RewardMap, []);
                _  ->
                    RewardMap= #{}, RewardList = []
            end,
            %% 总充值/消费金额
            case lib_custom_act_util:get_custom_act_open_info(Type, SubType) of
                #act_info{stime = STime, etime = ETime} -> ok;
                _ -> STime = 0, ETime = 0
            end,
            {SumRecharge, SumConsume} = get_sum_money(RoleId, RewardType, STime, ETime),
            {CheckLv, Res0} = check_lv(SubType, Figure#figure.lv),
            {CheckCanReceive, Res1} = check_can_receive(SubType, RewardMap, RewardType, GradeId, SumRecharge, SumConsume),
            {CheckHasReceive, Res2} = check_has_receive(GradeId, RewardList),
            if
                not CheckLv ->
                    lib_server_send:send_to_sid(
                        Player#player_status.sid, pt_331, 33105, [Res0, Type, SubType, GradeId]
                    ),
                    ok;
                not CheckCanReceive ->
                    lib_server_send:send_to_sid(
                        Player#player_status.sid, pt_331, 33105, [Res1, Type, SubType, GradeId]
                    ),
                    ok;
                not CheckHasReceive ->
                    lib_server_send:send_to_sid(
                        Player#player_status.sid, pt_331, 33105, [Res2, Type, SubType, GradeId]
                    ),
                    ok;
                true ->
                    SendReward = get_send_reward(SubType, GradeId),
                    Produce = #produce{reward = SendReward, type = great_reward},
                    {ok, NewPlayer} = lib_goods_api:send_reward_with_mail(Player, Produce, ?MOD_AC_CUSTOM),
                    NewRewardList = [GradeId|RewardList],
                    NewActData = ActData1#great_reward{reward_map = maps:put(RewardType, NewRewardList, RewardMap)},
                    NewCustomActData = CustomActData#custom_act_data{time = utime:unixtime(),act_data = NewActData},
                    lib_custom_act_data:save_custom_data(RoleId, NewCustomActData),
                    lib_server_send:send_to_sid(
                        Player#player_status.sid, pt_331, 33105, [?SUCCESS, Type, SubType, GradeId]
                    ),
                    log_send_goods(NewPlayer, Player, Type, SubType, SendReward),
                    {ok, NewPlayer}
            end;
        _ ->
            lib_server_send:send_to_sid(
                Player#player_status.sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
            ),
            ok
    end.

%% 记录日志
log_send_goods(NewPlayer, Player, Type, SubType, Reward) ->
    #player_status{id = RoleId, figure = Figure, gold = Gold, bgold = BGold} = Player,
    #player_status{gold = GoldNew, bgold = BGoldNew} = NewPlayer,
    #figure{vip = Vip, lv = Lv} = Figure,
    WorldLv = util:get_world_lv(),
    lib_log_api:log_custom_act_reward(RoleId, Type, SubType,
        0, WorldLv, Reward, [], Vip, Lv, Gold, GoldNew, BGold, BGoldNew, utime:unixtime()
    ),
    ok.

%% 奖励类型
get_reward_type(SubType, GradeId) ->
    case lib_custom_act_util:get_act_reward_cfg_info(?CUSTOM_ACT_TYPE_GREAT_REWARD, SubType, GradeId) of
        #custom_act_reward_cfg{condition = Condition} ->
            case Condition of
                [{recharge, _}] -> ?GREAT_REWARD_TYPE_RECHARGE;
                [{consume, _}] -> ?GREAT_REWARD_TYPE_CONSUME;
                [{recharge, _},{consume, _}] -> ?GREAT_REWARD_TYPE_BIG_REWARD;
                _ -> 0
            end;
        _ -> 0
    end.

%% 判断等级条件
check_lv(SubType, RoleLv) ->
    CfgLv = lib_custom_act_util:get_act_open_lv(?CUSTOM_ACT_TYPE_GREAT_REWARD, SubType),
    case RoleLv >= CfgLv of
        true -> {true, ?SUCCESS};
        _ -> {false, ?ERRCODE(err331_great_reward_lv_err)}
    end.

%% 判断是否能领取奖励,大奖需要同时满足 累充、累消、其他奖励已经领取完毕 条件
check_can_receive(SubType, RewardMap, RewardType, GradeId, SumRecharge, SumConsume) ->
    case lib_custom_act_util:get_act_reward_cfg_info(?CUSTOM_ACT_TYPE_GREAT_REWARD, SubType, GradeId) of
        #custom_act_reward_cfg{condition = Condition} ->
            case RewardType of
                ?GREAT_REWARD_TYPE_RECHARGE ->
                    case lists:keyfind(trans_reward_type(RewardType), 1, Condition) of
                        {_, NeedMoney} when SumRecharge >= NeedMoney -> {true, ?SUCCESS};
                        {_, _NeedMoney} -> {false, ?ERRCODE(err331_great_reward_recharge_not_enough)};
                        _ -> {false, ?ERRCODE(err331_great_reward_type_err)}
                    end;
                ?GREAT_REWARD_TYPE_CONSUME ->
                    case lists:keyfind(trans_reward_type(RewardType), 1, Condition) of
                        {_, NeedMoney} when SumConsume >= NeedMoney -> {true, ?SUCCESS};
                        {_, _NeedMoney} -> {false, ?ERRCODE(err331_great_reward_consume_not_enough)};
                        _ -> {false, ?ERRCODE(err331_great_reward_type_err)}
                    end;
                ?GREAT_REWARD_TYPE_BIG_REWARD ->
                    case lists:keyfind(trans_reward_type(?GREAT_REWARD_TYPE_RECHARGE), 1, Condition) of
                        {_, NeedMoney} when SumRecharge >= NeedMoney ->
                            case lists:keyfind(trans_reward_type(?GREAT_REWARD_TYPE_CONSUME), 1, Condition) of
                                {_, NeedMoney1} when SumConsume >= NeedMoney1 ->
                                    case check_other_reward(SubType, RewardMap) of
                                        true -> {true, ?SUCCESS};
                                        false -> {false, ?ERRCODE(err331_great_reward_must_receive_all)}
                                    end;
                                {_, _NeedMoney1} -> {false, ?ERRCODE(err331_great_reward_consume_not_enough)};
                                _ -> {false, ?ERRCODE(err331_great_reward_type_err)}
                            end;
                        {_, _NeedMoney} -> {false, ?ERRCODE(err331_great_reward_recharge_not_enough)};
                        _ -> {false, ?ERRCODE(err331_great_reward_type_err)}
                    end;
                _ -> {false, ?ERRCODE(err331_great_reward_type_err)}
            end;
        _ -> {false, ?ERRCODE(err331_great_reward_reward_err)}
    end.

%% 检查其他奖励是否已经领取完毕
check_other_reward(SubType, RewardMap) ->
    Type = ?CUSTOM_ACT_TYPE_GREAT_REWARD,
    GradeIdList = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    {RechargeGradeL, ConsumeGradeL, _BigRewardGradeL} = split_reward_type(GradeIdList, Type, SubType, {[], [], []}),
    RechargeRewardL = maps:get(?GREAT_REWARD_TYPE_RECHARGE, RewardMap, []),
    ConsumeRewardL = maps:get(?GREAT_REWARD_TYPE_CONSUME, RewardMap, []),
    case RechargeRewardL == RechargeGradeL andalso ConsumeRewardL == ConsumeGradeL of
        true -> true;
        _ -> false
    end.

%% 判断是否已领取过奖励
check_has_receive(GradeId, RewardList) ->
    case lists:member(GradeId, RewardList) of
        true -> {false, ?ERRCODE(err331_great_reward_has_received)};
        _ -> {true, ?SUCCESS}
    end.

%% 发放的奖励
get_send_reward(SubType, GradeId) ->
    case lib_custom_act_util:get_act_reward_cfg_info(?CUSTOM_ACT_TYPE_GREAT_REWARD, SubType, GradeId) of
        #custom_act_reward_cfg{reward = Reward} -> ok;
        _ -> Reward = []
    end,
    Reward.

%% ---------------------------------------------------------------
%% @doc 充值回调,注意不限制等级 @end
%% ---------------------------------------------------------------
handle_event(Player, #event_callback{type_id = ?EVENT_RECHARGE}) ->
    ActType = ?CUSTOM_ACT_TYPE_GREAT_REWARD,
    RewardType = ?GREAT_REWARD_TYPE_RECHARGE,
    case lib_custom_act_util:get_open_subtype_list(ActType) of
        [] ->
            {ok, Player};
        OpenActList ->
            F = fun(#act_info{key = {_, SubType}, stime = STime, etime = ETime}) ->
                    %% 直接通过接口拿数据,避免统计过程中不可控因素导致错误
                    Recharge = lib_recharge_data:get_my_recharge_between(Player#player_status.id, STime, ETime),
                    update_great_reward(Player, SubType, RewardType, Recharge),
                    ask_reward_status(Player, ActType, SubType)
            end,
            lists:foreach(F, OpenActList),
            {ok, Player}
    end;
%% ---------------------------------------------------------------
%% @doc 消费回调,注意不限制等级 @end
%% ---------------------------------------------------------------
handle_event(#player_status{id = RoleId} = Player, #event_callback{type_id = ?EVENT_MONEY_CONSUME, data = Data}) ->
    #callback_money_cost{money_type = MoneyType, cost = _Cost} = Data,
    ActType = ?CUSTOM_ACT_TYPE_GREAT_REWARD,
    %% 只有消耗钻石才需要处理
    case MoneyType of
        ?TYPE_GOLD ->
            case lib_custom_act_util:get_open_subtype_list(ActType) of
                [] ->
                    ignore;
                OpenActList ->
                    RewardType = ?GREAT_REWARD_TYPE_CONSUME,
                    F = fun(#act_info{key = {_, SubType}, stime = STime, etime = ETime}) ->
                            Consume = lib_consume_data:get_gold_consume_between(RoleId, STime, ETime),
                            update_great_reward(Player, SubType, RewardType, Consume),
                            ask_reward_status(Player, ActType, SubType)
                    end,
                    lists:map(F, OpenActList)
            end;
        _ -> ignore
    end,
    {ok, Player};
handle_event(Role, _) ->
    {ok, Role}.

%% ---------------------------------------------------------------
update_great_reward(#player_status{id = RoleId}, SubType, RewardType, Gold) ->
    Type = ?CUSTOM_ACT_TYPE_GREAT_REWARD,
    #custom_act_data{act_data = ActData} = CustomData =
    lib_custom_act_data:custom_act_data(Type, SubType),
    %% 已领取奖励信息
    case trans_great_reward(ActData) of
        #great_reward{recharge = Recharge, consume = Consume} = GreatReward ->
            case RewardType of
                ?GREAT_REWARD_TYPE_RECHARGE ->
                    NewRecharge = Gold,
                    NewConsume  = Consume;
                ?GREAT_REWARD_TYPE_CONSUME ->
                    NewRecharge = Recharge,
                    NewConsume  = Gold;
                _ -> %% 理论上不会发生
                    NewRecharge = Recharge,
                    NewConsume  = Consume
            end,
            NewGreatReward = GreatReward#great_reward{recharge = NewRecharge, consume = NewConsume},
            NewCustomData = CustomData#custom_act_data{
                type = Type, subtype = SubType, act_data = NewGreatReward,
                time = utime:unixtime()
            },
            lib_custom_act_data:save_custom_data(RoleId, NewCustomData);
        _ ->
            ignore
    end,
    ok.

%% ---------------------------------------------------------------
%% @doc 活动结束清理数据 & 邮件发放未领取奖励 @end
%% ---------------------------------------------------------------
act_end(#act_info{key = {?CUSTOM_ACT_TYPE_GREAT_REWARD = ActType, SubType}}) ->
    OpenLv = lib_custom_act_util:get_act_open_lv(ActType, SubType),
    SQL = io_lib:format(?SELECT_GREAT_REWARD_ACTIVE_ROLES, [OpenLv]),
    Roles = lists:flatten(db:get_all(SQL)),
    [begin
                Pid = misc:get_player_process(RoleId),
                case misc:is_process_alive(Pid) of
                    true ->
                        lib_player:apply_cast(Pid, ?APPLY_CAST_STATUS, ?MODULE, send_not_receive_reward, [ActType, SubType]);
                    false ->
                        send_offline_reward(RoleId, ActType, SubType)
                end
        end || RoleId <- Roles],
    ok;
act_end(_) ->
    ok.

%% 给在线玩家补发未领取奖励
send_not_receive_reward(Player, ActType, SubType) ->
    #player_status{id = RoleId} = Player,
    #custom_act_data{act_data = ActData} =
    lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_GREAT_REWARD, SubType),
    ActData1 = trans_great_reward(ActData),
    RewardList = calc_not_receive_reward(SubType, ActData1),
    send_reward_mail(RoleId, ActType, SubType, RewardList),
    {ok, Player}.

%% 补发离线玩家未领取奖励
send_offline_reward(RoleId, ActType, SubType) ->
    case get_act_data(RoleId, ActType, SubType) of
        #great_reward{} = ActData ->
            RewardList = calc_not_receive_reward(SubType, ActData),
            send_reward_mail(RoleId, ActType, SubType, RewardList);
        _ -> skip
    end.

%% 获取离线玩家活动数据
get_act_data(RoleId, Type, SubType) ->
    SQL = io_lib:format(?SELECT_GREAT_REWARD_CUSTOM_ACT_DATA, [RoleId, Type, SubType]),
    case db:get_row(SQL) of
        [] -> skip;
        [BinData] ->
            ActData = util:bitstring_to_term(BinData),
            case ActData of
                #great_reward{} ->
                    ActData;
                _ -> skip
            end
    end.

calc_not_receive_reward(SubType, ActData) -> %% todo:需要拿最新充值和消费金额？
    Type = ?CUSTOM_ACT_TYPE_GREAT_REWARD,
    #great_reward{reward_map = RewardMap, recharge = Recharge, consume = Consume} = ActData,
    GradeIdList = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    {RechargeGradeL, ConsumeGradeL, BigRewardGradeL} = split_reward_type(GradeIdList, Type, SubType, {[], [], []}),
    RewardList  = do_calc_not_receive_reward(Type, SubType, ?GREAT_REWARD_TYPE_RECHARGE, RechargeGradeL, Recharge, Consume, RewardMap),
    RewardList1 = do_calc_not_receive_reward(Type, SubType, ?GREAT_REWARD_TYPE_CONSUME, ConsumeGradeL, Recharge, Consume, RewardMap),
    RewardList2 = do_calc_not_receive_reward(Type, SubType, ?GREAT_REWARD_TYPE_BIG_REWARD, BigRewardGradeL, Recharge, Consume, RewardMap),
    SumRewardList = RewardList ++ RewardList1 ++ RewardList2,
    SumRewardList.

do_calc_not_receive_reward(Type, SubType, RewardType, RewardGradeList, Recharge, Consume, RewardMap) ->
    CanReceiveGradeL = calc_can_receive(RewardGradeList, Type, SubType, RewardType, Recharge, Consume),
    HasReceiveGradeL = maps:get(RewardType, RewardMap, []),
    NotReceiveGradeL = CanReceiveGradeL -- HasReceiveGradeL,
    RewardList = do_calc_reward(Type, SubType, NotReceiveGradeL),
    RewardList.

calc_can_receive(GradeList, Type, SubType, RewardType, Recharge, Consume) ->
    F = fun(GradeId, TCanList) ->
            case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
                #custom_act_reward_cfg{condition = Condition} ->
                    Key = trans_reward_type(RewardType),
                    case RewardType of
                        ?GREAT_REWARD_TYPE_RECHARGE ->
                            case lists:keyfind(Key, 1, Condition) of
                                {_, NeedMoney} when Recharge >= NeedMoney -> [GradeId|TCanList];
                                _ -> TCanList
                            end;
                        ?GREAT_REWARD_TYPE_CONSUME ->
                            case lists:keyfind(Key, 1, Condition) of
                                {_, NeedMoney} when Recharge >= NeedMoney -> [GradeId|TCanList];
                                _ -> TCanList
                            end;
                        ?GREAT_REWARD_TYPE_BIG_REWARD ->
                            case Condition of
                                [{recharge, NeedRecharge}, {consume, NeedConsume}]
                                when Recharge >= NeedRecharge, Consume >= NeedConsume ->
                                    [GradeId|TCanList];
                                _ -> TCanList
                            end
                    end;
                _ -> TCanList
            end end,
    CanList = lists:foldl(F, [], GradeList),
    CanList.

do_calc_reward(Type, SubType, ReceiveGradeList) ->
    F = fun(GradeId, TRewardList) ->
            case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
                #custom_act_reward_cfg{reward = Reward} ->
                    Reward ++ TRewardList;
                _ -> TRewardList
            end end,
    RewardList = lists:foldl(F, [], ReceiveGradeList),
    RewardList.

%% 邮件发送奖励
send_reward_mail(RoleId, ActType, SubType, RewardList) ->
    if
        RewardList =/= [] ->
            lib_mail_api:send_sys_mail([RoleId], ActType, SubType, utext:get(33112301), utext:get(33112302), RewardList);
        true -> skip
    end.
