%%%-------------------------------------------------------------------
%%% @doc 定制活动-阶段兑换
%%% Module: phase_exchange
%%% Created : 2020-11-09
%%% @Author: tyl
%%% @desc: 下一阶段开启后，上一阶段的购买数据会被清理掉
%%% @end
%%%-------------------------------------------------------------------
-module(phase_exchange).

-include("server.hrl").
-include("figure.hrl").
-include("common.hrl").
-include("errcode.hrl").
-include("def_fun.hrl").
-include("def_goods.hrl").
-include("def_module.hrl").
-include("custom_act.hrl").
-include("goods.hrl").

%% API
-export([
	send_phase_exchange_goods/2     %% 商品信息
	,exchange_goods/5               %% 商品兑换
	,open_phase_reward/4            %% 阶段开启奖励
]).

-record(phase_exchange_data, {
	phase           = 1     %% 当前阶段
	,lv             = 0     %% 第一次打开界面时候的等级
	,exchange_list  = []    %% 商品兑换情况[{grade_id,exchange_num,status}...]
	,open_reward    = []    %% 阶段开启奖励领取状态[{phase,status}...]
}).

%% 单个商品是否全部兑换完
-define(EXCHANGE_ALL,     1).
-define(NOT_EXCHANGE_ALL, 0).

%% 记录日志类型
-define(LOG_EXCHANGE_GOODS,   1).
-define(LOG_OPEN_PHASE_GOODS, 2).

%% ----------------------------------------------------------------------------------------
%% @doc 查询商品列表 @end
%% ----------------------------------------------------------------------------------------
send_phase_exchange_goods(Player, SubType) ->
	#player_status{id = RoleId, sid = Sid, figure = #figure{lv = Lv}} = Player,
	Type = ?CUSTOM_ACT_TYPE_PHASE_EXCHANGE,
	IsOpen = lib_custom_act_api:is_open_act(Type, SubType),
	OpenLv = act_open_lv(Type, SubType),
	case IsOpen of
		true ->
			case Lv >= OpenLv of
				false ->
					ignore;
				_ ->
					PhaseExchangeData = phase_exchange_data(Type, SubType),
					#phase_exchange_data{
						phase = NowPhase
						,lv   = FirstOpenLv
						,exchange_list = ExchangeList
						,open_reward   = OpenRewardList
					} = PhaseExchangeData,
					TrueLv = true_lv(FirstOpenLv, Lv),
					GradeList = all_grade_list(Type, SubType),
					AllPhase  = all_phase(Type, SubType, TrueLv),
					F = fun(Phase, GoodsList) ->
						PhaseGradeList = phase_grade_list(Type, SubType, Phase, TrueLv, GradeList),
						PackGoodsList  = pack_goods_list(Type, SubType, Phase, NowPhase, PhaseGradeList, ExchangeList),
						CanReceive = case Phase of
							             1 -> 0;
							             _ -> case lists:keyfind(Phase, 1, OpenRewardList) of
								                  {_, _Status} -> 0;
								                  _ -> 1
							                  end
						             end,
						[{Phase, CanReceive, PackGoodsList}|GoodsList]
					    end,
					SendGoodsList = lists:foldl(F, [], AllPhase),
					TrueLv =/= FirstOpenLv andalso save_phase_exchange_data(
						RoleId, Type, SubType, PhaseExchangeData#phase_exchange_data{lv = TrueLv}),
					lib_server_send:send_to_sid(Sid, pt_332, 33260, [SubType, SendGoodsList])
			end;
		_ ->
			lib_server_send:send_to_sid(
				Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
			)
	end,
	ok.

pack_goods_list(Type, SubType, Phase, NowPhase, PhaseGradeList, ExchangeList) ->
	if
		%% 已经兑换完的
		Phase < NowPhase ->
			PackF = fun(GradeId, {Pos, TList}) ->
				case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
					#custom_act_reward_cfg{condition = Condition, reward = Rewards} ->
						[{GoodType, GoodsId, Num}] = Rewards,
						case lists:keyfind(exchange_limit, 1, Condition) of
							{exchange_limit, LimitTimes} ->
								ExchangeTimes = LimitTimes;
							%% 没有exchange_limit字段的不限制兑换(其实是配置错误了)
							_ ->
								LimitTimes = 999,
								ExchangeTimes = LimitTimes
						end,
						CostGoodsNum = case lists:keyfind(cost, 1, Condition) of
							                {_, CNum} -> CNum;
											_ -> 0
						                end,
						TList1 = [{Pos, GradeId, GoodType, GoodsId, Num, ExchangeTimes, LimitTimes, CostGoodsNum}|TList],
						{Pos + 1, TList1};
					_ ->
						{Pos, TList}
				end end;
		%% 本阶段兑换的
		Phase == NowPhase ->
			PackF = fun(GradeId, {Pos, TList}) ->
				case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
					#custom_act_reward_cfg{condition = Condition, reward = Rewards} ->
						case lists:keyfind(exchange_limit, 1, Condition) of
							{exchange_limit, LimitTimes} -> ok;
							_ ->
								LimitTimes = 999
						end,
						[{GoodType, GoodsId, Num}] = Rewards,
						ExchangeTimes = case lists:keyfind(GradeId, 1, ExchangeList) of
							                {_, ENum, _} -> ENum;
							                _ -> 0
						                end,
						CostGoodsNum = case lists:keyfind(cost, 1, Condition) of
							               {_, CNum} -> CNum;
							               _ -> 0
						               end,
						TList1 = [{Pos, GradeId, GoodType, GoodsId, Num, ExchangeTimes, LimitTimes, CostGoodsNum}|TList],
						{Pos + 1, TList1};
					_ ->
						{Pos, TList}
				end end;
		%% 还未兑换的
		true ->
			PackF = fun(GradeId, {Pos, TList}) ->
				case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
					#custom_act_reward_cfg{condition = Condition, reward = Rewards} ->
						case lists:keyfind(exchange_limit, 1, Condition) of
							{exchange_limit, LimitTimes} -> ok;
							_ ->
								LimitTimes = 999
						end,
						[{GoodType, GoodsId, Num}] = Rewards,
						CostGoodsNum = case lists:keyfind(cost, 1, Condition) of
							               {_, CNum} -> CNum;
							               _ -> 0
						               end,
						TList1 = [{Pos, GradeId, GoodType, GoodsId, Num, 0, LimitTimes, CostGoodsNum}|TList],
						{Pos + 1, TList1};
					_ ->
						{Pos, TList}
				end end
	end,
	PhaseGradeList1 = lists:sort(PhaseGradeList),
	{_, PackList} = lists:foldl(PackF, {1, []}, PhaseGradeList1),
	PackList.

%% ----------------------------------------------------------------------------------------
%% @doc 商品兑换 @end
%% ----------------------------------------------------------------------------------------
exchange_goods(Player, SubType, Phase, GradeId, ExchangeTimes) ->
	#player_status{id = RoleId, sid = Sid, figure = #figure{lv = Lv, name = Name}} = Player,
	Type = ?CUSTOM_ACT_TYPE_PHASE_EXCHANGE,
	PhaseExchangeData = phase_exchange_data(Type, SubType),
	#phase_exchange_data{phase = NowPhase, exchange_list = ExchangeList, lv = FirstOpLv} = PhaseExchangeData,
	RewardCfg = case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
		            #custom_act_reward_cfg{} = Cfg -> Cfg;
		            _ -> []
	            end,
	CostGoodsNum = get_cost_material_num(Type, SubType, GradeId),
	CostGoodId   = get_cost_material(Type, SubType),
	IsOpen = lib_custom_act_api:is_open_act(Type, SubType),
	OpenLv = act_open_lv(Type, SubType),
	case IsOpen of
		false ->
			lib_server_send:send_to_sid(Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]),
			{ok, Player};
		true when Lv >= OpenLv ->
			CheckList = [
				{phase_true, NowPhase, Phase}, {grade_ture, Type, SubType, GradeId, FirstOpLv},
				{exchange_counts, Phase, GradeId, ExchangeTimes, ExchangeList, RewardCfg},
				{cost, CostGoodId, ExchangeTimes*CostGoodsNum}
			],
			case check_can_exchange(CheckList, Player) of
				true ->
					%% 免费
					case CostGoodsNum == 0 of
						true ->
							{Player1, NewExchangeList} = exchange_goods_core(
								Player, Type, SubType, Phase, GradeId, ExchangeTimes, PhaseExchangeData, RewardCfg
							),
							case lists:keyfind(GradeId, 1, NewExchangeList) of
								{_, HasExchange, _} -> ok;
								_ -> HasExchange = 0
							end,
							log_phase_exchange(?LOG_EXCHANGE_GOODS, Type, SubType, GradeId, RoleId, Name, Lv,
								CostGoodId, CostGoodsNum, ExchangeTimes),
							lib_server_send:send_to_sid(Sid, pt_332, 33261,
								[?SUCCESS, SubType, Phase, GradeId, HasExchange]),
							{ok, Player1};
						_ ->
							CostList = [{?TYPE_BIND_GOODS, CostGoodId, ExchangeTimes*CostGoodsNum}],
							case lib_goods_api:cost_object_list(Player, CostList, phase_exchange, "") of
								{true, Player1} ->
									{Player2, NewExchangeList} = exchange_goods_core(
										Player1, Type, SubType, Phase, GradeId, ExchangeTimes, PhaseExchangeData, RewardCfg
									),
									case lists:keyfind(GradeId, 1, NewExchangeList) of
										{_, HasExchange, _} -> ok;
										_ -> HasExchange = 0
									end,
									log_phase_exchange(?LOG_EXCHANGE_GOODS, Type, SubType, GradeId, RoleId, Name, Lv,
										CostGoodId, CostGoodsNum, ExchangeTimes),
									lib_server_send:send_to_sid(Sid, pt_332, 33261,
										[?SUCCESS, SubType, Phase, GradeId, HasExchange]),
									{ok, Player2};
								{false, ErrCode, _} ->
									lib_server_send:send_to_sid(Sid, pt_332, 33261, [ErrCode, SubType, Phase, GradeId, 0]),
									{ok, Player}
							end
					end;
				Res ->  %% 条件不满足
					lib_server_send:send_to_sid(Sid, pt_332, 33261, [Res, SubType, Phase, GradeId, 0]),
					{ok, Player}
			end;
		_ ->
			lib_server_send:send_to_sid(Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]),
			{ok, Player}
	end.

exchange_goods_core(Player, Type, SubType, Phase, GradeId, ExchangeTimes, PhaseExchangeData, RewardCfg) ->
	#custom_act_reward_cfg{reward = Reward} = RewardCfg,
	#phase_exchange_data{exchange_list = ExchangeList, lv = Lv} = PhaseExchangeData,
	AllPhaseList = all_phase(Type, SubType, Lv),
	NewGiveGoods = [{GoodType, GiveGoodId, Num*ExchangeTimes}||{GoodType, GiveGoodId, Num}<-Reward],
	%% 增加次数
	NewExchangeList = add_exchange_counts(SubType, GradeId, ExchangeTimes, ExchangeList),
	Produce = #produce{type = phase_exchange, subtype = 0,
		reward = NewGiveGoods, show_tips = 1
	},
	{ok, Player1} = lib_goods_api:send_reward_with_mail(Player, Produce),
	%% 检查是否本阶段全部兑换完
	CheckAll = check_phase_all_exchange(Type, SubType, Phase, Lv, NewExchangeList),
	NewPhaseExchangeData = new_exchange_data(
		CheckAll, Phase, AllPhaseList, PhaseExchangeData, NewExchangeList
	),
	save_phase_exchange_data(Player#player_status.id, Type, SubType, NewPhaseExchangeData),
	{Player1, NewExchangeList}.

new_exchange_data(CheckAll, Phase, AllPhaseList, PhaseExchangeData, NewExchangeList) ->
	case CheckAll of
		true ->
			case Phase >= lists:max(AllPhaseList) of
				true ->
					NewPhaseExchangeData = PhaseExchangeData#phase_exchange_data{
						exchange_list = NewExchangeList
					};
				_ ->
					NewPhaseExchangeData = PhaseExchangeData#phase_exchange_data{
						phase = Phase + 1,
						exchange_list = []
					}
			end;
		_ ->
			NewPhaseExchangeData = PhaseExchangeData#phase_exchange_data{
				exchange_list = NewExchangeList
			}
	end,
	NewPhaseExchangeData.

%% ----------------------------------------------------------------------------------------
%% @doc 阶段开启奖励 Option 1-奖励信息  2-领取奖励 @end
%% ----------------------------------------------------------------------------------------
open_phase_reward(Player, SubType, Phase, Option) ->
	#player_status{sid = Sid, figure = #figure{lv = Lv}} = Player,
	Type = ?CUSTOM_ACT_TYPE_PHASE_EXCHANGE,
	IsOpen = lib_custom_act_api:is_open_act(Type, SubType),
	OpenLv = act_open_lv(Type, SubType),
	case IsOpen of
		true when Lv >= OpenLv ->
			case Option of
				1 ->
					pack_phase_reward(Player, Type, SubType, Phase);
				2 ->
					receive_phase_reward(Player, Type, SubType, Phase);
				_ ->
					ok
			end;
		_ ->
			lib_server_send:send_to_sid(
				Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
			),
			{ok, Player}
	end.

pack_phase_reward(Player, Type, SubType, Phase) ->
	PhaseExchangeData = phase_exchange_data(Type, SubType),
	#phase_exchange_data{lv = Lv} = PhaseExchangeData,
	OpenGradeList = open_phase_grade_list(Type, SubType, Lv),
	case lists:keyfind(Phase, 2, OpenGradeList) of
		{GradeId, _} ->
			case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
				#custom_act_reward_cfg{reward = Reward} -> ok;
				_ -> Reward = []
			end,
			lib_server_send:send_to_sid(
				Player#player_status.sid, pt_332, 33262, [?SUCCESS, 1, SubType, Phase, Reward]
			);
		_ ->
			lib_server_send:send_to_sid(
				Player#player_status.sid, pt_332, 33262, [?ERRCODE(err331_reward_cfg_err), 1, SubType, Phase, []]
			)
	end,
	{ok, Player}.

receive_phase_reward(Player, Type, SubType, Phase) ->
	#player_status{id = RoleId, figure = #figure{lv = RoleLv, name = Name}} = Player,
	PhaseExchangeData = phase_exchange_data(Type, SubType),
	#phase_exchange_data{
		phase = NowPhase
		,lv   = Lv
		,open_reward = OpenRewardList
	} = PhaseExchangeData,
	if
		Phase > NowPhase ->
			lib_server_send:send_to_sid(Player#player_status.sid, pt_332, 33262,
				[?ERRCODE(err331_receive_phase_err), 2, SubType, Phase, []]
			),
			{ok, Player};
		true ->
			OpenGradeList = open_phase_grade_list(Type, SubType, Lv),
			case lists:keyfind(Phase, 2, OpenGradeList) of
				{GradeId, _} ->
					Reward = case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
						         #custom_act_reward_cfg{reward = TReward} -> TReward;
						         _ -> []
					         end,
					HasReceived = case lists:keyfind(Phase, 1, OpenRewardList) of
						              {_, 1} -> true;
									  _ -> false
					              end,
					case Reward =/= [] of
						true when HasReceived == false ->
							Produce = #produce{type = open_phase_reward, subtype = 0
								,reward = Reward, show_tips = 1},
							NewOpenRewardList = lists:keystore(Phase, 1, OpenRewardList, {Phase, 1}),
							NewPhaseExchangeData = PhaseExchangeData#phase_exchange_data{open_reward = NewOpenRewardList},
							save_phase_exchange_data(Player#player_status.id, Type, SubType, NewPhaseExchangeData),
							{ok, Player1} = lib_goods_api:send_reward_with_mail(Player, Produce, ?MOD_AC_CUSTOM),
							log_phase_exchange(?LOG_OPEN_PHASE_GOODS, Type, SubType, GradeId, RoleId, Name, RoleLv,
								0, 0, 0),
							lib_server_send:send_to_sid(
								Player#player_status.sid, pt_332, 33262, [?SUCCESS, 2, SubType, Phase, Reward]
							);
						true when HasReceived == true ->
							lib_server_send:send_to_sid(Player#player_status.sid, pt_332, 33262,
								[?ERRCODE(err331_phase_reward_received), 2, SubType, Phase, []]
							),
							Player1 = Player;
						_ ->
							lib_server_send:send_to_sid(
								Player#player_status.sid, pt_332, 33262, [?ERRCODE(err331_act_cfg_err), 2, SubType, Phase, []]
							),
							Player1 = Player
					end,
					{ok, Player1};
				_ ->
					lib_server_send:send_to_sid(
						Player#player_status.sid, pt_331, 33100, [?ERRCODE(err331_act_cfg_err), 2, SubType, Phase, []]
					),
					{ok, Player}
			end
	end.

%% ------------------------ builtin_function -----------------------------------
phase_exchange_data(Type, SubType) ->
	#custom_act_data{
		act_data = ActData
	} = lib_custom_act_data:custom_act_data(Type, SubType),
	case ActData of
		#phase_exchange_data{} -> ActData;
		_ -> #phase_exchange_data{}
	end.

save_phase_exchange_data(RoleId, Type, SubType, NewActData) ->
	ActData = #custom_act_data{
		type = Type, subtype = SubType, act_data = NewActData,
		time = utime:unixtime()
	},
	lib_custom_act_data:save_custom_data(RoleId, ActData).

%% 活动开放等级
act_open_lv(Type, SubType) ->
	case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
		#custom_act_cfg{condition = Condition} ->
			case lists:keyfind(role_lv, 1, Condition) of
				{_, Lv} -> Lv;
				_ -> 999
			end;
		_ ->
			999
	end.

%% 记录真实Lv
true_lv(OpenLv, Lv) ->
	case OpenLv == 0 of
		true -> Lv;
		_ -> OpenLv
	end.

%% 所有阶段
all_phase(Type, SubType, Lv) ->
	PhaseGradeList = open_phase_grade_list(Type, SubType, Lv),
	PhaseList = [Phase||{_, Phase}<-PhaseGradeList],
	MaxPhase = case PhaseList of
		           [_|_] -> lists:max(PhaseList);
		           _ -> 1
	           end,
	lists:seq(1, MaxPhase).

%% 所有奖励档次(所有阶段商城的)
all_grade_list(Type, SubType) ->
	GradeIdList = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
	GradeIdList.

%% 本阶段所有奖励档次
phase_grade_list(Type, SubType, Phase, Lv, GradeIdList) ->
	F = fun(GradeId, TList) ->
		case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
			#custom_act_reward_cfg{condition = Condition} ->
				case lists:keyfind(store, 1, Condition) of
					{store, Phase} ->
						case lists:keyfind(lv, 1, Condition) of
							{_, LLv, HLv} when LLv =< Lv, HLv >= Lv ->
								[GradeId|TList];
							_ -> TList
						end;
					_ -> TList
				end;
			_ -> TList
		end end,
	lists:foldl(F, [], GradeIdList).

%% 开放阶段奖励(type=2的才是阶段开放奖励)
open_phase_grade_list(Type, SubType, Lv) ->
	AllGradeList  = all_grade_list(Type, SubType),
	F = fun(TGradeId, TList) ->
		case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, TGradeId) of
			#custom_act_reward_cfg{condition = Condition} ->
				case lists:keyfind(open_reward, 1, Condition) of
					{_, Phase} ->
						case lists:keyfind(lv, 1, Condition) of
							     {_, LLv, HLv} when LLv =< Lv, HLv >= Lv ->
								     [{TGradeId, Phase}|TList];
								_ ->
									TList
						end;
					_ -> TList
				end;
			_ ->
				TList
		end end,
	lists:foldl(F, [], AllGradeList).

%% 日志记录
log_phase_exchange(LogType, Type, SubType, GradeId, RoleId, RoleName, RoleLv,
		 GoodCost, CostNum, ExchangeTimes) ->
	#custom_act_reward_cfg{reward = RewardList, condition = Condition} =
		lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId),
	case lists:keyfind(store, 1, Condition) of
		{_, StoreId} -> ok;
		_ -> StoreId = 0
	end,
	case lists:keyfind(lv, 1, Condition) of
		{_, _, _} = NeedLv -> ok;
		_ -> NeedLv = {}
	end,
	CostNum1    = CostNum * ExchangeTimes,
	RewardList1 = [{GoodType, GoodsId, Num*ExchangeTimes}||{GoodType, GoodsId, Num}<-RewardList],
	lib_log_api:log_phase_exchange(SubType, RoleId, RoleName, RoleLv, NeedLv,
		StoreId, GradeId, LogType, RewardList1, GoodCost, CostNum1).

%% ----------------------------------------------------------
%% 获取已经兑换的次数
get_exchange_counts(GradeId, ExchangeList) ->
	case lists:keyfind(GradeId, 1, ExchangeList) of
		{_, Num, _} -> Num;
		_ -> 0
	end.

%% 增加兑换的次数
add_exchange_counts(SubType, GradeId, ExchangeTimes, ExchangeList) ->
	Type = ?CUSTOM_ACT_TYPE_PHASE_EXCHANGE,
	case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
		#custom_act_reward_cfg{condition = Condition} -> ok;
		_ -> Condition = []
	end,
	LimitTimes = case lists:keyfind(exchange_limit, 1, Condition) of
		             {_, Limit} -> Limit;
					 _ -> 0
	             end,
	case lists:keyfind(GradeId, 1, ExchangeList) of
		{_, Num, _} ->
			NewNum = Num + ExchangeTimes;
		_ ->
			NewNum = ExchangeTimes
	end,
	Status = case NewNum >= LimitTimes of
		         true -> ?EXCHANGE_ALL;
		         _ -> ?NOT_EXCHANGE_ALL
	         end,
	lists:keystore(GradeId, 1, ExchangeList, {GradeId, NewNum, Status}).

%% 检查本阶段是否全部已经兑换完了
check_phase_all_exchange(Type, SubType, Phase, Lv, ExchangeList) ->
	AllGradeList   = all_grade_list(Type, SubType),
	PhaseGradeList = phase_grade_list(Type, SubType, Phase, Lv, AllGradeList),
	F = fun(GradeId) ->
		case lists:keyfind(GradeId, 1, ExchangeList) of
			{_, _, Status} when Status == 1 -> true;
			_ -> false
		end end,
	lists:all(F, PhaseGradeList).

%% 获取需要消耗的材料数量
get_cost_material_num(Type, SubType, GradeId) ->
	case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
		#custom_act_reward_cfg{condition = Condition} ->
			case lists:keyfind(cost, 1, Condition) of
				{_, Num} -> Num;
				_ -> 0
			end
	end.

%% 获取消耗材料
get_cost_material(Type, SubType) ->
	case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
		#custom_act_cfg{condition = Condition} -> ok;
		_ -> Condition = []
	end,
	case lists:keyfind(cost_type, 1, Condition) of
		{_, GoodId} -> GoodId;
		_ -> 0
	end.

check_can_exchange([], _Player) -> true;
check_can_exchange([Check|T], Player) ->
	case do_check(Check, Player) of
		true ->
			check_can_exchange(T, Player);
		{false, Err} -> Err
	end.

%% {phase_true, NowPhase, Phase}
do_check({phase_true, NowPhase, Phase}, _Player) ->
	case Phase == NowPhase of
		true -> true;
		_ -> {false, ?ERRCODE(err331_phase_err)}
	end;
do_check({grade_ture, Type, SubType, GradeId, FirstOpLv}, _Player) ->
	case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
		#custom_act_reward_cfg{condition = Condition} ->
			case lists:keyfind(lv, 1, Condition) of
				{_, LLv, HLv} when LLv =< FirstOpLv, HLv >= FirstOpLv ->
					true;
				_ ->
					{false, ?ERRCODE(err331_exchange_lv_err)}
			end;
		_ ->
			{false, ?ERRCODE(err331_reward_cfg_err)}
	end;
%% 检查玩家背包中物品兑换次数是否满足
do_check({can_exchange_times, ExchangeTimes1}, _Player) ->
	case ExchangeTimes1 of
		0 -> {false, ?GOODS_NOT_ENOUGH};
		_ -> true
	end;
%% 兑换的次数是否足够
do_check({exchange_counts, _Phase, GradeId, ExchangeTimes, ExchangeList, RewardCfg}, _Player) ->
	case ExchangeTimes =< 0 of
		true -> {false, ?ERRCODE(err331_exchage_times_err)};
		_ ->
			case RewardCfg of
				#custom_act_reward_cfg{condition = Condition} ->
					ExchangeCounts = get_exchange_counts(GradeId, ExchangeList),
					case lists:keyfind(exchange_limit, 1, Condition) of
						%% 不限制次数
						{_, 999} -> true;
						{_, ExchangeLimit} ->
							case ExchangeCounts + ExchangeTimes > ExchangeLimit of
								true -> {false, ?ERRCODE(err331_exchange_times_not_enough)};
								_ -> true
							end;
						_ ->
							{false, ?ERRCODE(err331_reward_cfg_err)}
					end;
				_ ->
					{false, ?ERRCODE(err331_reward_cfg_err)}
			end
	end;
%% 背包大小是否满足
do_check({bag_size, RewardCfg}, Player) ->
	#custom_act_reward_cfg{reward = Reward} = RewardCfg,
	case lib_goods_api:can_give_goods(Player, Reward) of
		true ->
			true;
		_ ->
			{false, ?ERRCODE(err151_no_null_cells)}
	end;
%% 消耗的兑换材料是否足够
do_check({cost, GoodId, CostNum}, Player) ->
	case CostNum == 0 of
		true -> true;
		_ ->
			case GoodId > 0 of
				true ->
					Cost = [{?TYPE_BIND_GOODS, GoodId, CostNum}],
					case check_is_cost_enough(Cost, Player) of
						true ->
							true;
						{false, Err} ->
							{false, Err}
					end;
				_ ->
					{false, ?ERRCODE(err331_act_cfg_err)}
			end
	end.

%% 检查兑换消耗材料是否足够
check_is_cost_enough([], _Player) -> true;
check_is_cost_enough([CostGood|T], Player) ->
	case lib_goods_api:check_object_list(Player, [CostGood]) of
		true -> check_is_cost_enough(T, Player);
		{false, Res} -> {false, Res}
	end.
