%% ---------------------------------------------------------
%% Author:  xyj
%% Email:   156702030@qq.com
%% Created: 2011-12-13
%% Description: 物品实用工具类
%% --------------------------------------------------------
-module(lib_goods_init).
-include("common.hrl").
-include("goods.hrl").
-include("def_goods.hrl").
-include("sql_goods.hrl").
-include("sell.hrl").
-include("def_fun.hrl").
-export([
    % 初始化玩家背包物品
    init_goods_online/3,
    get_location_dict/5
]).

%% 初始化玩家背包物品
init_goods_online(PlayerId, GoodsMaps, Lv) ->
    NewDict = init_goods(PlayerId, GoodsMaps, Lv),
    NewDict.

init_goods(PlayerId, GoodsMaps, Lv) ->
    LocationList = ?GOODS_LOC_ALL,

    NowTime = utime:unixtime(),
    NewGoodsDict = get_location_dict(LocationList, PlayerId, GoodsMaps, Lv, NowTime),
    NewGoodsDict.


get_location_dict([T|G], PlayerId, GoodsMaps, Lv, NowTime) ->
    Sql = io_lib:format(?SQL_GOODS_LIST_BY_LOCATION, [PlayerId, T]),
    NewGoodsMaps =
        case db:get_all(Sql) of
            [] ->
                GoodsMaps;
            GoodsList when is_list(GoodsList) ->
                GoodsDict = insert_dict(GoodsList, dict:new(), Lv, NowTime),
                maps:put(T, GoodsDict, GoodsMaps);
            _ ->
                GoodsMaps
        end,
    get_location_dict(G, PlayerId, NewGoodsMaps, Lv, NowTime);
get_location_dict([], _PlayerId, NewGoodsDict, _Lv, _NowTime) ->
    NewGoodsDict.

insert_dict([], Dict, _Lv, _NowTime) ->
    Dict;
insert_dict([Info|H], Dict, Lv, NowTime) ->
    case lib_goods_util:make_info(goods, Info, Lv) of
        [GoodsInfo] ->
            NewGoodsDict1 = dict:store(GoodsInfo#goods.id, GoodsInfo, Dict);
        _ ->
            NewGoodsDict1 = Dict
    end,
    insert_dict(H, NewGoodsDict1, Lv, NowTime).
