<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/4/10
 * Time: 下午9:52
 */

namespace bonus;

use Exception;
use errcode;
use Log;
use trans_wapper;

abstract class IGrab
{
    protected $mType;
    public function __construct($type) {
        $this->mType = $type;
    }

    abstract public function get_bonus($paramer);
}

class DirectGrab extends IGrab
{
    public function __construct($type) {
        parent::__construct($type);
    }

    public function get_bonus($type)
    {
        throw new Exception("指定人群的红包不用抢，直接领就好",errcode::ErrBonusType);
    }
}

class general_grab extends IGrab
{
    public function __construct($paramer) {
        parent::__construct($paramer);
    }

    public function get_bonus($paramer)
    {
        Log::record("general_grab->get_bonus begin",Log::DEBUG);

        $time_out = $paramer['time_out'];
        $session_id = $paramer['session_id'];
        $mobile = $paramer['mobile'];
        $type_id = $this->mType->getType_id();

        $user_bonus = Model('user_bonus');

        $is_new_grab = false;
        $is_new_bind = false;
        $bonus = $user_bonus->grab($type_id,$time_out,$mobile,$session_id,$is_new_grab,$is_new_bind);

        if(!empty($bonus))
        {
            $user_bonus = user_bonus::create_by_param($bonus);
            try
            {
                $trans = new trans_wapper(null,__METHOD__);
                if($is_new_grab && $is_new_bind)
                {
                    $bonus_val = $user_bonus->bonus_value();
                    Model('bonus_type')->edit(array('type_id' => $type_id),
                        array('grabed_num'    => array('exp', 'grabed_num+1'),
                            'binded_num'    => array('exp', 'binded_num+1'),
                            'remain_amount' => array('exp', "remain_amount-{$bonus_val}")));
                }
                else if($is_new_bind)
                {
                    $bonus_val = $user_bonus->bonus_value();
                    Model('bonus_type')->edit(array('type_id' => $type_id),
                        array('binded_num'    => array('exp', 'binded_num+1'),
                            'remain_amount' => array('exp', "remain_amount-{$bonus_val}")));
                }
                else if($is_new_grab) {
                    Model('bonus_type')->edit(array('type_id' => $type_id), array('grabed_num' => array('exp', 'grabed_num+1')));
                }
                $trans->commit();
                Log::record("general_grab->get_bonus end with one bonus.",Log::DEBUG);
                return $user_bonus;
            } catch (Exception $ex) {
                $trans->rollback();
                return array();
            }
        }
        else {
            Log::record("general_grab->get_bonus end with not grab bonus",Log::DEBUG);
            return array();
        }
    }
}

function create_grab($type)
{
    if(!$type->isStart()) {
        throw new Exception("红包还没开始抢",errcode::ErrBonusType);
    }
    if($type->isEnd()) {
        throw new Exception("抢红包已经结束",errcode::ErrBonusType);
    }

    if($type->isDirectType()) {
        return new DirectGenerator($type);
    }
    else if($type->isGeneralType()) {
        return new general_grab($type);
    }
    else {
        throw new Exception("错误的红包类型",errcode::ErrBonusType);
    }
}

?>