123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/4/6
- * Time: 下午3:00
- */
- namespace fcode;
- require_once (BASE_ROOT_PATH . '/helper/session_helper.php');
- use session_helper;
- use Log;
- use Exception;
- class operator
- {
- private $mCommonID;
- private $mod_fcode;
- private $mBatchCode;
- const time_out = 300;
- public function __construct($common_id, $batch_code)
- {
- $this->mCommonID = $common_id;
- $this->mBatchCode = $batch_code;
- $this->mod_fcode = Model('goods_fcode');
- }
- public function grabed($mobile = null)
- {
- $cond = [];
- $cond['goods_commonid'] = $this->mCommonID;
- $cond['batch_code'] = $this->mBatchCode;
- if($mobile == null)
- {
- if(session_helper::logined()) {
- $cond['mobile|session_id'] = array('_multi'=>true,session_helper::cur_mobile(),session_helper::session_id());
- }
- else {
- $cond['session_id'] = session_helper::session_id();
- }
- }
- else {
- $cond['mobile|session_id'] = array('_multi'=>true,$mobile,session_helper::session_id());
- }
- $fcode = $this->mod_fcode->getGoodsFCode($cond);
- if(empty($fcode)) {
- return false;
- }
- else {
- return $fcode;
- }
- }
- public function grab()
- {
- $fcode = $this->grabed();
- if($fcode != false) return $fcode;
- while(true)
- {
- $cond = array('goods_commonid' => $this->mCommonID,
- 'batch_code' => $this->mBatchCode,
- 'fc_state' => '0',
- 'grab_state' => array('in', array(0,1)),
- 'grab_time' => array('lt',time() - self::time_out));
- $fcodes = $this->mod_fcode->where($cond)->field('*')->order('fc_id asc')->limit(1)->select(array('master' => true));
- if(empty($fcodes) || empty($fcodes[0])) return false;
- $fcode = $this->try_grab($fcodes[0]);
- if($fcode != false) {
- return $fcode;
- }
- }
- }
- public function bind($fcode,$mobile)
- {
- $cond = [];
- $cond['goods_commonid'] = $this->mCommonID;
- $cond['batch_code'] = $this->mBatchCode;
- $cond['user_key'] = $fcode['user_key'];
- $cond['fc_id'] = $fcode['fc_id'];
- $cond['fc_state'] = $fcode['fc_state'];
- $cond['grab_state'] = $fcode['grab_state'];
- $cond['grab_time'] = $fcode['grab_time'];
- $cond['session_id'] = $fcode['session_id'];
- $cond['mobile'] = $fcode['mobile'];
- $datas = array('grab_state' => 2,
- 'mobile' => $mobile,
- 'user_key' => mt_rand(1000, 9999),
- 'usable_time' => time() + ($fcode['usable_days'] * 24 * 3600));
- try
- {
- $this->mod_fcode->beginTransaction();
- $ret = $this->mod_fcode->where($cond)->update($datas);
- $affect_rows = $this->mod_fcode->affected_rows();
- $this->mod_fcode->commit();
- Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
- if($ret != false && $affect_rows > 0) {
- $fcode = array_merge($fcode,$datas);
- return $fcode;
- } else {
- return false;
- }
- } catch (Exception $ex) {
- $this->mod_fcode->rollback();
- return false;
- }
- }
- public function change($fcode,$mobile)
- {
- return $this->bind($fcode,$mobile);
- }
- public function lock($pay_sn)
- {
- $fcode = $this->grabed();
- if($fcode != false) return $fcode;
- if(!session_helper::logined()) {
- return false;
- }
- while(true)
- {
- $cond = array('goods_commonid' => $this->mCommonID,
- 'batch_code' => $this->mBatchCode,
- 'fc_state' => '0',
- 'grab_state' => array('in', array(0,1)),
- 'grab_time' => array('lt',time() - self::time_out));
- $fcodes = $this->mod_fcode->where($cond)->field('*')->order('fc_id asc')->limit(1)->select(array('master' => true));
- if(empty($fcodes) || empty($fcodes[0])) return false;
- $fcode = $this->try_lock($fcodes[0],$pay_sn);
- if($fcode != false) {
- return $fcode;
- }
- }
- }
- public static function unlock($pay_sn)
- {
- $mod_fcode = Model('goods_fcode');
- $cur_time = time();
- $ret = $mod_fcode->where(['pay_sn' => $pay_sn,'fc_state' => 3])->update(['fc_state' => 0,
- 'usable_time' => array('exp', "usable_days * 86400 + {$cur_time}")]);
- $affect_rows = $mod_fcode->affected_rows();
- if($ret != false && $affect_rows > 0) {
- return $affect_rows;
- } else {
- return 0;
- }
- }
- public static function reset($pay_sn)
- {
- $mod_fcode = Model('goods_fcode');
- $ret = $mod_fcode->where(['pay_sn' => $pay_sn,'fc_state' => 3])->update(['fc_state' => 0,
- 'usable_time' => 0,
- 'mobile' => '',
- 'session_id' => '',
- 'grab_state' => 0,
- 'grab_time' => 0,
- 'pay_sn' => 0]);
- $affect_rows = $mod_fcode->affected_rows();
- if($ret != false && $affect_rows > 0) {
- return $affect_rows;
- } else {
- return 0;
- }
- }
- private function try_lock($fcode,$pay_sn)
- {
- $cond = [];
- $cond['goods_commonid'] = $this->mCommonID;
- $cond['batch_code'] = $this->mBatchCode;
- $cond['user_key'] = $fcode['user_key'];
- $cond['fc_id'] = $fcode['fc_id'];
- $cond['fc_state'] = $fcode['fc_state'];
- $cond['grab_state'] = $fcode['grab_state'];
- $cond['grab_time'] = $fcode['grab_time'];
- $cond['session_id'] = $fcode['session_id'];
- $cond['mobile'] = $fcode['mobile'];
- $datas = array( 'grab_state' => 2,
- 'grab_time' => time(),
- 'session_id' => session_helper::session_id(),
- 'mobile' => session_helper::cur_mobile(),
- 'fc_state' => 3,
- 'pay_sn' => $pay_sn,
- 'user_key' => mt_rand(1000, 9999));
- try
- {
- $this->mod_fcode->beginTransaction();
- $ret = $this->mod_fcode->where($cond)->update($datas);
- $affect_rows = $this->mod_fcode->affected_rows();
- $this->mod_fcode->commit();
- Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
- if($ret != false && $affect_rows > 0) {
- $fcode = array_merge($fcode,$datas);
- return $fcode;
- } else {
- return false;
- }
- } catch (Exception $ex) {
- $this->mod_fcode->rollback();
- return false;
- }
- }
- private function try_grab($fcode)
- {
- $cond = [];
- $cond['goods_commonid'] = $this->mCommonID;
- $cond['batch_code'] = $this->mBatchCode;
- $cond['user_key'] = $fcode['user_key'];
- $cond['fc_id'] = $fcode['fc_id'];
- $cond['fc_state'] = $fcode['fc_state'];
- $cond['grab_state'] = $fcode['grab_state'];
- $cond['grab_time'] = $fcode['grab_time'];
- $cond['session_id'] = $fcode['session_id'];
- $cond['mobile'] = $fcode['mobile'];
- if(session_helper::logined() || session_helper::isVerfiyMobile()) { // 如果是登录状态直接绑定
- $datas = array( 'grab_state' => 2,
- 'grab_time' => time(),
- 'session_id' => session_helper::session_id(),
- 'mobile' => session_helper::cur_mobile(),
- 'user_key' => mt_rand(1000, 9999),
- 'usable_time' => time() + ($fcode['usable_days'] * 24 * 3600));
- }
- else {
- $datas = array( 'grab_state' => 1,
- 'grab_time' => time(),
- 'session_id' => session_helper::session_id());
- }
- try
- {
- $this->mod_fcode->beginTransaction();
- $ret = $this->mod_fcode->where($cond)->update($datas);
- $affect_rows = $this->mod_fcode->affected_rows();
- $this->mod_fcode->commit();
- Log::record("fcode::grab try_grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
- if($ret != false && $affect_rows > 0) {
- $fcode = array_merge($fcode,$datas);
- return $fcode;
- } else {
- return false;
- }
- } catch (Exception $ex) {
- $this->mod_fcode->rollback();
- return false;
- }
- }
- }
|