user_bonus.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_shake() {
  62. $status = intval($this->mParamer['bonus_status']);
  63. return ($status == self::GrabStatus || $status == self::BindStatus);
  64. }
  65. public function expired() {
  66. $usable_time = intval($this->mParamer['usable_time']);
  67. return ($usable_time <= time());
  68. }
  69. public function remain_amount() {
  70. $val = intval(doubleval($this->mParamer['remain_amount']) * 100 + 0.5);
  71. return doubleval($val) / 100;
  72. }
  73. public function spend_over() {
  74. $remain = intval(doubleval($this->mParamer['remain_amount']) * 100 + 0.5);
  75. return ($remain == 0);
  76. }
  77. public function type_sn() {
  78. return $this->mParamer['type_sn'];
  79. }
  80. public function get_param() {
  81. return $this->mParamer;
  82. }
  83. public function user_mobile() {
  84. return $this->mParamer['user_mobile'];
  85. }
  86. public function user_name()
  87. {
  88. $user_name = $this->mParamer['user_name'];
  89. if(empty($user_name)) {
  90. $user_name = substr_replace($this->mParamer['user_mobile'], '****', 3, 4);
  91. }
  92. return $user_name;
  93. }
  94. public function user_id() {
  95. return intval($this->mParamer['user_id']);
  96. }
  97. public function user_comment()
  98. {
  99. $user_comment = $this->mParamer['user_comment'];
  100. if(mb_strlen($user_comment) < 20) {
  101. return $user_comment;
  102. } else {
  103. return mb_substr($user_comment,0,15) . '...';
  104. }
  105. }
  106. public function get_time()
  107. {
  108. return intval($this->mParamer['get_time']);
  109. }
  110. public function get_time_format() {
  111. $get_time = $this->mParamer['get_time'];
  112. return strftime("%m-%d %H:%M",$get_time);
  113. }
  114. public function usable_time() {
  115. return intval($this->mParamer['usable_time']);
  116. }
  117. public function bonus_rate() {
  118. return intval($this->mParamer['bonus_rate']);
  119. }
  120. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  121. static public function create_by_param($param) {
  122. return new user_bonus($param);
  123. }
  124. static public function create_by_sn($bonus_sn)
  125. {
  126. $mod_bonus = Model('user_bonus');
  127. $items = $mod_bonus->get(array('bonus_sn' => $bonus_sn));
  128. if(empty($items)) {
  129. throw new Exception("错误的红包SN号.",errcode::ErrBonusSN);
  130. } else {
  131. return new user_bonus($items[0]);
  132. }
  133. }
  134. }