123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace refill\event;
- trait crash
- {
- private function can_submit($card_no)
- {
- if(empty($this->mChannels)) {
- return true;
- }
- $val = $this->read($card_no);
- if($val === false) {
- return true;
- }
- //目标是最佳解决撞单问题,唯一有效办法是睡眠.
- $osc = $val['sc']; //old succ channel
- $ost = $val['st']; //old succ time
- //如果有充值中的相同卡号的单子,则睡眠.
- if(!empty($val['cc'])) {
- return false;
- }
- //成功的通道是撞单通道,并且成功时间还没过期,则睡眠
- $now = time();
- if (in_array($osc, $this->mChannels) and ($ost + $this->mSuccInterval >= $now)) {
- return false;
- }
- return true;
- }
- private function submit($card_no, $oid)
- {
- if(empty($this->mChannels)) {
- return;
- }
- $org_val = [
- 'sc' => '', //succ ch_name
- 'st' => 0, //succ time
- 'cc' => [] //正在提交的通道
- ];
- $val = $this->read($card_no);
- if($val === false) {
- $val = $org_val;
- }
- $val['cc'][$oid] = '';
- $this->write($card_no,$val);
- }
- private function can_commit($card_no, $oid, $ch_name)
- {
- if(empty($this->mChannels)) {
- return true;
- }
- //非撞单通道都可以提
- if(!in_array($ch_name,$this->mChannels)) {
- return true;
- }
- $val = $this->read($card_no);
- if($val === false) {
- return true;
- }
- $ost = $val['st']; //old succ time
- $now = time();
- if($ost + $this->mSuccInterval >= $now) {
- return false;
- }
- $commits = count($val['cc']);
- if($commits > 1) {
- return false;
- }
- return true;
- }
- private function commit($card_no, $oid, $ch_name)
- {
- if(empty($this->mChannels)) {
- return;
- }
- $val = $this->read($card_no);
- if($val === false) {
- return;
- }
- $val['cc'][$oid] = $ch_name;
- $this->write($card_no,$val);
- }
- private function notify($card_no, $oid, $ch_name,$succ)
- {
- if(empty($this->mChannels)) {
- return;
- }
- $val = $this->read($card_no);
- if($val === false) {
- return;
- }
- if(!$succ) {
- $val['cc'][$oid] = '';
- $this->write($card_no,$val);
- } else {
- $val['cc'][$oid] = $ch_name;
- $this->write($card_no,$val);
- }
- }
- private function complete($card_no, $oid, $ch_name,$succ)
- {
- if(empty($this->mChannels)) {
- return;
- }
- $val = $this->read($card_no);
- if($val === false) {
- return;
- }
- unset($val['cc'][$oid]);
- if($succ)
- {
- $now = time();
- if(empty($val['sc'])) {
- $val['sc'] = $ch_name;
- $val['st'] = $now;
- }
- else
- {
- $osc = $val['sc']; //old succ channel
- $ost = $val['st']; //old succ time
- if(in_array($osc,$this->mChannels))
- {
- //过去的成功通道,在撞单通道中。
- //如果新的通道也在撞单通道中,或者老的成功时间过了撞单周期
- //更新成功通道信息
- if (in_array($ch_name, $this->mChannels) or ($ost + $this->mSuccInterval < $now)) {
- $val['sc'] = $ch_name;
- $val['st'] = $now;
- }
- }
- else
- {
- $val['sc'] = $ch_name;
- $val['st'] = $now;
- }
- }
- }
- $this->write($card_no,$val);
- }
- private function write($card_no, $val)
- {
- $data = [$card_no => json_encode($val)];
- wcache(card_crash::cache_name, $data, 'refill-');
- }
- private function read($card_no)
- {
- $ret = rcache(card_crash::cache_name, 'refill-', $card_no);
- if (empty($ret)) {
- return false;
- }
- $values = $ret[$card_no];
- return json_decode($values, true);
- }
- private function delete($card_no)
- {
- dcache(card_crash::cache_name, 'refill-', $card_no);
- }
- }
|