generator.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/10
  6. * Time: 下午9:42
  7. */
  8. namespace bonus;
  9. use \Exception;
  10. use \errcode;
  11. use \Log;
  12. abstract class IGenerator
  13. {
  14. protected $mType;
  15. public function __construct($type) {
  16. $this->mType = $type;
  17. }
  18. abstract public function make_bonus($rates);
  19. public function make_type()
  20. {
  21. $this->mType->setType_sn(make_bonus_sn());
  22. $param = $this->mType->get_param();
  23. $id = Model('bonus_type')->add($param);
  24. if($id > 0) {
  25. $this->mType->setType_id($id);
  26. } else {
  27. throw new Exception("生成红包类型失败",errcode::ErrBonusMake);
  28. }
  29. }
  30. public function type_sn() {
  31. return $this->mType->getType_sn();
  32. }
  33. }
  34. //发给指定人群的红包
  35. class DirectGenerator extends IGenerator
  36. {
  37. public function __construct($type) {
  38. parent::__construct($type);
  39. }
  40. public function make_bonus($rates)
  41. {
  42. }
  43. }
  44. //发个任意人群的红包
  45. class GeneralGenerator extends IGenerator
  46. {
  47. private $mDatas;
  48. public function __construct($type) {
  49. parent::__construct($type);
  50. }
  51. public function make_bonus($rates)
  52. {
  53. $paramer = $this->mType->get_param();
  54. $type_id = $this->mType->getType_id();
  55. $type_sn = $this->mType->getType_sn();
  56. $can_share = $this->mType->can_share();
  57. $this->mDatas = [];
  58. $min_val = -1;
  59. $max_val = -1;
  60. foreach ($rates as $item)
  61. {
  62. $amount = $item['amount'];
  63. $num = $item['num'];
  64. $rate = $item['rate'];
  65. if($this->mType->isRandomAmount())
  66. {
  67. $this->random($type_id,$type_sn,$paramer['min_amount'],$amount,$num,$rate,$can_share,$min_tmp,$max_tmp);
  68. if($min_val == -1) {
  69. $min_val = $min_tmp;
  70. }
  71. else
  72. {
  73. if(intval($min_tmp * 100 + 0.5) < intval($min_val * 100 + 0.5)) {
  74. $min_val = $min_tmp;
  75. }
  76. }
  77. if($max_val == -1) {
  78. $max_val = $max_tmp;
  79. }
  80. else
  81. {
  82. if(intval($max_tmp * 100 + 0.5) > intval($max_val * 100 + 0.5)) {
  83. $max_val = $max_tmp;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. $fixed_money = $amount;
  90. $this->general($type_id,$type_sn,$num,$fixed_money,$rate,$can_share);
  91. }
  92. }
  93. if(empty($this->mDatas)) {
  94. return false;
  95. }
  96. $bonus = $this->mislead();
  97. $mod_bonus = Model('user_bonus');
  98. $ret = $mod_bonus->insertAll($bonus);
  99. if(!$ret) {
  100. Log::record("insert user_bonus err: type_id = {$type_id}.");
  101. return false;
  102. }
  103. if($this->mType->isRandomAmount()) {
  104. Model('bonus_type')->edit(['type_id' => $type_id], ['min_amount' => $min_val,'max_amount' => $max_val]);
  105. }
  106. return true;
  107. }
  108. private function mislead()
  109. {
  110. $ret = $this->mDatas;
  111. $num = count($ret);
  112. for($index = 0; $index < $num - 1; $index++) {
  113. $pos_a = mt_rand(0, $num - 1);
  114. $pos_b = mt_rand(0, $num - 1);
  115. $tmp = $ret[$pos_a];
  116. $ret[$pos_a] = $ret[$pos_b];
  117. $ret[$pos_b] = $tmp;
  118. }
  119. return $ret;
  120. }
  121. private function random($type_id,$type_sn,$min_amount,$amount,$num,$rate,$can_share,&$min_val,&$max_val)
  122. {
  123. $allocator = new allocator();
  124. $moneys = $allocator->separate_money($min_amount,$amount,$num,$min_val,$max_val);
  125. foreach($moneys as $val)
  126. {
  127. $item =['bonus_sn' => make_bonus_sn(),
  128. 'bonus_value' => $val,
  129. 'remain_amount' => $val,
  130. 'type_id' => $type_id,
  131. 'bonus_status' => 0,
  132. 'bonus_rate' => $rate,
  133. 'type_sn' => $type_sn,
  134. 'can_share' => $can_share];
  135. $this->mDatas[] = $item;
  136. }
  137. }
  138. private function general($type_id,$type_sn,$total_num,$fixed_money,$rate,$can_share)
  139. {
  140. $val = $fixed_money;
  141. for($i = 0; $i < $total_num; $i++)
  142. {
  143. $item =['bonus_sn' => make_bonus_sn(),
  144. 'bonus_value' => $val,
  145. 'remain_amount' => $val,
  146. 'type_id' => $type_id,
  147. 'bonus_status' => 0,
  148. 'bonus_rate' => $rate,
  149. 'type_sn' => $type_sn,
  150. 'can_share' => $can_share];
  151. $this->mDatas[] = $item;
  152. }
  153. }
  154. }
  155. function create_generator($type)
  156. {
  157. if($type->isDirectType()) {
  158. return new DirectGenerator($type);
  159. }
  160. else if($type->isGeneralType()) {
  161. return new GeneralGenerator($type);
  162. }
  163. else {
  164. throw new Exception("错误的红包类型",errcode::ErrBonusType);
  165. }
  166. }