%% ---------------------------------------------------------
%% Author:  xyj
%% Email:   156702030@qq.com
%% Created: 2011-12-31
%% Description: 活动礼包工具类
%% --------------------------------------------------------
-module(lib_gift_util).
-compile(export_all).
-include("def_goods.hrl").
-include("goods.hrl").
-include("gift.hrl").
-include("server.hrl").
-include("sql_goods.hrl").
-include("common.hrl").
-include("def_fun.hrl").

%%-----------------------------------------------------------------
%% @doc 取打开礼包需要的格子
%% -spec get_gift_cell(GiftInfo, GiftNum) -> TotalNum when
%% GiftInfo     ::      #ets_info{}         礼包记录
%% GiftNum      ::      integer()           礼包数量
%% TotalNum     ::      integer()           总数量
%% @end
%%-----------------------------------------------------------------
get_gift_cell(GiftReward, Num) ->
    #ets_gift_reward{fixed_gifts = FixedGifts, rand = Rand, rand_gifts = RandGifts, drop_gifts = DropGifts} = GiftReward,
    %% 走掉落规则需要的最大格子数
    DropNum = lists:sum([CalcNum||{_, CalcNum} <- DropGifts]) * Num,
    CellMaps = #{?GOODS_LOC_BAG => DropNum},
    %% 获取固定礼包物品的格子数
    FixedCellMaps = ?IF(FixedGifts == [], CellMaps, get_gifts_fixlist_cell(FixedGifts, Num, CellMaps)),

    %% 获取随机物品需要的最大格子数
    NewRandGifts = [{Type, GoodTypeId, GoodsNum * Num * Rand}||{{Type, GoodTypeId, GoodsNum}, _Ratio}<-RandGifts],
    RandCellMaps = get_one_gifts_cell(NewRandGifts, FixedCellMaps),
    RandCellMaps.

%% 礼包固定物品需要的格子数
get_gifts_fixlist_cell(FixList, GiftNum, CellMaps) ->
    NewFixList = [{Type, GoodsTypeId, GoodsNum*GiftNum}||{Type, GoodsTypeId, GoodsNum}<-FixList],
    LastFixList = lib_goods_api:make_reward_unique(NewFixList),
    lists:foldl(fun get_gifts_cell/2, CellMaps, LastFixList).

%%------------------------------------------------------
%% @doc 获取礼包物品列表需要的格子数
%% -spec get_gift_cell(Item, SumMaps) -> Num        when
%% Item     ::        atom                类型
%% SumMaps  ::        #{Location => Num}  格子总数
%% Num      ::        integer             新的总数
%% @end
%%------------------------------------------------------
get_gifts_cell({Type, GoodTypeId, Num}, SumMaps) when
      Type == ?TYPE_GOODS;  Type == ?TYPE_BIND_GOODS ->
    case data_goods_type:get(GoodTypeId) of
        #ets_goods_type{max_overlap = MaxOverlap, bag_location = Location} ->
            CellSum = maps:get(Location, SumMaps, 0),
            NewCellSumSum = if
                MaxOverlap > Num -> CellSum + 1;
                MaxOverlap == 0 -> CellSum + Num;
                true -> CellSum + util:ceil(Num / MaxOverlap)
            end,
            maps:put(Location, NewCellSumSum, SumMaps);
        _ ->
            SumMaps
    end;
get_gifts_cell(_, SumMaps) -> SumMaps.

%% 获取随机物品需要的最大格子数
get_one_gifts_cell([], CellMaps) -> CellMaps;
get_one_gifts_cell([{Type, GoodTypeId, Num}|RandGifts], CellMaps) when
      Type == ?TYPE_GOODS;  Type == ?TYPE_BIND_GOODS ->
    NewCellMaps = case data_goods_type:get(GoodTypeId) of
        #ets_goods_type{max_overlap = MaxOverlap, bag_location = Location} ->
            CellNum = if
                MaxOverlap > Num -> 1;
                MaxOverlap == 0 -> Num;
                true -> util:ceil(Num / MaxOverlap)
            end,
            CellMax = maps:get(Location, CellMaps, 0),
            maps:put(Location, max(CellNum, CellMax), CellMaps);
        _ -> CellMaps
    end,
    get_one_gifts_cell(RandGifts, NewCellMaps);
get_one_gifts_cell([_|RandGifts], CellMaps) ->
  get_one_gifts_cell(RandGifts, CellMaps).

add_gift_card(PlayerId, Card, Type) ->
    Sql = io_lib:format(?SQL_GIFT_CARD_INSERT, [PlayerId, Card, Type, utime:unixtime()]),
    db:execute(Sql).

get_base_gift_card(Accname) ->
    Sql = io_lib:format(?SQL_GIFT_CARD_BASE_SELECT, [Accname]),
    db:get_row(Sql).

get_gift_card(Card) ->
    Sql = io_lib:format(?SQL_GIFT_CARD_SELECT, [Card]),
    db:get_row(Sql).

get_charge(PlayerId) ->
    Sql = io_lib:format(?SQL_CHARGE_SELECT, [PlayerId]),
    db:get_row(Sql).

%% 检查是否充值
check_charge(PlayerId) ->
    case get_charge(PlayerId) of
        [Id] when Id > 0 -> true;
        _ -> false
    end.
