123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?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['user_mobile'];
- $user_id = $paramer['user_id'];
- $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,$user_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);
- }
- }
- ?>
|