transfer_timeout.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace refill;
  3. use QueueClient;
  4. class transfer_timeout
  5. {
  6. private $mMchid2Infos;
  7. public function __construct()
  8. {
  9. $this->load();
  10. }
  11. private static $stInstance = null;
  12. public static function instance()
  13. {
  14. if (self::$stInstance == null) {
  15. self::$stInstance = new transfer_timeout();
  16. }
  17. return self::$stInstance;
  18. }
  19. public function load()
  20. {
  21. $mch_checker = function ($mchid) {
  22. $item = Model()->table('merchant')->field('mchid,admin_id')->where(['mchid' => $mchid])->find();
  23. return intval($item['admin_id']);
  24. };
  25. $this->mMchid2Infos = [];
  26. $i = 0;
  27. while (true)
  28. {
  29. $start = $i * 1000;
  30. $items = Model()->table('merchant')->field('mchid,timeout_transfer_cfg')->order('mchid asc')->limit("{$start},1000")->select();
  31. if(empty($items)) {
  32. return;
  33. }
  34. $i++;
  35. foreach ($items as $item)
  36. {
  37. $cfg = $item['timeout_transfer_cfg'];
  38. if(empty($cfg)) {
  39. continue;
  40. }
  41. $cfg = unserialize($cfg);
  42. if($cfg === false) {
  43. continue;
  44. }
  45. $mchid = intval($item['mchid']);
  46. $opened = intval($cfg['transfer_opened']);
  47. $tmchid = intval($cfg['transfer_mchid']);
  48. $maxtm = intval($cfg['transfer_maxtm']);
  49. if ($opened == 1 && $tmchid > 0 && $tmchid != $mchid && $maxtm > 0)
  50. {
  51. $admin_id = $mch_checker($tmchid);
  52. if ($admin_id > 0) {
  53. $this->mMchid2Infos[$mchid] = ['transfer_mchid' => $tmchid, 'admin_id' => $admin_id, 'transfer_maxtm' => $maxtm];
  54. }
  55. }
  56. }
  57. }
  58. }
  59. private function time_out($mchid)
  60. {
  61. return $this->mMchid2Infos[$mchid]['transfer_maxtm'];
  62. }
  63. public function need_monitor($mchid)
  64. {
  65. if(array_key_exists($mchid,$this->mMchid2Infos)) {
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. }
  71. public function has_tmout($mchid,$mch_order)
  72. {
  73. $need_monitor = $this->need_monitor($mchid);
  74. if(!$need_monitor) {
  75. return false;
  76. }
  77. $order_state = util::query_queue_order($mchid,$mch_order);
  78. if($order_state === false || $order_state == ORDER_STATE_TIMEOUT) {
  79. return false;
  80. }
  81. $mod_refill = Model('refill_order');
  82. $detail = $mod_refill->get_detail($mchid, $mch_order);
  83. if(empty($detail)) {
  84. return false;
  85. }
  86. $order_state = intval($detail['order_state']);
  87. if($order_state == ORDER_STATE_HANDLED) {
  88. return false;
  89. }
  90. $order_time = intval($detail['order_time']);
  91. $tmout = $this->time_out($mchid);
  92. if (time() >= $order_time + $tmout) {
  93. return true;
  94. } else {
  95. $delta = $order_time + $tmout - time();
  96. QueueClient::async_push("QueryMchOrderTimeout", ['mchid' => $mchid, 'mch_order' => $mch_order], $delta);
  97. return false;
  98. }
  99. }
  100. public function monitor(order $order)
  101. {
  102. $mchid = $order->mchid();
  103. $mch_order = $order->mch_order();
  104. $tmout = $this->time_out($mchid);
  105. QueueClient::async_push("QueryMchOrderTimeout", ['mchid' => $mchid, 'mch_order' => $mch_order], $tmout);
  106. }
  107. public function transfer_info($mchid)
  108. {
  109. if(array_key_exists($mchid,$this->mMchid2Infos))
  110. {
  111. $transfer_cfg = $this->mMchid2Infos[$mchid];
  112. $transfer_mchid = $transfer_cfg['transfer_mchid'];
  113. $admin_id = $transfer_cfg['admin_id'];
  114. return [$transfer_mchid,$admin_id];
  115. }
  116. return [0,0];
  117. }
  118. public function transfers()
  119. {
  120. return $this->mMchid2Infos;
  121. }
  122. }