%%-----------------------------------------------------------------------------
%% @Module  :       lib_custom_act_do_recharge_consume
%% @Author  :       Ywf
%% @Email   :       349162727@qq.com
%% @Created :       2019-05-08
%% @Description:	充值消费
%%-----------------------------------------------------------------------------
-module (lib_custom_act_do_recharge_consume).

-compile(export_all).
-include("custom_act.hrl").
-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("def_fun.hrl").

send_info(PS, SubType) ->
	ActType = ?CUSTOM_ACT_TYPE_RECHARGE_CONSUME,
    #player_status{id = RoleId, figure = #figure{lv = Lv}} = PS,
    OpenLv = lib_custom_act_util:get_act_open_lv(ActType, SubType),
    case Lv >= OpenLv of
        false ->
            Code = ?LEVEL_LIMIT,
            RechargeNum = 0,
            ConsumeNum = 0;
        true ->
            case lib_custom_act_api:is_open_act(ActType, SubType) of
                false ->
                    Code = ?ERRCODE(err331_act_closed),
                    RechargeNum = 0,
                    ConsumeNum = 0;
                true ->
                    Code = ?SUCCESS,
                    {money_type, MoneyType} = lib_custom_act_util:keyfind_act_condition(ActType, SubType, money_type),
                    ActInfo = lib_custom_act_util:get_custom_act_open_info(ActType, SubType),
                    #act_info{
                        stime = STime,
                        etime = ETime
                    } = ActInfo,
                    RechargeNum = lib_recharge_data:get_my_recharge_between(RoleId, STime, ETime),
                    ConsumeNum = lib_consume_data:get_consume_between(RoleId, MoneyType, STime, ETime)
            end
    end,
    {ok, Bin} = pt_331:write(33164, [Code, SubType, RechargeNum, ConsumeNum]),
    lib_server_send:send_to_uid(RoleId, Bin).

check_receive_reward_conditions(Player, #act_info{stime = Stime, etime = Etime}, RwCfg) ->
	#custom_act_reward_cfg{condition = Conditions} = RwCfg,
    RechargeCon = lists:keyfind(recharge, 1, Conditions),
    ConsumeCon = lists:keyfind(consume, 1, Conditions),
    PlayerId = if is_record(Player, player_status) -> Player#player_status.id; true -> Player end,
    IfRecharge = case RechargeCon of
        {recharge, NeedRechargeNum} ->
            case lib_recharge_data:get_recharge_between(PlayerId, Stime, Etime) of
                RechargeNum when RechargeNum >= NeedRechargeNum -> true;
                _ -> {false, ?ERRCODE(err331_act_can_not_get)}
            end;
        _ -> true
    end,
    IfConsume = case ConsumeCon of
        {consume, MoneyType, NeedConsumeNum} ->
            case lib_consume_data:get_consume_between(PlayerId, MoneyType, Stime, Etime) of
                ConsumeValue when ConsumeValue >= NeedConsumeNum -> true;
                _ -> {false, ?ERRCODE(err331_act_can_not_get)}
            end;
        _ -> true
    end,
    if
        IfRecharge =/= true -> IfRecharge;
        IfConsume =/= true -> IfConsume;
        true -> true
    end.

get_extra_log(#custom_act_reward_cfg{condition = Condition}) ->
    case [lists:keyfind(recharge, 1, Condition), lists:keyfind(consume, 1, Condition)] of
        [{recharge, _RechargeNum}, {consume, _ConsumeType, _ConsumeNum}] -> io_lib:format("final_reward", []);
        [false, {consume, _ConsumeType, ConsumeNum}] -> io_lib:format("consume:~p", [ConsumeNum]);
        [{recharge, RechargeNum}, false] -> io_lib:format("recharge:~p", [RechargeNum]);
        _ -> []
    end.

get_act_roles(#act_info{key = {ActType, SubType}}) ->
    OpenLv = lib_custom_act_util:get_act_open_lv(ActType, SubType),
    SQL = io_lib:format("SELECT `id` FROM `player_low` WHERE `lv` >= ~p", [OpenLv]),
    lists:flatten(db:get_all(SQL)).

check_has_rewards_for_forget(RoleId, ActInfo, RwCfg, GotTimes) ->
    case lib_custom_act_check:check_receive_reward_conditions(RoleId, ActInfo, RwCfg) of
        true when GotTimes == 0 -> {ok, 1};
        _ -> false
    end.