%% -----------------------------------
%% @doc         :定制活动-塔罗牌
%% module       :custom_act_tarot.erl
%% author       :hh
%% time         :20/10/26
%% -----------------------------------

-module(custom_act_tarot).

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

-export ([
    get_tarot_info/2        %% 获取塔罗牌信息
    ,get_tarot_reward/3     %% 获取塔罗牌奖励
    ,refresh_tarot_info/2   %% 刷新塔罗牌信息
    ,get_tarot_log/2        %% 塔罗牌日志
    ]).

-define (TAROT_POS, 8).

%% 获取塔罗牌信息
get_tarot_info(Player, SubType) ->
    #player_status{sid = Sid, id = RoleId} = Player,
    Type = ?CUSTOM_ACT_TYPE_TAROT,
    ActInfo = lib_custom_act_util:get_custom_act_open_info(Type, SubType),
    if
        ActInfo == false ->
            lib_server_send:send_to_sid(Sid, pt_332, 33255, [?ERRCODE(err331_act_closed), SubType, [], []]);
        true ->
            CustomData = lib_custom_act_data:custom_act_data(Type, SubType),
            #custom_act_data{act_data = ActData} = CustomData,
            case ActData of
                #custom_act_tarot{} ->
                    {UpdateCheck, NewActData} = check_tarot_status_update(Type, SubType, ActData),
                    #custom_act_tarot{pos_status = PosStatus, reward_pools = RewardPools} = NewActData,
                    RewardPoolsInfo = pack_reward_pool(Type, SubType, RewardPools),
                    % ?ERR("~p ~p", [RewardPoolsInfo, PosStatus]),
                    if UpdateCheck == true ->
                            NewCustomData = CustomData#custom_act_data{act_data = NewActData},
                            lib_custom_act_data:save_custom_data(RoleId, NewCustomData);
                        true ->
                            ok
                    end,
                    lib_server_send:send_to_sid(Sid, pt_332, 33255, [?SUCCESS, SubType, PosStatus, RewardPoolsInfo]);
                _ -> %% init初始化
                    case init_tarot_info(Type, SubType) of
                        TarotData when is_record(TarotData, custom_act_tarot) ->
                            #custom_act_tarot{pos_status = PosStatus, reward_pools = RewardPools} = TarotData,
                            NewCustomData   = CustomData#custom_act_data{act_data = TarotData},
                            lib_custom_act_data:save_custom_data(RoleId, NewCustomData),
                            RewardPoolsInfo = pack_reward_pool(Type, SubType, RewardPools),
                            lib_server_send:send_to_sid(Sid, pt_332, 33255, [?SUCCESS, SubType, PosStatus, RewardPoolsInfo]);
                        Error ->
                            ErrorCode = get_error_code_ret(Error),
                            lib_server_send:send_to_sid(Sid, pt_332, 33255, [ErrorCode, SubType, [], []])
                    end
            end
    end.

%% 获取错误码
get_error_code_ret(error) ->
    ?ERRCODE(missing_config);
get_error_code_ret(tarot_count_error) ->
    ?ERRCODE(tarot_count_error);
get_error_code_ret(tarot_reward_error) ->
    ?ERRCODE(tarot_reward_error);
get_error_code_ret(_) ->
    ?FAIL.

%% 打包数据
pack_reward_pool(Type, SubType, RewardPools) ->
    lists:foldl(fun({Index, GradeId, Status}, Acc) ->
        case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
            #custom_act_reward_cfg{reward = [{GoodsType, GoodsId, GoodsNum}|_]} ->
                [{Index, GoodsType, GoodsId, GoodsNum, Status}|Acc];
            _ ->
                Acc
        end
    end, [], RewardPools).


%% 塔罗牌翻牌
get_tarot_reward(Player, SubType, PosId) ->
    #player_status{sid = Sid, id = RoleId, figure = #figure{vip = Vip, lv = Lv}} = Player,
    Type = ?CUSTOM_ACT_TYPE_TAROT,
    ActInfo = lib_custom_act_util:get_custom_act_open_info(Type, SubType),
    if
        ActInfo == false ->
            lib_server_send:send_to_sid(Sid, pt_332, 33256, [?ERRCODE(err331_act_closed), SubType, PosId, 0, 0, 0, 0]);
        true ->
            CustomData = lib_custom_act_data:custom_act_data(Type, SubType),
            #custom_act_data{act_data = TarotData} = CustomData,
            NewTarotData =
            case TarotData of
                #custom_act_tarot{} ->
                    TarotData;
                _ ->
                    init_tarot_info(Type, SubType)
            end,
            if
                not is_record(NewTarotData, custom_act_tarot) ->
                    ErrorCode = get_error_code_ret(NewTarotData),
                    lib_server_send:send_to_sid(Sid, pt_332, 33256, [ErrorCode, SubType, PosId, 0, 0, 0, 0]);
                true ->
                    case do_get_tarot_reward(Player, NewTarotData, Type, SubType, PosId) of
                        {true, NewPlayer, NewTarotData1, Index, GradeId, RewardList} ->
                            #player_status{id = RoleId, figure = #figure{name = RoleName}} = Player,
                            [{GoodsType, GoodsId, GoodsNum}|_] = RewardList,
                            NewCustomData = CustomData#custom_act_data{act_data = NewTarotData1},
                            lib_custom_act_data:save_custom_data(RoleId, NewCustomData),
                            lib_server_send:send_to_sid(Sid, pt_332, 33256, [?SUCCESS, SubType, PosId, Index, GoodsType, GoodsId, GoodsNum]),
                            % UpdateCheck andalso begin
                            %     #custom_act_tarot{pos_status = NewPosStatus, reward_pools = NewRewardPools} = NewTarotData1,
                            %     RewardPoolsInfo = pack_reward_pool(Type, SubType, NewRewardPools),
                            %     lib_server_send:send_to_sid(Sid, pt_332, 33255, [?SUCCESS, SubType, NewPosStatus, RewardPoolsInfo])
                            % end,
                            %% 增加日志
                            case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
                                #custom_act_reward_cfg{condition = Condition} ->
                                    IsLog = case lists:keyfind(ui_show, 1, Condition) of
                                        {_, LogCfg} -> LogCfg;
                                        _           -> 0
                                    end,
                                    IsTv  = case lists:keyfind(tv_show, 1, Condition) of
                                        {_, TvCfg} -> TvCfg;
                                        _          -> 0
                                    end,
                                    #act_info{wlv = WLv} = ActInfo,
                                    NowTime = utime:unixtime(),
                                    IsLog =/= 0 andalso tarot_log_mgr:add_tarot_log(SubType, RoleId, RoleName, RewardList),
                                    %% 传闻
                                    IsTv =/= 0 andalso lib_chat:send_TV({all}, ?MOD_AC_CUSTOM, 14, [RoleName, GoodsId, GoodsNum]),
                                    lib_log_api:log_custom_act_reward(RoleId, Type, SubType, GradeId, WLv, RewardList, [],
                                        Vip, Lv, Player#player_status.gold, NewPlayer#player_status.gold, Player#player_status.bgold, NewPlayer#player_status.bgold, NowTime),
                                    lib_server_send:send_to_sid(Sid, pt_332, 33259, [SubType, GoodsType, GoodsId, GoodsNum, RoleName]);
                                _ ->
                                    skip
                            end,
                            {ok, NewPlayer};
                        {false, ErrorCode} ->
                            lib_server_send:send_to_sid(Sid, pt_332, 33256, [ErrorCode, SubType, PosId, 0, 0, 0, 0])
                    end
            end
    end.

%% 刷新重置塔罗牌信息
refresh_tarot_info(Player, SubType) ->
    #player_status{sid = Sid, id = RoleId} = Player,
    Type = ?CUSTOM_ACT_TYPE_TAROT,
    ActInfo = lib_custom_act_util:get_custom_act_open_info(Type, SubType),
    if
        ActInfo == false ->
            lib_server_send:send_to_sid(Sid, pt_332, 33257, [?ERRCODE(err331_act_closed), SubType]);
        true ->
            CustomData = lib_custom_act_data:custom_act_data(Type, SubType),
            #custom_act_data{act_data = TarotData} = CustomData,
            CustomActCfg = lib_custom_act_util:get_act_cfg_info(Type, SubType),
            if
                not is_record(TarotData, custom_act_tarot) ->
                    lib_server_send:send_to_sid(Sid, pt_332, 33257, [?FAIL, SubType]);
                not is_record(CustomActCfg, custom_act_cfg) ->
                    lib_server_send:send_to_sid(Sid, pt_332, 33257, [?ERRCODE(missing_config), SubType]);
                true ->
                    % RefreshCheckFun = fun({_PosId, Status}) -> Status == 0 end,
                    % NotNeedRefresh = lists:all(RefreshCheckFun, TarotData#custom_act_tarot.pos_status),
                    #custom_act_cfg{condition = Condition} = CustomActCfg,
                    RefreshCostCfg = lists:keyfind(refresh_cost, 1, Condition),
                    if
                        % NotNeedRefresh ->
                        %     lib_server_send:send_to_sid(Sid, pt_332, 33257, [?ERRCODE(err331_no_need_refresh_tarot), SubType]);
                        not is_record(TarotData, custom_act_tarot) ->
                            ErrorCode = get_error_code_ret(TarotData),
                            lib_server_send:send_to_sid(Sid, pt_332, 33257, [ErrorCode, SubType]);
                        RefreshCostCfg == false ->
                            lib_server_send:send_to_sid(Sid, pt_332, 33257, [?ERRCODE(missing_config), SubType]);
                        true ->
                            {_, CostList} = RefreshCostCfg,
                            % ?ERR("~p", [CostList]),
                            case lib_goods_api:cost_objects_with_auto_buy(Player, CostList, refresh_tarot_cost, "") of
                                {true, NewPlayer, _} ->
                                    #custom_act_tarot{pos_status = PosStatus, reward_pools = RewardPools} = NewTarotData  = init_tarot_info(Type, SubType),
                                    NewCustomData   = CustomData#custom_act_data{act_data = NewTarotData},
                                    lib_custom_act_data:save_custom_data(RoleId, NewCustomData),
                                    RewardPoolsInfo = pack_reward_pool(Type, SubType, RewardPools),
                                    {ok, BinData}   = pt_332:write(33257, [?SUCCESS, SubType]),
                                    {ok, BinData1}  = pt_332:write(33255, [?SUCCESS, SubType, PosStatus, RewardPoolsInfo]),
                                    lib_server_send:send_to_sid(Sid, <<BinData/binary, BinData1/binary>>),
                                    {ok, NewPlayer};
                                {false, ErrorCode, _} ->
                                    lib_server_send:send_to_sid(Sid, pt_332, 33257, [ErrorCode, SubType])
                            end
                    end
            end
    end.

%% 获取塔罗牌日志
get_tarot_log(Player, SubType) ->
    #player_status{sid = Sid} = Player,
    tarot_log_mgr:get_tarot_log(Sid, SubType),
    ok.

do_get_tarot_reward(Player, TarotData, Type, SubType, PosId) ->
    #custom_act_tarot{
        pos_status = PosStatus, reward_pools = RewardPools, num = Num
    } = TarotData,
    CheckList = [
        {custom_act_cfg, Type, SubType}
        ,{pos_status, PosId, PosStatus}
        ,{reward_pools, RewardPools}
        ,{tarot_num, Num}
        % ,{cost, Player, Type, SubType, Num}
    ],
    case reward_check(CheckList) of
        true ->
            NewNum = Num + 1,
            #custom_act_cfg{condition = Condition} = lib_custom_act_util:get_act_cfg_info(Type, SubType),
            CostList0 = calc_cost_list(Condition, NewNum),
            {DisT,CostList}  = calc_real_cost(Player,CostList0),
            Ret =
            if CostList =/= [] ->
                    case lib_goods_api:cost_objects_with_auto_buy(Player, CostList, tarot_cost, "") of
                        {true, NewPlayer, _} ->
                            {true, NewPlayer};
                        {false, ErrorCode, _} ->
                            {false, ErrorCode}
                    end;
                true ->
                    {true, Player}
            end,
            case Ret of
                {true, NewPlayer1} ->
                    case DisT /= 0 of
                        true->
                            custom_unite_buff:cost_times(NewPlayer1,DisT,?CUSTOM_ACT_TYPE_TAROT);
                        _->
                            skip
                    end,
                    {Index, GradeId, TarotReward, NewRewardPools} = calc_tarot_reward(Type, SubType, RewardPools, PosId),
                    NewPlayer2    = lib_goods_api:send_reward(NewPlayer1, TarotReward, tarot_reward),
                    NewPosStatus = lists:keystore(PosId, 1, PosStatus, {PosId, 1}),
                    NewTarotData = TarotData#custom_act_tarot{
                        pos_status = NewPosStatus, reward_pools = NewRewardPools, num = NewNum},
                    % {UpdateCheck, NewTarotData1} = check_tarot_status_update(Type, SubType, NewTarotData),
                    {true, NewPlayer2, NewTarotData, Index, GradeId, TarotReward};
                {false, ErrorCode1} ->
                    {false, ErrorCode1}
            end;
        {false, Error} ->
            {false, Error}
    end.

%% 检查卡牌是否翻完一轮
%% 已翻完一轮自动刷新
check_tarot_status_update(Type, SubType, TarotData) ->
    #custom_act_tarot{pos_status = PosStatus} = TarotData,
    case lists:all(fun({_PosId, Status}) ->
        Status == 1
    end, PosStatus) of
        true ->
            case init_tarot_info(Type, SubType) of
                NewTarotData when is_record(NewTarotData, custom_act_tarot) ->
                    {true, NewTarotData};
                Error ->
                    ?ERR("tarot auto refresh error ~p", [Error]),
                    {false, TarotData}
            end;
        _ -> %% 无需重置
            {false, TarotData}
    end.

calc_cost_list(Condition, Num) ->
    {_, GoodsId} = lists:keyfind(cost_type, 1, Condition),
    {_, NumCostList} = lists:keyfind(cost_num, 1, Condition),
    CostNum = lists:nth(Num, NumCostList),
    if CostNum =/= 0 ->
            [{?TYPE_GOODS, GoodsId, CostNum}];
        true ->
            []
    end.

calc_tarot_reward(Type, SubType, RewardPools, PosId) ->
    UnRewardPools = lists:filter(fun({_Index, _GradeId, StatusX}) ->
        StatusX == 0
    end, RewardPools),
    {RandIndex, RandGradeId, _} = urand:list_rand(UnRewardPools),
    case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, RandGradeId) of
        #custom_act_reward_cfg{reward = [{RandGoodsType, RandGoodsId, RandNum}|_]} ->
            ok;
        _ ->
            RandGoodsType = 0, RandGoodsId = 0, RandNum = 0
    end,
    % NewRewardPools  = lists:keydelete(RandIndex, 1, RewardPools),
    NewRewardPools = lists:keystore(RandIndex, 1, RewardPools, {RandIndex, RandGradeId, PosId}),
    {RandIndex, RandGradeId, [{RandGoodsType, RandGoodsId, RandNum}], NewRewardPools}.

%% 初始化塔罗牌奖池
-spec init_tarot_info(Type, SubType) -> Ret when
    Type :: ?CUSTOM_ACT_TYPE_TAROT | integer(),
    SubType :: integer(),
    Ret :: error                %% 配置错误
    | tarot_count_error         %% 卡牌张数有误，请联系客服
    | tarot_reward_error        %% 卡牌奖池有误，请联系客服
    | #custom_act_tarot{}.      %% 正确结果
init_tarot_info(Type, SubType) ->
    case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
        #custom_act_cfg{condition = Condition} ->
            case lists:keyfind(reward_store, 1, Condition) of
                {_, PoolList} ->
                    GradeIds = lib_custom_act_util:get_act_reward_cfg_list(Type, SubType),
                    GradeIds1 = [Grade || #custom_act_reward_cfg{grade = Grade} <- GradeIds],
                    case init_reward_pool_by_grade(Type, SubType, GradeIds1, PoolList) of
                        RewardPools = [_|_] ->
                            PosStatus = init_pos_status(),
                            #custom_act_tarot{reward_pools = RewardPools, pos_status = PosStatus};
                        ErrorRet ->
                            ErrorRet
                    end;
                _ ->
                    error
            end;
        _ ->
            error
    end.

-spec init_reward_pool_by_grade(Type, SubType, GradeIds, PoolList) -> Ret when
    Type :: ?CUSTOM_ACT_TYPE_TAROT | integer(),
    SubType :: integer(),
    GradeIds :: list(),     %% 配置的档位列表
    PoolList :: list(),     %% 奖励池
    Ret :: tarot_count_error | tarot_reward_error | [{Index, GradeId, Status},...],
    Index :: integer(),     %% 序号
    GradeId :: integer(),   %% 奖励档位
    Status :: integer().    %% 档位领取状态
init_reward_pool_by_grade(Type, SubType, GradeIds, PoolList) ->
    case check_reward_store_cfg(PoolList) of
        NewPoolList = [_|_] ->
            case calc_reward_pool_by_grade(Type, SubType, GradeIds, NewPoolList) of
                RewardPoolList = [_|_] ->
                    RewardPoolList;
                Ret ->
                    Ret
            end;
        _ ->
            tarot_count_error
    end.

-spec calc_reward_pool_by_grade(Type, SubType, GradeIds, PoolList) -> Ret when
    Type :: ?CUSTOM_ACT_TYPE_TAROT | integer(),
    SubType :: list(),
    GradeIds :: list(),
    PoolList :: list(),
    Ret :: tarot_count_error | tarot_reward_error | [{GradeId, Status},...],
    GradeId :: integer(),
    Status :: integer().
calc_reward_pool_by_grade(Type, SubType, GradeIds, PoolList) ->
    PoolCfgList = get_pool_weight_list(Type, SubType, GradeIds),
    case check_count_pool_cfg(PoolCfgList, PoolList) of
        true ->
            tarot_count_error;
        _ ->
            case lists:foldl(fun({PoolId, Count}, Acc) ->
                case lists:keyfind(PoolId, 1, PoolCfgList) of
                    {_, PoolWeights} ->
                        SelectedRewards = urand:list_rand_by_weight(PoolWeights, Count),
                        lists:append(SelectedRewards, Acc);
                    _ ->
                        Acc
                end
            end, [], PoolList) of
                RewardPoolList = [_|_] ->
                    {_, NewRewardPoolList} = lists:foldl(fun(GradeIdX, {Index, Acc}) ->
                        {Index+1, [{Index, GradeIdX, 0}|Acc]}
                    end, {1, []}, RewardPoolList),
                    NewRewardPoolList;
                _Ret ->
                    tarot_reward_error
            end
    end.

%% 获取各类库权重列表
get_pool_weight_list(Type, SubType, GradeIds) ->
    lists:foldl(fun(GradeId, Acc) ->
        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
                    {_, PoolId} ->
                        case lists:keyfind(weight, 1, Condition) of
                            {_, Weight} ->
                                case lists:keyfind(PoolId, 1, Acc) of
                                    {_, PoolL} ->
                                        lists:keystore(PoolId, 1, Acc, {PoolId, [{Weight, GradeId}|PoolL]});
                                    _ ->
                                        lists:keystore(PoolId, 1, Acc, {PoolId, [{Weight, GradeId}]})
                                end;
                            _ -> Acc
                        end;
                    _ -> Acc
                end;
            _ -> Acc
        end
    end, [], GradeIds).

%% 塔罗牌固定有8个位置
init_pos_status() ->
    lists:foldl(fun(PosId, Acc) ->
        [{PosId, 0}|Acc]
    end, [], lists:seq(1, ?TAROT_POS)).

update_pool_cfg([], _) ->
    error;
update_pool_cfg([{PoolId, Num}|Rest], decrease) ->
    NewNum = max(Num - 1, 0),
    SumNum = NewNum + lists:sum([NX || {_PX, NX} <- Rest]),
    if SumNum > ?TAROT_POS ->
            if NewNum == 0 -> update_pool_cfg(Rest, decrease);
                true       -> update_pool_cfg([{PoolId, NewNum}|Rest], decrease)
            end;
        true ->
            if NewNum == 0 -> Rest;
                true       -> [{PoolId, NewNum}|Rest]
            end
    end;
update_pool_cfg([{PoolId, Num}|Rest], increase) ->
    NewNum = Num + 1,
    SumNum = NewNum + lists:sum([NX || {_PX, NX} <- Rest]),
    if SumNum < ?TAROT_POS ->
            update_pool_cfg([{PoolId, NewNum}|Rest], increase);
        true ->
            [{PoolId, NewNum}|Rest]
    end.

check_reward_store_cfg(PoolList) ->
    SumCount = lists:sum([Count || {_PoolId, Count} <- PoolList]),
    if SumCount > ?TAROT_POS ->
            SortedPoolList = lists:sort(fun({PoolIdX1, _CountX1}, {PoolIdX2, _CountX2}) ->
                PoolIdX1 >= PoolIdX2
            end, PoolList),
            update_pool_cfg(SortedPoolList, decrease);
        SumCount == ?TAROT_POS ->
            PoolList;
        true ->
            SortedPoolList = lists:sort(fun({PoolIdX1, _CountX1}, {PoolIdX2, _CountX2}) ->
                PoolIdX1 >= PoolIdX2
            end, PoolList),
            update_pool_cfg(SortedPoolList, increase)
    end.

check_count_pool_cfg(PoolCfgList, PoolList) ->
    lists:any(fun({PoolIdX, Count}) ->
        case lists:keyfind(PoolIdX, 1, PoolCfgList) of
            {_, GradePools} ->
                CfgLen = length(GradePools),
                CfgLen < Count;
            _ ->
                true
        end
    end, PoolList).


reward_check([]) -> true;
reward_check([{custom_act_cfg, Type, SubType}|Rest]) ->
    case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
        #custom_act_cfg{} ->
            reward_check(Rest);
        _ ->
            {false, ?ERRCODE(missing_config)}
    end;
reward_check([{pos_status, PosId, PosStatus}|Rest]) ->
    case lists:keyfind(PosId, 1, PosStatus) of
        {_, 0} ->
            reward_check(Rest);
        {_, Status} when Status =/= 0 ->
            {false, ?ERRCODE(err331_tarot_pos_error)};
        _ ->
            {false, ?FAIL}
    end;
reward_check([{reward_pools, RewardPools}|Rest]) ->
    case lists:any(fun({_Index, _GradeId, Status}) ->
        Status == 0
    end, RewardPools) of
        true ->
            reward_check(Rest);
        _ ->
            {false, ?FAIL}
    end;
reward_check([{cost, Player, Type, SubType, OldNum}|Rest]) ->
    #custom_act_cfg{condition = Condition} = lib_custom_act_util:get_act_cfg_info(Type, SubType),
    CostGoodsType = lists:keyfind(cost_type, 1, Condition),
    CostNumList   = lists:keyfind(cost_num, 1, Condition),
    if
        CostGoodsType == false ->
            {false, ?ERRCODE(err331_tarot_cost_cfg_error)};
        CostNumList == false ->
            {false, ?ERRCODE(err331_tarot_cost_cfg_error)};
        true ->
            NewNum = OldNum + 1,
            {_, NumCostList} = CostNumList,
            Len = length(NumCostList),
            if NewNum > Len ->
                    {false, ?ERRCODE(err331_tarot_cost_cfg_error)};
                true ->
                    {_, GoodsId} = CostGoodsType,
                    NumCost  = lists:nth(NewNum, NumCostList),
                    CostList = [{?TYPE_GOODS, GoodsId, NumCost}],
                    case lib_goods_api:check_object_list(Player, CostList) of
                        true ->
                            reward_check(Rest);
                        {false, Error} ->
                            {false, Error}
                    end
            end
    end;
reward_check([{tarot_num, Num}|Rest]) ->
    if Num >= ?TAROT_POS ->
            {false, ?ERRCODE(err331_tarot_num_error)};
        true ->
            reward_check(Rest)
    end;
reward_check([_Check|_Rest]) ->
    {false, ?FAIL}.


%%折扣计算
%%先看有没有免费
%%再看看有没有折扣
calc_real_cost(_Player,[])->
    {0,[]};
calc_real_cost(Player,CostList)->
    UniteNumFree = custom_unite_buff:check_times(Player,activity_free,?CUSTOM_ACT_TYPE_TAROT),
    case UniteNumFree>0 of
        true->
            {activity_free,[]}; %%免费
        _->
            UniteNumDis = custom_unite_buff:check_times(Player,activity_discount,?CUSTOM_ACT_TYPE_TAROT),
            case UniteNumDis>0 of
                true->
                    DisC = custom_unite_buff:check_discount(Player,activity_discount),
                    [{_,CostId,CostV}|_] = CostList,
                    NeedNum = round(CostV*DisC),
                    [{_, OwnNum}] = lib_goods_api:get_goods_num(Player, [CostId]),
                    case OwnNum>=NeedNum of
                        true->
                            {activity_discount,[{K,T,NeedNum}||{K,T,_V}<-CostList]};
                        _->
                            #goods_price{price_type = PriceT,price = Per}=data_goods_price:get_goods_price(CostId),
                            case OwnNum>0 of
                                true->
                                    {activity_discount,[{?TYPE_GOODS,CostId,OwnNum},{PriceT,?GOODS_ID_GOLD,(NeedNum-OwnNum)*Per}]};
                                _->
                                    {activity_discount,[{PriceT,?GOODS_ID_GOLD,NeedNum*Per}]}
                            end
                    end;
                _->
                    {0,CostList}
            end
    end.