%%-----------------------------------------------------------------------------
%% @Module  :       lib_red_envelopes_data
%% @Author  :       Czc
%% @Email   :       389853407@qq.com
%% @Created :       2017-09-20
%% @Description:    红包data管理(通用框架)
%%-----------------------------------------------------------------------------
-module(lib_red_envelopes_data).

-include("figure.hrl").
-include("red_envelopes.hrl").
-include("common.hrl").
-include("def_module.hrl").

-export([
    get_red_envelopes_map/0
    , save_red_envelopes_map/1
    % , get_obtain_record_map/0
    % , save_obtain_record_map/1
    , get_red_envelopes_list/2
    % , get_obtain_record_list/2
    , get_red_envelopes_by_id/1
    , get_red_envelopes/3
    % , get_obtain_record_new_id/0
    , add_red_envelopes/1
    % , add_obtain_record/1
    , db_insert_red_envelopes/1
    , get_times_limit/2
    , db_clear_by_ownership_id/1
    % , del_obtain_record/2
    ]).

%% 获得红包Maps
get_red_envelopes_map() -> get(?P_RED_ENVELOPES).

%% 保存红包Maps
save_red_envelopes_map(RedEnvelopesMap) -> put(?P_RED_ENVELOPES, RedEnvelopesMap).

%% 根据归属获取红包列表
get_red_envelopes_list(OwnershipType, OwnershipId) ->
    RedEnvelopesMap = get_red_envelopes_map(),
    TypeRedEnvelopesMap = maps:get(OwnershipType, RedEnvelopesMap, #{}),
    maps:get(OwnershipId, TypeRedEnvelopesMap, []).

%% 根据红包id获取红包(不推荐使用)
get_red_envelopes_by_id(RedEnvelopesId) ->
    RedEnvelopesMap = get_red_envelopes_map(),
    List = maps:fold(fun(_Key, Val, AccList) ->
        AccList ++ maps:values(Val)
    end, [], RedEnvelopesMap),
    lists:keyfind(RedEnvelopesId, #red_envelopes.id, lists:flatten(List)).

get_red_envelopes(OwnershipType, OwnershipId, RedEnvelopesId) ->
    RedEnvelopesMap = get_red_envelopes_map(),
    Map = maps:get(OwnershipType, RedEnvelopesMap, #{}),
    List = maps:get(OwnershipId, Map, []),
    lists:keyfind(RedEnvelopesId, #red_envelopes.id, List).

get_times_limit(Type, CfgId) ->
    case Type of
        ?MOD_RED_ENVELOPES_GOODS ->
            case data_red_envelopes:get_goods_cfg(CfgId) of
                #red_envelopes_goods_cfg{times_lim = TimesLim} ->
                    TimesLim;
                _ -> -1
            end;
        _ -> -1
    end.

%% Map里增加新的红包或者更新红包
add_red_envelopes(RedEnvelopes) ->
    #red_envelopes{
        id = RedEnvelopesId, ownership_type = OwnershipType, ownership_id = OwnershipId
    } = RedEnvelopes,
    RedEnvelopesMap = get_red_envelopes_map(),
    OneTypeMap = maps:get(OwnershipType, RedEnvelopesMap, #{}),
    List = maps:get(OwnershipId, OneTypeMap, []),
    NewRedEnvelopesList = [RedEnvelopes|lists:keydelete(RedEnvelopesId, #red_envelopes.id, List)],
    NewOneTypeMap = maps:put(OwnershipId, NewRedEnvelopesList, OneTypeMap),
    NewRedEnvelopesMap = maps:put(OwnershipType, NewOneTypeMap, RedEnvelopesMap),
    save_red_envelopes_map(NewRedEnvelopesMap).

db_insert_red_envelopes(RedEnvelopes) ->
    #red_envelopes{
        id = Id, type = Type, ownership_type = OwnershipType, ownership_id = OwnershipId,
        owner_id = OwnerId, status = Status, money = Money, money_list=MoneyList, share_list=ShareList,
        split_num = SplitNum, packet_id = PacketId, greet = Greet, stime = Stime, ctime = Ctime
    } = RedEnvelopes,
    db:execute(io_lib:format(?sql_insert_red_envelopes,
        [Id, Type, OwnershipType, OwnershipId, OwnerId, Status, Money, util:term_to_bitstring(MoneyList), util:term_to_bitstring(ShareList), SplitNum, PacketId, Greet, Stime, Ctime])).

db_clear_by_ownership_id(OwnershipId) ->
    db:execute(io_lib:format(?sql_del_red_envelopes_by_ownership_id, [OwnershipId])),
    db:execute(io_lib:format(?sql_del_red_envelopes_record_by_ownership_id, [OwnershipId])).

