%%%-------------------------------------------------------------------
%%% @doc    孩子物品(载具，玩具, 时装)
%%% Module  : lib_child_items.erl
%%% Created : 2020-03-06
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(lib_child_items).

-include("common.hrl").
-include("server.hrl").
-include("child.hrl").
-include("role.hrl").
-include("figure.hrl").
-include("def_goods.hrl").
-include("goods.hrl").
-include("def_fun.hrl").
-include("def_module.hrl").
-include("predefine.hrl").
-include("errcode.hrl").


-export([
	get_child_vehicle_info/1            %% 获取孩子载具信息
	,active_child_vehicle/2             %% 激活孩子载具
	,upgrade_child_vehicle/2            %% 孩子载具升阶

	%% 以下是旧逻辑,不使用
	,get_toy_slot_info/1                %% 获取玩具槽位信息
	,get_toy_info/2                     %% 查询单个玩具信息
	,inlay_toy/3                        %% 装备玩具
	,load_off_toy/3                     %% 卸下玩具
	,stren_toy/3                        %% 玩具强化
	,stren_return/2                     %% 玩具强化回退
	,calc_toy_weight/1                  %% 计算玩具总重量
	,calc_can_carry_weight/4            %% 计算可以承受的玩具重量
	,compose_toy/2                      %% 玩具合成
	,decompose_toy/2                    %% 玩具分解
	,get_toy_forge_info/1               %% 获取玩具打造位信息
	,forge_toy/2                        %% 打造玩具
	,draw_out_toy/2                     %% 领取玩具
	,speed_up_forge/2                   %% 玩具加速打造
	,check_toy_goods/1                  %% 检查玩具是否可以分解
	,calc_can_stren_lv/6                %% 计算可以强化的等级
	,calc_cost/5                        %% 计算强化消耗
	,calc_stren_material/4              %% 计算玩具强化到当前等级所消耗的材料
	,active_toy_slot/2                  %% 激活玩具位
	,active_forge_slot/2                %% 激活打造位

	%% 以下是新玩具逻辑
	,get_toy_suit_info/1                %% 获取套装信息
	,get_toy_suit_reward_info/1         %% 获取套装奖励信息
	,receive_toy_suit_reward/2          %% 领取套装奖励
	,active_toy/3                       %% 激活玩具
	,upgrade_toy_box/1                  %% 升级玩具盒
	,log_toy_decompose/3
	,init_suit_map/1                    %% 登录初始化玩具数据
	,check_toy_active/2                 %% 检查玩具是否已经激活

	,get_fashion_info/2                 %% 获取时装基本信息
	,put_on_fashion/3                   %% 穿戴时装
	,put_off_fashion/3                  %% 脱下时装
	,active_fashion/3                   %% 激活时装
	,upgrade_fashion/3                  %% 升级时装
	,trans_to_fashion/1                 %% 登录时装数据格式转换

	,check_if_active_toy_slot/2         %% 检查是否达到玩具位开放条件
	,check_if_active_forge_slot/1       %% 检查是否达到打造位开放条件

	%% gm操作
	,gm_set_child_vehicle_stage/3       %% 设置载具的阶数
	,gm_quick_open_toy_slot/1           %% 快速开启玩具槽位&打造位

]).

%% 获取载具信息
get_child_vehicle_info(Player) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{show_vehicle = ShowVehicle, vehicle = VehicleList} = StatusChild,
	lib_child:send_msg(Sid, 16514, [ShowVehicle, VehicleList]).

%% 激活载具
active_child_vehicle(Player, VehicleId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{
		child_sex = ChildSex,
		vehicle   = VehicleList
	} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {is_child_vehicle, VehicleId},
		{has_active, VehicleId, VehicleList},
		{is_active_material_enough, VehicleId}
	],
	case lib_child_util:check_active_vehicle(CheckList) of
		true ->
			case do_active_vehicle(Player, VehicleId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16515, [VehicleId, 1]),
					{ok, NewPlayer};
				{false, ErrCode} ->
					lib_child:send_error(Sid, ErrCode),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 升阶孩子载具
upgrade_child_vehicle(Player, VehicleId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{child_sex = ChildSex, vehicle = VehicleList} = StatusChild,
	VehicleInfo =
		case lists:keyfind(VehicleId, 1, VehicleList) of
			{_, _} = TVehicleInfo -> TVehicleInfo;
			_                     -> {VehicleId, 0}
		end,
	CheckList = [
		{has_adopt_child, ChildSex}, {is_child_vehicle, VehicleId},
		{has_active, VehicleId, VehicleList}, {is_max_stage, VehicleInfo},
		{is_upgrade_material_enough, VehicleInfo}
	],
	case lib_child_util:check_upgrade_vehicle(CheckList) of
		true ->
			case do_upgrade_child_vehicle(Player, VehicleInfo) of
				{true, NewPlayer, NewVehicleInfo} ->
					{_TVehicle, NewStage} = NewVehicleInfo,
					lib_child:send_msg(Sid, 16515, [VehicleId, NewStage]),
					{ok, NewPlayer};
				{false, ErrCode} ->
					lib_child:send_error(Sid, ErrCode),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 获取玩具槽位信息
get_toy_slot_info(Player) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{
		toy_list     = ToyList,
		can_carry    = CanCarry,
		carry_weight = CarryWeight
	} = StatusChild,
	PackSlotList = pack_slot_toy_list(ToyList),
	BagToyList   = get_all_toy_list(),
	PackToyList  = pack_bag_toy_list(BagToyList),
	lib_child:send_msg(Sid, 16520, [CarryWeight, CanCarry, PackSlotList, PackToyList]).

%% 打包槽位上的玩具
pack_slot_toy_list(ToyList) ->
	case ToyList of
		[_|_] ->
			F = fun({SlotId, ToyId}, TList) ->
				case ToyId > 0 of
					%% 已装备
					true ->
						case lib_goods_api:get_goods_info(ToyId, ?GOODS_LOC_CHILD_TOY_SLOT) of
							#goods{goods_id = GoodTypeId, extra_data = ExtraData} ->
								case ExtraData of
									[{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}] -> ok;
									_ -> Lv = 0
								end,
								[{SlotId, 1, ToyId, GoodTypeId, Lv}|TList];
							_ ->
								[{SlotId, 1, 0, 0, 0}|TList]
						end;
					_ ->
						[{SlotId, 1, 0, 0, 0}|TList]
				end end,
			lists:foldl(F, [], ToyList);
		_ ->
			[]
	end.
%% 打包背包中的玩具
pack_bag_toy_list(BagToyList) ->
	case BagToyList of
		[_|_] ->
			F = fun(GoodInfo, TList) ->
				case GoodInfo of
					#goods{id = ToyId, goods_id = GoodTypeId, extra_data = [{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}]} ->
						[{ToyId, GoodTypeId, Lv}|TList];
					#goods{id = ToyId, goods_id = GoodTypeId} ->
						[{ToyId, GoodTypeId, 0}|TList];
					_ ->
						TList
				end
			end,
			lists:foldl(F, [], BagToyList);
		_ ->
			[]
	end.

%% 查询玩具信息
get_toy_info(Player, GoodId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{toy_list = ToyList} = StatusChild,
	case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
		#goods{id = ToyId, goods_id = GoodTypeId, extra_data = Extra} ->
			Pack = pack_toy_info(ToyId, GoodTypeId, Extra, ToyList);
		_ ->
			case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_SLOT) of
				#goods{id = ToyId, goods_id = GoodTypeId, extra_data = Extra} ->
					Pack = pack_toy_info(ToyId, GoodTypeId, Extra, ToyList);
				_ ->
					Pack = []
			end
	end,
	lib_child:send_msg(Sid, 16521, [Pack]).

pack_toy_info(ToyId, GoodTypeId, Extra, ToyList) ->
	case Extra of
		[{?GOODS_OTHER_KEY_CHILD_TOY_LV, {lv, Lv}}] ->
			case lists:keyfind(ToyId, 2, ToyList) of
				{_, _} ->
					[ToyId, GoodTypeId, Lv, 1];
				_ ->
					[ToyId, GoodTypeId, Lv, 0]
			end;
		_ ->
			case lists:keyfind(ToyId, 2, ToyList) of
				{_, _} ->
					[ToyId, GoodTypeId, 0, 1];
				_ ->
					[ToyId, GoodTypeId, 0, 0]
			end
	end.

%% 装备玩具
inlay_toy(#player_status{status_child = StatusChild, sid = Sid} = Player, SlotId, GoodId) ->
	#status_child{child_sex = ChildSex, toy_list = ToyList, can_carry = CanCarry} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {is_toy, GoodId},
		{is_slot_open, ToyList, SlotId},
		{can_carry, ToyList, CanCarry, SlotId, GoodId}
	],
	case lib_child_util:check_inlay_toy(CheckList, Player) of
		{true, _Player1} ->
			case do_inlay_toy(Player, SlotId, GoodId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16522, [1, SlotId, GoodId]),
					NewPlayer;
				{false, Err} ->
					lib_child:send_error(Sid, Err), Player
			end;
		{false, ErrCode} ->
			lib_child:send_error(Sid, ErrCode), Player
	end.

%% 卸下玩具
load_off_toy(#player_status{status_child = StatusChild, sid = Sid} = Player, SlotId, GoodId) ->
	#status_child{toy_list = ToyList} = StatusChild,
	CheckList = [{is_slot_open, ToyList, SlotId}, {has_good, SlotId, ToyList}],
	case lib_child_util:check_load_off_toy(CheckList, Player) of
		{true, _Player1} ->
			case do_load_off_toy(Player, SlotId) of
				{true, NewPlayer}  ->
					lib_child:send_msg(Sid, 16522, [2, SlotId, GoodId]),
					NewPlayer;
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					Player
			end;
		{false, ErrCode} ->
			lib_child:send_error(Sid, ErrCode), Player
	end.

%% 玩具强化
stren_toy(Player, GoodId, AimLv) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{child_sex = ChildSex} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {is_toy, GoodId},
		{aim_lv_legal, GoodId, AimLv},
		{cost_enough, GoodId, AimLv}
	],
	case lib_child_util:check_stren_toy(CheckList, Player) of
		{true, _Player1} ->
			case do_stren_toy(Player, GoodId, AimLv) of
				{true, Player2, NewLv} ->
					lib_child:send_msg(Sid, 16523, [1, GoodId, NewLv]),
					Player2;
				{false, Error} ->
					lib_child:send_error(Sid, Error), Player
			end;
		{false, Err} ->
			lib_child:send_error(Sid, Err), Player
	end.

%% 强化回退
stren_return(Player, GoodId) ->
	#player_status{sid = Sid, id = _RoleId, status_child = StatusChild} = Player,
	#status_child{toy_list = ToyList} = StatusChild,
	GoodInfo =
	case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
		#goods{} = TGoodInfo -> TGoodInfo;
		_ ->
			case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_SLOT) of
				#goods{} = TGoodInfo -> TGoodInfo;
				_ -> []
			end
	end,
	case GoodInfo of
		#goods{goods_id = _GoodTypeId, extra_data = ExtraData} ->
			case ExtraData of
				[{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}] -> ok;
				_ -> Lv = 0
			end,
			case Lv > 0 of
				false ->
					lib_child:send_error(Sid, ?ERRCODE(err165_toy_has_not_stren)), Player;
				_ ->
					SumCostList = return_stren_material([GoodId]),
					Produce = Produce = #produce{
						title = utext:get(13), content = utext:get(14),
						type = toy_stren_material_return, reward = SumCostList,
						show_tips = 1, remark = ""
					},
					case SumCostList of
						[] -> Player1 = Player;
						_ -> {ok, Player1} = lib_goods_api:send_reward_with_mail(Player, Produce)
					end,
					%% 更新槽位中的玩具数据
					case goods_logic:update_goods_info_extra([GoodInfo#goods{extra_data = [{?GOODS_OTHER_KEY_CHILD_TOY_LV, 0}]}]) of
						{ok, [_NewGoodsInfo|_]} ->
							%% 重新计算属性
							case lists:keyfind(GoodId, 2, ToyList) of
								{_, _} ->
									% lib_log_api:log_child_toy(RoleId, GoodTypeId, GoodInfo#goods.id, 0, 1),
									RefreshPlayer = lib_child:refresh_attr(Player1, toy_return_stren),
									mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
									lib_child:send_msg(Sid, 16523, [2, GoodId, 0]), RefreshPlayer;
								_ ->
									% lib_log_api:log_child_toy(RoleId, GoodTypeId, GoodInfo#goods.id, 0, 0),
									lib_child:send_msg(Sid, 16523, [2, GoodId, 0]), Player1
							end;
						Res ->
							lib_child:send_error(Sid, Res), Player
					end
			end;
		_ ->
			lib_child:send_error(Sid, ?ERRCODE(err165_has_not_toy)), Player
	end.

%% 计算当前可以承受的玩具重量
calc_can_carry_weight(Year, Month, VehicleList, FashionList) ->
	case data_child:get_child_grow_up_cfg(Year, Month) of
		#child_grow_up_cfg{can_carry_weight = BCanCarry} -> ok;
		_ -> BCanCarry = 0
	end,
	F = fun({VehicleId, Stage}, TCarry) ->
		case data_child:get_child_vehicle_cfg(VehicleId, Stage) of
			#child_vehicle_cfg{add_carry_weight = AddWeight} -> ok;
			_ -> AddWeight = 0
		end,
		TCarry + AddWeight
	end,
	VCanCarry = lists:foldl(F, 0, VehicleList),
	F1 = fun({FashionId, Lv}, TCarry1) ->
		case data_child:get_child_fashion_cfg(FashionId, Lv) of
			#child_fashion_cfg{add_carry_weight = AddWeight1} -> ok;
			_ -> AddWeight1 = 0
		end,
		TCarry1 + AddWeight1
	end,
	FCanCarry = lists:foldl(F1, 0, FashionList),
	BCanCarry + VCanCarry + FCanCarry.

%% 计算玩具总重量
calc_toy_weight(ToyList) ->
	F = fun({_SlotId, GoodId}, TWeight) ->
		GoodInfo =
		case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
			#goods{} = TGoodInfo -> TGoodInfo;
			_ ->
				case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_SLOT) of
					#goods{} = TGoodInfo -> TGoodInfo;
					_ -> []
				end
		end,
		case GoodInfo of
			#goods{goods_id = GoodTypeId} ->
				case data_child:get_child_toy_cfg(GoodTypeId) of
					#child_toy_cfg{toy_weight = Weight} ->
						Weight + TWeight;
					_ ->
						TWeight
				end;
			_ ->
				TWeight
		end end,
	lists:foldl(F, 0, ToyList).

%% 合成玩具
compose_toy(Player, GoodsList) ->
	% 判断条件
	% 1.是否满足6件
	% 2.是否同品质
	% 3.是否已经装备
	% 4.是否已经达到最高品质
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{child_sex = ChildSex, toy_list = ToyList} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex},
		{goods_enough_six, GoodsList},
		{is_same_color, GoodsList},
		{has_inlay, ToyList, GoodsList},
		{max_color, GoodsList}
	],
	case lib_child_util:check_compose_toy(CheckList, Player) of
		{true, _Player1} ->
			case do_compose_toy(Player, GoodsList) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16524, [?SUCCESS]),
					{ok, NewPlayer};
				{false, ErrCode} ->
					lib_child:send_error(Sid, ErrCode),
					{ok, Player}
			end;
		{false, Err} ->
			lib_child:send_error(Sid, Err), {ok, Player}
	end.

%% 检查玩具是否可分解
check_toy_goods(GoodsInfo) ->
	#goods{location = Location} = GoodsInfo,
	case Location of
		?GOODS_LOC_CHILD_TOY_SLOT ->
			{false, ?ERRCODE(err165_toy_is_on_slot)};
		_ ->
			true
	end.

%% 检查玩具是否已经激活
check_toy_active(Player, GoodsList) ->
	GoodIdList = calc_good_id(GoodsList, []),
	case GoodIdList of
		[GoodId|_] ->
			case data_goods_type:get(GoodId) of
				#ets_goods_type{type = Type, subtype = SubType} when
					Type == ?GOODS_TYPE_BABY andalso SubType == ?GOODS_CHILD_TOY ->
						#player_status{status_child = StatusChild} = Player,
						#status_child{toy_suit = ToySuitM} = StatusChild,
							ToyIds = get_active_toy(ToySuitM),
							case lists:all(fun(TGoodId) -> lists:member(TGoodId, ToyIds) end, GoodIdList) of
								true -> true;
								_ -> {false, ?ERRCODE(err165_has_toy_not_active)}
							end;
				_ -> true
			end;
		_ ->
			{false, ?ERRCODE(err150_no_goods)}
	end.

calc_good_id([], Acc) -> Acc;
calc_good_id([{_, GoodId, _Num}|L], Acc) ->
	calc_good_id(L, [GoodId|Acc]);
calc_good_id([_|L], Acc) ->
	calc_good_id(L, Acc).

%% 已经激活的玩具列表
get_active_toy(ToySuitMap) ->
	ToySuits = maps:values(ToySuitMap),
	ToyLists = [ToyList||#toy_suit{toy_list = ToyList}<-ToySuits],
	ToyIds = lists:foldl(fun(ToyList, Acc) ->
							ToyList ++ Acc
						 end, [], ToyLists),
	ToyIds.


%% 玩具分解(废弃, 使用通用的物品分解协议)
decompose_toy(Player, GoodId) ->
	% 判断条件
	% 1.槽位上的不能分解
	% 2.是否有这个物品
	% 3.是否强化了(2,3一起判断)
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{child_sex = ChildSex, toy_list = ToyList} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex},
		{is_on_slot, ToyList, GoodId},
		{has_stren, GoodId}
	],
	case lib_child_util:check_decompose_toy(CheckList, Player) of
		{true, _Player1} ->
			case do_decompose_toy(Player, GoodId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16525, [?SUCCESS]),
					{ok, NewPlayer};
				{false, Res} ->
					lib_child:send_error(Sid, Res),
					{ok, Player}
			end;
		{false, Err} ->
			lib_child:send_error(Sid, Err),
			{ok, Player}
	end.

%% 查询玩具打造位信息
get_toy_forge_info(#player_status{sid = Sid, status_child = StatusChild} = _Player) ->
	#status_child{forge_list = ForgeList} = StatusChild,
	F = fun(ForgeSlot, TList) ->
		case ForgeSlot of
			#forge_slot{
				slot_id    = SlotId,
				start_time = StartTime,
				select_id  = SelectId,
				status     = Status
			} ->
				NewStatus = case Status > ?FORGE_TOY_USING of
					true -> Status;
					_    -> get_new_status(StartTime, SelectId)
	            end,
				[{SlotId, 1, NewStatus, StartTime, SelectId}|TList];
			_ ->
				TList
		end end,
	PackList = lists:foldl(F, [], ForgeList),
	lib_child:send_msg(Sid, 16525, [PackList]).

%% 打造玩具
forge_toy(#player_status{sid = Sid, status_child = StatusChild} = Player, SlotId) ->
	#status_child{child_sex = ChildSex, forge_list = ForgeList} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex},
		{has_open, ForgeList, SlotId},
		{is_using, ForgeList, SlotId}
	],
	case lib_child_util:check_forge_toy(CheckList, Player) of
		{true, _Player1} ->
			NewPlayer = do_forge_toy(Player, SlotId),
			lib_child:send_msg(Sid, 16526, [?SUCCESS]),
			{ok, NewPlayer};
		{false, Error} ->
			lib_child:send_error(Sid, Error),
			{ok, Player}
	end.

%% 领取打造完成的玩具
draw_out_toy(#player_status{sid = Sid, status_child = StatusChild} = Player, SlotId) ->
	#status_child{forge_list = ForgeList} = StatusChild,
	CheckList = [
		{has_open, ForgeList, SlotId},
		{is_finish, ForgeList, SlotId}
	],
	case lib_child_util:check_draw_out_toy(CheckList, Player) of
		{true, _Player1} ->
			case do_draw_out_toy(Player, SlotId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16527, [?SUCCESS]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Error} ->
			lib_child:send_error(Sid, Error),
			{ok, Player}
	end.

%% 打造加速
speed_up_forge(Player, SlotId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{forge_list = ForgeList} = StatusChild,
	NowTime = utime:unixtime(), % 保持检查和实际扣除判断的时间戳相同
	CheckList = [
		{has_open, ForgeList, SlotId},
		{use_status, ForgeList, SlotId, NowTime},
		{cost_enough, ForgeList, SlotId, NowTime}
	],
	case lib_child_util:check_speed_up_forge(CheckList, Player) of
		{true, _Player1} ->
			case do_speed_up(Player, SlotId, NowTime) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16528, [?SUCCESS]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Error} ->
			lib_child:send_error(Sid, Error),
			{ok, Player}
	end.

%% 激活玩具位
active_toy_slot(Player, SlotId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{child_sex = ChildSex, toy_list = ToyList} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {has_active, ToyList, SlotId},
		{cost_enough, SlotId}, {is_toy_slot, SlotId}
	],
	case lib_child_util:check_active_toy_slot(CheckList, Player) of
		{true, _Player1} ->
			case do_active_toy_slot(Player, SlotId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16532, [1, SlotId]),
					{ok, NewPlayer};
				{false, Res} ->
					lib_child:send_error(Sid, Res),
					{ok, Player}
			end;
		{false, Err} ->
			lib_child:send_error(Sid, Err),
			{ok, Player}
	end.

%% 激活打造位
active_forge_slot(Player, SlotId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{child_sex = ChildSex, forge_list = ForgeList} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {has_active, ForgeList, SlotId},
		{cost_enough, SlotId}, {is_forge_slot, SlotId}
	],
	case lib_child_util:check_active_forge_slot(CheckList, Player) of
		{true, _Player1} ->
			case do_active_forge_slot(Player, SlotId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16532, [2, SlotId]),
					{ok, NewPlayer};
				{false, Res} ->
					lib_child:send_error(Sid, Res),
					{ok, Player}
			end;
		{false, Err} ->
			lib_child:send_error(Sid, Err),
			{ok, Player}
	end.

%% 获取玩具套装信息
get_toy_suit_info(#player_status{sid = Sid, status_child = StatusChild} = Player) ->
	#status_child{toy_suit = ToySuits, box_lv = BoxLv} = StatusChild,
	AllSuit = data_child:get_all_toy_suit_cfg(),
	PackList = lists:foldl(fun(SuitType, TPackL) ->
								case maps:get(SuitType, ToySuits, []) of
									#toy_suit{toy_list = ToyList} ->
										TPackL ++ ToyList;
									_ -> TPackL
								end
	                       end, [], AllSuit),
	lib_child:send_msg(Sid, 16533, [BoxLv, PackList]),
	{ok, Player}.

%% 套装奖励信息
get_toy_suit_reward_info(#player_status{sid = Sid, status_child = StatusChild} = Player) ->
	#status_child{toy_suit = ToySuits} = StatusChild,
	AllSuit = data_child:get_all_toy_suit_cfg(),
	PackList = lists:foldl(fun(SuitType, TPackL)->
								case maps:get(SuitType, ToySuits, []) of
									[] ->
										TPackL;
									#toy_suit{toy_list = ToyList, status = Status, time = CollectTime} ->
										Progress = length(ToyList),
										[{SuitType, Progress, Status, CollectTime}|TPackL]
								end
	                       end, [], AllSuit),
	lib_child:send_msg(Sid, 16534, [PackList]),
	{ok, Player}.

%% 领取套装奖励
receive_toy_suit_reward(#player_status{sid = Sid, status_child = StatusChild} = Player, SuitType) ->
	#status_child{child_sex = ChildSex, toy_suit = ToySuits} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {is_suit_type_true, SuitType}, {has_receive, ToySuits, SuitType}],
	case lib_child_util:check_receive_suit_reward(CheckList) of
		true ->
			case do_receive_toy_suit_reward(Player, SuitType) of
				{true, NewPlayer, NewToySuit} ->
					#toy_suit{toy_list = ToyList, status = Status, time = Time} = NewToySuit,
					lib_child:send_msg(Sid, 16535, [SuitType, length(ToyList), Status, Time]),
					{ok, NewPlayer};
				{false, Res} ->
					lib_child:send_error(Sid, Res),
					{ok, Player}
			end;
		{false, Error} ->
			lib_child:send_error(Sid, Error),
			{ok, Player}
	end.

%% 登录初始化玩具数据
init_suit_map(RoleId) ->
	ToysMap = case lib_child_util:get_db_toy(RoleId) of
		[] -> #{};
		ToysInfo ->
			lists:foldl(fun([SuitType, ToyId], TMap) ->
							OToyList = maps:get(SuitType, TMap, []),
							ToyList  = [ToyId|OToyList],
							maps:put(SuitType, ToyList, TMap)
			            end, #{}, ToysInfo)
	end,
	SuitMap = case lib_child_util:get_db_toy_suit(RoleId) of
		[] -> #{};
		SuitsInfo ->
			lists:foldl(fun([SuitType1, Status, CollectTime], TMap1)->
							TToyList = maps:get(SuitType1, ToysMap, []),
							ToySuit  = #toy_suit{type = SuitType1, toy_list = TToyList, status = Status, time = CollectTime},
							maps:put(SuitType1, ToySuit, TMap1)
			            end, #{}, SuitsInfo)
	end,
	SuitMap.

%% 激活玩具
active_toy(#player_status{sid = Sid, status_child = StatusChild} = Player, SuitType, ToyId) ->
	#status_child{child_sex = ChildSex, toy_suit = ToySuits} = StatusChild,
	CheckList = [{has_adopt_child, ChildSex}, {has_active, ToySuits, SuitType, ToyId},
		{is_toy_material_enough, ToyId}, {is_suit_type_true, SuitType, ToyId}],
	case lib_child_util:check_active_toy(CheckList) of
		true ->
			case do_active_toy(Player, SuitType, ToyId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16536, [SuitType, ToyId]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 升级玩具盒子
upgrade_toy_box(#player_status{sid = Sid, status_child = StatusChild} = Player) ->
	#status_child{child_sex = ChildSex, box_lv = BoxLv} = StatusChild,
	CheckList = [{has_adopt_child, ChildSex},{is_upgrade_material_enough, BoxLv}],
	case lib_child_util:check_upgrade_toy_box(CheckList, Player) of
		{true, _Player0} ->
			case do_upgrade_toy_box(Player) of
				{true, NewPlayer, NewLv} ->
					lib_child:send_msg(Sid, 16537, [NewLv]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 获取时装信息
get_fashion_info(#player_status{sid = Sid, status_child = StatusChild} = Player, Type) ->
	#status_child{fashion_list = FashionList, dress_up_fashion = DressUps} = StatusChild,
	PackList = pack_child_fashion(FashionList, DressUps, Type),
	lib_child:send_msg(Sid, 16529, [Type, PackList]),
	{ok, Player}.

pack_child_fashion(FashionList, DressUps, Type) ->
	lists:foldl(fun({FashionId, Lv}, PackL) ->
		case data_child:get_child_fashion_cfg(FashionId, Lv) of
			#child_fashion_cfg{fashion_type = FType} when FType == Type ->
				case lists:keyfind(FashionId, 2, DressUps) of
					{_, _} ->
						[{FashionId, Lv, 1}|PackL];
					_ ->
						[{FashionId, Lv, 0}|PackL]
				end;
			_ -> PackL
		end end,
	[], FashionList).

%% 穿戴时装
put_on_fashion(Player, Type, FashionId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{
		child_sex = ChildSex, fashion_list = FashionList,
		dress_up_fashion = DressUp
	} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {has_fashion, FashionList, FashionId},
		{has_dressed, DressUp, FashionId}, {is_type_true, FashionId, Type}],
	case lib_child_util:check_put_on_fashion(CheckList) of
		true ->
			case do_put_on_fashion(Player, Type, FashionId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16530, [1, Type, FashionId]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 脱下时装
put_off_fashion(Player, Type, FashionId) ->
	#player_status{sid = Sid, status_child = StatusChild} = Player,
	#status_child{
		child_sex = ChildSex, fashion_list = FashionList,
		dress_up_fashion = DressUp
	} = StatusChild,
	CheckList = [
		{has_adopt_child, ChildSex}, {has_fashion, FashionList, FashionId},
		{has_dressed, DressUp, FashionId}, {is_type_true, FashionId, Type}],
	case lib_child_util:check_put_off_fashion(CheckList) of
		true ->
			case do_put_off_fashion(Player, Type, FashionId) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16530, [2, Type, FashionId]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 激活时装
active_fashion(#player_status{sid = Sid, status_child = StatusChild} = Player,
		FashionId, IsAuto) ->
	#status_child{child_sex = ChildSex, child_age_year = ChildAgeYear
		,fashion_list = FashionList} = StatusChild,
	#child_constant_cfg{constant = NeedLv} = data_child:get_child_kv_cfg(skill_open_stage),
	CheckList = [
		{has_adopt_child, ChildSex},
		{is_reach_fashion_open_lv, NeedLv, ChildAgeYear},
		{has_active, FashionList, FashionId}
	],
	case lib_child_util:check_active_fashion(CheckList) of
		true ->
			case do_active_fashion(Player, FashionId, IsAuto) of
				{true, NewPlayer} ->
					lib_child:send_msg(Sid, 16531, [1, FashionId, 1]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 升级时装
upgrade_fashion(#player_status{sid = Sid, status_child = StatusChild} = Player,
		FashionId, IsAuto) ->
	#status_child{fashion_list = FashionList} = StatusChild,
	CheckList = [{max_lv, FashionList, FashionId}],  %% 消耗检查直接放在扣除物品时
	case lib_child_util:check_upgrade_fashion(CheckList) of
		true ->
			case do_upgrade_fashion(Player, FashionId, IsAuto) of
				{true, NewPlayer, NewLv} ->
					lib_child:send_msg(Sid, 16531, [2, FashionId, NewLv]),
					{ok, NewPlayer};
				{false, Err} ->
					lib_child:send_error(Sid, Err),
					{ok, Player}
			end;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			{ok, Player}
	end.

%% 时装格式转换
trans_to_fashion(FashionInfos) ->
	F = fun([FashionId, Lv, State], {FashionL, DressUps}) ->
		case data_child:get_child_fashion_cfg(FashionId, Lv) of
			#child_fashion_cfg{fashion_type = Type} -> ok;
			_ -> Type = 0
		end,
		NFashionL = lists:keystore(FashionId, 1, FashionL, {FashionId, Lv}),
		case State == 1 of
			true ->
				NDressUps = lists:keystore(FashionId, 2, DressUps, {Type, FashionId});
			_ ->
				NDressUps = DressUps
		end,
		{NFashionL, NDressUps}
	end,
	lists:foldl(F, {[], []}, FashionInfos).

%% 玩具位开启条件:1.免费 2.条件开启
check_if_active_toy_slot(#player_status{sid = Sid, status_child = StatusChild} = Player, Type) ->
	case Type of
		?SLOT_OPEN_TYPE_CHILD_AGE ->
			NewToyList = check_content(Player, Type);
		% ?SLOT_OPEN_TYPE_HEART_LV ->
		% 	NewToyList = check_content(Player, Type);
		?SLOT_OPEN_TYPE_ROLE_LV ->
			NewToyList = check_content(Player, Type);
		?SLOT_OPEN_TYPE_OPEN_DAY ->
			NewToyList = check_content(Player, Type);
		?SLOT_OPEN_TYPE_FREE ->
			NewToyList = check_content(Player, Type)
	end,
	NewStatusChild = StatusChild#status_child{toy_list = NewToyList},
	ActiveToyL = NewToyList -- StatusChild#status_child.toy_list,
	[lib_child:send_msg(Sid, 16532, [1, SlotId])||{SlotId, _} <- ActiveToyL],
	NewPlayer = Player#player_status{status_child = NewStatusChild},
	{ok, NewPlayer}.

%% 打造位开启条件:1.免费 2.付费(免费配置为空)
check_if_active_forge_slot(#player_status{id = RoleId, status_child = StatusChild} = Player) ->
	#status_child{forge_list = ForgeList} = StatusChild,
	NewForgeList = check_two(ForgeList, RoleId),
	ActiveForgeL = NewForgeList -- StatusChild#status_child.forge_list,
	[lib_child:send_msg(Player#player_status.sid, 16532, [2, SlotId])||#forge_slot{slot_id = SlotId} <- ActiveForgeL],
	NewStatusChild = StatusChild#status_child{forge_list = NewForgeList},
	NewPlayer = Player#player_status{status_child = NewStatusChild},
	{ok, NewPlayer}.

check_content(Player, Type) ->
	#player_status{id = RoleId, status_child = StatusChild, figure = Figure} = Player,
	#status_child{child_age_month = AgeMonth, toy_list = ToyList
	} = StatusChild,
	case Type of
		?SLOT_OPEN_TYPE_CHILD_AGE ->
			CheckType = child_age,
			Data      = AgeMonth;
		?SLOT_OPEN_TYPE_ROLE_LV ->
			CheckType = role_lv,
			Data      = Figure#figure.lv;
		?SLOT_OPEN_TYPE_OPEN_DAY ->
			CheckType = open_day,
			Data      = util:get_open_day();
		?SLOT_OPEN_TYPE_FREE ->
			CheckType = free,
			Data      = 0
	end,
	NewToyList = check_one(ToyList, CheckType, Data, RoleId),
	NewToyList.

check_one(ToyList, CheckType, Data, RoleId) ->
	F = fun(TSlotId, TList) ->
		case data_child:get_toy_slot_open_cfg(TSlotId) of
			[{CheckType, _Need}] when CheckType == free -> [TSlotId|TList];
			[{CheckType, Need}] ->
				case Data >= Need of
					true ->
						[TSlotId|TList];
					_ -> TList
				end;
			_ -> TList
		end end,
	case lists:foldl(F, [], ?TOY_SLOTS) of
		[_|_] = OpenL ->
			F1 = fun(SlotId, TList1) ->
				case lists:keyfind(SlotId, 1, TList1) of
					{_, _} -> TList1;
					_ ->
						lib_child_util:save_db_toy_slot_info(RoleId, SlotId, 0),
						lists:keystore(SlotId, 1, TList1, {SlotId, 0})
				end end,
			NewToyList = lists:foldl(F1, ToyList, OpenL);
		_ -> NewToyList = ToyList
	end,
	NewToyList.

check_two(ForgeList, RoleId) ->
	F = fun(TSlotId, TList) ->
		case data_child:get_toy_forge_slot_cfg(TSlotId) of
			[] ->
				[TSlotId|TList];
			_ -> TList
		end end,
	case lists:foldl(F, [], ?FORGE_SLOTS) of
		[_|_] = CanOpenL ->
			F1 = fun(SlotId, TList1) ->
				case lists:keyfind(SlotId, #forge_slot.slot_id, TList1) of
					#forge_slot{} -> TList1;
					_ ->
						lib_child_util:save_db_forge_slot_info(RoleId, SlotId, 0, 0, 0) ,
						lists:keystore(SlotId, #forge_slot.slot_id, TList1, #forge_slot{slot_id = SlotId})
				end end,
			NewForgeList = lists:foldl(F1, ForgeList, CanOpenL);
		_ ->
			NewForgeList = ForgeList
	end,
	NewForgeList.

%% ----------------------------- builtin function ----------------------------
do_active_vehicle(#player_status{id = RoleId, status_child = StatusChild} = Player, VehicleId) ->
	#status_child{vehicle = VehicleList, passive_skill = PassiveSkills} = StatusChild,
	#child_vehicle_cfg{upgrade_cost = CostList} = data_child:get_child_vehicle_cfg(VehicleId, 1),
	case lib_goods_api:cost_object_list_with_check(Player, CostList, child_active_vehicle_cost, "") of
		{true, Player1, _} ->
			NewVehicleList = lists:keystore(VehicleId, 1, VehicleList, {VehicleId, 1}),
			%% 检查是否激活被动技能
			case lib_child:check_if_active_passive_skill(VehicleId, 1) of
				TPassiveSkill when TPassiveSkill > 0 ->
					NewPassiveSkills = ?IF(lists:member(TPassiveSkill, PassiveSkills) == true, PassiveSkills, [TPassiveSkill|PassiveSkills]);
				_ ->
					NewPassiveSkills = PassiveSkills
			end,
			%% 计算玩具可载重
			% CanCarry = calc_can_carry_weight(Year, Month, NewVehicleList, FashionList),
			NewStatusChild = StatusChild#status_child{vehicle = NewVehicleList, passive_skill = NewPassiveSkills},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, active_child_vehicle),

			lib_child_util:save_db_child_vehicle(RoleId, VehicleId, 1),
			%% 更新场景信息
			AddPassive = [{SkillId, 1} || SkillId <- NewPassiveSkills--PassiveSkills],
			case AddPassive =/= [] of
				true ->
					mod_scene_agent:update(RefreshPlayer, [{passive_skill, AddPassive},
						{battle_attr, RefreshPlayer#player_status.battle_attr}]);
				_ ->
					mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}])
			end,
			lib_log_api:log_child_item(RoleId, 0, 1, VehicleId, CostList, 0, 1),

			case CostList of
				[{_, GoodsId, CostNum}|_] -> ok;
				_ -> GoodsId = 0, CostNum = 0
			end,
			ta_agent_fire:child_vehicle(RefreshPlayer, [GoodsId, 1, CostNum]),
			{true, RefreshPlayer};
		{false, ErrCode, _Player2} ->
			{false, ErrCode}
	end.

do_upgrade_child_vehicle(#player_status{id = RoleId, status_child = StatusChild} = Player, VehicleInfo) ->
	#status_child{vehicle = VehicleList, passive_skill = PassiveSkills} = StatusChild,
	{VehicleId, Stage} = VehicleInfo,
	#child_vehicle_cfg{upgrade_cost = CostList} = data_child:get_child_vehicle_cfg(VehicleId, Stage + 1),
	case lib_goods_api:cost_object_list_with_check(Player, CostList, child_upgrade_vehicle_cost, "") of
		{true, Player1, _} ->
			NewVehicleInfo = {VehicleId, Stage + 1},
			NewVehicleList = lists:keystore(VehicleId, 1, VehicleList, NewVehicleInfo),
			%% 检查是否激活被动技能
			case lib_child:check_if_active_passive_skill(VehicleId, Stage + 1) of
				TPassiveSkill when TPassiveSkill > 0 ->
					NewPassiveSkills = ?IF(lists:member(TPassiveSkill, PassiveSkills) == true, PassiveSkills, [TPassiveSkill|PassiveSkills]);
				_ ->
					NewPassiveSkills = PassiveSkills
			end,
			%% 计算玩具可载重
			% CanCarry = calc_can_carry_weight(Year, Month, NewVehicleList, FashionList),
			NewStatusChild = StatusChild#status_child{vehicle = NewVehicleList, passive_skill = NewPassiveSkills},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			%% 计算属性
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, upgrade_child_vehicle),
			%% 存库
			lib_child_util:update_db_child_vehicle(RoleId, VehicleId, Stage + 1),
			%% 更新场景信息
			AddPassive = [{SkillId, 1} || SkillId <- NewPassiveSkills--PassiveSkills],
			case AddPassive =/= [] of
				true ->
					mod_scene_agent:update(RefreshPlayer, [{passive_skill, AddPassive},
						{battle_attr, RefreshPlayer#player_status.battle_attr}]);
				_ ->
					mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}])
			end,
			lib_log_api:log_child_item(RoleId, 1, 1, VehicleId, CostList, Stage, Stage+1),
			case CostList of
				[{_, GoodsId, CostNum}|_] -> ok;
				_ -> GoodsId = 0, CostNum = 0
			end,
			ta_agent_fire:child_vehicle(RefreshPlayer, [GoodsId, Stage+1, CostNum]),
			{true, RefreshPlayer, NewVehicleInfo};
		{false, ErrCode, _Player2} ->
			{false, ErrCode}
	end.

do_inlay_toy(Player, SlotId, GoodId) ->
	#player_status{id = RoleId, status_child = StatusChild} = Player,
	#status_child{toy_list = ToyList} = StatusChild,
	NewToyList = lists:keystore(SlotId, 1, ToyList, {SlotId, GoodId}),
	ToyWeight  = calc_toy_weight(NewToyList),
	NewStatusChild = StatusChild#status_child{toy_list = NewToyList, carry_weight = ToyWeight},
	case change_toy_bag_to_slot(RoleId, ToyList, SlotId, GoodId) of
		true ->
			lib_child_util:update_db_toy_slot_info(RoleId, SlotId, GoodId),
			NewPlayer = Player#player_status{status_child = NewStatusChild},
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, inlay_toy),
			mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
			{true, RefreshPlayer};
		_ ->
			{false, ?FAIL}
	end.

change_toy_bag_to_slot(RoleId, ToyList, SlotId, GoodId) ->
	GoodsStatus = lib_goods_do:get_goods_status(),
	{_, OldGoodId} = lists:keyfind(SlotId, 1, ToyList),
	InGoodsInfo  = lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG),
	OutGoodsInfo = lib_goods_api:get_goods_info(OldGoodId, ?GOODS_LOC_CHILD_TOY_SLOT),
	LocBag   = ?GOODS_LOC_CHILD_TOY_BAG,
	LocEquip = ?GOODS_LOC_CHILD_TOY_SLOT,

	lib_goods_dict:start_dict(),
	case is_record(OutGoodsInfo, goods) of
		true ->
			F = fun() ->
				%% 原本槽位上有玩具则替换位置
				[_NewOutGoodsInfo, GoodsStatus1]  = goods_logic:change_goods_location(OutGoodsInfo, LocBag, 0, GoodsStatus),
				[_NewInGoodsInfo, NewGoodsStatus] = goods_logic:change_goods_location(InGoodsInfo, LocEquip, SlotId, GoodsStatus1),
				[NewGoodsStatus]
			end;
		false ->
			F = fun() ->
				[_NewInGoodsInfo, NewGoodsStatus] = goods_logic:change_goods_location(InGoodsInfo, LocEquip, SlotId, GoodsStatus),
				[NewGoodsStatus]
			end
	end,
	case catch goods_transaction:transaction(F) of
		[FNewGoodsStatus] ->
			#goods_status{dict = Dict} = FNewGoodsStatus,
			{NewGoodsDict, GoodsL} = lib_goods_dict:handle_dict_and_notify(Dict),
			NewGoodsStatus1 = FNewGoodsStatus#goods_status{dict = NewGoodsDict},
			lib_goods_do:set_goods_status(NewGoodsStatus1),
			lib_goods_api:notify_client_num(RoleId, [InGoodsInfo#goods{num = 0}]),
			lib_goods_api:notify_client(RoleId, GoodsL, child_equipment),
			true;
		_ ->
			false
	end.

do_load_off_toy(Player, SlotId) ->
	#player_status{id = RoleId, status_child = StatusChild} = Player,
	#status_child{toy_list = ToyList} = StatusChild,
	NewToyList = lists:keystore(SlotId, 1, ToyList, {SlotId, 0}),
	ToyWeight  = calc_toy_weight(NewToyList),
	NewStatusChild = StatusChild#status_child{toy_list = NewToyList, carry_weight = ToyWeight},
	case change_toy_slot_to_bag(RoleId, ToyList, SlotId) of
		true ->
			lib_child_util:update_db_toy_slot_info(RoleId, SlotId, 0),
			NewPlayer = Player#player_status{status_child = NewStatusChild},
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, inlay_toy),
			mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
			{true, RefreshPlayer};
		_ ->
			{false, ?FAIL}
	end.

change_toy_slot_to_bag(RoleId, ToyList, SlotId) ->
	GoodsStatus = lib_goods_do:get_goods_status(),
	{_, OldGoodId} = lists:keyfind(SlotId, 1, ToyList),
	OutGoodsInfo = lib_goods_api:get_goods_info(OldGoodId, ?GOODS_LOC_CHILD_TOY_SLOT),
	LocBag   = ?GOODS_LOC_CHILD_TOY_BAG,

	lib_goods_dict:start_dict(),
	case is_record(OutGoodsInfo, goods) of
		true ->
			F = fun() ->
				[_NewOutGoodsInfo, GoodsStatus1] = goods_logic:change_goods_location(OutGoodsInfo, LocBag, 0, GoodsStatus),
				[GoodsStatus1]
			end,
			case catch goods_transaction:transaction(F) of
				[TNewGoodsStatus] ->
					#goods_status{dict = Dict} = TNewGoodsStatus,
					{NewGoodsDict, GoodsL} = lib_goods_dict:handle_dict_and_notify(Dict),
					NewGoodsStatus = TNewGoodsStatus#goods_status{dict = NewGoodsDict},
					lib_goods_do:set_goods_status(NewGoodsStatus),
					lib_goods_api:notify_client_num(RoleId, [OutGoodsInfo#goods{num = 0}]),
					lib_goods_api:notify_client(RoleId, GoodsL, child_equipment),
					true;
				_ ->
					skip
			end;
		false ->
			skip
	end.

do_stren_toy(Player, GoodId, AimLv) ->
	#player_status{id = _RoleId, status_child = StatusChild} = Player,
	#status_child{toy_list = ToyList} = StatusChild,
	GoodInfo =
	case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
		#goods{} = TGoodInfo -> TGoodInfo;
		_ ->
			case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_SLOT) of
				#goods{} = TGoodInfo -> TGoodInfo;
				_ -> []
			end
	end,
	case GoodInfo of
		#goods{goods_id = GoodTypeId, extra_data = ExtraData} ->
			case ExtraData of
				[{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}] -> ok;
				_ -> Lv = 0
			end,
			#child_toy_cfg{toy_color = Color, toy_type = Type} = data_child:get_child_toy_cfg(GoodTypeId),
			% #child_toy_stren_cfg{cost = [{_, GoodTypeId, _}]} = data_child:get_toy_stren_cfg(Type, Color, Lv),
			CanLv = calc_can_stren_lv(Player, Type, Color, Lv, [], true),
			NewLv = ?IF(CanLv >= AimLv, AimLv, CanLv),
			CostList = calc_cost(Type, Color, Lv, NewLv, []),
			case lib_goods_api:cost_object_list(Player, CostList, toy_stren_cost, "") of
				{true, Player2} ->
					case goods_logic:update_goods_info_extra([GoodInfo#goods{extra_data = [{?GOODS_OTHER_KEY_CHILD_TOY_LV, NewLv}]}]) of
						{ok, [_NewGoodsInfo|_]} ->
							%% 重新计算属性
							case lists:keyfind(GoodId, 2, ToyList) of
								{_, _} ->
									% lib_log_api:log_child_toy(RoleId, GoodTypeId, GoodInfo#goods.id, NewLv, 1),
									NewPlayer = lib_child:refresh_attr(Player2, stren_child_toy),
									mod_scene_agent:update(NewPlayer, [{battle_attr, NewPlayer#player_status.battle_attr}]),
									{true, NewPlayer, NewLv};
								_ ->
									% lib_log_api:log_child_toy(RoleId, GoodTypeId, GoodInfo#goods.id, 0, 0),
									{true, Player2, NewLv}
							end;
						Res ->
							{false, Res}
					end;
				{false, ErrCode, _Player2} ->
					{false, ErrCode}
			end;
		_ ->
			{false, ?ERRCODE(err165_has_not_toy)}
	end.

calc_can_stren_lv(_Player, _Type, _Color, Lv, _CostList, Ack)
	when Ack == false -> Lv-1;
calc_can_stren_lv(Player, Type, Color, TLv, TACostList, _TAck) ->
	case data_child:get_toy_stren_cfg(Type, Color, TLv+1) of
		#child_toy_stren_cfg{cost = CostList} -> skip;
		_ -> CostList = []
	end,
	case CostList == [] of
		true ->
			NewCostList = TACostList,
			Ack = false;
		_ ->
			NewCostList = combine_cost(TACostList, CostList),
			Ack = ensure_material_enough(Player, NewCostList)
	end,
	calc_can_stren_lv(Player, Type, Color, TLv+1, NewCostList, Ack).

ensure_material_enough(Player, CostList) ->
	F = fun({_, GoodTypeId, NeedNum}, TList) ->
		[{_, OwnNum}] = lib_goods_api:get_goods_num(Player, [GoodTypeId]),
		case OwnNum >= NeedNum of
			true ->
				[1|TList];
			_ ->
				[0|TList]
		end
	end,
	length(CostList) == lists:sum(lists:foldl(F, [], CostList)).

calc_cost(_Type, _Color, AimLv, AimLv, CostList) ->
	CostList;
calc_cost(Type, Color, NowLv, AimLv, TCostList) ->
	case data_child:get_toy_stren_cfg(Type, Color, NowLv+1) of
		#child_toy_stren_cfg{cost = [{GoodType, GoodTypeId, NeedNum}]} ->
			case lists:keyfind(GoodTypeId, 2, TCostList) of
				{_, _, Num} ->
					NewCostList = lists:keystore(GoodTypeId, 2, TCostList, {GoodType, GoodTypeId, Num + NeedNum});
				_ ->
					NewCostList = lists:keystore(GoodTypeId, 2, TCostList, {GoodType, GoodTypeId, NeedNum})
			end;
		_ ->
			NewCostList = TCostList
	end,
	calc_cost(Type, Color, NowLv + 1, AimLv, NewCostList).

combine_cost(AllCostList, CostList) ->
	F = fun({GoodType, GoodTypeId, Num}, TACostList) ->
		case lists:keyfind(GoodTypeId, 2, TACostList) of
			{_, _, OldNum} ->
				lists:keystore(GoodTypeId, 2, TACostList, {GoodType, GoodTypeId, Num + OldNum});
			_ ->
				lists:keystore(GoodTypeId, 2, TACostList, {GoodType, GoodTypeId, Num})
		end
	end,
	lists:foldl(F, AllCostList, CostList).

do_compose_toy(Player, GoodsList) ->
%%	%% 筛选玩具类型
%%	Filter = fun(TGoodId, TList) ->
%%		case lib_goods_api:get_goods_info(TGoodId) of
%%			#goods{goods_id = GoodTypeId} ->
%%				case data_child:get_child_toy_cfg(GoodTypeId) of
%%					#child_toy_cfg{toy_type = Type} ->
%%						case lists:keyfind(Type, 1, TList) of
%%							{_, TNum} -> lists:keystore(Type, 1, TList, {Type, TNum+1});
%%							_         -> lists:keystore(Type, 1, TList, {Type, 1})
%%						end;
%%					_ -> TList
%%				end;
%%			_ -> TList
%%		end end,
%%	FList = lists:foldl(Filter, [], GoodsList),
%%	WeightL = [{Weight, T}||{T, Weight}<- FList],
%%	%% 按权重随机筛选出合成后的玩具类型
%%	case urand:rand_with_weight(WeightL) of
%%		ToyType when is_integer(ToyType) ->
%%			skip;
%%		_ ->
%%			{ToyType, _} = lists:last(FList)
%%	end,
	GoodId = urand:list_rand(GoodsList),
	case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
		#goods{goods_id = GoodTypeId, extra_data = OExtraData} ->
			case OExtraData of
				[{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}] -> skip;
				_ -> Lv = 0
			end,
			#child_toy_cfg{toy_type = Type, toy_color = Color} = data_child:get_child_toy_cfg(GoodTypeId),
			case data_child:get_child_toy_cfg2(Type, Color+1) of
				[ToyId] ->
					%% 计算返还的强化材料
					SumCostList = return_stren_material(GoodsList),
					Produce = Produce = #produce{
						title = utext:get(13), content = utext:get(14),
						type = toy_stren_material_return, reward = SumCostList,
						show_tips = 1, remark = ""
					},
					%% 发放合成物品
					ExtraData = [{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}],
					Produce1 = #produce{
						title = utext:get(13), content = utext:get(14),
						type = compose_toy, reward = [{?TYPE_ATTR_GOODS, ToyId, 1, [{extra_data, ExtraData}]}],
						show_tips = 1, remark = ""
					},
					DeleteList = get_delete_list(GoodsList),
					case lib_goods_api:delete_more_by_list(Player, DeleteList, child_toy_compose) of
						1 ->
							case SumCostList of
								[] -> Player1 = Player;
								_ -> {ok, Player1} = lib_goods_api:send_reward_with_mail(Player, Produce)
							end,
							{ok, Player2} = lib_goods_api:send_reward_with_mail(Player1, Produce1),
							{true, Player2};
						ErrorCode ->
							{false, ErrorCode}
					end;
				_ ->
					{false, ?ERRCODE(err165_toy_max_color)}
			end;
		_ ->
			{false, ?ERRCODE(err165_has_not_toy)}
	end.

return_stren_material(GoodsList) ->
	F = fun(GoodId, TMaterialL) ->
		GoodInfo =
		case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
			#goods{} = TGoodInfo -> TGoodInfo;
			_ ->
				case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_SLOT) of
					#goods{} = TGoodInfo -> TGoodInfo;
					_ -> []
				end
		end,
		case GoodInfo of
			#goods{goods_id = GoodTypeId, extra_data =  ExtraData} ->
				case ExtraData of
					[{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}] when Lv > 0 ->
						CostList = calc_stren_material(GoodTypeId, 1, Lv, []);
					_ ->
						CostList = []
				end,
				CostList++TMaterialL;
			_ ->
				TMaterialL
		end
	end,
	lists:foldl(F, [], GoodsList).

calc_stren_material(GoodTypeId, TLv, Lv, CostList) when TLv == Lv ->
	case data_child:get_child_toy_cfg(GoodTypeId) of
		#child_toy_cfg{toy_type = Type, toy_color = Color} ->
			case data_child:get_toy_stren_cfg(Type, Color, TLv) of
				#child_toy_stren_cfg{cost = [{?TYPE_GOODS, MaterialId, CNum}]} ->
					case lists:keyfind(MaterialId, 2, CostList) of
						{_, _, Num} -> lists:keystore(MaterialId, 2, CostList, {?TYPE_GOODS, MaterialId, Num + CNum});
						_ -> lists:keystore(MaterialId, 2, CostList, {?TYPE_GOODS, MaterialId, CNum})
					end;
				_ -> CostList
			end;
		_ -> CostList
	end;
calc_stren_material(GoodTypeId, TLv, Lv, TCostList) ->
	case data_child:get_child_toy_cfg(GoodTypeId) of
		#child_toy_cfg{toy_type = Type, toy_color = Color} ->
			case data_child:get_toy_stren_cfg(Type, Color, TLv) of
				#child_toy_stren_cfg{cost = [{?TYPE_GOODS, MaterialId, CNum}]} ->
					NewCostList =
					case lists:keyfind(MaterialId, 2, TCostList) of
						{_, _, Num} -> lists:keystore(MaterialId, 2, TCostList, {?TYPE_GOODS, MaterialId, Num + CNum});
						_ -> lists:keystore(MaterialId, 2, TCostList, {?TYPE_GOODS, MaterialId, CNum})
					end,
					calc_stren_material(GoodTypeId, TLv+1, Lv, NewCostList);
				_ ->
					calc_stren_material(GoodTypeId, TLv+1, Lv, TCostList)
			end;
		_ ->
			calc_stren_material(GoodTypeId, TLv+1, Lv, TCostList)
	end.

get_delete_list(GoodsList) ->
	F = fun(GoodId, TList) ->
		GoodInfo =
		case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
			#goods{} = TGoodInfo -> TGoodInfo;
			_ ->
				case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_SLOT) of
					#goods{} = TGoodInfo -> TGoodInfo;
					_ -> []
				end
		end,
		case GoodInfo of
			#goods{goods_id = GoodTypeId} ->
				[{GoodId, GoodTypeId, 1}|TList];
			_ -> TList
		end
	end,
	lists:foldl(F, [], GoodsList).

do_decompose_toy(Player, GoodId) -> %%不使用
	GoodsInfo =
	case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_BAG) of
		#goods{} = TGoodInfo -> TGoodInfo;
		_ ->
			case lib_goods_api:get_goods_info(GoodId, ?GOODS_LOC_CHILD_TOY_SLOT) of
				#goods{} = TGoodInfo -> TGoodInfo;
				_ -> []
			end
	end,
	#goods{goods_id = GoodTypeId, extra_data = [{?GOODS_OTHER_KEY_CHILD_TOY_LV, Lv}]} = GoodsInfo,
	StrenCostList = calc_stren_material(GoodTypeId, 1, Lv, []),
	Produce = #produce{
		title = utext:get(13), content = utext:get(14),
		type = thank_reward, reward = StrenCostList,
		show_tips = 1, remark = ""
	},
	case lib_goods_api:delete_one_by_id(Player, GoodId, GoodTypeId, 1, decompose_child_toy) of
		1 ->
			{ok, Player1} = lib_goods_api:send_reward_with_mail(Player, Produce),
			{true, Player1};
		_ ->
			{false, ?FAIL}
	end.

do_forge_toy(Player, SlotId) ->
	#player_status{id = RoleId, status_child = StatusChild} = Player,
	#status_child{forge_list = ForgeList} = StatusChild,
	ForgeSlot = lists:keyfind(SlotId, #forge_slot.slot_id, ForgeList),
	SelectId  = rand_select_id(),
	StartTime = utime:unixtime(),
	Status    = ?FORGE_TOY_USING,
	NewForgeSlot = ForgeSlot#forge_slot{select_id = SelectId, status = Status, start_time = StartTime},
	NewForgeList = lists:keystore(SlotId, #forge_slot.slot_id, ForgeList, NewForgeSlot),
	lib_child_util:update_db_toy_forge_slot(RoleId, SlotId, StartTime, Status, SelectId),
	NewStatusChild = StatusChild#status_child{forge_list = NewForgeList},
	NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player),
	NewPlayer.

rand_select_id() ->
	SelectL = data_child:get_all_forge_time_cfg(),
	SelectId = urand:list_rand(SelectL),
	SelectId.

get_new_status(StartTime, SelectId) ->
	case data_child:get_toy_forge_time_cfg(SelectId) of
		#toy_forge_time_cfg{need_time = NeedTime} -> ok;
		_ -> NeedTime = 0
	end,
	NowTime = utime:unixtime(),
	if
		StartTime > 0 andalso NowTime >= (StartTime + NeedTime)  ->
			Status = ?FORGE_TOY_FINISH;
		StartTime > 0 ->
			Status = ?FORGE_TOY_USING;
		true ->
			Status = ?FORGE_TOY_WAIT
	end,
	Status.

do_draw_out_toy(Player, SlotId) ->
	#player_status{id = RoleId, status_child = StatusChild} = Player,
	#status_child{forge_list = ForgeList} = StatusChild,
	#forge_slot{select_id = SelectId} = lists:keyfind(SlotId, #forge_slot.slot_id, ForgeList),
	%% 根据库Id筛选奖励
	case get_toy_by_select_id(RoleId, SelectId) of
		[] -> {false, ?ERRCODE(err165_select_id_err)};
		Reward ->
			Produce = #produce{
				title = utext:get(11), content = utext:get(12),
				type = thank_reward, reward = Reward,
				show_tips = 1, remark = ""
			},
			{ok, Player1} =  lib_goods_api:send_reward_with_mail(Player, Produce, ?MOD_CHILD),
			%% 重置打造位状态
			ForgeSlot = #forge_slot{slot_id = SlotId},
			NewForgeList = lists:keystore(SlotId, #forge_slot.slot_id, ForgeList, ForgeSlot),
			NewStatusChild = StatusChild#status_child{forge_list = NewForgeList},
			lib_child_util:update_db_toy_forge_slot(RoleId, SlotId, 0, 0, 0),
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			{true, NewPlayer}
	end.

get_toy_by_select_id(RoleId, SelectId) ->
	SelectCount = lib_counter:get_count(RoleId, ?MOD_CHILD, 1),
	RewardIdL = data_child:get_reward_by_id_cfg(SelectId),
	LeastCountL = data_child:get_all_least_count_cfg(),
	ReachCountL = [TCount || TCount <- LeastCountL, TCount =< SelectCount],
	F = fun(TRewardId, TList) ->
		F1 = fun(LCount, TList1) ->
			case data_child:get_toy_forge_content_cfg(SelectId, TRewardId, LCount) of
				#toy_forge_content_cfg{weight = Weight} ->
					[{Weight, {SelectId, TRewardId, LCount}}|TList1];
				_ ->
					TList1
			end
		end,
		lists:foldl(F1, TList, ReachCountL)
	end,
	CfgL = lists:foldl(F, [], RewardIdL),
	case urand:rand_with_weight(CfgL) of
		{SelectId, RewardId, Count} ->
			#toy_forge_content_cfg{reward_content = Reward} = data_child:get_toy_forge_content_cfg(SelectId, RewardId, Count);
		_ ->
			Reward = []
	end,
	Reward.

do_speed_up(#player_status{id = RoleId, status_child = StatusChild} = Player, SlotId, NowTime) ->
	#status_child{forge_list = ForgeList} = StatusChild,
	case lists:keyfind(SlotId, #forge_slot.slot_id, ForgeList) of
		#forge_slot{start_time = StartTime, select_id = SelectId} = ForgeSlot ->
			#toy_forge_time_cfg{need_time = NeedTime} = data_child:get_toy_forge_time_cfg(SelectId),
			LeftTime = StartTime + NeedTime - NowTime,
			case data_child:get_toy_speed_up_cfg(LeftTime) of
				[] ->  %% 直接加速
					NewForgeSlot = ForgeSlot#forge_slot{status = ?FORGE_TOY_FINISH},
					NewForgeList = lists:keystore(SlotId, #forge_slot.slot_id, ForgeList, NewForgeSlot),
					NewStatusChild = StatusChild#status_child{forge_list = NewForgeList},
					lib_child_util:update_db_toy_forge_slot(RoleId, SlotId, StartTime, ?FORGE_TOY_FINISH, SelectId),
					NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player),
					{true, NewPlayer};
				[{PriceType, Price}] ->
					case lib_goods_api:cost_money(Player, PriceType, Price, toy_speed_up_cost, "") of
						{1, Player1} ->
							NewForgeSlot = ForgeSlot#forge_slot{status = ?FORGE_TOY_FINISH},
							NewForgeList = lists:keystore(SlotId, #forge_slot.slot_id, ForgeList, NewForgeSlot),
							NewStatusChild = StatusChild#status_child{forge_list = NewForgeList},
							lib_child_util:update_db_toy_forge_slot(RoleId, SlotId, StartTime, ?FORGE_TOY_FINISH, SelectId),
							NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
							{true, NewPlayer};
						{ErrCode, _Player1} ->
							{false, ErrCode}
					end
			end;
		_ ->
			{false, ?ERRCODE(err165_forge_slot_not_open)}
	end.

get_all_toy_list() ->
	GoodsStatus = lib_goods_do:get_goods_status(),
	get_all_toy_list(GoodsStatus).
get_all_toy_list(GoodsStatus) ->
	lib_goods_util:get_goods_list(GoodsStatus#goods_status.dict, ?GOODS_LOC_CHILD_TOY_BAG).

do_put_on_fashion(#player_status{id = RoleId, status_child = StatusChild} = Player, Type, FashionId) ->
	#status_child{dress_up_fashion = DressUps} = StatusChild,
	Ack =
	case lists:keyfind(Type, 1, DressUps) of
		{_, OldFashionId} ->
			F = fun() ->
				lib_child_util:update_db_fashion_state(RoleId, FashionId, 1),
				lib_child_util:update_db_fashion_state(RoleId, OldFashionId, 0),
				ok
			end,
			case catch db:transaction(F) of
				ok -> true;
				_  -> false
			end;
		_ ->
			lib_child_util:update_db_fashion_state(RoleId, FashionId, 1),
			true
	end,
	case Ack == true of
		true ->
			NewDressUps = lists:keystore(Type, 1, DressUps, {Type, FashionId}),
			NewStatusChild = StatusChild#status_child{dress_up_fashion = NewDressUps},
			%% 更新玩家身上数据
			Player1 = lib_child:update_player_figure(child_fashion, Player, NewDressUps),
			%% 更新场景数据
			mod_scene_agent:update(Player1, [{child_fashion, NewDressUps}]),
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			%% 场景广播
			lib_child:broadcast_to_scene(NewPlayer),
			{true, NewPlayer};
		_ ->
			{false, ?FAIL}
	end.

do_put_off_fashion(#player_status{id = RoleId, status_child = StatusChild} = Player, Type, FashionId) ->
	#status_child{dress_up_fashion = DressUps} = StatusChild,
	lib_child_util:update_db_fashion_state(RoleId, FashionId, 0),
	NewDressUps = lists:keydelete(Type, 1, DressUps),
	NewStatusChild = StatusChild#status_child{dress_up_fashion = NewDressUps},
	%% 更新玩家身上数据
	Player1 = lib_child:update_player_figure(child_fashion, Player, NewDressUps),
	%% 更新场景数据
	mod_scene_agent:update(Player1, [{child_fashion, NewDressUps}]),
	NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
	%% 场景广播
	lib_child:broadcast_to_scene(NewPlayer),
	{true, NewPlayer}.

do_active_fashion(#player_status{id = RoleId, status_child = StatusChild} = Player,
		FashionId, IsAuto) ->
	#status_child{fashion_list = FashionList} = StatusChild,
	NewFashionList = lists:keystore(FashionId, 1, FashionList, {FashionId, 1}),
	#child_fashion_cfg{upgrade_cost = CostList, buy_type = BuyType, buy_price = BuyPrice}
		= data_child:get_child_fashion_cfg(FashionId, 1),
	Result = case lib_goods_api:cost_object_list_with_check(Player, CostList, active_child_fashion, "") of
		{true, Player1, _} ->
			{true, Player1};
		{false, Res, _Player1} ->
			case BuyType == 0 andalso IsAuto == 1 of
				true -> % 直接购买
					{MType, _, Money} = BuyPrice,
					[{GType, _, NeedNum}] = CostList,
					[{_, OwnNum}] = lib_goods_api:get_goods_num(Player, [FashionId]),
					CostMoney = (NeedNum - OwnNum) * Money,
					Return = case OwnNum > 0 of
						true ->
							case lib_goods_api:cost_object_list(Player, [{GType, FashionId, OwnNum}], active_child_fashion, "") of
								{true, Player1} -> Player1;
								{false, Err, _} -> {false, Err}
							end;
						_ -> Player
					end,
					case Return of
						{false, Err1} -> {false, Err1};
						RPlayer when is_record(RPlayer, player_status) ->
							case lib_goods_api:cost_money(RPlayer, MType, CostMoney, active_child_fashion, "") of
								{1, TPlayer} ->
									{true, TPlayer};
								_ ->
									TErr = case MType of
										       1 -> ?ERRCODE(gold_not_enough);
										       _ -> ?ERRCODE(bgold_not_enough)
									       end,
									{false, TErr}
							end;
						_ -> {false, ?FAIL}
					end;
				_ -> {false, Res}
			end
	end,
	case Result of
		{true, Player2} ->
			%% 计算玩具可载重
			% CanCarry = calc_can_carry_weight(Year, Month, VehicleList, NewFashionList),
			NewStatusChild = StatusChild#status_child{fashion_list = NewFashionList},
			lib_child_util:save_db_child_fashion(RoleId, FashionId, 1, 0),
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player2),
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, active_fashion),
			mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
			{ok, RefreshPlayer1} = lib_wardrobe_active:active_child_fashion(RefreshPlayer, calc_child_fashion(NewFashionList), FashionId),
			handle_fashion_achievement(Player, FashionList),
			lib_log_api:log_child_item(RoleId, 0, 0, FashionId, CostList, 0, 1),
			case CostList of
				[{_, GoodsId, CostNum}|_] -> ok;
				_ -> GoodsId = 0, CostNum = 0
			end,
			ta_agent_fire:child_fashion(RefreshPlayer1, [GoodsId, 1, CostNum]),
			{true, RefreshPlayer1};
		{false, Error} ->
			{false, Error}
	end.

%% 统计激活的宝宝时装
calc_child_fashion(FashionList) ->
	F = fun({FashionId, _Lv}, CalcList) ->
		case data_goods_type:get(FashionId) of
			#ets_goods_type{color = Color} ->
				case lists:keyfind({1, Color}, 1, CalcList) of
					{_, Num} -> lists:keystore({1, Color}, 1, CalcList, {{1, Color}, Num + 1});
					_ -> lists:keystore({1, Color}, 1, CalcList, {{1, Color}, 1})
				end;
			_ -> CalcList
		end end,
	ColorList = lists:foldl(F, [], FashionList),
	ColorList.

%% 时装成就
handle_fashion_achievement(Player, FashionList) ->
	lib_achievement_api:child_fashion_event(Player, erlang:length(FashionList)),
	ok.

do_upgrade_fashion(#player_status{id = RoleId, status_child = StatusChild} = Player,
		FashionId, IsAuto) ->
	#status_child{fashion_list = FashionList} = StatusChild,
	{_, Lv} = lists:keyfind(FashionId, 1, FashionList),
	NextLv = Lv + 1,
	#child_fashion_cfg{upgrade_cost = CostList, buy_type = BuyType, buy_price = BuyPrice}
		= data_child:get_child_fashion_cfg(FashionId, NextLv),
	Result = case lib_goods_api:cost_object_list_with_check(Player, CostList, upgrade_child_fashion_cost, "") of
		{true, Player1, _} ->
			{true, Player1};
		{false, Res, _Player1} ->
			case BuyType == 0 andalso IsAuto == 1 of
				true -> % 直接购买
					{MType, _, Money} = BuyPrice,
					[{GType, _, NeedNum}] = CostList,
					[{_, OwnNum}] = lib_goods_api:get_goods_num(Player, [FashionId]),
					CostMoney = (NeedNum - OwnNum) * Money,
					Return = case OwnNum > 0 of
						true ->
						    case lib_goods_api:cost_object_list(Player, [{GType, FashionId, OwnNum}], upgrade_child_fashion_cost, "") of
								{true, Player1} -> Player1;
								{false, Err, _} -> {false, Err}
						    end;
						_ -> Player
					end,
					case Return of
						{false, Err1} -> {false, Err1};
						RPlayer when is_record(RPlayer, player_status) ->
							case lib_goods_api:cost_money(RPlayer, MType, CostMoney, upgrade_child_fashion_cost, "") of
								{1, TPlayer} ->
									{true, TPlayer};
								_ ->
									TErr = case MType of
										      1 -> ?ERRCODE(gold_not_enough);
										      _ -> ?ERRCODE(bgold_not_enough)
									      end,
									{false, TErr}
							end;
						_ -> {false, ?FAIL}
					end;
				_ ->
					{false, Res}
			end
	end,
	case Result of
		{true, Player2} ->
			NewFashionList = lists:keystore(FashionId, 1, FashionList, {FashionId, NextLv}),
			%% 计算玩具可载重
			% CanCarry = calc_can_carry_weight(Year, Month, VehicleList, NewFashionList),
			NewStatusChild = StatusChild#status_child{fashion_list = NewFashionList},
			lib_child_util:update_db_fashion_lv(RoleId, FashionId, NextLv),

			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player2),
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, upgrade_child_fashion),
			mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
			lib_log_api:log_child_item(RoleId, 1, 0, FashionId, CostList, Lv, NextLv),
			case CostList of
				[{_, GoodsId, CostNum}|_] -> ok;
				_ -> GoodsId = 0, CostNum = 0
			end,
			ta_agent_fire:child_fashion(RefreshPlayer, [GoodsId, NextLv, CostNum]),
			{true, RefreshPlayer, NextLv};
		{false, Error} ->
			{false, Error}
	end.

do_active_toy_slot(Player, SlotId) ->
	#player_status{id = RoleId, status_child = StatusChild} = Player,
	#status_child{toy_list = ToyList} = StatusChild,
	[{Type, Money}] = data_child:get_toy_slot_open_cfg(SlotId),
	case lib_goods_api:cost_money(Player, Type, Money, active_toy_slot, "") of
		{1, Player1} ->
			NewToyList = lists:keystore(SlotId, 1, ToyList, {SlotId, 0}),
			lib_child_util:save_db_toy_slot_info(RoleId, SlotId, 0),
			NewStatusChild = StatusChild#status_child{toy_list = NewToyList},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			{true, NewPlayer};
		{ErrCode, _Player1} ->
			{false, ErrCode}
	end.

do_active_forge_slot(Player, SlotId) ->
	#player_status{id = RoleId, status_child = StatusChild} = Player,
	#status_child{forge_list = ForgeList} = StatusChild,
	[{Type, Money}] = data_child:get_toy_forge_slot_cfg(SlotId),
	case lib_goods_api:cost_money(Player, Type, Money, active_forge_slot, "") of
		{1, Player1} ->
			NewForgeList = lists:keystore(SlotId, #forge_slot.slot_id, ForgeList, #forge_slot{slot_id = SlotId}),
			lib_child_util:save_db_forge_slot_info(RoleId, SlotId, 0, 0, 0),
			NewStatusChild = StatusChild#status_child{forge_list = NewForgeList},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			{true, NewPlayer};
		{ErrCode, _Player1} ->
			{false, ErrCode}
	end.

do_receive_toy_suit_reward(#player_status{status_child = StatusChild, id = RoleId
	} = Player,SuitType) ->
	#status_child{toy_suit = ToySuits} = StatusChild,
	case data_child:get_toy_suit_cfg(SuitType) of
		#child_toy_suit_cfg{collect_reward = Reward} ->
			Produce = #produce{title = utext:get(13), content = utext:get(14),
				type = toy_suit, reward = Reward, show_tips = 0, remark = ""},
			{ok, Player1} = lib_goods_api:send_reward_with_mail(Player, Produce, ?MOD_CHILD),
			ToySuit = maps:get(SuitType, ToySuits, #toy_suit{}),
			Status = ?TOY_SUIT_RECEIVE_REWARD,
			NewToySuit = ToySuit#toy_suit{status = Status},

			lib_child_util:update_db_toy_suit(RoleId, SuitType, Status, ToySuit#toy_suit.time),

			NewToySuits = maps:put(SuitType, NewToySuit, ToySuits),
			NewStatusChild = StatusChild#status_child{toy_suit = NewToySuits},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			{true, NewPlayer, NewToySuit};
		_ ->
			{false, ?ERRCODE(err_165_toy_suit_cfg_err)}
	end.

do_active_toy(#player_status{status_child = StatusChild, id = RoleId} = Player, SuitType, ToyId) ->
	#status_child{toy_suit = ToySuits} = StatusChild,
	case lib_goods_api:cost_object_list(Player, [{?TYPE_GOODS, ToyId, 1}], active_toy_cost, "") of
		{true, Player1} ->
			case maps:get(SuitType, ToySuits, []) of
				[] ->
					lib_child_util:save_db_toy_suit(RoleId, SuitType, 0, 0),
					NewToySuit = #toy_suit{type = SuitType, toy_list = [ToyId]};
				#toy_suit{toy_list = ToyList, status = Status} = ToySuit ->
					NewToyList = [ToyId|ToyList],
					case length(NewToyList) == length(data_child:get_suit_all_toy_cfg(SuitType)) of
						true ->
							Status1 = ?TOY_SUIT_COLLECT_ALL,
							CollectTime = utime:unixtime(),
							lib_child_util:update_db_toy_suit(RoleId, SuitType, Status1, CollectTime);
						_ ->
							Status1 = Status, CollectTime = 0
					end,
					NewToySuit = ToySuit#toy_suit{toy_list = NewToyList, status = Status1, time = CollectTime}
			end,
			lib_child_util:save_db_toy(RoleId, SuitType, ToyId),

			lib_log_api:log_child_toy(RoleId, SuitType, ToyId, [{?TYPE_GOODS, ToyId, 1}]),

			NewToySuits = maps:put(SuitType, NewToySuit, ToySuits),
			NewStatusChild = StatusChild#status_child{toy_suit = NewToySuits},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, active_child_toy),
			handle_active_toy_achievement(Player, NewToySuits),
			mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
			{true, RefreshPlayer};
		{false, Err, _} -> {false, Err}
	end.

handle_active_toy_achievement(Player, ToySuits) ->
	SuitList = maps:to_list(ToySuits),
	F = fun({Type, ToySuit}, {TSumActive, TSumSuit}) ->
		#toy_suit{toy_list = ToyList} = ToySuit,
		ToyNum = erlang:length(ToyList),
		TSumActive1 = TSumActive + ToyNum,
		IsSuit = ToyNum =:= erlang:length(data_child:get_suit_all_toy_cfg(Type)),
		case IsSuit of
			true -> TSumSuit1 = TSumSuit + 1;
			_ -> TSumSuit1 = TSumSuit
		end,
		{TSumActive1, TSumSuit1}
	end,
	{SumActive, SumSuit} = lists:foldl(F, {0, 0}, SuitList),
	lib_achievement_api:child_toy_event(Player, SumActive),
	lib_achievement_api:child_toy_suit_event(Player, SumSuit),
	ok.

do_upgrade_toy_box(#player_status{status_child = StatusChild, id = RoleId} = Player) ->
	#status_child{box_lv = BoxLv} = StatusChild,
	#child_toy_box_upgrade_cfg{next_cost = NextCost} = data_child:get_child_toy_box_upgrade_cfg(BoxLv),
	case NextCost of
		[{_, GoodsId, CostNum}] -> ok;
		_ -> GoodsId = 0, CostNum = 0
	end,
	OldNum = lib_goods_api:get_currency(Player, GoodsId),
	case lib_goods_api:cost_object_list(Player, NextCost, upgrade_toy_box_cost, "") of
		{true, Player1} ->
			NewBoxLv = BoxLv + 1,
			NewStatusChild = StatusChild#status_child{box_lv = NewBoxLv},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),

			lib_child_util:update_db_toy_box(RoleId, NewBoxLv),

			lib_log_api:log_child_toy_box(RoleId, 1, BoxLv, NewBoxLv, OldNum, max(0, OldNum - CostNum), []),

			RefreshPlayer = lib_child:refresh_attr(NewPlayer, upgrade_child_toy_box),
			mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
			{true, RefreshPlayer, NewBoxLv};
		{false, Err, _} ->
			{false, Err}
	end.

log_toy_decompose(Player, CostList, GiveList) ->
	#player_status{status_child = StatusChild, id = RoleId} = Player,
	#status_child{box_lv = BoxLv} = StatusChild,
	case GiveList of
		[{_, GoodsId, AddNum}|_] -> ok;
		[{GoodsId, AddNum}|_] -> ok;
		_ -> GoodsId = 0, AddNum = 0
	end,
	CostList1 = lib_goods_util:merge_same_goods(CostList),
	NewNum = lib_goods_api:get_currency(Player, GoodsId),
	lib_log_api:log_child_toy_box(RoleId, 0, BoxLv, BoxLv, max(0, NewNum-AddNum), NewNum, CostList1),
	ok.

%% ------------------------------------------- gm 操作 ---------------------------------------------
gm_set_child_vehicle_stage(Player, VehicleId, Stage) ->
	#player_status{sid = Sid, id = RoleId, status_child = StatusChild} = Player,
	#status_child{child_sex = ChildSex, vehicle = VehicleList, passive_skill = PassiveSkills} = StatusChild,
	VehicleInfo = {VehicleId, Stage},
	CheckList = [
		{has_adopt_child, ChildSex}, {is_child_vehicle, VehicleId},
		{has_active, VehicleId, VehicleList}, {is_max_stage, VehicleInfo}
	],
	case lib_child_util:check_upgrade_vehicle(CheckList) of
		true ->
			NewVehicleList =
			case lists:keyfind(VehicleId, 1, VehicleList) of
				{VehicleId, _} ->
					case data_child:get_child_vehicle_cfg(VehicleId, Stage + 1) of
						#child_vehicle_cfg{} ->
							lib_child_util:update_db_child_vehicle(RoleId, VehicleId, Stage),
							lists:keystore(VehicleId, 1, VehicleList, {VehicleId, Stage});
						_ ->
							VehicleList
					end;
				_ ->
					VehicleList
			end,
			case lib_child:check_if_active_passive_skill(VehicleId, Stage) of
				TPassiveSkill when TPassiveSkill > 0 ->
					NewPassiveSkills = ?IF(lists:member(TPassiveSkill, PassiveSkills) == true, PassiveSkills, [TPassiveSkill|PassiveSkills]);
				_ ->
					NewPassiveSkills = PassiveSkills
			end,
			NewStatusChild = StatusChild#status_child{vehicle = NewVehicleList, passive_skill = NewPassiveSkills},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player),
			lib_child:send_msg(Sid, 16515, [VehicleId, Stage]),
			RefreshPlayer = lib_child:refresh_attr(NewPlayer, upgrade_child_vehicle),
			mod_scene_agent:update(RefreshPlayer, [{battle_attr, RefreshPlayer#player_status.battle_attr}]),
			RefreshPlayer;
		{false, Res} ->
			lib_child:send_error(Sid, Res),
			Player
	end.

gm_quick_open_toy_slot(Player) ->
	imit_open_toy_slot(Player),
	imit_open_toy_forge_slot(Player).

imit_open_toy_slot(#player_status{id = RoleId, status_child = StatusChild} = Player) ->
	case StatusChild#status_child.toy_list of
		[_|_] -> ok;
		_ ->
			SlotList = [{1,0},{2,0},{3,0},{4,0},{5,0},{6,0}],
			NewStatusChild = StatusChild#status_child{toy_list = SlotList},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player),
			F = fun({SlotId, GoodId}, Ack) ->
				lib_child_util:save_db_toy_slot_info(RoleId, SlotId, GoodId),
				Ack
			    end,
			lists:foldl(F, true, SlotList),
			{ok, NewPlayer}
	end.

imit_open_toy_forge_slot(#player_status{id = RoleId, status_child = StatusChild} = Player) ->
	case StatusChild#status_child.toy_list of
		[_|_] -> ok;
		_ ->
			ForgeList = [
				#forge_slot{slot_id = 1}, #forge_slot{slot_id = 2}, #forge_slot{slot_id = 3}, #forge_slot{slot_id = 4}],
			NewStatusChild = StatusChild#status_child{forge_list = ForgeList},
			NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player),
			F = fun(ForgeSlot, Ack) ->
				#forge_slot{slot_id = SlotId} = ForgeSlot,
				lib_child_util:save_db_forge_slot_info(RoleId, SlotId, 0, 0, 0),
				Ack
			    end,
			lists:foldl(F, true, ForgeList),
			{ok, NewPlayer}
	end.
