%%%------------------------------------------------
%%% File    : data_goods.hrl
%%% Author  : xyj
%%% Created : 2012-4-18
%%% Description: 物品常量数据和计算公式
%%%------------------------------------------------
-module(data_goods).
-compile(export_all).
-include("figure.hrl").
-include("def_goods.hrl").
-include("sql_goods.hrl").
-include("goods.hrl").
-include("shop.hrl").
-include("predefine.hrl").
-include("attr.hrl").
-include("common.hrl").
-include("def_fun.hrl").
-include("errcode.hrl").

%% 获取产出类型
get_produce_type(Type) ->
    case data_produce_type:get_produce(Type) of
        0 when Type == unkown -> 0;
        0 ->
            ?ERR("unkown produce_type:~p~n", [Type]),
            0;
        Id -> Id
    end.

%% 消费类型 带有一些附加数据的
get_consume_type({Type, _}) ->
    get_consume_type(Type);

%% 获取消费类型
get_consume_type(Type) ->
    case data_produce_type:get_consume(Type) of
        0 when Type == unkown -> 0;
        0 ->
            ?ERR("unkown consume_type:~p", [Type]),
            0;
        Id -> Id
    end.

%% 获取物品效果
get_effect_val(Id, Type) ->
    case data_goods_effect:get(Id) of
        [] when Type =:= buff -> [];
        [] -> 0;
        #goods_effect{effect_list = EffectList} = Info ->
            case Type of
                exp -> get_val(Type, 1, EffectList, 0);
                coin -> get_val(Type, 1, EffectList, 0);
                gold -> get_val(Type, 1, EffectList, 0);
                bgold -> get_val(Type, 1, EffectList, 0);
                fgold -> get_val(Type, 1, EffectList, 0);
                charge_card -> get_val(Type, 1, EffectList, 0);
                physical -> get_val(Type, 1, EffectList, 0);
                time -> Info#goods_effect.time;
                buff -> {Info#goods_effect.buff_type, Info#goods_effect.effect_list, Info#goods_effect.time, Info#goods_effect.limit_scene};
                add_times -> get_val(Type, 1, EffectList, 0);
                onhook_time -> get_val(Type, 1, EffectList, 0);
                power_live -> get_val(Type, 1, EffectList, 0);
                del_pk_value -> get_val(Type, 1, EffectList, 0);
                _ -> []
            end
    end.

%% 获取物品buff效果列表
get_effect_list(Id) ->
    case data_goods_effect:get(Id) of
        [] -> [];
        #goods_effect{effect_list = EffectList} ->
            EffectList
    end.

get_val(Key, Pos, List, Default) ->
    case lists:keyfind(Key, Pos, List) of
        false -> Default;
        {Key, Value} -> Value
    end.

%% 获取物品名字
get_goods_name(GoodsTypeId)->
    case data_goods_type:get(GoodsTypeId) of
        #ets_goods_type{goods_name = Name} ->
            util:make_sure_binary(Name);
        _ -> <<>>
    end.

%% 获取物品的出售价格
get_goods_sell_price(GoodsId) ->
    case data_goods_price:get_goods_price(GoodsId) of
        #goods_price{sell_price_type = SellType, sell_price = SellPrice}
          when SellType > 0, SellPrice > 0 ->
            {SellType, SellPrice};
        _ -> {0, 0}
    end.

%% 获取物品的购买价格
get_goods_buy_price(GoodsId) ->
    case data_goods_price:get_goods_price(GoodsId) of
        #goods_price{price_type = PriceType, price = Price}
          when PriceType > 0, Price > 0 -> {PriceType, Price};
        _ -> {0, 0}
    end.

%% 获取物品的品质
get_goods_color(GoodsTypeId) ->
    case data_goods_type:get(GoodsTypeId) of
        #ets_goods_type{color = Color} ->
            Color;
        _ -> 0
    end.

%% 背包空间不足详细错误码
get_no_cell_errcode(?GOODS_LOC_BAG) -> ?ERRCODE(err150_no_cell);
get_no_cell_errcode(?GOODS_LOC_FHORSE_BAG) -> ?ERRCODE(err150_mount_no_cell);
get_no_cell_errcode(?GOODS_LOC_FJARVIS_BAG) -> ?ERRCODE(err150_fjarvis_no_cell);
get_no_cell_errcode(?GOODS_LOC_PET_STONE_BAG) -> ?ERRCODE(err150_pet_stone_no_cell);
get_no_cell_errcode(?GOODS_LOC_CHILD_TOY_BAG) -> ?ERRCODE(err150_baby_no_cell);
get_no_cell_errcode(?GOODS_LOC_CAPSULE_EGG_BAG) -> ?ERRCODE(err170_warehose_not_enough);
get_no_cell_errcode(?GOODS_LOC_GOD_BAG) -> ?ERRCODE(err150_god_no_cell);
get_no_cell_errcode(?GOODS_LOC_GALAXY_BAG) -> ?ERRCODE(err150_galaxy_no_cell);
get_no_cell_errcode(?GOODS_LOC_WAR_SOUL_BAG) -> ?ERRCODE(err150_warsoul_no_cell);
get_no_cell_errcode(_) -> ?ERRCODE(err150_no_cell).

%% 货币不足错误码
get_money_not_enough_error(?TYPE_GOLD) -> ?ERRCODE(gold_not_enough);
get_money_not_enough_error(?TYPE_COIN) -> ?ERRCODE(coin_not_enough);
get_money_not_enough_error(?TYPE_BGOLD) -> ?ERRCODE(bgold_not_enough);
get_money_not_enough_error(?TYPE_MATE) -> ?ERRCODE(mate_not_enough);
get_money_not_enough_error(?TYPE_CHILD_TOY) -> ?ERRCODE(err165_child_toy_debris_not_enough);
get_money_not_enough_error(?TYPE_WORKROOM) -> ?ERRCODE(err421_fashion_coin_not_enough);
get_money_not_enough_error(?TYPE_LEGION) -> ?ERRCODE(err603_legion_arms_not_enough);
get_money_not_enough_error(_) -> ?ERRCODE(money_not_enough).

%% 背包格子数
get_location_size(Location) ->
    data_goods_type:get_bag_default_size(Location).

%% 背包最大格子数
get_location_max_size(Location) ->
    data_goods_type:get_bag_max_size(Location).
