-module(pt_151).
-include("common.hrl").
-export([
        read/2,
        write/2
    ]).

read(15101, Bin0) ->
    <<RuleId:32, Bin1/binary>> = Bin0, 
    <<MainGoodsId:64, Bin2/binary>> = Bin1, 
    <<MainGoodsLocation:16, Bin3/binary>> = Bin2, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<GoodsId:64, _Args1/binary>> = RestBin0, 
        <<TypeId:32, _Args2/binary>> = _Args1, 
        <<GoodsNum:16, _Args3/binary>> = _Args2, 
        {{GoodsId, TypeId, GoodsNum},_Args3}
        end,
    {CostList, Bin4} = pt:read_array(FunArray0, Bin3),

    <<Times:16, Bin5/binary>> = Bin4, 
    FunArray1 = fun(<<RestBin1/binary>>) -> 
        <<ShopId:32, _Args1/binary>> = RestBin1, 
        <<ShopNum:32, _Args2/binary>> = _Args1, 
        {{ShopId, ShopNum},_Args2}
        end,
    {AutoBuyList, _Bin6} = pt:read_array(FunArray1, Bin5),

    {ok, [RuleId, MainGoodsId, MainGoodsLocation, CostList, Times, AutoBuyList]};
read(_Cmd, _R) -> {error, no_match}.

write (15101,[
    Code,
    ComposeTypeId,
    ComposeTypeNum
] = Src_Data) ->
    Data = <<
        Code:32,
        ComposeTypeId:32,
        ComposeTypeNum:32
    >>,
    {ok, pt:encode(15101, Data, Src_Data)};

write(_Cmd, _R) -> ?DEBUG("no_match:~p,~p~n", [_Cmd, _R]), {ok, pt:pack(0, <<>>)}.


