%% ---------------------------------------------------------------------------
%% @doc
%% @author xiaoxiang
%% @since  2017-04-06
%% @deprecated
%% ---------------------------------------------------------------------------
-module (lib_recharge_act).

-include("server.hrl").
-include("rec_recharge.hrl").
-include("def_recharge.hrl").
-include("def_module.hrl").

-export([
    login/1,
    send_recharge_goods/2
]).

%% 充值活动登陆
login(Player) ->
    %% 福利卡初始化
    WelFarePlayer    = lib_recharge_welfare:login(Player),
    %% 充值返利
    RechargeReturnPS = lib_recharge_return:login(WelFarePlayer),
    %% 首充信息
    FirstChargePS    = lib_recharge_first:login(RechargeReturnPS),
    FirstChargePS.


send_recharge_goods(Player, ProductType) ->
    #player_status{id = RoleId} = Player,
    IdList = data_recharge:get_recharge_goods_by_type(ProductType),
    Info = lists:foldl(
        fun(ProductId, Temp) ->
            #base_recharge_goods{
                limit = Limit
            } = Cfg = data_recharge:get_recharge_goods(ProductId),
            case is_open_sale(Cfg) of
                true  ->
                    Already =
                        case Limit of
                            [?PRODUCT_LIMIT_DAILY, _Count] ->
                                lib_daily:get_count(RoleId, ?MOD_RECHARGE, 1, ProductId);
                            [?PRODUCT_LIMIT_WEEK, _Count] ->
                                lib_week:get_count(RoleId, ?MOD_RECHARGE, 1, ProductId);
                            [?PRODUCT_LIMIT_LIFE, _Count] ->
                                lib_counter:get_count(RoleId, ?MOD_RECHARGE, 1, ProductId);
                            _ ->
                                0
                        end,
                    [{ProductId, Already}|Temp];
                false ->
                    Temp
            end
        end, [], IdList
    ),
    Now = utime:unixtime(),
    ZeroTime = utime:tomorrow(Now),
    NextWeek = utime:unixtime({next_week, Now}),
    Next30   = calc_30_day_time(),
    Timestamps = [
        {1, ZeroTime},
        {2, NextWeek},
        {3, Next30}
    ],
    [ProductType, Info, Timestamps].

is_open_sale(Cfg) ->
    lib_recharge_check:check_time(Cfg).


%% 以30天做为一个轮回，计算当前轮次的结束时间
calc_30_day_time() ->
    OpenDay = util:get_open_day(),
    Round   = (OpenDay - 1) div 30,
    EndDay  = (Round + 1) * 30,
    Date    = erlang:date(),
    EndDate = utime:get_before_day(Date, EndDay - OpenDay, 1),
    utime:unixtime({EndDate, {23, 59, 59}}).
