crash.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace refill\event;
  3. trait crash
  4. {
  5. private function can_submit($card_no)
  6. {
  7. if(empty($this->mChannels)) {
  8. return true;
  9. }
  10. $val = $this->read($card_no);
  11. if($val === false) {
  12. return true;
  13. }
  14. //目标是最佳解决撞单问题,唯一有效办法是睡眠.
  15. $osc = $val['sc']; //old succ channel
  16. $ost = $val['st']; //old succ time
  17. //如果有充值中的相同卡号的单子,则睡眠.
  18. if(!empty($val['cc'])) {
  19. return false;
  20. }
  21. //成功的通道是撞单通道,并且成功时间还没过期,则睡眠
  22. $now = time();
  23. if (in_array($osc, $this->mChannels) and ($ost + $this->mSuccInterval >= $now)) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. private function submit($card_no, $oid)
  29. {
  30. if(empty($this->mChannels)) {
  31. return;
  32. }
  33. $org_val = [
  34. 'sc' => '', //succ ch_name
  35. 'st' => 0, //succ time
  36. 'cc' => [] //正在提交的通道
  37. ];
  38. $val = $this->read($card_no);
  39. if($val === false) {
  40. $val = $org_val;
  41. }
  42. $val['cc'][$oid] = '';
  43. $this->write($card_no,$val);
  44. }
  45. private function can_commit($card_no, $oid, $ch_name)
  46. {
  47. if(empty($this->mChannels)) {
  48. return true;
  49. }
  50. //非撞单通道都可以提
  51. if(!in_array($ch_name,$this->mChannels)) {
  52. return true;
  53. }
  54. $val = $this->read($card_no);
  55. if($val === false) {
  56. return true;
  57. }
  58. $ost = $val['st']; //old succ time
  59. $now = time();
  60. if($ost + $this->mSuccInterval >= $now) {
  61. return false;
  62. }
  63. $commits = count($val['cc']);
  64. if($commits > 1) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. private function commit($card_no, $oid, $ch_name)
  70. {
  71. if(empty($this->mChannels)) {
  72. return;
  73. }
  74. $val = $this->read($card_no);
  75. if($val === false) {
  76. return;
  77. }
  78. $val['cc'][$oid] = $ch_name;
  79. $this->write($card_no,$val);
  80. }
  81. private function notify($card_no, $oid, $ch_name,$succ)
  82. {
  83. if(empty($this->mChannels)) {
  84. return;
  85. }
  86. $val = $this->read($card_no);
  87. if($val === false) {
  88. return;
  89. }
  90. if(!$succ) {
  91. $val['cc'][$oid] = '';
  92. $this->write($card_no,$val);
  93. } else {
  94. $val['cc'][$oid] = $ch_name;
  95. $this->write($card_no,$val);
  96. }
  97. }
  98. private function complete($card_no, $oid, $ch_name,$succ)
  99. {
  100. if(empty($this->mChannels)) {
  101. return;
  102. }
  103. $val = $this->read($card_no);
  104. if($val === false) {
  105. return;
  106. }
  107. unset($val['cc'][$oid]);
  108. if($succ)
  109. {
  110. $now = time();
  111. if(empty($val['sc'])) {
  112. $val['sc'] = $ch_name;
  113. $val['st'] = $now;
  114. }
  115. else
  116. {
  117. $osc = $val['sc']; //old succ channel
  118. $ost = $val['st']; //old succ time
  119. if(in_array($osc,$this->mChannels))
  120. {
  121. //过去的成功通道,在撞单通道中。
  122. //如果新的通道也在撞单通道中,或者老的成功时间过了撞单周期
  123. //更新成功通道信息
  124. if (in_array($ch_name, $this->mChannels) or ($ost + $this->mSuccInterval < $now)) {
  125. $val['sc'] = $ch_name;
  126. $val['st'] = $now;
  127. }
  128. }
  129. else
  130. {
  131. $val['sc'] = $ch_name;
  132. $val['st'] = $now;
  133. }
  134. }
  135. }
  136. $this->write($card_no,$val);
  137. }
  138. private function write($card_no, $val)
  139. {
  140. $data = [$card_no => json_encode($val)];
  141. wcache(card_crash::cache_name, $data, 'refill-');
  142. }
  143. private function read($card_no)
  144. {
  145. $ret = rcache(card_crash::cache_name, 'refill-', $card_no);
  146. if (empty($ret)) {
  147. return false;
  148. }
  149. $values = $ret[$card_no];
  150. return json_decode($values, true);
  151. }
  152. private function delete($card_no)
  153. {
  154. dcache(card_crash::cache_name, 'refill-', $card_no);
  155. }
  156. }