%% ---------------------------------------------------------
%% Author:  zmh
%% Created: 2019-11-11
%% Description: 玩家进阶系统
%% --------------------------------------------------------
-module(pp_foster).
-export([handle/3]).
-include("server.hrl").
-include("skill.hrl").
-include("goods.hrl").
-include("predefine.hrl").
-include("role_foster.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-include("battle.hrl").
-include("def_goods.hrl").
-include("attr.hrl").
-include("def_event.hrl").
-include("common.hrl").

%%获取子系统信息
handle(14600, Status, [Type]) ->
    #player_status{id=RoleID} = Status,
    {_IsOpen,CellFoster} = lib_role_foster:get_sub_foster(Type,Status),
    Bin = lib_role_foster:pack_14600(CellFoster),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};


%% 自动进阶培养(只允许强进阶系统进阶)
handle(14601,Status,[Type,AUp,ABuy,AutoBGold])
	when AUp >= 1 andalso (Type == ?FHORSE orelse Type == ?FWING orelse Type == ?FPEARL orelse Type == ?FWEAPON ) ->
    #player_status{
        id=RoleID,
        role_foster = RoleFoster,
        figure=#figure{name=Name}
    } = Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    {IsOpen,CellFoster} = lib_role_foster:get_sub_foster(Type,Status),
    #cell_foster{grade=OGrade,star=OStar,exp = OldExp} = CellFoster,
    Res = lib_role_foster:auto_foster_up(IsOpen,Status,AUp,ABuy,AutoBGold,CellFoster,[], 0),
    NStatus = case Res of
        {ok,CostList,CellFoster0, _FosterTimes} when CostList /= []->
            %io:format("------:~p~n",[CostList]),
            {TrueCostList, CostGoodNum, CostBGold, CostGold} = calc_cost(Status, CostList),
            Result = case ABuy>=1 of
                true->%%自动购买
                    lib_goods_api:cost_objects_with_auto_buy(Status, CostList, role_foster, ["used foster"]);
                _->
                    lib_goods_api:cost_object_list_with_check(Status, CostList, role_foster, ["used foster"])
            end,
            IsU = lib_role_foster:check_up_flag(OGrade,OStar,CellFoster0),
            case Result of
                {true,Status0,_}->
	                #cell_foster{grade=Grade,star=Star,display=Dis,exp=Exp,etime=ETime} = CellFoster0,
                    NCellFoster = lib_role_foster:after_foster_up(OGrade/=Grade,RoleID,CellFoster0),
                    #base_foster{award=Awards,is_tv=IsTv} = data_foster:get_base_foster(Type,Grade,Star),
                    lib_role_foster:update_role_foster(main,RoleID,NCellFoster),
                    NFosters   = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
                    Status1  = Status0#player_status{role_foster = RoleFoster#role_foster{fosters=NFosters}},
                    case IsU of
                        true when Awards /= []->
                            Produce = #produce{type=role_foster,reward=Awards},
                            lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_FOSTER);
                        _->
                            skip
                    end,
                    Status2 = lib_role_foster:update_role_figure(Status1),
                    Status3 = lib_role_foster:count_foster_attr(Status2),
                    Status4 = lib_player:count_player_attribute(Status3),
                    lib_player:send_attribute_change_notify(Status4, ?NOTIFY_ATTR),
                    lib_role_foster:update_foster_attr_to_scene(Status4, Type),

	                {ok, Bin0}  = pt_146:write(14601, [?SUCCESS,AUp,ABuy,AutoBGold,Type,Grade,Star,0,0]),
                    Bin1 = lib_role_foster:pack_14600(Type,Status4),
                    Bin  = <<Bin0/binary,Bin1/binary>>,
                    lib_server_send:send_to_uid(RoleID,Bin),
                    case OGrade /= Grade of
                        true->
                            Figure4 = Status4#player_status.figure,
                            mod_scene_agent:update(Status4,[{role_foster,Figure4#figure.role_foster}]),
                            {ActiveType, KeyValues} = lib_role_foster:calc_all_foster(Type, NFosters),
                            {ok, Status5} = lib_wardrobe_active:active_foster(Status4, ActiveType, KeyValues, [Type, Grade]),

                            % mod_scene_agent:update({battle_train, Type, RoleTrains}, Status4),
                            IsTv>0 andalso lib_chat:send_TV({all}, ?MOD_FOSTER, Type, [Name,RoleID,Grade]),

                            %% 脱下其他外观(为减少其他操作影响，采用异步)
                            lib_player:apply_cast(RoleID, ?APPLY_CAST_STATUS, lib_role_foster, put_off_other_appearance, [Type, 4, Dis]),
                            lib_player_event:async_dispatch(RoleID, ?EVENT_FOSTER_GRADE_UP, {Type,Grade});
                        _->
                            IsU andalso lib_player_event:async_dispatch(RoleID, ?EVENT_FOSTER_GRADE_STAR_UP, {Type,Grade,Star}),
                            Status5 = Status4
                    end,
                    mod_scene_agent:update(Status5, [{battle_attr, Status5#player_status.battle_attr}]),
                    lib_rush_rank_api:refresh_rank_by_foster(Type,Status5),
                    lib_task_api:foster_lv_up(Status5,Type),
                    lib_achievement_api:foster_event(Status5, [{Type, Grade, Star}]),
                    lib_achievement_api:foster_skill_event(Status5, lib_role_foster:calc_foster_skills(NFosters)),
                    {ok, Status6} = lib_seven_days_goal:do_finish_task(Status5, ?MOD_FOSTER, Type, Grade, true),
                    case data_foster:get_base_foster(Type, OGrade, OStar) of
                        #base_foster{max = Max} -> ok;
                        _ -> Max = 0
                    end,
                    AddExp = max(0, Max - OldExp),
                    case CostList of
                        [{_, CostGoods, _}|_] -> ok;
                        _ -> CostGoods = 0
                    end,
                    ta_agent_fire:foster_up(Status6, [lib_role_foster:get_type_name(Type), Grade, Star, Exp, AddExp, Max, ETime, ABuy,
                        CostGoods, CostGoodNum, CostGold, CostBGold, Status6#player_status.gold, Status6#player_status.bgold]),
                    LogData = [
                        RoleID,Name,Type,Grade,Star,Exp,ETime,ABuy
                        ,util:term_to_string(TrueCostList),utime:unixtime()
                    ],
                    lib_log_api:log_foster_upgrade(LogData),
                    Status6;
                {false,Err,_}->
                    {ok, Bin} = pt_146:write(14601, [Err,AUp,ABuy,AutoBGold,Type,OGrade,OStar,0,0]),
                    lib_server_send:send_to_uid(RoleID, Bin),
                    Status
            end;
        {false,Err}->
            {ok, Bin} = pt_146:write(14601, [Err,AUp,ABuy,AutoBGold,Type,OGrade,OStar,0,0]),
            lib_server_send:send_to_uid(RoleID, Bin),
            Status;
         _->
            Err = ?GOODS_NOT_ENOUGH,
            {ok, Bin} = pt_146:write(14601, [Err,AUp,ABuy,AutoBGold,Type,OGrade,OStar,0,0]),
            lib_server_send:send_to_uid(RoleID, Bin),
            Status
    end,
    {ok, NStatus};


%% 培养(只允许强进阶系统进阶)
handle(14601,Status, [Type,AUp,ABuy,_AutoBGold])
	when Type == ?FHORSE; Type == ?FWING; Type == ?FPEARL; Type == ?FWEAPON ->
    #player_status{
        id=RoleID,
        role_foster = RoleFoster,
        figure=#figure{name=Name}
    } = Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    {IsOpen,CellFoster} = lib_role_foster:get_sub_foster(Type,Status),
    #cell_foster{grade=OGrade,star=OStar} = CellFoster,
    NStatus = case lib_role_foster:do_foster_up(IsOpen,CellFoster) of
        {ok,CostList,IsJ,AddExp,CellFoster0}->
            {TrueCostList, CostGoodNum, CostBGold, CostGold} = calc_cost(Status, CostList),
            Res = case ABuy>0 of
                true->%%自动购买
                    lib_goods_api:cost_objects_with_auto_buy(Status, CostList, role_foster, ["used foster"]);
                _->
                    lib_goods_api:cost_object_list_with_check(Status, CostList, role_foster, ["used foster"])
            end,
            IsU = lib_role_foster:check_up_flag(OGrade,OStar,CellFoster0),
            case Res of
                {true,Status0,_}->
                    #cell_foster{grade=Grade,star=Star,display=Dis,exp=Exp,etime=ETime} = CellFoster0,
                    NCellFoster = lib_role_foster:after_foster_up(OGrade/=Grade,RoleID,CellFoster0),
                    #base_foster{award=Awards,is_tv=IsTv} = data_foster:get_base_foster(Type,Grade,Star),
                    lib_role_foster:update_role_foster(main,RoleID,NCellFoster),
                    NFosters   = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
                    Status1  = Status0#player_status{role_foster = RoleFoster#role_foster{fosters=NFosters}},
                    case IsU of
                        true when Awards /= []->
                            Produce = #produce{type=role_foster,reward=Awards},
                            lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_FOSTER);
                        _->
                            skip
                    end,
                    Status2 = lib_role_foster:update_role_figure(Status1),
                    Status3 = lib_role_foster:count_foster_attr(Status2),
                    Status4 = lib_player:count_player_attribute(Status3),
                    lib_player:send_attribute_change_notify(Status4, ?NOTIFY_ATTR),
                    lib_role_foster:update_foster_attr_to_scene(Status4, Type),

                    {ok, Bin0}  = pt_146:write(14601, [?SUCCESS,AUp,ABuy,_AutoBGold,Type,Grade,Star,IsJ,AddExp]),
                    Bin1 = lib_role_foster:pack_14600(Type,Status4),
                    Bin  = <<Bin0/binary,Bin1/binary>>,
                    lib_server_send:send_to_uid(RoleID,Bin),
                    case OGrade /= Grade of
                        true->
                            Figure4 = Status4#player_status.figure,
                            mod_scene_agent:update(Status4,[{role_foster,Figure4#figure.role_foster}]),
                            {ActiveType, KeyValues} = lib_role_foster:calc_all_foster(Type, NFosters),
                            {ok, Status5} = lib_wardrobe_active:active_foster(Status4, ActiveType, KeyValues, [Type, Grade]),
                            % mod_scene_agent:update({battle_train, Type, RoleTrains}, Status4),
                            IsTv>0 andalso lib_chat:send_TV({all}, ?MOD_FOSTER, Type, [Name,RoleID,Grade]),
                            %% 脱下其他外观
                            lib_player:apply_cast(RoleID, ?APPLY_CAST_STATUS, lib_role_foster, put_off_other_appearance, [Type, 4, Dis]),

                            lib_player_event:async_dispatch(RoleID, ?EVENT_FOSTER_GRADE_UP, {Type,Grade});
                        _->
                            IsU andalso lib_player_event:async_dispatch(RoleID, ?EVENT_FOSTER_GRADE_STAR_UP, {Type,Grade,Star}),
                            Status5 = Status4
                    end,
                    mod_scene_agent:update(Status5, [{battle_attr, Status5#player_status.battle_attr}]),
                    lib_rush_rank_api:refresh_rank_by_foster(Type,Status5),
                    lib_task_api:foster_lv_up(Status5,Type),
                    lib_achievement_api:foster_event(Status5, [{Type, Grade, Star}]),
                    lib_achievement_api:foster_skill_event(Status5, lib_role_foster:calc_foster_skills(NFosters)),
                    {ok, Status6} = lib_seven_days_goal:do_finish_task(Status5, ?MOD_FOSTER, Type, Grade, true),
                    case data_foster:get_base_foster(Type, OGrade, OStar) of
                        #base_foster{max = Max} -> ok;
                        _ -> Max = 0
                    end,
                    case CostList of
                        [{_, CostGoods, _}|_] -> ok;
                        _ -> CostGoods = 0
                    end,
                    ta_agent_fire:foster_up(Status6, [lib_role_foster:get_type_name(Type), Grade, Star, Exp, AddExp, Max, ETime, ABuy,
                        CostGoods, CostGoodNum, CostGold, CostBGold, Status6#player_status.gold, Status6#player_status.bgold]),
                    LogData = [
                        RoleID,Name,Type,Grade,Star,Exp,ETime,ABuy
                        ,util:term_to_string(TrueCostList),utime:unixtime()
                    ],
                    lib_log_api:log_foster_upgrade(LogData),
                    Status6;
                {false,Err,_}->
                    {ok, Bin} = pt_146:write(14601, [Err,AUp,ABuy,_AutoBGold,Type,OGrade,OStar,0,0]),
                    lib_server_send:send_to_uid(RoleID, Bin),
                    Status
            end;
        {false,Err}->
            {ok, Bin} = pt_146:write(14601, [Err,AUp,ABuy,_AutoBGold,Type,OGrade,OStar,0,0]),
            lib_server_send:send_to_uid(RoleID, Bin),
            Status
    end,
    {ok, NStatus};

%% 核心培养 -- 使用属性丹(限制属性丹类型)
handle(14602,Status,[Type,OpTy,Num])->
	#player_status{
		id = RoleID,
		role_foster = RoleFoster,
		figure = #figure{name=Name}
	} = Status,
	CheckList = [{check_parma, Type, OpTy, Num}],
	case util:check_lists(lib_role_foster, CheckList) of
		true ->
			#role_foster{fosters=Fosters}=RoleFoster,
			{IsOpen,CellFoster} = lib_role_foster:get_sub_foster(Type,Status),
			NStatus = case lib_role_foster:eat_core_pill(OpTy,Num,IsOpen,CellFoster) of
				          {ok,Cost,NCellFoster}->
					          case lib_goods_api:cost_objects_with_auto_buy(Status, Cost, role_foster_core, ["used foster"]) of
						          {true,Status0,RealCost}->
							          lib_role_foster:update_role_foster(core,RoleID,NCellFoster),
							          NFosters   = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
							          Value = case OpTy of
								                  0 ->
									                  NCellFoster#cell_foster.lowcore;
								                  1 ->
									                  NCellFoster#cell_foster.highcore;
								                  _ ->
									                  NewCoreExt = NCellFoster#cell_foster.core_ext,
									                  case lists:keyfind(OpTy, 1, NewCoreExt) of
										                  {_, TValue} -> TValue;
										                  _ -> 0
									                  end
							                  end,
							          Status1  = Status0#player_status{role_foster=RoleFoster#role_foster{fosters=NFosters}},
							          Status2  = lib_role_foster:count_foster_attr(Status1),
							          Status3  = lib_player:count_player_attribute(Status2),
							          lib_player:send_attribute_change_notify(Status3, ?NOTIFY_ATTR),
							          lib_role_foster:update_foster_attr_to_scene(Status3, Type),
							          {ok, Bin0}  = pt_146:write(14602, [?SUCCESS,OpTy,Type,Value]),
							          Bin1 = lib_role_foster:pack_14600(Type,Status3),
							          Bin  = <<Bin0/binary,Bin1/binary>>,
							          lib_server_send:send_to_uid(RoleID,Bin),
							          %mod_scene_agent:update({battle_train, Type, RoleTrains},Status2),
							          mod_scene_agent:update(Status3, [{battle_attr, Status3#player_status.battle_attr}]),
							          lib_rush_rank_api:refresh_rank_by_foster(Type,Status3),
							          LogData = [
								          RoleID,Name,Type,OpTy,Value,
								          util:term_to_string(RealCost),utime:unixtime()
							          ],
							          lib_log_api:log_foster_core_upgrade(LogData),
							          Status3;
						          {false,Res,_}->
							          {ok, Bin} = pt_146:write(14602, [Res,OpTy,Type,0]),
							          lib_server_send:send_to_uid(RoleID, Bin),
							          Status
					          end;
				          {false,Res}->
					          {ok, Bin} = pt_146:write(14602, [Res,OpTy,Type,0]),
					          lib_server_send:send_to_uid(RoleID, Bin),
					          Status
			          end,
			{ok, NStatus};
		_ ->
			{ok, Bin} = pt_146:write(14602, [?FAIL, OpTy, Type, 0]),
			lib_server_send:send_to_uid(RoleID, Bin),
			{ok, Status}
	end;

%% 幻化进阶
handle(14603,Status,[Type,Dis])->
    #player_status{
        id=RoleID,
        role_foster = RoleFoster
    }=Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    {IsOpen,CellFoster}  = lib_role_foster:get_sub_foster(Type,Status),
    case lib_role_foster:change_display(IsOpen,Dis,CellFoster) of
        {ok, NCellFoster}->
            %% 取消其他外观的幻化
            Status1 =
            case Dis > 0 of
                true -> lib_role_foster:put_off_other_appearance(Status, Type, 4, Dis);
                _ -> Status
            end,

            lib_role_foster:update_role_foster(display,RoleID,NCellFoster),
            {ok, Bin} = pt_146:write(14603,[?SUCCESS,Type,Dis]),
            lib_server_send:send_to_uid(RoleID,Bin),

            NFosters = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
            Status2 = Status1#player_status{role_foster = RoleFoster#role_foster{fosters=NFosters}},
            NStatus = lib_role_foster:update_role_figure(Status2),
            LFigure = NStatus#player_status.figure,
            mod_scene_agent:update(NStatus,[{role_foster,LFigure#figure.role_foster}]),
            {ok,NStatus};
        {false,Err}->
            {ok, Bin} = pt_146:write(14603,[Err,Type,Dis]),
            lib_server_send:send_to_uid(RoleID,Bin),
            {ok, Status}
    end;

%%骑乘
handle(14609,Status,[OpTy])->
    #player_status{
        id=RoleID,
        scene = Scene,
        scene_pool_id = PoolID,
        copy_id = CopyId,
        x = X, y = Y,
        role_foster = RoleFoster
    }=Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    {IsOpen,CellFoster}  = lib_role_foster:get_sub_foster(?FHORSE,Status),
    case lib_role_foster:change_hoese_state(IsOpen,OpTy,CellFoster) of
        {ok, NCellFoster}->
            %lib_role_foster:update_role_foster(horse_state,RoleID,NCellFoster),
            {ok, Bin} = pt_146:write(14609,[?SUCCESS,OpTy]),
            lib_server_send:send_to_uid(RoleID,Bin),

            %%场景广播
            {ok, Bin1} = pt_120:write(12016, [?MOD_FOSTER,14609,RoleID,OpTy]),
            lib_server_send:send_to_area_scene(Scene,PoolID,CopyId,X,Y,Bin1),

            NFosters = lists:keystore(?FHORSE,#cell_foster.type,Fosters,NCellFoster),
            NStatus0 = Status#player_status{role_foster = RoleFoster#role_foster{fosters=NFosters}},
            NStatus1 = lib_role_foster:update_role_figure(NStatus0),
            NStatus  = lib_player:count_player_attribute(NStatus1),
            BattleAttr = NStatus#player_status.battle_attr,
            lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
            LFigure = NStatus#player_status.figure,
            mod_scene_agent:update(NStatus,[{speed, BattleAttr#battle_attr.speed},{foster_ride,LFigure#figure.is_ride}]),
            {ok,NStatus};
        {false,Err}->
            {ok, Bin} = pt_146:write(14609,[Err,OpTy]),
            lib_server_send:send_to_uid(RoleID,Bin),
            {ok, Status}
    end;

%%技能升级
handle(14604, Status, [Type,Pos])->
    #player_status{
        id=RoleID,
        role_foster = RoleFoster,
        figure = #figure{name=Name}
    }=Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    {IsOpen,CellFoster}  = lib_role_foster:get_sub_foster(Type,Status),
    case lib_role_foster:foster_skill_lvup(IsOpen,Type,Pos,CellFoster) of
        {ok, NCellFoster,SkillID,Lv,Cost}->
            case lib_goods_api:cost_objects_with_auto_buy(Status, Cost, role_foster_skill, ["used foster"]) of
                {true,Status0,_}->
                    lib_role_foster:update_role_foster(skill,RoleID,NCellFoster),
                    {ok, Bin} = pt_146:write(14604,[?SUCCESS,Type,Pos,Lv,SkillID]),
                    lib_server_send:send_to_uid(RoleID,Bin),
                    NFosters = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
                    NStatus0 = Status0#player_status{role_foster=RoleFoster#role_foster{fosters=NFosters}},
                    % mod_scene_agent:update({battle_train, Type, RoleTrains}, NStatus0),
                    NStatus1  = lib_role_foster:count_foster_attr(NStatus0),
                    NStatus  = lib_player:count_player_attribute(NStatus1),
                    lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
                    lib_role_foster:update_foster_attr_to_scene(NStatus, Type),
                    mod_scene_agent:update(NStatus, [{battle_attr, NStatus#player_status.battle_attr}]),
                    lib_rush_rank_api:refresh_rank_by_foster(Type,NStatus),
                    LogData = [
                        RoleID,Name,Type,SkillID,Lv,
                        util:term_to_string(Cost),utime:unixtime()
                    ],
                    lib_achievement_api:foster_skill_event(NStatus, lib_role_foster:calc_foster_skills(NFosters)),
                    lib_log_api:log_foster_skill_upgrade(LogData),
                    {ok,NStatus};
                {false,Err,_}->
                    {ok, Bin} = pt_146:write(14604,[Err,Type,Pos,0,0]),
                    lib_server_send:send_to_uid(RoleID,Bin),
                    {ok, Status}
            end;
        {false,Err}->
            {ok, Bin} = pt_146:write(14604,[Err,Type,Pos,0,0]),
            lib_server_send:send_to_uid(RoleID,Bin),
            {ok, Status}
    end;

%%装备升级
handle(14606,Status,[Type,Grade,Pos,Uids])->
    #player_status{
        id=RoleID,
        role_foster = RoleFoster,
        figure = #figure{name=Name}
    } = Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    CellFoster = case lists:keyfind(Type,#cell_foster.type,Fosters) of
        CValue when is_record(CValue,cell_foster)->
            CValue;
        _->
            #cell_foster{}
    end,

    GEquip = case lists:keyfind(Grade,#grade_equip.grade,CellFoster#cell_foster.equips) of
        GValue0 when is_record(GValue0,grade_equip)->
            GValue0;
        _->
            #grade_equip{}
    end,


    FEquip = case lists:keyfind(Pos,#foster_equip.pos,GEquip#grade_equip.equips) of
        FValue0 when is_record(FValue0,foster_equip)->
            FValue0;
        _->
            #foster_equip{}
    end,

    LocBag = case Type of
        ?FHORSE->
            ?GOODS_LOC_FHORSE_BAG;
        ?FJARVIS->
            ?GOODS_LOC_FJARVIS_BAG;
        _->
            0
    end,

    % LocEquip = case Type of
    %     ?FHORSE->
    %         ?GOODS_LOC_FHORSE_EQUIP_BAG;
    %     ?FJARVIS->
    %         ?GOODS_LOC_FJARVIS_EQUIP_BAG;
    %     _->
    %         0
    %end,
    GoodsStatus = lib_goods_do:get_goods_status(),
    GoodsDict = GoodsStatus#goods_status.dict,
    GoodsInfos = [lib_goods_util:get_goods(GoodsId,GoodsDict,LocBag)||GoodsId<-Uids],

    IsCanResolve0 = [lib_role_foster:is_can_resolve(Type,GoodsInfo)||GoodsInfo<-GoodsInfos],

    IsCanResolve = lists:member(false,IsCanResolve0),

    UPGoodsInfo = lib_role_foster:get_pos_goods(Type,Grade,FEquip#foster_equip.pos),

    CKList = [
        {check_unlock,CellFoster,Type},
        {check_resolve,IsCanResolve},
        {check_goods_exist,UPGoodsInfo},
        {check_goods_foster_equip,Type,UPGoodsInfo},
        {check_goods_lv_up,Type,FEquip}
    ],
    case util:check_lists(lib_role_foster,CKList) of
        true->
            case lib_role_foster:do_foster_equip_lv(Status,Type,FEquip,Pos,UPGoodsInfo,GoodsInfos) of
                {ok,NewFosterEquip}->
                    GEquips = lists:keyreplace(Pos,#foster_equip.pos,GEquip#grade_equip.equips,NewFosterEquip),
                    NGradeEquip = GEquip#grade_equip{equips=GEquips},
                    NCellEquips = lists:keyreplace(Grade,#grade_equip.grade,CellFoster#cell_foster.equips,NGradeEquip),
                    NCellFoster = CellFoster#cell_foster{equips=NCellEquips},
                    NFosters = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
                    NStatus0 = Status#player_status{role_foster=RoleFoster#role_foster{fosters=NFosters}},
                    lib_role_foster:update_role_foster({equips,Grade},RoleID,NCellFoster),
                    NStatus1  = lib_role_foster:count_foster_attr(NStatus0),
                    NStatus  = lib_player:count_player_attribute(NStatus1),
                    lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
                    lib_role_foster:update_foster_attr_to_scene(NStatus, Type),
                    mod_scene_agent:update(NStatus, [{battle_attr, NStatus#player_status.battle_attr}]),
                    lib_rush_rank_api:refresh_rank_by_foster(Type,NStatus),

                    Costs = lists:foldl(fun(C,Args)->
                        case C of
                            #goods{goods_id=Cid}->
                                [Cid|Args];
                            _->
                                Args
                        end
                    end,[],GoodsInfos),
                    LogData = [
                        RoleID,Name,Type,Grade,Pos,UPGoodsInfo#goods.goods_id,
                        FEquip#foster_equip.lv,FEquip#foster_equip.exp,
                        NewFosterEquip#foster_equip.lv,NewFosterEquip#foster_equip.exp,
                        util:term_to_string(Costs),utime:unixtime()
                    ],
                    lib_log_api:log_foster_equip_lv(LogData),
                    Res = ?SUCCESS;
                {false,Err}->
                    Res = Err,NStatus=Status
            end;
        {false,Err}->
            Res = Err,NStatus=Status
    end,
    {ok,Bin} = pt_146:write(14606,[Res,Type,Grade]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%% 装备道具
%% 卸装备
handle(14607,Status,[0,Type,Grade,Uid])->
    #player_status{id=RoleID,role_foster=RoleFoster,figure=#figure{name=Name}}=Status,
    #role_foster{fosters=Fosters} = RoleFoster,
    CellFoster = case lists:keyfind(Type,#cell_foster.type,Fosters) of
        CValue when is_record(CValue,cell_foster)->
            CValue;
        _->
            #cell_foster{}
    end,

    GEquip = case lists:keyfind(Grade,#grade_equip.grade,CellFoster#cell_foster.equips) of
        GValue0 when is_record(GValue0,grade_equip)->
            GValue0;
        _->
            #grade_equip{grade=Grade}
    end,

    LocEquip = case Type of
        ?FHORSE->
            ?GOODS_LOC_FHORSE_EQUIP_BAG;
        ?FJARVIS->
            ?GOODS_LOC_FJARVIS_EQUIP_BAG;
        _->
            0
    end,


    LocBag = case Type of
        ?FHORSE->
            ?GOODS_LOC_FHORSE_BAG;
        ?FJARVIS->
            ?GOODS_LOC_FJARVIS_BAG;
        _->
            0
    end,

    GoodsStatus = lib_goods_do:get_goods_status(),
    #goods_status{dict = Dict} = GoodsStatus,
    OutGoodsInfo = lib_goods_util:get_goods(Uid,Dict,LocEquip),
    Pos = case OutGoodsInfo of
        #goods{goods_id=Gtype}->
            case data_foster:get_foster_equip(Gtype) of
                #equip_conf{pos=P}->
                    P;
                _->
                    0
            end;
        _->
            0
    end,

    CKList = [
        {check_goods_exist,OutGoodsInfo},
        {check_goods_foster_equip,Type,OutGoodsInfo},
        {check_grade_equip,OutGoodsInfo,GEquip}
    ],
    case util:check_lists(lib_role_foster,CKList) of
        true->
            F = fun() ->
                ok = lib_goods_dict:start_dict(),
                [_NewOutGoodsInfo, NewGoodsStatus1] = goods_logic:change_goods_location(OutGoodsInfo, LocBag, 0, GoodsStatus),
                #goods_status{dict = Dict0} = NewGoodsStatus1,
                {NewGoodsDict, GoodsL0} = lib_goods_dict:handle_dict_and_notify(Dict0),
                NewGoodsStatus2 = NewGoodsStatus1#goods_status{dict = NewGoodsDict},
                {ok,NewGoodsStatus2,GoodsL0}
            end,
            case goods_transaction:transaction(F) of
                {ok,NewGoodsStatus,GoodsL}->
                    lib_goods_do:set_goods_status(NewGoodsStatus),
                    lib_goods_api:notify_client(RoleID, GoodsL, role_foster),
                    lib_goods_api:notify_client_num(RoleID, [OutGoodsInfo]),
                    NewEquips = lists:keydelete(Pos,#foster_equip.pos,GEquip#grade_equip.equips),
                    NewAssist = lib_role_foster:calc_equip_assist_active(Type, Grade, GEquip#grade_equip.assist, NewEquips),
                    %% 助战变更后推送
                    NewAssist =/= GEquip#grade_equip.assist andalso
                        lib_server_send:send_to_uid(RoleID, pt_146, 14621, [Type]),
                    NewGEquip = GEquip#grade_equip{equips=NewEquips,assist=NewAssist},
                    NCellEquips = lists:keystore(Grade,#grade_equip.grade,CellFoster#cell_foster.equips,NewGEquip),
                    NCellFoster = CellFoster#cell_foster{equips=NCellEquips},
                    NFosters    = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
                    NRoleFoster = RoleFoster#role_foster{fosters=NFosters},
                    lib_role_foster:update_role_foster({equips,Grade},RoleID,NCellFoster),
                    NStatus0 = Status#player_status{role_foster=NRoleFoster},
                    {ok,Bin} = pt_146:write(14607,[?SUCCESS,0,Type,Grade,Pos,Uid]),
                    if GEquip#grade_equip.assist =/= NewAssist ->
                            {ok, Bin1} = pt_146:write(14615, [Type,Grade,NewAssist]),
                            lib_server_send:send_to_uid(RoleID,<<Bin/binary,Bin1/binary>>);
                        true ->
                            lib_server_send:send_to_uid(RoleID,Bin)
                    end,
                    NStatus1  = lib_role_foster:count_foster_attr(NStatus0),
                    NStatus  = lib_player:count_player_attribute(NStatus1),
                    lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
                    lib_role_foster:update_foster_attr_to_scene(NStatus, Type),
                    mod_scene_agent:update(NStatus, [{battle_attr, NStatus#player_status.battle_attr}]),
                    LogData = [RoleID,Name,Type,Grade,0,Pos, OutGoodsInfo#goods.goods_id, 0, utime:unixtime()],
                    lib_achievement_api:foster_equip_event(NStatus, {Type, calc_foster_equip_suit(NCellEquips)}),
                    lib_log_api:log_foster_equip(LogData),
                    {ok,NStatus};
                _Reason->
                    {ok,Bin} = pt_146:write(14607,[?FAIL,0,Type,Grade,Pos,Uid]),
                    lib_server_send:send_to_uid(RoleID,Bin),
                    {ok,Status}
            end;
        {false,Err}->
            {ok,Bin} = pt_146:write(14607,[Err,0,Type,Grade,Pos,Uid]),
            lib_server_send:send_to_uid(RoleID,Bin),
            {ok,Status}
    end;
%% 穿装备
handle(14607,Status,[OpTy,Type,Grade,Uid])->
    #player_status{id=RoleID,role_foster=RoleFoster,figure=#figure{name=Name}}=Status,
    #role_foster{fosters=Fosters} = RoleFoster,

    CellFoster = case lists:keyfind(Type,#cell_foster.type,Fosters) of
        CValue when is_record(CValue,cell_foster)->
            CValue;
        _->
            #cell_foster{}
    end,

    GEquip = case lists:keyfind(Grade,#grade_equip.grade,CellFoster#cell_foster.equips) of
        GValue0 when is_record(GValue0,grade_equip)->
            GValue0;
        _->
            #grade_equip{grade=Grade}
    end,

    GoodsStatus = lib_goods_do:get_goods_status(),
    #goods_status{dict = Dict} = GoodsStatus,

    LocBag = case Type of
        ?FHORSE->
            ?GOODS_LOC_FHORSE_BAG;
        ?FJARVIS->
            ?GOODS_LOC_FJARVIS_BAG;
        _->
            0
    end,
    InGoodsInfo0 = lib_goods_util:get_goods(Uid,Dict,LocBag), %%需要穿戴的道具

    case InGoodsInfo0 of
        #goods{goods_id=Gtype}->
            case data_foster:get_foster_equip(Gtype) of
                #equip_conf{pos=Pos}->
                    skip;
                _->
                    Pos = 0
            end;
        _->
            Pos = 0
    end,

    FEquip = case lists:keyfind(Pos,#foster_equip.pos,GEquip#grade_equip.equips) of
        FValue0 when is_record(FValue0,foster_equip)->
            FValue0;
        _->
            #foster_equip{}
    end,

    LocEquip = case Type of
        ?FHORSE->
            ?GOODS_LOC_FHORSE_EQUIP_BAG;
        ?FJARVIS->
            ?GOODS_LOC_FJARVIS_EQUIP_BAG;
        _->
            0
    end,

    OutGoodsInfo =  lib_role_foster:get_pos_goods(Type,Grade,FEquip#foster_equip.pos),
    NowGrade = case lists:member(Type, ?GRADE_FOSTER) of
                   true -> CellFoster#cell_foster.grade;
                   _ -> CellFoster#cell_foster.lv
               end,
    CKList = [
        {check_unlock,CellFoster,Type},
        {check_equip_grade, Type, NowGrade, Grade},
        {check_goods_exist,InGoodsInfo0},
        {check_goods_foster_bag,Type,InGoodsInfo0}
    ],
    case util:check_lists(lib_role_foster,CKList) of
        true->
            InGoodsInfo = update_extra_data(Grade,InGoodsInfo0),
            F = fun() ->
                ok = lib_goods_dict:start_dict(),
                case is_record(OutGoodsInfo, goods) of
                    true ->
                        [_NewOutGoodsInfo0, GoodsStatus1] = goods_logic:change_goods_location(OutGoodsInfo, LocBag, 0, GoodsStatus),
                        [NewInGoodsInfo0, NewGoodsStatus1] = goods_logic:change_goods_location(InGoodsInfo, LocEquip, Pos, GoodsStatus1);
                    false ->
                        [NewInGoodsInfo0, NewGoodsStatus1] = goods_logic:change_goods_location(InGoodsInfo, LocEquip, Pos, GoodsStatus)
                end,
                #goods_status{
                    dict = Dict0
                } = NewGoodsStatus1,
                {NewGoodsDict, GoodsL0} = lib_goods_dict:handle_dict_and_notify(Dict0),
                NewGoodsStatus2 = NewGoodsStatus1#goods_status{dict = NewGoodsDict},
                {ok,NewGoodsStatus2,GoodsL0,NewInGoodsInfo0}
            end,
            case goods_transaction:transaction(F) of
                {ok,NewGoodsStatus,GoodsL,NewInGoodsInfo}->
                    lib_goods_do:set_goods_status(NewGoodsStatus),
                    lib_goods_api:notify_client_num(RoleID, [InGoodsInfo#goods{num=0}]),
                    lib_goods_api:notify_client(RoleID, GoodsL, role_foster),

                    NewFEquip = lib_role_foster:trans_foster_equip(NewInGoodsInfo,Pos,FEquip),
                    GEquips = lists:keystore(Pos,#foster_equip.pos,GEquip#grade_equip.equips,NewFEquip),
                    NewAssist = lib_role_foster:calc_equip_assist_active(Type, Grade, GEquip#grade_equip.assist, GEquips),
                    NGradeEquip = GEquip#grade_equip{equips=GEquips,assist=NewAssist},
                    NCellEquips = lists:keystore(Grade,#grade_equip.grade,CellFoster#cell_foster.equips,NGradeEquip),
                    NCellFoster = CellFoster#cell_foster{equips=NCellEquips},
                    NFosters = lists:keystore(Type,#cell_foster.type,Fosters,NCellFoster),
                    NStatus0 = Status#player_status{role_foster=RoleFoster#role_foster{fosters=NFosters}},
                    lib_role_foster:update_role_foster({equips,Grade},RoleID,NCellFoster),
                    {ok,Bin} = pt_146:write(14607,[?SUCCESS,OpTy,Type,Grade,Pos,Uid]),
                    if GEquip#grade_equip.assist =/= NewAssist ->
                            {ok, Bin1} = pt_146:write(14615, [Type,Grade,NewAssist]),
                            lib_server_send:send_to_uid(RoleID, <<Bin/binary, Bin1/binary>>);
                        true ->
                            lib_server_send:send_to_uid(RoleID, Bin)
                    end,
                    NStatus1  = lib_role_foster:count_foster_attr(NStatus0),
                    NStatus  = lib_player:count_player_attribute(NStatus1),
                    lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
                    lib_role_foster:update_foster_attr_to_scene(NStatus, Type),
                    mod_scene_agent:update(NStatus, [{battle_attr, NStatus#player_status.battle_attr}]),
                    lib_rush_rank_api:refresh_rank_by_foster(Type,NStatus),
                    case OutGoodsInfo of
                        #goods{goods_id=Out}->
                            skip;
                        _->
                            Out = 0
                    end,
                    LogData = [RoleID,Name,Type,Grade,OpTy,Pos, Out, InGoodsInfo#goods.goods_id, utime:unixtime()],
                    lib_achievement_api:foster_equip_event(NStatus, {Type, calc_foster_equip_suit(NCellEquips)}),
                    lib_log_api:log_foster_equip(LogData),
                    {ok,NStatus};
                _Reason->
                    ?INFO("equip_foster:~p ~p ~p  ", [?MODULE, ?LINE, _Reason]),
                    {ok,Bin} = pt_146:write(14607,[?FAIL,OpTy,Type,Grade,0,Uid]),
                    lib_server_send:send_to_uid(RoleID,Bin),
                    {ok,Status}
            end;
        {false,Err}->
            {ok,Bin} = pt_146:write(14607,[Err,OpTy,Type,Grade,0,Uid]),
            lib_server_send:send_to_uid(RoleID,Bin),
            {ok,Status}
    end;

%% 装备信息
handle(14608,Status,[Type,Grade])->
    #player_status{id=RoleID,role_foster=RoleFoster}=Status,
    #role_foster{fosters=Fosters} = RoleFoster,

    CellFoster = case lists:keyfind(Type,#cell_foster.type,Fosters) of
        CValue when is_record(CValue,cell_foster)->
            CValue;
        _->
            #cell_foster{}
    end,

    GEquip = case lists:keyfind(Grade,#grade_equip.grade,CellFoster#cell_foster.equips) of
        GValue0 when is_record(GValue0,grade_equip)->
            GValue0;
        _->
            #grade_equip{}
    end,
    Equips0 = GEquip#grade_equip.equips,
    Equips = lib_role_foster:pack_equips(Type,Grade,Equips0),
    {ok,Bin} = pt_146:write(14608,[Type,Grade,Equips]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};

handle(14610,Status,[Type])->
    #player_status{id=RoleID,role_foster_skin=RoleSkin}=Status,
    #role_foster_skin{fosters=Fosters} = RoleSkin,
    CellSkin = case lists:keyfind(Type,#cell_foster_skin.type,Fosters) of
        CValue when is_record(CValue,cell_foster_skin)->
            CValue;
        _->
            #cell_foster_skin{}
    end,
    #cell_foster_skin{
        skins = Skins
    }=CellSkin,
    NSkins = [{Id,Star,DisStar}||#cell_skin{id=Id,star=Star,display=DisStar}<-Skins],
    {ok,Bin} = pt_146:write(14610,[Type,NSkins]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};

%% 激活珍宝
handle(14611,Status,[Type,Id,0])->
    #player_status{
        id=RoleID,
        figure=#figure{name=Name}
    }=Status,
    case lib_role_foster_skin:active_foster_skin(Type,Id,Status) of
        {ok,Res,Cost,NStatus}->
            #base_foster_skin{name=SName} = data_foster:get_foster_skin(Type,Id,0),
            lib_chat:send_TV({all}, ?MOD_FOSTER, 9, [Name,RoleID,SName]),
            lib_rush_rank_api:refresh_rank_by_foster(Type,NStatus),
            LogData = [RoleID,Name,Type,0,Id, 0, 1, util:term_to_string(Cost), utime:unixtime()],
            lib_log_api:log_foster_skin_upgrade(LogData),
            ok;
        {false,Res}->
            NStatus = Status
    end,
    {ok,Bin} = pt_146:write(14611,[Type,Id,1,0,Res]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%% 珍宝升星
handle(14611,Status,[Type,Id,OpTy])->
     #player_status{
        id=RoleID,
        scene = Scene,
        scene_pool_id = PoolID,
        copy_id = CopyId,
        x = X, y = Y,
        figure = Figure
    }=Status,
    #figure{name=Name}=Figure,
    case lib_role_foster_skin:upgrade_foster_skin(Type,Id,Status) of
        {ok,Res,{ShowSkin,ShowStar},OStar,Cost,Star,NStatus0}->
            #base_foster_skin{model=OldM}=data_foster:get_foster_skin(Type,Id,ShowStar),
            #base_foster_skin{model=NewM}=data_foster:get_foster_skin(Type,Id,Star),
            case ShowSkin =:= Id andalso OldM /= NewM of
                true->%%外观发生变化
                     {ok, Bin1} = pt_120:write(12016, [?MOD_FOSTER,Type+1000,RoleID,util:term_to_string({Id,Star})]),
                     lib_server_send:send_to_area_scene(Scene,PoolID,CopyId,X,Y,Bin1),
                     FosterFigure = lib_role_foster_skin:get_foster_skin_figure(NStatus0),
                     mod_scene_agent:update(NStatus0,[{foster_skin,FosterFigure}]),
                     NFigure = Figure#figure{foster_skin=FosterFigure},
                     lib_role:update_role_show(RoleID, [{figure,NFigure}]),
                     NStatus = NStatus0#player_status{figure=NFigure};
                _->
                    NStatus = NStatus0
            end,
            #base_foster_skin{name=SName} = data_foster:get_foster_skin(Type,Id,Star),
            lib_chat:send_TV({all}, ?MOD_FOSTER, 10, [Name,RoleID,SName,Star]),
            lib_rush_rank_api:refresh_rank_by_foster(Type,NStatus),
            LogData = [RoleID,Name,Type,OpTy,Id, OStar, Star, util:term_to_string(Cost), utime:unixtime()],
            lib_log_api:log_foster_skin_upgrade(LogData);
        {false,Res}->
            NStatus = Status,
            Star = 1
    end,
    {ok,Bin} = pt_146:write(14611,[Type,Id,Star,OpTy,Res]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%% 幻化珍宝
handle(14612,Status,[Type,SkinID,Star])->
    #player_status{
        id=RoleID,
        scene = Scene,
        scene_pool_id = PoolID,
        copy_id = CopyId,
        x = X, y = Y,
        role_foster_skin = RoleSkin
    }=Status,
    #role_foster_skin{fosters=Fosters}=RoleSkin,
    case lib_role_foster_skin:change_display(RoleID,Type,SkinID,Star,Fosters) of
        {ok,IsUpdate,NCellFoster}->
            %% 如果是穿戴，卸下原有的外观
            Status1 =
            case SkinID > 0 andalso Star > 0 of
                true -> lib_role_foster:put_off_other_appearance(Status, Type, 2, {SkinID, Star});
                _ ->
                    %% 如果是卸下，直接推送场景协议
                    {ok, Bin1} = pt_120:write(12016, [?MOD_FOSTER, Type+1000, RoleID, util:term_to_string({SkinID, Star})]),
                    lib_server_send:send_to_area_scene(Scene, PoolID, CopyId, X, Y, Bin1),
                    Status
            end,

            #cell_foster_skin{display={CurSkin,CurStar}}=NCellFoster,
            {ok, Bin} = pt_146:write(14612,[?SUCCESS,Type,CurSkin,CurStar]),
            lib_server_send:send_to_uid(RoleID,Bin),

            NFosters = lists:keystore(Type,#cell_foster_skin.type,Fosters,NCellFoster),
            NStatus = Status1#player_status{role_foster_skin = RoleSkin#role_foster_skin{fosters=NFosters}},
            FosterFigure = lib_role_foster_skin:get_foster_skin_figure(NStatus),
            IsUpdate andalso mod_scene_agent:update(NStatus,[{foster_skin,FosterFigure}]),
            Figure1 = Status1#player_status.figure,
            NFigure = Figure1#figure{foster_skin=FosterFigure},
            lib_role:update_role_show(RoleID, [{figure,NFigure}]),

            %% 处理卸下皮肤
            {ok,NStatus#player_status{figure=NFigure}};
        {false,Err}->
            {ok, Bin} = pt_146:write(14612,[Err,Type,SkinID,Star]),
            lib_server_send:send_to_uid(RoleID,Bin),
            {ok, Status}
    end;

%% 进阶装备助战状态查询-全部数据
handle(14614,Status,[Type])->
    #player_status{sid=Sid,role_foster=RoleFoster}=Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    StateList =
    case lists:keyfind(Type,#cell_foster.type,Fosters) of
        #cell_foster{equips = GEquips} ->
            lists:foldl(fun(#grade_equip{grade = Grade, assist = Assist}, AccState) -> 
                [{Grade, Assist}|AccState]
            end, [], GEquips);
        _ -> []
    end,
    lib_server_send:send_to_sid(Sid,pt_146,14614,[Type,StateList]),
    ok;


%%进阶装备助战状态查询
handle(14615,Status,[Type,Grade])->
    #player_status{id=RoleID,role_foster=RoleFoster}=Status,
    #role_foster{fosters=Fosters} = RoleFoster,

    CellFoster = case lists:keyfind(Type,#cell_foster.type,Fosters) of
        CValue when is_record(CValue,cell_foster)->
            CValue;
        _->
            #cell_foster{}
    end,
    #cell_foster{equips = GEquips} = CellFoster,
    case lists:keyfind(Grade, #grade_equip.grade, GEquips) of
        #grade_equip{assist = Assist} -> ok;
        _ -> Assist = 0
    end,
    lib_server_send:send_to_uid(RoleID,pt_146,14615,[Type,Grade,Assist]),
    ok;

%% 手动激活进阶装备助战状态
handle(14616,Status,[Type,Grade])->
    #player_status{id=RoleID,sid=Sid,role_foster=RoleFoster}=Status,
    #role_foster{fosters=Fosters}=RoleFoster,
    case lists:keyfind(Type,#cell_foster.type,Fosters) of
        CValue = #cell_foster{equips = Equips} ->
            case lists:keyfind(Grade,#grade_equip.grade,Equips) of
                Equip = #grade_equip{assist = Assist} ->
                    if Assist == 1 ->
                            NewAssist = 2,
                            NewEquip = Equip#grade_equip{assist = NewAssist},
                            NewEquips = lists:keystore(Grade,#grade_equip.grade,Equips,NewEquip),
                            NewFosters = lists:keystore(Type,#cell_foster.type,Fosters,CValue#cell_foster{equips = NewEquips}),
                            NewRoleFoster = RoleFoster#role_foster{fosters=NewFosters},
                            NewStatus0 = Status#player_status{role_foster = NewRoleFoster},
                            NewStatus1  = lib_role_foster:count_foster_attr(NewStatus0),
                            NStatus  = lib_player:count_player_attribute(NewStatus1),
                            lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
                            lib_role_foster:update_foster_attr_to_scene(NStatus, Type),
                            mod_scene_agent:update(NStatus, [{battle_attr, NStatus#player_status.battle_attr}]),
                            db:execute(?SQL_UPDATE_FOSTER_EQUIP_ASSIST, [NewAssist, RoleID, Type, Grade]),
                            lib_server_send:send_to_uid(RoleID, pt_146, 14621, [Type]),
                            lib_server_send:send_to_sid(Sid,pt_146,14616,[?SUCCESS,Type,Grade]),
                            {ok, NStatus};
                        true ->
                            if Assist == 0 ->
                                    lib_server_send:send_to_sid(Sid,pt_146,14616,[?ERRCODE(err146_assist_state_error),Type,Grade]);
                                true ->
                                    lib_server_send:send_to_sid(Sid,pt_146,14616,[?ERRCODE(err146_have_actived),Type,Grade])
                            end
                    end;
                _ ->
                    lib_server_send:send_to_sid(Sid,pt_146,14616,[?ERRCODE(err146_grade_limit),Type,Grade])
            end;
        _ ->
            lib_server_send:send_to_sid(Sid,pt_146,14616,[?FAIL,Type,Grade])
    end;

%% 皮肤信息
handle(14617, Status, [Type]) when
    Type == ?FJARVIS orelse Type == ?FGUN orelse Type == ?FCLOUD orelse Type == ?FARMOUR ->
    lib_role_foster:sub_foster_skin_info(Status, Type);

%% 皮肤升级和激活
handle(14618, Status, [Type, Id, Option]) when
    Type == ?FJARVIS orelse Type == ?FGUN orelse Type == ?FCLOUD orelse Type == ?FARMOUR ->
    lib_role_foster:upgrade_sub_foster_skin(Status, Type, Id, Option);

%% 皮肤幻化
handle(14619, Status, [Type, Id, Star]) when
    Type == ?FJARVIS orelse Type == ?FGUN orelse Type == ?FCLOUD orelse Type == ?FARMOUR ->
    lib_role_foster:use_foster_skin(Status, Type, Id, Star);

%% 弱进阶升级
handle(14620, Status, [Type, MaterialType, IsAuto]) when
    Type == ?FJARVIS orelse Type == ?FGUN orelse Type == ?FCLOUD orelse Type == ?FARMOUR ->
    lib_role_foster:sub_foster_up(Status, Type, MaterialType, IsAuto);

handle(_Cmd, _Status, _Data) ->
    ?ERR("pp_foster no match"),
    {error, "pp_foster no match"}.


update_extra_data(Grade,InGoodsInfo)->
    #goods{extra_data=ExtraDs}=InGoodsInfo,
    NExtraDs = lists:keystore(?GOODS_OTHER_KEY_FOSTER_GRADE,1,ExtraDs,{?GOODS_OTHER_KEY_FOSTER_GRADE,Grade}),
    InGoodsInfo#goods{extra_data=NExtraDs}.

%% 计算拥有某种颜色套装的数量, return - {color => num}
calc_foster_equip_suit(CellEquips) ->
    ForFun = fun(#grade_equip{equips = Equips}, TNumMap) ->
        case erlang:length(Equips) =:= 6 of
            true ->
                F = fun(#foster_equip{color = TColor}, TList) ->
                    case lists:member(TColor, TList) of
                        true -> TList;
                        _ -> [TColor|TList]
                    end;
                    (_, TList) -> TList
                    end,
                List = lists:foldl(F, [], Equips),
                case erlang:length(List) =:= 1 of
                    true ->
                        [#foster_equip{color = Color}|_] = Equips,
                        Num = maps:get(Color, TNumMap, 0),
                        maps:put(Color, Num + 1, TNumMap);
                    _ ->
                        TNumMap
                end;
            _ ->
                TNumMap
        end end,
    ColorNumMap = lists:foldl(ForFun, #{}, CellEquips),
    ColorNumMap.

%% 计算上报数据扣除的材料/费用
calc_cost(Player, CostList) ->
    #player_status{bgold = BGold} = Player,
    CostList1 =
    case CostList of
        [{_, CostGoodsId, CostNum}|_] when CostGoodsId > 0 ->
            [{_, OwnNum}] = lib_goods_api:get_goods_num(Player, [CostGoodsId]),
            TrueCostNum = min(OwnNum, CostNum),
            case OwnNum >= CostNum of
                true ->
                    [{?TYPE_GOODS, CostGoodsId, TrueCostNum}];
                _ ->
                    case data_goods_price:get_goods_price(CostGoodsId) of
                        #goods_price{price = Price} ->
                            LeftNum = max(0, CostNum - OwnNum),
                            NeedMoney = LeftNum * Price,
                            case BGold >= NeedMoney of
                                true ->
                                    [{?TYPE_GOODS, CostGoodsId, TrueCostNum}, {?TYPE_BGOLD, 0, NeedMoney}];
                                _ ->
                                    [{?TYPE_GOODS, CostGoodsId, TrueCostNum},
                                        {?TYPE_BGOLD, 0, BGold},
                                        {?TYPE_GOLD, 0, max(0, NeedMoney - BGold)}
                                    ]
                            end;
                        _ ->
                            [{?TYPE_GOODS, CostGoodsId, TrueCostNum}]
                    end
            end;
        _ ->
            []
    end,
    TrueCostList = [{GoodsType, GoodsTypeId, GoodsNum}||{GoodsType, GoodsTypeId, GoodsNum}<-CostList1, GoodsNum =/= 0],
    case lists:keyfind(?TYPE_GOODS, 1, TrueCostList) of
        {_, _, CostGoodNum} -> ok;
        _ -> CostGoodNum = 0
    end,
    case lists:keyfind(?TYPE_BGOLD, 1, TrueCostList) of
        {_, _, CostBGold} -> ok;
        _ -> CostBGold = 0
    end,
    case lists:keyfind(?TYPE_GOLD, 1, TrueCostList) of
        {_, _, CostGold} -> ok;
        _ -> CostGold = 0
    end,
    {TrueCostList, CostGoodNum, CostBGold, CostGold}.