%%-----------------------------------------------------------------------------
%% @Module  :       lib_custom_act_loans
%% @Author  :       zmh
%% @Created :       20201116
%% @Description:	彩钻复制机
%%-----------------------------------------------------------------------------
-module (lib_custom_act_loans).

-compile(export_all).
-include("custom_act.hrl").
-include("common.hrl").
-include("server.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include("def_goods.hrl").
-include("def_module.hrl").
-include("goods.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").


%%获取已购信息
get_loans_info(SubType,Player)->
    #player_status{id=RoleID}=Player,
    CustomActData = lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_LOANS, SubType),
    #custom_act_data{act_data = ActData} = CustomActData,
    case ActData of
        [#custom_act_loans{money=Total,ltime=LTime,stime=STime,etime=ETime}|_]->
            skip;
        _->
            Total = 0,LTime=0,STime=0,ETime=0
    end,
    {ok,Bin} = pt_331:write(33131,[SubType,Total,LTime,STime,ETime]),
    lib_server_send:send_to_uid(RoleID,Bin),
    ok.

%%存金
%%1是终身首冲触发的
%%避免每次充值遍历这个活动  约定1独占
buy_loans(SubType,Add,Player) when SubType =:= 1->
    #player_status{id=RoleID,figure=#figure{lv=RoleLv}}=Player,
    CustomActData = lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_LOANS, SubType),
    #custom_act_data{act_data = ActData} = CustomActData,
    case ActData of
        [#custom_act_loans{money=Total,ltime=LTime,stime=STime,etime=ETime}|_]->
            skip;
        _->
            Total = 0,LTime=0,STime=0,ETime=0
    end,
    Now = utime:unixtime(),
    Max = get_condition_value(max_amount,?CUSTOM_ACT_TYPE_LOANS,SubType,0),
    NeedLv = get_condition_value(role_lv,?CUSTOM_ACT_TYPE_LOANS,SubType,0),
    CkList = [
        {check_lv,RoleLv,NeedLv},
        {check_add,Add},
        {check_open,STime,ETime,Now},
        {check_max,Total+Add,Max},
        {check_pre_fetch,Total,LTime}
    ],
    case util:check_lists(?MODULE,CkList) of
        true->
            Cost = [{?TYPE_GOLD,?GOODS_ID_GOLD,Add}],
            case lib_goods_api:cost_object_list_with_check(Player, Cost, act_loans, ["loans buy"]) of
                {true,NPlayer,_}->
                    NLTime = case Total=<0 of
                        true->
                            Now;
                        _->
                            LTime
                    end,
                    NewActData = [#custom_act_loans{money=Total+Add,ltime=NLTime,stime=STime,etime=ETime}],
                    lib_custom_act_data:save_custom_data(RoleID, CustomActData#custom_act_data{act_data = NewActData}),
                    lib_log_api:custom_act_reward_log(NPlayer, Player, ?CUSTOM_ACT_TYPE_LOANS, SubType, 1, Cost),
                    Res = ?SUCCESS;
                {false,Res,_}->
                    NPlayer = Player
            end;
        {false,Res}->
            NPlayer = Player
    end,
    {ok,Bin} = pt_331:write(33132,[Res,SubType]),
    lib_server_send:send_to_uid(RoleID,Bin),
    NPlayer;
%%存金
buy_loans(SubType,Add,Player)->
    #player_status{id=RoleID,figure=#figure{lv=RoleLv}}=Player,
    CustomActData = lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_LOANS, SubType),
    #custom_act_data{act_data = ActData} = CustomActData,
    case ActData of
        [#custom_act_loans{money=Total,ltime=LTime}|_]->
            skip;
        _->
            Total = 0,LTime=0
    end,
    case lib_custom_act_util:get_act_cfg_info(?CUSTOM_ACT_TYPE_LOANS,SubType) of
        #custom_act_cfg{start_time = STime,end_time=ETime}->
            skip;
        _->
            STime = 0, ETime = 0
    end,
    Max = get_condition_value(max_amount,?CUSTOM_ACT_TYPE_LOANS,SubType,0),
    NeedLv = get_condition_value(role_lv,?CUSTOM_ACT_TYPE_LOANS,SubType,0),
    CkList = [
        {check_lv,RoleLv,NeedLv},
        {check_add,Add},
        {check_open,?CUSTOM_ACT_TYPE_LOANS, SubType},
        {check_max,Total+Add,Max},
        {check_pre_fetch,Total,LTime}
    ],
    case util:check_lists(?MODULE,CkList) of
        true->
            Cost = [{?TYPE_GOLD,?GOODS_ID_GOLD,Add}],
            case lib_goods_api:cost_object_list_with_check(Player, Cost, act_loans, ["loans buy"]) of
                {true,NPlayer,_}->
                    Now = utime:unixtime(),
                    NLTime = case Total=<0 of
                        true->
                            Now;
                        _->
                            LTime
                    end,
                    NewActData = [#custom_act_loans{money=Total+Add,ltime=NLTime,stime=STime,etime=ETime}],
                    lib_custom_act_data:save_custom_data(RoleID, CustomActData#custom_act_data{act_data = NewActData}),
                    lib_log_api:custom_act_reward_log(NPlayer, Player, ?CUSTOM_ACT_TYPE_LOANS, SubType, 1, Cost),
                    Res = ?SUCCESS;
                {false,Res,_}->
                    NPlayer = Player
            end;
        {false,Res}->
            NPlayer = Player
    end,
    {ok,Bin} = pt_331:write(33132,[Res,SubType]),
    lib_server_send:send_to_uid(RoleID,Bin),
    NPlayer.

%%利息领取
fetch_loans(SubType,Player)->
    #player_status{id=RoleID}=Player,
    CustomActData = lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_LOANS, SubType),
    #custom_act_data{act_data = ActData} = CustomActData,
    case ActData of
        [#custom_act_loans{money=Total,ltime=LTime,stime=STime,etime=ETime}|_]->
            skip;
        _->
            Total = 0,LTime=0,STime=0,ETime=0
    end,
    Rate = get_condition_value(interest_rate,?CUSTOM_ACT_TYPE_LOANS,SubType,0),
    CkList0 =  [{check_loans,Total},{check_done,LTime}],
    Now = utime:unixtime(),
    CkList  = case SubType of
        1->
            [{check_open,STime,ETime,Now}|CkList0];
        _->
            [{check_open,?CUSTOM_ACT_TYPE_LOANS, SubType}|CkList0]
    end,
    case util:check_lists(?MODULE,CkList) of
        true->
            Add = util:ceil(Total*Rate/100),
            Awards = [{?TYPE_GOLD,?GOODS_ID_GOLD,Add}],
            Produce = #produce{type=act_loans,reward=Awards},
            {ok,NPlayer} = lib_goods_api:send_reward_with_mail(Player, Produce, ?MOD_AC_CUSTOM),
            NewActData = [#custom_act_loans{money=Total,ltime=Now,stime=STime,etime=ETime}],
            lib_custom_act_data:save_custom_data(RoleID, CustomActData#custom_act_data{act_data = NewActData}),
            lib_log_api:custom_act_reward_log(NPlayer, Player, ?CUSTOM_ACT_TYPE_LOANS, SubType, 2, Awards),
            Res = ?SUCCESS;
        {false,Res}->
            NPlayer = Player
    end,
    {ok,Bin} = pt_331:write(33133,[Res,SubType]),
    lib_server_send:send_to_uid(RoleID,Bin),
    NPlayer.

check({check_lv,RoleLv,NeedLv})->
    case RoleLv>=NeedLv of
        true->
            true;
        _->
            {false,?ERRCODE(err333_role_lev_less)}
    end;
check({check_pre_fetch,_Total,LTime}) when LTime=<0->
    true;
check({check_pre_fetch,Total,LTime})->
    case utime:is_today(LTime) of
        true->
            true;
        _->
            case Total=<0 of
                true->
                    true;
                _->
                    {false,?ERRCODE(err331_need_fetch_prior)}
            end
    end;
check({check_add,Add})->
    case Add>0 andalso Add rem 100 =:= 0 of
        true->
            true;
        _->
            {false,?ERRCODE(args_error)}
    end;
check({check_done,LTime})->
    case utime:is_today(LTime) of
        true->
            {false,?ERRCODE(err331_has_fetch_today)};
        _->
            true
    end;
check({check_loans,Total})->
    case Total>0 of
        true->
            true;
        _->
            {false,?ERRCODE(err331_need_loans_gold)}
    end;
check({check_open,STime,ETime,Now})->
    case STime=<Now andalso Now=<ETime of
        true->
            true;
        _->
            {false,?ERRCODE(err331_act_closed)}
    end;
check({check_open,Type,SubType})->
    case lib_custom_act_api:is_open_act(Type,SubType) of
        true->
            true;
        _->
            {false,?ERRCODE(err331_act_closed)}
    end;
check({check_max,Total,Max})->
    case Total>Max of
        true->
            {false,?ERRCODE(err331_been_full_loans)};
        _->
            true
    end.


%%跨天
handle_event(Status, #event_callback{type_id = ?EVENT_ZERO_CLOCK}) ->
    Delay = urand:rand(60,300)*1000,
    erlang:send_after(Delay,self(),{'mod',?MODULE,check_delay_mail,[]}),
    SubTypes = lib_custom_act_api:get_open_subtype_ids(?CUSTOM_ACT_TYPE_LOANS),
    [get_loans_info(SubType,Status)||SubType<-SubTypes],
    {ok,Status};
%%充值事件
handle_event(Status, #event_callback{type_id = ?EVENT_RECHARGE}) ->
    #player_status{id=RoleID}=Status,
    SubType = 1,
    case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_LOANS, SubType) of
        true->
            CustomActData = lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_LOANS, SubType),
            #custom_act_data{act_data = ActData} = CustomActData,
            case ActData of
                [#custom_act_loans{stime=STime}|_]->
                    skip;
                _->
                    STime=0
            end,
            case STime>0 of
                true-> %%不需要重复激活
                    skip;
                _-> %%初次激活活动
                    NSTime =  utime:unixdate(),
                    Day = get_condition_value(last_days,?CUSTOM_ACT_TYPE_LOANS, SubType,0),
                    NETime = utime:that_day_after(Day),
                    NewActData = [#custom_act_loans{ltime=NSTime,stime=NSTime,etime=NETime}],
                    lib_custom_act_data:save_custom_data(RoleID, CustomActData#custom_act_data{act_data = NewActData}),
                    get_loans_info(SubType,Status)
            end;
        _->
            skip
    end,
    {ok,Status};
%%登录
handle_event(Status, #event_callback{type_id = ?EVENT_LOGIN_CAST}) ->
    check_delay_mail(Status),
    {ok,Status};
handle_event(Status, _) -> {ok, Status}.


%%获取活动KeyV
get_condition_value(Key,Type,SubType,DefV)->
    case lib_custom_act_util:get_act_cfg_info(Type,SubType) of
        #custom_act_cfg{condition=Cons}->
            case lists:keyfind(Key,1,Cons) of
                {_,KeyV}->
                    skip;
                _->
                    KeyV = DefV
            end;
        _->
            KeyV = DefV
    end,
    KeyV.


check_delay_mail(Status)->
    Type = ?CUSTOM_ACT_TYPE_LOANS,
    SubTypes = lib_custom_act_util:get_subtype_list(Type),
    Now = utime:unixtime(),
    check_delay_mail(Status,Now,Type,SubTypes).

check_delay_mail(Status,_Now,_Type,[])->
    Status;
check_delay_mail(Status,Now,Type,[H|T])->
    #player_status{id=RoleID}=Status,
    CustomActData = lib_custom_act_data:custom_act_data(Type, H),
    #custom_act_data{act_data = ActData} = CustomActData,
    case ActData of
        [#custom_act_loans{money=Total,stime=STime,etime=ETime}|_] when Total>0->
            case Now>=ETime of
                true->
                    case H of
                        1->
                            NewActData = [#custom_act_loans{stime=STime,etime=ETime}],
                            lib_custom_act_data:save_custom_data(RoleID, CustomActData#custom_act_data{act_data = NewActData});
                        _->
                            lib_custom_act_data:role_clean_up_custom_data(Status, Type, H)
                    end,
                    Rate = get_condition_value(interest_rate,Type,H,0),
                    Add = util:ceil(Total*Rate/100),
                    Gain = [{?TYPE_GOLD,?GOODS_ID_GOLD,Total+Add}],
                    Title = utext:get(3310501),
                    Content = utext:get(3310502),
                    lib_log_api:custom_act_reward_log(Status, Status, Type, H, 3, Gain),
                    lib_mail_api:send_sys_mail([RoleID], Title, Content, Gain);
                _->
                    skip
            end,
            check_delay_mail(Status,Now,Type,T);
        _->
            check_delay_mail(Status,Now,Type,T)
    end.
