generator.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 \Model;
  12. use \Log;
  13. abstract class IGenerator
  14. {
  15. protected $mType;
  16. public function __construct($type) {
  17. $this->mType = $type;
  18. }
  19. abstract public function make_bonus();
  20. public function make_type()
  21. {
  22. $this->mType->setType_sn(make_sn());
  23. $param = $this->mType->get_param();
  24. $id = Model('bonus_type')->add($param);
  25. if($id > 0) {
  26. $this->mType->setType_id($id);
  27. } else {
  28. throw new Exception("生成红包类型失败",errcode::ErrBonusMake);
  29. }
  30. }
  31. public function type_sn() {
  32. return $this->mType->getType_sn();
  33. }
  34. }
  35. //发给指定人群的红包
  36. class DirectGenerator extends IGenerator
  37. {
  38. public function __construct($type) {
  39. parent::__construct($type);
  40. }
  41. public function make_bonus()
  42. {
  43. }
  44. }
  45. //发个任意人群的红包
  46. class GeneralGenerator extends IGenerator
  47. {
  48. public function __construct($type) {
  49. parent::__construct($type);
  50. }
  51. public function make_bonus()
  52. {
  53. $mod_bonus = Model('user_bonus');
  54. $paramer = $this->mType->get_param();
  55. $type_id = $this->mType->getType_id();
  56. $total_num = $this->mType->getTotal_num();
  57. $type_sn = $this->mType->getType_sn();
  58. if($this->mType->isRandomAmount())
  59. {
  60. $moneys = $this->separate_money($paramer,$min_val,$max_val);
  61. Model('bonus_type')->edit(array('type_id' => $type_id),array('min_amount' => $min_val,'max_amount' => $max_val));
  62. foreach($moneys as $val)
  63. {
  64. $datas = array('bonus_sn' => make_sn(),
  65. 'bonus_value' => $val,
  66. 'type_id' => $type_id,
  67. 'status' => 0,
  68. 'type_sn' => $type_sn);
  69. $ret = $mod_bonus->add($datas);
  70. if($ret <= 0) {
  71. Log::record("insert user_bonus err: {$type_id}.");
  72. return false;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. $val = $paramer['fixed_money'];
  79. for($i = 0; $i < $total_num; $i++) {
  80. $datas = array( 'bonus_sn' => make_sn(),
  81. 'bonus_value' => $val,
  82. 'type_id' => $type_id,
  83. 'status' => 0,
  84. 'type_sn' => $type_sn);
  85. $ret = $mod_bonus->add($datas);
  86. if($ret <= 0) {
  87. Log::record("insert user_bonus err: {$type_id}.");
  88. return false;
  89. }
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * @param $paramer
  96. */
  97. private function separate_money($paramer,&$min_val,&$max_val)
  98. {
  99. $min_amount = floatval($paramer['min_amount']) * 100;
  100. $base_amount = $min_amount;
  101. $num = intval($paramer['total_num']);
  102. $total_amount = floatval($paramer['total_amount']) * 100 - $num * $base_amount;
  103. $ret = array();
  104. for ($i = 0; $i < $num; $i++) {
  105. array_push($ret,$base_amount);
  106. }
  107. $max_amount = $total_amount;
  108. $max_val = 0;
  109. for ($i = 0; $i < $num && $total_amount > 0;)
  110. {
  111. $amount = mt_rand(0, $max_amount);
  112. $ret[$i] += $amount;
  113. $total_amount -= $amount;
  114. $max_val = $ret[$i] > $max_val ? $ret[$i] : $max_val;
  115. $max_amount = $total_amount;
  116. if($i == $num -1) {
  117. $i = 0;
  118. } else {
  119. $i++;
  120. }
  121. }
  122. //$y = array_sum($ret); //验证制造的数据是否可以正好相等
  123. $max_val = $max_val / 100;
  124. $min_val = $ret[0];
  125. foreach ($ret as &$val) {
  126. $min_val = $min_val > $val ? $val : $min_val;
  127. $val = $val / 100;
  128. }
  129. $min_val = $min_val / 100;
  130. return $ret;
  131. }
  132. }
  133. function create_generator($type)
  134. {
  135. if($type->isDirectType()) {
  136. return new DirectGenerator($type);
  137. }
  138. else if($type->isGeneralType()) {
  139. return new GeneralGenerator($type);
  140. }
  141. else {
  142. throw new Exception("错误的红包类型",errcode::ErrBonusType);
  143. }
  144. }