123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/1/31
- * Time: 下午4:56
- */
- namespace async;
- use room;
- use trans_wapper;
- use Exception;
- use Log;
- use push_helper;
- use bonus;
- use bonus_helper;
- class bargain
- {
- private $mBargain;
- public function __construct($bargain_id)
- {
- $mod_bargain = Model('room_bargain');
- $info = $mod_bargain->getBargainById($bargain_id);
- $this->mBargain = new room\bargain($info);
- }
- public function onClose()
- {
- if($this->mBargain->closed()) {
- $this->reward();
- }
- else
- {
- if($this->mBargain->has_over()) {
- $this->close();
- $this->reward();
- }
- else
- {
- $discount_cent = intval($this->mBargain->discount() * 100 + 0.5);
- $dest = $this->mBargain->goods_price() - $this->mBargain->lowest_price();
- $dest_cent = intval($dest * 100 + 0.5);
- if($dest_cent <= $discount_cent) {
- $this->close();
- $this->reward();
- }
- else {
- return false;
- }
- }
- }
- return true;
- }
- private function close()
- {
- $mod_bargain = Model('room_bargain');
- $mod_bargain->editBargain($this->mBargain->bargain_id(),['closed' => 1]);
- }
- private function reward()
- {
- if($this->mBargain->rewarded()) {
- return false;
- }
- else {
- try
- {
- $mod_bargain = Model('room_bargain');
- $trans = new trans_wapper($mod_bargain);
- $mod_bargain->editBargain($this->mBargain->bargain_id(),['rewarded' => 1]);
- $trans->commit();
- $this->send_bonus($this->mBargain->discount(),$this->mBargain->creator());
- }
- catch (Exception $ex) {
- $trans->rollback();
- Log::record($ex->getTraceAsString(),Log::ERR);
- }
- }
- }
- private function send_bonus($discount,$userid)
- {
- $cent = intval($discount * 100 + 0.5);
- if($cent <= 0) return false;
- $params = bonus\parameters::bargain_fixed(100,$discount);
- foreach ($params as $param) {
- $type = bonus_helper::create_type_input($param);
- $money = $type->getTotal_amount();
- $ret = bonus_helper::make_bonus($param, $param['rate_money']);
- if ($ret != false) {
- $type_sn = $ret['type_sn'];
- bonus_helper::send($type_sn, [$userid]);
- push_helper::bargain_bonus($userid, $discount, $type_sn);
- } else {
- return false;
- }
- }
- }
- }
|