%% ---------------------------------------------------------------------------
%% @doc 星辰系统数据操作
%% @author zmh
%% @since  20200817
%% ---------------------------------------------------------------------------
-module(lib_galaxy_db).


-include("role_galaxy.hrl").
-include("common.hrl").
-include("server.hrl").

-export([
        load_role_galaxy/1                    %% 加载角色数据
        ,save_role_galaxy/2                   %% 保存相关数据

        ,update_skin/2                        %% 更新幻化的皮肤
        ,save_role_collect/2                  %% 更新图鉴
        ,update_galaxy_display/2              %% 更新是否显示
        ,save_galaxy_skin/2                   %% 保存皮肤数据
    ]).

load_role_galaxy(RoleId) ->
    case db:get_row(io_lib:format(?SQL_GALAXY_GET, [RoleId])) of
        [PosListBin,Skin,Display,RTime,RTimes,Items0] ->
            PosList =  util:bitstring_to_term(PosListBin),
            Items =  util:bitstring_to_term(Items0);
        _ ->
            PosList = [],Skin=0,Display=0,RTime=0,RTimes=0,Items=[]
    end,
    %% 皮肤数据
    SkinList = case db:get_all(io_lib:format(?SQL_GALAXY_SKIN_GET, [RoleId])) of
        SkinDbList when is_list(SkinDbList) ->
            format_skin_list(SkinDbList, []);
        _ ->
            []
    end,
    Collects = load_galaxy_collect(RoleId),
    #role_galaxy{
         pos_list = PosList
         ,skin_list = SkinList
         ,skin = Skin
         ,display = Display
         ,collects = Collects
         ,refresh_time = RTime
         ,refresh_times = RTimes
         ,item_list=Items
    }.


load_galaxy_collect(RoleID)->
    Sql = io_lib:format(?SQL_GALAXY_COLLECT_GET,[RoleID]),
    Datas = db:get_all(Sql),
    Collects = format_collect(Datas,[]),
    Collects.

format_collect([],Collects)->
    Collects;
format_collect([[Type,SubT,Gain0,State]|T],Collects)->
    Gain =  util:bitstring_to_term(Gain0),
    Data = #galaxy_collect{
        key = {Type,SubT},
        gains = Gain,
        state = State
    },
    format_collect(T,[Data|Collects]).


%% 皮肤数据
format_skin_list([], SkinList) ->
    SkinList;
format_skin_list([DbInfo | T], SkinList) ->
    [Skin, Star] = DbInfo,
    Info = #galaxy_skin{skin  = Skin, star = Star},
    SkinList2 = [Info | SkinList],
    format_skin_list(T, SkinList2).

%%图鉴存储
save_role_collect(RoleID,Collect)->
    #galaxy_collect{
        key = {Type,SubT},
        gains = Gain0,
        state = State
    } = Collect,
    Gain = util:term_to_string(Gain0),
    Sql = io_lib:format(?SQL_GALAXY_COLLECT_REPLACE, [RoleID, Type,SubT, Gain,State]),
    db:execute(Sql),
    ok.

%% @doc 保存系统相关数据
save_role_galaxy(RoleId, RoleGalaxy) ->
    #role_galaxy{
        pos_list = PosList0
        ,skin = Skin
        ,display=Display
        ,refresh_time=RTime
        ,refresh_times=RTimes
        ,item_list = Items0
    } = RoleGalaxy,
    Items = util:term_to_string(Items0),
    PosList = util:term_to_string(PosList0),
    Data = [RoleId,PosList,Skin,Display,RTime,RTimes,Items],
    Sql = io_lib:format(?SQL_GALAXY_SAVE, Data),
    db:execute(Sql),
    ok.


% %% @doc 更新幻化的皮肤
update_skin(RoleId, Skin) ->
    Sql = io_lib:format(?SQL_GALAXY_SKIN_UPDATE, [Skin, RoleId]),
    db:execute(Sql),
    ok.


%% @doc 保存角皮肤数据
save_galaxy_skin(RoleId, SkinInfo) ->
    #galaxy_skin{skin = SkinId, star = Star} = SkinInfo,
    Sql = io_lib:format(?SQL_GALAXY_SKIN_REPLACE, [RoleId, SkinId, Star]),
    db:execute(Sql),
    ok.

%% @doc 更新是否显示
update_galaxy_display(RoleId, RoleGalaxy)->
    #role_galaxy{display=Dis}=RoleGalaxy,
    Sql = io_lib:format(?SQL_GALAXY_DISPLAY_UPDATE, [Dis, RoleId]),
    db:execute(Sql),
    ok.



