123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/4/10
- * Time: 下午9:42
- */
- namespace bonus;
- use \Exception;
- use \errcode;
- use \Model;
- use \Log;
- abstract class IGenerator
- {
- protected $mType;
- public function __construct($type) {
- $this->mType = $type;
- }
- abstract public function make_bonus();
- public function make_type()
- {
- $this->mType->setType_sn(make_sn());
- $param = $this->mType->get_param();
- $id = Model('bonus_type')->add($param);
- if($id > 0) {
- $this->mType->setType_id($id);
- } else {
- throw new Exception("生成红包类型失败",errcode::ErrBonusMake);
- }
- }
- public function type_sn() {
- return $this->mType->getType_sn();
- }
- }
- //发给指定人群的红包
- class DirectGenerator extends IGenerator
- {
- public function __construct($type) {
- parent::__construct($type);
- }
- public function make_bonus()
- {
- }
- }
- //发个任意人群的红包
- class GeneralGenerator extends IGenerator
- {
- public function __construct($type) {
- parent::__construct($type);
- }
- public function make_bonus()
- {
- $mod_bonus = Model('user_bonus');
- $paramer = $this->mType->get_param();
- $type_id = $this->mType->getType_id();
- $total_num = $this->mType->getTotal_num();
- $type_sn = $this->mType->getType_sn();
- if($this->mType->isRandomAmount())
- {
- $moneys = $this->separate_money($paramer,$min_val,$max_val);
- Model('bonus_type')->edit(array('type_id' => $type_id),array('min_amount' => $min_val,'max_amount' => $max_val));
- foreach($moneys as $val)
- {
- $datas = array('bonus_sn' => make_sn(),
- 'bonus_value' => $val,
- 'type_id' => $type_id,
- 'status' => 0,
- 'type_sn' => $type_sn);
- $ret = $mod_bonus->add($datas);
- if($ret <= 0) {
- Log::record("insert user_bonus err: {$type_id}.");
- return false;
- }
- }
- }
- else
- {
- $val = $paramer['fixed_money'];
- for($i = 0; $i < $total_num; $i++) {
- $datas = array( 'bonus_sn' => make_sn(),
- 'bonus_value' => $val,
- 'type_id' => $type_id,
- 'status' => 0,
- 'type_sn' => $type_sn);
- $ret = $mod_bonus->add($datas);
- if($ret <= 0) {
- Log::record("insert user_bonus err: {$type_id}.");
- return false;
- }
- }
- }
- return true;
- }
- /**
- * @param $paramer
- */
- private function separate_money($paramer,&$min_val,&$max_val)
- {
- $min_amount = floatval($paramer['min_amount']) * 100;
- $base_amount = $min_amount;
- $num = intval($paramer['total_num']);
- $total_amount = floatval($paramer['total_amount']) * 100 - $num * $base_amount;
- $ret = array();
- for ($i = 0; $i < $num; $i++) {
- array_push($ret,$base_amount);
- }
- $max_amount = $total_amount;
- $max_val = 0;
- for ($i = 0; $i < $num && $total_amount > 0;)
- {
- $amount = mt_rand(0, $max_amount);
- $ret[$i] += $amount;
- $total_amount -= $amount;
- $max_val = $ret[$i] > $max_val ? $ret[$i] : $max_val;
- $max_amount = $total_amount;
- if($i == $num -1) {
- $i = 0;
- } else {
- $i++;
- }
- }
- //$y = array_sum($ret); //验证制造的数据是否可以正好相等
- $max_val = $max_val / 100;
- $min_val = $ret[0];
- foreach ($ret as &$val) {
- $min_val = $min_val > $val ? $val : $min_val;
- $val = $val / 100;
- }
- $min_val = $min_val / 100;
- return $ret;
- }
- }
- function create_generator($type)
- {
- if($type->isDirectType()) {
- return new DirectGenerator($type);
- }
- else if($type->isGeneralType()) {
- return new GeneralGenerator($type);
- }
- else {
- throw new Exception("错误的红包类型",errcode::ErrBonusType);
- }
- }
|