user_bonus.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/17
  6. * Time: 下午7:03
  7. */
  8. // `bonus_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  9. // `type_id` tinyint(3) unsigned NOT NULL DEFAULT '0',
  10. // `bonus_sn` bigint(20) unsigned NOT NULL DEFAULT '0',
  11. // `bonus_value` decimal(10,2) NOT NULL DEFAULT '0.00',
  12. // `user_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
  13. // `user_mobile` varchar(20) NOT NULL DEFAULT '',
  14. // `user_name` varchar(45) NOT NULL,
  15. // `user_comment` varchar(45) NOT NULL DEFAULT '',
  16. // `get_time` int(11) unsigned NOT NULL DEFAULT '0',
  17. // `status` int(2) NOT NULL DEFAULT '0' COMMENT '0 没有被抢,1,被抢,2 被领',
  18. // `grab_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '抢红包时间点',
  19. // `session_id` varchar(45) NOT NULL DEFAULT '',
  20. namespace bonus;
  21. use \Exception;
  22. use \errcode;
  23. class user_bonus
  24. {
  25. const OriginStatus = 0;
  26. const GrabStatus = 1;
  27. const BindStatus = 2;
  28. const TopupStatus = 3;
  29. private $mParamer;
  30. private function __construct($param) {
  31. $this->mParamer = $param;
  32. }
  33. public function isBinded() {
  34. return (intval($this->mParamer['bonus_status']) >= 2);
  35. }
  36. public function grab_time()
  37. {
  38. return intval($this->mParamer['grab_time']);
  39. }
  40. public function bonus_sn() {
  41. return $this->mParamer['bonus_sn'];
  42. }
  43. public function bonus_id() {
  44. return intval($this->mParamer['bonus_id']);
  45. }
  46. public function type_id() {
  47. return intval($this->mParamer['type_id']);
  48. }
  49. public function bonus_value() {
  50. $val = intval(doubleval($this->mParamer['bonus_value']) * 100 + 0.5);
  51. return doubleval($val) / 100;
  52. }
  53. public function bonus_status()
  54. {
  55. if(!isset($this->mParamer['bonus_status']) || empty($this->mParamer['bonus_status'])) {
  56. return 0;
  57. } else {
  58. return intval($this->mParamer['bonus_status']);
  59. }
  60. }
  61. public function can_share() {
  62. return intval($this->mParamer['can_share']) == 1;
  63. }
  64. public function client_can_share() {
  65. return (!$this->expired() && $this->can_share() && $this->spend_over() == false && $this->isBinded());
  66. }
  67. public function can_shake() {
  68. $status = intval($this->mParamer['bonus_status']);
  69. return ($status == self::GrabStatus || $status == self::BindStatus);
  70. }
  71. public function expired() {
  72. $usable_time = intval($this->mParamer['usable_time']);
  73. return ($usable_time <= time());
  74. }
  75. public function remain_amount() {
  76. $val = intval(doubleval($this->mParamer['remain_amount']) * 100 + 0.5);
  77. return doubleval($val) / 100;
  78. }
  79. public function pay_amount() {
  80. $val = intval(doubleval($this->mParamer['pay_amount']) * 100 + 0.5);
  81. return doubleval($val) / 100;
  82. }
  83. public function send_amount() {
  84. $val = intval(doubleval($this->mParamer['send_amount']) * 100 + 0.5);
  85. return doubleval($val) / 100;
  86. }
  87. public function spend_over() {
  88. $remain = intval(doubleval($this->mParamer['remain_amount']) * 100 + 0.5);
  89. return ($remain == 0);
  90. }
  91. public function type_sn() {
  92. return $this->mParamer['type_sn'];
  93. }
  94. public function get_param() {
  95. return $this->mParamer;
  96. }
  97. public function user_mobile() {
  98. return $this->mParamer['user_mobile'];
  99. }
  100. public function user_name()
  101. {
  102. $user_name = $this->mParamer['user_name'];
  103. if(empty($user_name)) {
  104. $user_name = substr_replace($this->mParamer['user_mobile'], '****', 3, 4);
  105. }
  106. return $user_name;
  107. }
  108. public function user_id() {
  109. return intval($this->mParamer['user_id']);
  110. }
  111. public function user_comment()
  112. {
  113. $user_comment = $this->mParamer['user_comment'];
  114. if(mb_strlen($user_comment) < 20) {
  115. return $user_comment;
  116. } else {
  117. return mb_substr($user_comment,0,15) . '...';
  118. }
  119. }
  120. public function get_time()
  121. {
  122. return intval($this->mParamer['get_time']);
  123. }
  124. public function get_time_format() {
  125. $get_time = $this->mParamer['get_time'];
  126. return strftime("%m-%d %H:%M",$get_time);
  127. }
  128. public function usable_time() {
  129. return intval($this->mParamer['usable_time']);
  130. }
  131. public function bonus_rate() {
  132. return intval($this->mParamer['bonus_rate']);
  133. }
  134. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. static public function create_by_param($param) {
  136. return new user_bonus($param);
  137. }
  138. static public function create_by_sn($bonus_sn)
  139. {
  140. $mod_bonus = Model('user_bonus');
  141. $items = $mod_bonus->get(array('bonus_sn' => $bonus_sn));
  142. if(empty($items)) {
  143. throw new Exception("错误的红包SN号.",errcode::ErrBonusSN);
  144. } else {
  145. return new user_bonus($items[0]);
  146. }
  147. }
  148. }