%%-----------------------------------------------------------------------------
%% @Module  :       lib_custom_act_do_continue_consume
%% @Author  :       Ywf
%% @Email   :       349162727@qq.com
%% @Created :       2019-05-13
%% @Description:	连续消费
%%-----------------------------------------------------------------------------
-module (lib_custom_act_do_continue_consume).

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

check_receive_reward_conditions(Player, #act_info{key = {?CUSTOM_ACT_TYPE_CONTINUE_CONSUME, _}, stime = Stime, etime = Etime}, RwCfg) ->
    UnixDate = utime:unixdate(),
    RoleId = ?IF(is_record(Player, player_status), Player#player_status.id, Player),
    #custom_act_reward_cfg{condition = Condition} = RwCfg,
    [ClearType, GoldLim] = lib_custom_act_check:check_act_condtion([type, gold], Condition),
    case ClearType of
        1 -> %% 每日的
            case lib_consume_data:get_gold_consume_between(RoleId, UnixDate, Etime) >= GoldLim of
                true -> true;
                false -> {false, ?ERRCODE(err331_act_can_not_get)}
            end;
        2 -> %% 累计连续的
           [CfgDay] = lib_custom_act_check:check_act_condtion([day], Condition),
            F = fun({_, Gold}, Acc) when Gold >= GoldLim -> Acc + 1;
                (_, Acc) -> Acc
                end,
            case lists:foldl(F, 0, lib_consume_data:get_gold_consume_between(RoleId, Stime, Etime)) >= CfgDay of
                true -> true;
                false -> {false, ?ERRCODE(err331_act_can_not_get)}
            end;
        _ -> {false, ?ERRCODE(err_config)}
    end.

check_reward_receive_times(#player_status{status_custom_act = #status_custom_act{reward_map = RewardMap}},
    #act_info{key = {Type = ?CUSTOM_ACT_TYPE_CONTINUE_CONSUME, SubType}, stime = Stime, etime = Etime}, #custom_act_reward_cfg{grade = GradeId}) ->
    #custom_act_reward_cfg{condition = Condition} = lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId),
    [ClearType] = lib_custom_act_check:check_act_condtion([type], Condition),
    case maps:get({Type, SubType, GradeId}, RewardMap, []) of
        #reward_status{utime = Utime, receive_times = ReceiveTimes} ->
            case ClearType of
                1 -> %% 单天的
                    case Utime >= utime:unixdate() andalso ReceiveTimes > 0 of
                        true -> false;
                        false -> {true, 1}
                    end;
                2 -> %% 累计的
                    case Utime >= Stime andalso Utime =< Etime andalso ReceiveTimes > 0 of
                        true -> false;
                        false -> {true, 1}
                    end
            end;
        _ -> {true, 1}
    end.

calc_got_reward_times(StatusCustomAct, #act_info{key = {?CUSTOM_ACT_TYPE_CONTINUE_CONSUME = Type, SubType}, stime = Stime, etime = Etime}, GradeId) ->
    RewardMap = StatusCustomAct#status_custom_act.reward_map,
    #custom_act_reward_cfg{condition = Condition} = lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId),
    [ClearType] = lib_custom_act_check:check_act_condtion([type], Condition),
    case maps:get({Type, SubType, GradeId}, RewardMap, []) of
        #reward_status{utime = Utime, receive_times = ReceiveTimes} ->
            case ClearType of
                1 ->
                    case Utime >= utime:unixdate() andalso ReceiveTimes > 0 of
                        true -> ReceiveTimes;
                        false -> 0
                    end;
                2 ->
                    case Utime >= Stime andalso Utime =< Etime andalso ReceiveTimes > 0 of
                        true -> ReceiveTimes;
                        false -> 0
                    end
            end;
        _ -> 0
    end.

send_info(#player_status{sid = Sid, id = RoleId}, SubType) ->
	Type = ?CUSTOM_ACT_TYPE_CONTINUE_CONSUME,
    case lib_custom_act_util:get_custom_act_open_info(Type, SubType) of
        #act_info{stime = Stime, etime = Etime} -> %% 获取开启时间和结束时间
            %% 筛选累计消费的奖品
            GL = [GId || GId <- lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
                case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GId) of
                    #custom_act_reward_cfg{condition = Condition} -> %% type为2表示累计消费
                        case lib_custom_act_check:check_act_condtion([type], Condition) of
                            [2] -> true;
                            _ -> false
                        end;
                    _ -> ?ERR("cfg err!~n", []), false
                end],
            case GL of
                [GradeId | _] ->
                    #custom_act_reward_cfg{condition = Condition} = lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId),
                    case lib_custom_act_check:check_act_condtion([gold], Condition) of
                        [GoldLim] -> ok;
                        _ -> GoldLim = 0
                    end,
                    case lib_custom_act_check:check_act_condtion([bgold], Condition) of
                        [BGoldLim] -> ok;
                        _ -> BGoldLim = 0
                    end,
                    if
                        GoldLim > 0 ->
                            LimType = gold,
                            Lim = GoldLim;
                        BGoldLim > 0 ->
                            LimType = gold,
                            Lim = BGoldLim
                    end,
                    DayNum = lib_consume_data:get_consume_between(RoleId, LimType, utime:unixdate(), Etime), %% 获取今天一共花了多少
                    F = fun
                            ({_, Consume}, Acc) when Consume >= Lim -> Acc + 1; %% 这里要求配置每天的消费钻石数量相等
                            (_, Acc) -> Acc
                        end,
                    ContDay = lists:foldl(F, 0, lib_consume_data:get_consume_between(RoleId, LimType, Stime, Etime)), %% 获取活动开始到结束之间每天玩家消费
                    lib_server_send:send_to_sid(Sid, pt_331, 33143, [DayNum, ContDay]);
                _ ->
                    lib_server_send:send_to_sid(Sid, pt_331, 33100, [?ERRCODE(err_config)])
            end;
        _ ->
            lib_server_send:send_to_sid(Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)])
    end.
