<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/1/6
 * Time: 下午5:56
 */
namespace bonus;

use Exception;
use member_info;
use bonus;

class parameters
{
    const admin_member_id = 36429;
    const admin_name      = '熊猫美妆';
    const usable_days     = 30;

    public static function invite($inviter)
    {
        try
        {
            $info = new member_info($inviter);
            $nickname = $info->nickname();
            $type_name = "{$nickname}的红包";
        }
        catch (Exception $ex) {
            $type_name = "邀请好友红包";
            $nickname = self::admin_name;
        }

        $amount = 10;
        $bless  = "领{$amount}元美妆红包，加入 {$nickname} 亲友团";
        $param['make_type']  = type::MakeInviteType;
        $param['type_name']  = $type_name;
        $param['type_bless'] = $bless;
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_EXEXIST;
        $param['relayer_id'] = $inviter;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = ['amount' => $amount,'num' => 10, 'rate' => 50];

        return $param;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static function user_reigster_fixed()
    {
        $params = [];
        $param['make_type']  = type::MakeRegisterType;
        $param['type_name']  = "新人首单红包";
        $param['type_bless'] = "用熊猫美妆，购百分百正品";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']    = self::admin_member_id;
        $param['sender_name']  = self::admin_name;
        $param['usable_days']  = self::usable_days;
        $param['rate_money'][] = ['amount' => 100,'num' => 1, 'rate' => 30];
        $params[] = $param;

        return $params;
    }

    public static function guide_inviter($nickname,$level)
    {
        $rate_moneys = [
            ['amount' => 5,   'num' => 1, 'rate' => 100],
            ['amount' => 1,   'num' => 1, 'rate' => 100],
            ['amount' => 0.5, 'num' => 1, 'rate' => 100],
            ['amount' => 0.25,'num' => 1, 'rate' => 100],
            ['amount' => 0.10,'num' => 1, 'rate' => 100],
        ];

        $count = count($rate_moneys);
        $cur_index = $level < $count ? $level : $count -1;

        $params = [];

        $param['make_type'] = type::MakeInviteRewardType;
        $param['type_name'] = "邀请 {$nickname}，奖励红包";
        $level = $level + 1;
        $param['type_bless'] = "{$nickname} 成为您的{$level}度粉丝";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = $rate_moneys[$cur_index];
        $params[] = $param;

        return $params;
    }

    public static function guide_invitee($inviter_name, $invitee_name, $level, $num)
    {
        $rate_moneys = [
            ['amount' => 0.5,  'num' => $num, 'rate' => 100],
            ['amount' => 0.25, 'num' => $num, 'rate' => 100],
            ['amount' => 0.10, 'num' => $num, 'rate' => 100],
        ];

        $count = count($rate_moneys);
        $cur_index = $level < $count ? $level : $count -1;

        $params = [];
        $param['make_type'] = type::MakeInviteRewardType;
        $param['type_name'] = "雨露均沾红包";
        $level = $level + 1;
        $param['type_bless'] = "{$invitee_name}成为{$inviter_name}的{$level}度粉丝";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = $rate_moneys[$cur_index];
        $params[] = $param;

        return $params;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    static private function order_complete_fixed_rates($order_amount)
    {
        $money = intval($order_amount);

        if($money > 1000) {
            return ['amount' => 100,'num' => 1, 'rate' => 40];
        }
        elseif($money > 500) {
            return ['amount' => 100,'num' => 1, 'rate' => 30];
        }
        elseif ($money > 300) {
            return ['amount' => 100,'num' => 1, 'rate' => 30];
        }
        elseif ($money > 200) {
            return ['amount' => 50,'num' => 1, 'rate' => 30];
        }
        elseif($money > 0) {
            return ['amount' => 30,'num' => 1, 'rate' => 30];
        }
        else {
            return ['amount' => 5,'num' => 1, 'rate' => 30];
        }
    }

    static private function order_complete_random_rates($order_amount)
    {
        $money = intval($order_amount/100) * 100;

        if($money > 1000) {
            return ['amount' => 200,'num' => 10, 'rate' => 40];
        }
        elseif($money > 500) {
            return ['amount' => 200,'num' => 10, 'rate' => 30];
        }
        elseif ($money > 300) {
            return ['amount' => 200,'num' => 10, 'rate' => 30];
        }
        elseif ($money > 200) {
            return ['amount' => 100,'num' => 10, 'rate' => 30];
        }
        else {
            return ['amount' => 100,'num' => 10, 'rate' => 30];
        }
    }

    public static function order_complete_fixed($order_amount)
    {
        $params = [];
        $param['make_type']   = type::MakePayType;
        $param['type_name']   = "下单奖励";
        $param['type_bless']  = "我们看重你的每一笔订单，也希望后会有期～";
        $param['send_type']   = type::SendType_Fixed;
        $param['grab_type']   = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days + 15;
        $param['rate_money'][] = self::order_complete_fixed_rates($order_amount);

        $params[] = $param;
        return $params;
    }

    public static function order_complete_random($order_amount)
    {
        $params = [];
        $param['make_type']  = type::MakePayType;
        $param['type_name']  = "下单奖励";
        $param['type_bless'] = "我们看重你的每一笔订单，也希望后会有期～";
        $param['send_type']  = type::SendType_Random;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = self::order_complete_random_rates($order_amount);
        $params[] = $param;

        return $params;
    }

    public static function order_complete_inviter($cash,$nickname,$level)
    {
        if($cash > 10)
        {
            $rate_moneys = [
                ['amount' => 3,   'num' => 1, 'rate' => 100],
                ['amount' => 1,   'num' => 1, 'rate' => 100],
                ['amount' => 0.5, 'num' => 1, 'rate' => 100],
                ['amount' => 0.25,'num' => 1, 'rate' => 100],
                ['amount' => 0.10,'num' => 1, 'rate' => 100],
                ['amount' => 0.05,'num' => 1, 'rate' => 100],
            ];
        }
        else
        {
            $rate_moneys = [
                ['amount' => 0.5, 'num' => 1, 'rate' => 100],
                ['amount' => 0.25,'num' => 1, 'rate' => 100],
                ['amount' => 0.10,'num' => 1, 'rate' => 100],
                ['amount' => 0.05,'num' => 1, 'rate' => 100],
            ];
        }


        $count = count($rate_moneys);
        $cur_index = $level < $count ? $level : $count -1;

        $params = [];

        $level = $level + 1;
        $param['make_type'] = type::MakePayRewardInviterType;
        $param['type_name'] = "{$level}度粉丝下单，奖励红包";
        $param['type_bless'] = "您的{$level}度粉丝 {$nickname} 下单成功";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = $rate_moneys[$cur_index];
        $params[] = $param;

        return $params;
    }

    public static function order_complete_all($nickname,$num,$amount)
    {
        $params = [];
        $param['make_type'] = type::MakePayRewardInviterType;
        $param['type_name'] = "{$nickname}下单，雨露均沾红包";
        $param['type_bless'] = "{$nickname}下单引发红包风暴来袭，{$amount}元全抵红包人人有份～";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = 7;
        $param['rate_money'][] =  ['amount' => $amount,   'num' => $num, 'rate' => 100];
        $params[] = $param;

        return $params;
    }

    public static function order_complete_invitee($nickname, $level, $num)
    {
        $rate_moneys = [
            ['amount' => 0.5, 'num' => $num, 'rate' => 100],
            ['amount' => 0.25,'num' => $num, 'rate' => 100],
            ['amount' => 0.10,'num' => $num, 'rate' => 100],
        ];

        $count = count($rate_moneys);
        $cur_index = $level < $count ? $level : $count -1;

        $params = [];

        $param['make_type'] = type::MakePayRewardInviteeType;
        $param['type_name'] = "雨露均沾红包";
        $param['type_bless'] = "好友 {$nickname} 下单成功";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = $rate_moneys[$cur_index];
        $params[] = $param;

        return $params;
    }

    public static function order_evaluate_fixed()
    {
        $params = [];
        $param['make_type'] = type::MakeEvaluateType;
        $param['type_name'] = "订单评论奖励红包";
        $param['type_bless'] = "感谢您的每一个评价，小熊猫都会继续努力～";
        $param['send_type'] = type::SendType_Fixed;
        $param['grab_type'] = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = ['amount' => 5,'num' => 1, 'rate' => 50];
        $params[] = $param;

        return $param;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static function poor_user()
    {
        $params = [];

        $param['make_type'] = type::MakeAllowanceType;
        $param['type_name']  = "津贴红包";
        $param['type_bless'] = "您的红包余额不足，好心人紧急为您充值～";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = ['amount' => 50, 'num' => 1, 'rate' => 20];
        $params[] = $param;

        return $params;
    }
    /////////////////////////////////////////////////////////////////////////////////////////////
    public static function awken_user($rate,$amount,$num,$name,$bless)
    {
        $params = [];

        $param['make_type'] = type::MakeAllowanceType;
        $param['type_name']  = $name;
        $param['type_bless'] = "$bless";
        $param['send_type']  = type::SendType_Fixed;
        $param['grab_type']  = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = ['amount' => $amount, 'num' => $num, 'rate' => $rate];
        $params[] = $param;

        return $params;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static function admin_fixed($rate,$amount,$num,$type_name)
    {
        $param['make_type'] = type::MakeSendType;
        $param['type_name'] = empty($type_name) ? "系统红包" : $type_name;
        $param['send_type'] = type::SendType_Fixed;
        $param['grab_type'] = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = ['amount' => $amount,'num' => $num, 'rate' => $rate];

        return $param;
    }

    public static function appreciate($sender_id,$rate,$amount,$bless)
    {
        $param['make_type'] = type::MakeSendType;
        $param['type_name'] = "赞赏红包";
        $param['type_bless'] = $bless;
        $param['send_type'] = type::SendType_Fixed;
        $param['grab_type'] = type::GrabType_All;
        $param['sender_id']   = $sender_id;
        $param['sender_name'] = self::admin_name;
        $param['usable_days'] = self::usable_days;
        $param['rate_money'][] = ['amount' => $amount,'num' => 1, 'rate' => $rate];

        return $param;
    }

    public static function shake_system($rate_moneys)
    {
        $param['make_type'] = type::MakeShakeGainType;
        $param['type_name'] = "从熊猫美妆摇来的红包";
        $param['send_type'] = type::SendType_Random;
        $param['grab_type'] = type::GrabType_All;
        $param['sender_id']   = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['rate_money'] = $rate_moneys;

        return $param;
    }

    public static function shake_gain($from_id,$mobile,$nickname,$rate,$money)
    {
        $param['type_name'] = "从{$nickname}摇来的红包";
        $param['sender_id'] = $from_id;
        $param['sender_mobile'] = $mobile;
        $param['sender_name'] = $nickname;
        $param['make_type'] = type::MakeShakeGainType;
        $param['send_type'] = type::SendType_Fixed;
        $param['grab_type'] = type::GrabType_All;
        $param['rate_money'][] = ['amount' => $money,'num' => 1, 'rate' => $rate];
        return $param;
    }

    public static function shake_lost($from_id,$mobile,$nickname,$rate,$money)
    {
        $param['sender_id'] = $from_id;
        $param['sender_mobile'] = $mobile;
        $param['sender_name'] = $nickname;
        $param['make_type'] = type::MakeShakeLostType;
        $param['type_name'] = "{$nickname}摇飞的红包";
        $param['send_type'] = type::SendType_Fixed;
        $param['grab_type'] = type::GrabType_All;
        $param['rate_money'][] = ['amount' => $money,'num' => 1, 'rate' => $rate];
        return $param;
    }

    public static function order_cancel($bonus_rates)
    {
        $params = [];
        foreach ($bonus_rates as $rate => $amount)
        {
            $param = [];
            $param['make_type'] = bonus\type::MakeOrderCancelType;
            $param['type_name'] = '订单退款';
            $param['sender_id'] = self::admin_member_id;
            $param['sender_name'] = self::admin_name;
            $param['send_type'] = type::SendType_Fixed;
            $param['grab_type'] = type::GrabType_All;
            $param['rate_money'][] = ['amount' => $amount,'num' => 1, 'rate' => $rate];

            $params[] = $param;
        }
        return $params;
    }

    public static function bonus_refund($rate,$amount)
    {
        $param['make_type'] = \bonus\type::MakeBonusRefundType;
        $param['type_name'] = '红包过期退款';
        $param['sender_id'] = self::admin_member_id;
        $param['sender_name'] = self::admin_name;
        $param['send_type'] = type::SendType_Fixed;
        $param['grab_type'] = type::GrabType_All;
        $param['rate_money'][] = ['amount' => $amount,'num' => 1, 'rate' => $rate];

        return $param;
    }
}

class sys_bonus
{
    private $mType_sn;
    public function __construct() {
        $this->mType_sn = null;
    }
    public function reset($type_sn) {
        $this->mType_sn = $type_sn;
    }
    private function rate_moneys()
    {
        $result = [];
        {
            $item['amount'] = 50;
            $item['num'] = 10;
            $item['rate'] = 50;
            $result[] = $item;
        }
        {
            $item['amount'] = 100;
            $item['num'] = 20;
            $item['rate'] = 40;
            $result[] = $item;
        }
        {
            $item['amount'] = 850;
            $item['num'] = 70;
            $item['rate'] = 30;
            $result[] = $item;
        }

        return $result;
    }

    public function type_sn() {
        return $this->mType_sn;
    }
    public function bind_over()
    {
        if($this->mType_sn == null) {
            return true;
        }
        $type = type::create_by_sn($this->mType_sn);
        return $type->binded_over();
    }

    public function gen_param(&$rates)
    {
        $rates = $this->rate_moneys();
        return parameters::shake_system($rates);
    }
}