123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?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())
- {
- $allocator = new allocator();
- $moneys = $allocator->separate_money($paramer['min_amount'],$paramer['total_amount'],$paramer['total_num'],$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,
- 'bonus_status' => 0,
- 'type_sn' => $type_sn);
- }
- $ret = $mod_bonus->insertAll($datas);
- if(!$ret) {
- 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,
- 'bonus_status' => 0,
- 'type_sn' => $type_sn);
- }
- $ret = $mod_bonus->insertAll($datas);
- if(!$ret) {
- Log::record("insert user_bonus err: {$type_id}.");
- return false;
- }
- }
- return true;
- }
- }
- 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);
- }
- }
|