%% ---------------------------------------------------------------------------
%% @doc    充值系统逻辑检查
%% @author hek
%% @since  2016-11-14
%% @deprecated
%% ---------------------------------------------------------------------------

-module (lib_recharge_check).
-include ("common.hrl").
-include ("rec_recharge.hrl").
-include ("def_recharge.hrl").
-include ("errcode.hrl").
-include ("def_fun.hrl").
-include ("activitycalen.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-compile (export_all).

%% 检查商品是否开放，是否符合商品条件
check_product(Player, ProductId) when is_integer(ProductId) ->
    Product = data_recharge:get_product(ProductId),
    check_product(Player, Product);
check_product(Player, Product) when is_record(Product, base_recharge_product) ->
    #base_recharge_product{
        product_id 		= ProductId
    } = Product,
    ProductGoods = data_recharge:get_recharge_goods(ProductId),
    CkList = [
        {product_ctrl_exist, ProductGoods},     %% 商品配置
        {time, ProductGoods},                   %% 开放时间
        {count, Player#player_status.id, ProductGoods},                  %% 购买次数
        {extra_condition, Player, ProductGoods} %% 购买条件
    ],
    case checklist(CkList) of
        true  -> {true, Product};
        {false, Error} -> {false, Error}
    end;
check_product(_, _) ->
    {false, ?ERRCODE(err158_product_error)}.

check_product(ProductId) when is_integer(ProductId) ->
	Product = data_recharge:get_product(ProductId),
    check_product(Product);

check_product(Product) when is_record(Product, base_recharge_product) ->
    #base_recharge_product{
        product_id 		= ProductId
    } = Product,
    ProductGoods = data_recharge:get_recharge_goods(ProductId),
    CkList = [
        {product_ctrl_exist, ProductGoods},     %% 商品配置
        {time, ProductGoods}                    %% 开放时间
    ],
	case checklist(CkList) of
		true -> {true, Product};
		{false, Res} -> {false, Res}
	end;
check_product(_) ->
    {false, ?ERRCODE(err158_product_error)}.

checklist([]) -> true;
checklist([H|T]) ->
    case check(H) of
        true -> checklist(T);
        {false, Res} -> {false, Res}
    end.

%% 该充值商品缺失控制信息配置
check({product_ctrl_exist, ProductCtrl}) ->
	case ProductCtrl of
		[] -> {false, ?ERRCODE(err158_product_ctrl_error)};
		_ -> true
	end;

%% 开放时间
check({time, ProductCtrl}) ->
    case check_time(ProductCtrl) of
        true -> true;
        false -> {false, ?ERRCODE(err158_time_error)}
    end;

%% 购买条件条件
check({extra_condition, Player, ProductCtrl}) ->
    check_extra_condition(Player, ProductCtrl#base_recharge_goods.condition);

%% 次数限制
check({count, RoleId, ProductCtrl}) ->
    #base_recharge_goods{id = ProductId, type = Type, sub_type = SubType, limit = Limit} = ProductCtrl,
    CanBuy =
    case Limit of
        [?PRODUCT_LIMIT_DAILY, Count] ->
            Already = lib_daily:get_count(RoleId, ?MOD_RECHARGE, 1, ProductId),
            Already < Count;
        [?PRODUCT_LIMIT_WEEK, Count] ->
            Already = lib_week:get_count(RoleId, ?MOD_RECHARGE, 1, ProductId),
            Already < Count;
        [?PRODUCT_LIMIT_LIFE, Count] ->
            Already = lib_counter:get_count(RoleId, ?MOD_RECHARGE, 1, ProductId),
            Already < Count;
        _ ->
            true
    end,
    CanBuy2 =
    if
        Type == 2 ->
            %% 尊享卡每日限购
            if
                SubType == ?WELFARE_SUBTYPE_WEEK ->
                    Value = lib_daily:get_count(RoleId, ?MOD_RECHARGE_ACT, 101, 1),
                    ?WEEK_MAX >= Value;
                SubType == ?WELFARE_SUBTYPE_MONTH ->
                    Value = lib_daily:get_count(RoleId, ?MOD_RECHARGE_ACT, 102, 1),
                    ?MONTH_MAX >= Value;
                true ->
                    Value = lib_daily:get_count(RoleId, ?MOD_RECHARGE_ACT, 103, 1),
                    ?QUARTER_MAX >= Value
            end;
        true -> true
    end,
    case CanBuy andalso CanBuy2 of
        true  -> true;
        false -> {false, ?ERRCODE(err158_not_enough_times)}
    end;
check(_) ->
    {false, ?FAIL}.

check_time(ProductCtrl) ->
    #base_recharge_goods{
        start_time      = StartTime,              %% 开始时间
        end_time        = EndTime,                %% 结束时间
        open_day        = OpenDay,                %% 开服
        merge_day       = MergeDay                %% 合服开始
    } = ProductCtrl,
    BaseAc = #base_ac{
        open_day        = OpenDay,
        merge_day       = MergeDay,
        start_timestamp = StartTime,            % 开始时间戳
        end_timestamp   = EndTime,              % 结束时间戳
        type            = 1
    },
    CheckList = [timestamp, open_day, merge_day],
    lib_activitycalen_util:do_check_ac_sub(BaseAc, CheckList).

%% @return true | {false, errorcode}
check_extra_condition(_, []) -> true;
check_extra_condition(Player, [{lv, NeedLv}|T]) ->
    RoleLv = Player#player_status.figure#figure.lv,
    case RoleLv >= NeedLv of
        true  ->
            check_extra_condition(Player, T);
        false ->
            {false, ?ERRCODE(err158_not_enough_lv)}
    end;
check_extra_condition(Player, [{vip, NeedLv}|T]) ->
    VipLv = lib_vip_api:get_vip_lv(Player),
    case VipLv >= NeedLv of
        true  ->
            check_extra_condition(Player, T);
        false ->
            {false, ?ERRCODE(err158_not_enough_vip)}
    end;
check_extra_condition(Player, [{custom_act, Type, SubType, _GradeId}|T]) ->
    case custom_act_personal:is_personal_act(Type) of
        true ->
            Ret = custom_act_personal:is_personal_act_open(Player, Type, SubType);
        _ ->
            Ret = lib_custom_act_api:is_open_act(Type, SubType)
    end,
    case Ret of
        {true, _ActInfo} ->
            check_extra_condition(Player, T);
        true ->
            check_extra_condition(Player, T);
        false ->
            {false, ?ERRCODE(err331_act_closed)}
    end;
check_extra_condition(Player, [_H|T]) ->
    check_extra_condition(Player, T).

get_product(ProductId) ->
	case data_recharge:get_product(ProductId) of
		[] -> #base_recharge_product{};
		Product -> Product
	end.
