generator.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $allocator = new allocator();
  61. $moneys = $allocator->separate_money($paramer['min_amount'],$paramer['total_amount'],$paramer['total_num'],$min_val,$max_val);
  62. Model('bonus_type')->edit(array('type_id' => $type_id),array('min_amount' => $min_val,'max_amount' => $max_val));
  63. foreach($moneys as $val)
  64. {
  65. $datas[] = array('bonus_sn' => make_sn(),
  66. 'bonus_value' => $val,
  67. 'type_id' => $type_id,
  68. 'bonus_status' => 0,
  69. 'type_sn' => $type_sn);
  70. }
  71. $ret = $mod_bonus->insertAll($datas);
  72. if(!$ret) {
  73. Log::record("insert user_bonus err: {$type_id}.");
  74. return false;
  75. }
  76. }
  77. else
  78. {
  79. $val = $paramer['fixed_money'];
  80. for($i = 0; $i < $total_num; $i++) {
  81. $datas[] = array( 'bonus_sn' => make_sn(),
  82. 'bonus_value' => $val,
  83. 'type_id' => $type_id,
  84. 'bonus_status' => 0,
  85. 'type_sn' => $type_sn);
  86. }
  87. $ret = $mod_bonus->insertAll($datas);
  88. if(!$ret) {
  89. Log::record("insert user_bonus err: {$type_id}.");
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. }
  96. function create_generator($type)
  97. {
  98. if($type->isDirectType()) {
  99. return new DirectGenerator($type);
  100. }
  101. else if($type->isGeneralType()) {
  102. return new GeneralGenerator($type);
  103. }
  104. else {
  105. throw new Exception("错误的红包类型",errcode::ErrBonusType);
  106. }
  107. }