<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/6/27
 * Time: 上午12:34
 * 扣款者,用来做红包扣款
 */

namespace bonus;

use bonus_helper;
use QueueClient;

function asc_usable(user_bonus $left,user_bonus $right)
{
    $t_l = $left->usable_time();
    $t_r = $right->usable_time();

    if($t_l > $t_r) {
        return 1;
    }
    elseif($t_l == $t_r) {
        return 0;
    }
    else {
        return -1;
    }
}

class witholder
{
    private $member_id;
    private $mRateBonus;
    private $usable_amount; //可用红包总金额
    private $mUsedType;

    private $mod_bonus;

    public function __construct($member_id,$used_type)
    {
        $this->mod_bonus = Model('user_bonus');
        $this->member_id = $member_id;
        $this->mRateBonus = array();
        $this->usable_amount = 0.00;

        $this->usable_amount = $this->mod_bonus->getUsableSum($this->member_id);
        $items = $this->mod_bonus->getUsableBonus($this->member_id);

        foreach ($items as $item) {
            $bonus = user_bonus::create_by_param($item);
            $this->add_bouns($bonus);
        }

        foreach ($this->mRateBonus as &$item) {
            $bonuses = &$item['bonus'];
            uasort($bonuses,'bonus\asc_usable');
        }
        $this->mUsedType = $used_type;
    }

    private function add_bouns(user_bonus $bonus)
    {
        $rate = $bonus->bonus_rate();
        if(array_key_exists($rate,$this->mRateBonus) == false) {
            $this->mRateBonus[$rate]['bonus'] = [];
            $this->mRateBonus[$rate]['amount'] = 0.00;
            $this->mRateBonus[$rate]['rate'] = $rate;
        }

        $this->mRateBonus[$rate]['bonus'][] = $bonus;
        $this->mRateBonus[$rate]['amount'] = $this->mRateBonus[$rate]['amount'] + $bonus->remain_amount();
    }

    public function usable_amount() {
        return $this->usable_amount;
    }

    public function withold_bonus($rate,$bonus_sn,$money)
    {
        if(array_key_exists($rate,$this->mRateBonus) == false) return false;

        $bounses = $this->mRateBonus[$rate]['bonus'];
        foreach ($bounses as $bonus)
        {
            if($bonus_sn == $bonus->bonus_sn())
            {
                $left  = intval($bonus->remain_amount() * 100 + 0.5);
                $money = intval($money * 100 + 0.5);
                $left = $left - $money;
                $left = $left >= 0 ? $left / 100 : 0.00;
                $rate = $bonus->bonus_rate();

                $param = $bonus->get_param();
                $param['remain_amount'] = $left;
                if($this->mUsedType == bonus_helper::send_bonus_withold) {
                    $send = intval($bonus->send_amount() * 100 + 0.5);
                    $send = $send + $money;
                    $send = $send / 100;
                    $param['send_amount'] = $send;
                }
                elseif($this->mUsedType == bonus_helper::pay_order_withold) {
                    $paied = intval($bonus->pay_amount() * 100 + 0.5);
                    $paied = $paied + $money;
                    $paied = $paied / 100;
                    $param['pay_amount'] = $paied;
                }
                QueueClient::push("onUseBonus",['time' => time(),'use_type' => $this->mUsedType,'rate' => $rate,'amount' => $money / 100,'member_id' => $this->member_id]);

                $this->mod_bonus->replace($param);
                return true;
            }
        }
        return false;
    }

    public function withold($rate,$money)
    {
        if(array_key_exists($rate,$this->mRateBonus) == false) {
            return false;
        }

        $r_bonuses = &$this->mRateBonus[$rate];

        $left = intval($money * 100 + 0.5);
        if($left <= 0 || intval($r_bonuses['amount'] * 100 + 0.5) < $left) {
            return false;
        }
        $r_bonuses['amount'] -= $money;

        $datas = array();
        $bonuses = &$r_bonuses['bonus'];
        foreach ($bonuses as $bonus)
        {
            if($left <= 0) break;

            $rate = $bonus->bonus_rate();
            $param = $bonus->get_param();
            $remain = $bonus->remain_amount();
            $remain = intval($remain * 100 + 0.5);

            if($left >= $remain) {
                $left -= $remain;
                $param['remain_amount'] = 0.00;
                $used = $remain;
            } else {
                $remain -= $left;
                $param['remain_amount'] = $remain / 100;
                $used = $left;
                $left = 0;
            }

            if($this->mUsedType == bonus_helper::send_bonus_withold) {
                $send = intval($bonus->send_amount() * 100 + 0.5);
                $send = $send + $used;
                $send = $send / 100;
                $param['send_amount'] = $send;
            }
            elseif($this->mUsedType == bonus_helper::pay_order_withold) {
                $paied = intval($bonus->pay_amount() * 100 + 0.5);
                $paied = $paied + $used;
                $paied = $paied / 100;
                $param['pay_amount'] = $paied;
            }

            QueueClient::push("onUseBonus",['time' => time(),'use_type' => $this->mUsedType,'rate' => $rate,'amount' => $used / 100,'member_id' => $this->member_id]);
            array_push($datas,$param);
        }

        if(!empty($datas)) {
            $this->mod_bonus->replaceAll($datas);
        }

        return true;
    }
}