grab.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/10
  6. * Time: 下午9:52
  7. */
  8. namespace bonus;
  9. use Exception;
  10. use errcode;
  11. use Log;
  12. use trans_wapper;
  13. abstract class IGrab
  14. {
  15. protected $mType;
  16. public function __construct($type) {
  17. $this->mType = $type;
  18. }
  19. abstract public function get_bonus($paramer);
  20. }
  21. class DirectGrab extends IGrab
  22. {
  23. public function __construct($type) {
  24. parent::__construct($type);
  25. }
  26. public function get_bonus($type)
  27. {
  28. throw new Exception("指定人群的红包不用抢,直接领就好",errcode::ErrBonusType);
  29. }
  30. }
  31. class general_grab extends IGrab
  32. {
  33. public function __construct($paramer) {
  34. parent::__construct($paramer);
  35. }
  36. public function get_bonus($paramer)
  37. {
  38. Log::record("general_grab->get_bonus begin",Log::DEBUG);
  39. $time_out = $paramer['time_out'];
  40. $session_id = $paramer['session_id'];
  41. $mobile = $paramer['user_mobile'];
  42. $user_id = $paramer['user_id'];
  43. $type_id = $this->mType->getType_id();
  44. $user_bonus = Model('user_bonus');
  45. $is_new_grab = false;
  46. $is_new_bind = false;
  47. $bonus = $user_bonus->grab($type_id,$time_out,$mobile,$session_id,$user_id,$is_new_grab,$is_new_bind);
  48. if(!empty($bonus))
  49. {
  50. $user_bonus = user_bonus::create_by_param($bonus);
  51. try
  52. {
  53. $trans = new trans_wapper(null,__METHOD__);
  54. if($is_new_grab && $is_new_bind)
  55. {
  56. $bonus_val = $user_bonus->bonus_value();
  57. Model('bonus_type')->edit(array('type_id' => $type_id),
  58. array('grabed_num' => array('exp', 'grabed_num+1'),
  59. 'binded_num' => array('exp', 'binded_num+1'),
  60. 'remain_amount' => array('exp', "remain_amount-{$bonus_val}")));
  61. }
  62. else if($is_new_bind)
  63. {
  64. $bonus_val = $user_bonus->bonus_value();
  65. Model('bonus_type')->edit(array('type_id' => $type_id),
  66. array('binded_num' => array('exp', 'binded_num+1'),
  67. 'remain_amount' => array('exp', "remain_amount-{$bonus_val}")));
  68. }
  69. else if($is_new_grab) {
  70. Model('bonus_type')->edit(array('type_id' => $type_id), array('grabed_num' => array('exp', 'grabed_num+1')));
  71. }
  72. $trans->commit();
  73. Log::record("general_grab->get_bonus end with one bonus.",Log::DEBUG);
  74. return $user_bonus;
  75. } catch (Exception $ex) {
  76. $trans->rollback();
  77. return array();
  78. }
  79. }
  80. else {
  81. Log::record("general_grab->get_bonus end with not grab bonus",Log::DEBUG);
  82. return array();
  83. }
  84. }
  85. }
  86. function create_grab($type)
  87. {
  88. if(!$type->isStart()) {
  89. throw new Exception("红包还没开始抢",errcode::ErrBonusType);
  90. }
  91. if($type->isEnd()) {
  92. throw new Exception("抢红包已经结束",errcode::ErrBonusType);
  93. }
  94. if($type->isDirectType()) {
  95. return new DirectGenerator($type);
  96. }
  97. else if($type->isGeneralType()) {
  98. return new general_grab($type);
  99. }
  100. else {
  101. throw new Exception("错误的红包类型",errcode::ErrBonusType);
  102. }
  103. }
  104. ?>