%%%--------------------------------------
%%% @Module  : lib_draw_record.
%%% @Author  : lhy
%%% @Created : 2019-06-05
%%% @Description:  定制活动抽奖记录
%%%--------------------------------------

-module(lib_draw_record).
-include("custom_act.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("role.hrl").
-include("server.hrl").
-include("goods.hrl").
-include("common.hrl").
-include("custom_act_draw_record.hrl").

-export([
         make_record/2
	    ,login/1
        ,reset/4                          %% 清除玩家个人记录

        ,refresh_local_draw_record/4      %% 刷新本服抽奖记录 本服活动
        ,refresh_kf_draw_record_local/4   %% 刷新个人记录(本服节点) 跨服活动
        ,refresh_kf_draw_record/4         %% 刷新跨服抽奖记录(跨服节点) 跨服活动
        ,send_draw_reward_record_local/3  %% 发送本服活动抽奖记录
        ,send_draw_reward_record_kf/3     %% 发送跨服活动抽奖记录
]).

make_record(draw_role, [Type, SubType, RoleId, GradeId, Time]) ->
    #draw_role{
         type = Type,                  %% 活动类型
         sub_type = SubType,           %% 活动子类型
         role_id = RoleId,             %% 玩家ID
         grade_id = GradeId,           %% 奖励
         time = Time                   %% 时间
    }.

login(RoleId) ->
    %% 玩家本服抽奖记录
    List1 = db_draw_record_role_select(RoleId),
    DrawRecordList = [make_record(draw_role,
                      [Type, SubType, TmpRoleId, GradeId, Time]) || [Type, SubType, TmpRoleId, GradeId, Time] <- List1],
    DrawRecordMaps = do_login(DrawRecordList),
    %% 玩家跨服抽奖记录
    List3 = db_kf_draw_record_role_select(RoleId),
    KfDrawRecordList = [make_record(draw_role,
                        [Type, SubType, TmpRoleId, GradeId, Time]) || [Type, SubType, TmpRoleId, GradeId, Time] <- List3],
    KfDrawRecordMaps = do_login(KfDrawRecordList),
    #draw_record{draw_record_local = DrawRecordMaps, draw_record_kf = KfDrawRecordMaps}.

do_login(RoleList) ->
    F = fun(#draw_role{type = Type, sub_type = SubType} = T, Maps) ->
    	LimitLength = lib_draw_record_api:get_upper_limit_length(Type, SubType),
    	case maps:get({Type, SubType}, Maps, false) of
            false ->
                TmpL = [];
            TmpL ->
                skip
        end,
        if
            length(TmpL) >= LimitLength ->
                Maps;
            true ->
                NewList = [T | lists:reverse(TmpL)],
                maps:put({Type, SubType}, lists:reverse(NewList), Maps)
        end
    end,
    lists:foldl(F, #{}, RoleList).

%% 玩家进程 清除玩家个人记录
reset(PS, draw_record_local, Type, SubType) ->
    #player_status{draw_record = #draw_record{draw_record_local = DrawRecordMaps} = DrawRecord} = PS,
    NewDrawRecordMaps = maps:remove({Type, SubType}, DrawRecordMaps),
    PS#player_status{draw_record = DrawRecord#draw_record{draw_record_local = NewDrawRecordMaps}};
reset(PS, draw_record_kf, Type, SubType) ->
    #player_status{draw_record = #draw_record{draw_record_kf = KfDrawRecordMaps} = DrawRecord} = PS,
    NewKfDrawRecordMaps = maps:remove({Type, SubType}, KfDrawRecordMaps),
    PS#player_status{draw_record = DrawRecord#draw_record{draw_record_kf = NewKfDrawRecordMaps}}.

%% 刷新本服抽奖记录 本服活动
refresh_local_draw_record(PS, Type, SubType, GradeIdList) ->
    #player_status{id = RoleId, draw_record = #draw_record{draw_record_local = DrawRecordMaps} = DrawRecord} = PS,
    LimitLength = lib_draw_record_api:get_upper_limit_length(Type, SubType),
    NewDrawRecordMaps = do_refresh_personal_draw_record(DrawRecordMaps, draw_record_local, Type, SubType, RoleId, GradeIdList, LimitLength),
    {ok, PS#player_status{draw_record = DrawRecord#draw_record{draw_record_local = NewDrawRecordMaps}}}.

%% 刷新个人记录(本服节点) 跨服活动
refresh_kf_draw_record_local(PS, Type, SubType, GradeIdList) ->
    #player_status{id = RoleId, draw_record = #draw_record{draw_record_kf = KfDrawRecordMaps} = DrawRecord} = PS,
    LimitLength = lib_draw_record_api:get_upper_limit_length(Type, SubType),
    NewKfDrawRecordMaps = do_refresh_personal_draw_record(KfDrawRecordMaps, draw_record_kf, Type, SubType, RoleId, GradeIdList, LimitLength),
    {ok, PS#player_status{draw_record = DrawRecord#draw_record{draw_record_kf = NewKfDrawRecordMaps}}}.

do_refresh_personal_draw_record(Maps, Atom, Type, SubType, RoleId, GradeIdList, LimitLength) ->
    case maps:get({Type, SubType}, Maps, false) of
        false -> DrawRecordList = [];
        TmpList ->  DrawRecordList = TmpList
    end,
    F = fun(GradeId, TmpDrawRecordList) ->
        do_refresh_personal_draw_record_core(Atom, Type, SubType, RoleId, GradeId, TmpDrawRecordList)
    end,
    TmpDrawRecordList = lists:foldl(F, DrawRecordList, GradeIdList),
    NewDrawRecordList = lists:sublist(TmpDrawRecordList, LimitLength),
    maps:put({Type, SubType}, NewDrawRecordList, Maps).

do_refresh_personal_draw_record_core(draw_record_local, Type, SubType, RoleId, GradeId, DrawRecordList) ->
    DrawRecord = make_record(draw_role, [Type, SubType, RoleId, GradeId, utime:unixtime()]),
    db_draw_record_insert(DrawRecord),
    %% 是否刷新本服记录
    case lib_draw_record_api:get_record_config_local(Type, SubType, GradeId) of
        true ->
            mod_draw_record_local:refresh_draw_reward_record(DrawRecord);
        false ->
            skip
    end,
    [DrawRecord | DrawRecordList];
do_refresh_personal_draw_record_core(draw_record_kf, Type, SubType, RoleId, GradeId, DrawRecordList) ->
    DrawRecord = make_record(draw_role, [Type, SubType, RoleId, GradeId, utime:unixtime()]),
    db_kf_draw_record_local_insert(DrawRecord),
    [DrawRecord | DrawRecordList].

%% 刷新跨服抽奖记录(跨服节点) 跨服活动
refresh_kf_draw_record(PS, Type, SubType, GradeIdList) ->
    #player_status{id = RoleId, server_id = ServerId, platform = Platform, server_num = ServerNum,
                  figure = #figure{name = RoleName}} = PS,
    F = fun(GradeId) ->
        DrawRecord = lib_draw_record_mod_kf:make_record(kf_draw_role, [Type, SubType, RoleId, ServerId, Platform,
                                                                     ServerNum, RoleName, GradeId, utime:unixtime()]),
        clusters_node:apply_cast(mod_draw_record_kf, refresh_draw_reward_record, [DrawRecord])
    end,
    lists:foreach(F, GradeIdList).

%% 发送本服活动抽奖记录
send_draw_reward_record_local(PS, Type, SubType) ->
    #player_status{id = RoleId, draw_record = DrawRecord} = PS,
    #draw_record{draw_record_local = DrawRecordMaps} = DrawRecord,
    PersonalRecords = maps:get({Type, SubType}, DrawRecordMaps, []),
    mod_draw_record_local:send_draw_reward_record(RoleId, Type, SubType, PersonalRecords).

%% 发送跨服活动抽奖记录
send_draw_reward_record_kf(PS, Type, SubType) ->
    #player_status{id = RoleId, server_id = ServerId, draw_record = DrawRecord} = PS,
    #draw_record{draw_record_kf = KfDrawRecordMaps} = DrawRecord,
    PersonalRecords = maps:get({Type, SubType}, KfDrawRecordMaps, []),
    clusters_node:apply_cast(mod_draw_record_kf, send_draw_reward_record, [RoleId, Type, SubType, ServerId, PersonalRecords]).

%% ------------------------------------- db ---------------------------------------------
db_draw_record_role_select(RoleId) ->
    SQL = io_lib:format(?sql_custom_act_draw_record_role_select, [RoleId]),
    db:get_all(SQL).
db_kf_draw_record_role_select(RoleId) ->
    SQL = io_lib:format(?sql_custom_act_draw_record_role_kf_select, [RoleId]),
    db:get_all(SQL).
db_draw_record_insert(#draw_role{type = Type, sub_type = SubType, role_id = RoleId, grade_id = GradeId}) ->
    SQL = io_lib:format(?sql_custom_act_draw_record_insert, [Type, SubType, RoleId, GradeId, utime:unixtime()]),
    db:execute(SQL).
db_kf_draw_record_local_insert(#draw_role{type = Type, sub_type = SubType, role_id = RoleId, grade_id = GradeId}) ->
    SQL = io_lib:format(?sql_kf_draw_record_local_insert, [Type, SubType, RoleId, GradeId, utime:unixtime()]),
    db:execute(SQL).
