transfer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace refill;
  3. class transfer
  4. {
  5. private $mMchid2Infos;
  6. public function __construct()
  7. {
  8. $this->load();
  9. }
  10. private static $stInstance = null;
  11. public static function instance()
  12. {
  13. if (self::$stInstance == null) {
  14. self::$stInstance = new transfer();
  15. }
  16. return self::$stInstance;
  17. }
  18. public function load()
  19. {
  20. $mch_checker = function ($mchid) {
  21. $item = Model()->table('merchant')->field('mchid,admin_id')->where(['mchid' => $mchid])->find();
  22. return intval($item['admin_id']);
  23. };
  24. $this->mMchid2Infos = [];
  25. $i = 0;
  26. while (true)
  27. {
  28. $start = $i * 1000;
  29. $items = Model()->table('merchant')->field('mchid,transfer_cfg')->order('mchid asc')->limit("{$start},1000")->select();
  30. if(empty($items)) {
  31. return;
  32. }
  33. $i++;
  34. foreach ($items as $item)
  35. {
  36. $transfer_cfg = $item['transfer_cfg'];
  37. if(empty($transfer_cfg)) {
  38. continue;
  39. }
  40. $transfer_cfg = unserialize($transfer_cfg);
  41. if($transfer_cfg === false) {
  42. continue;
  43. }
  44. $mchid = intval($item['mchid']);
  45. $opened = intval($transfer_cfg['transfer_opened']);
  46. $tmchid = intval($transfer_cfg['transfer_mchid']);
  47. $lowestratio = round($transfer_cfg['transfer_lowestratio'], 3);
  48. $card_types = $transfer_cfg['card_types'] ?? [];
  49. if ($opened == 1 and $tmchid > 0 and $tmchid != $mchid and $lowestratio > 0.001)
  50. {
  51. $admin_id = $mch_checker($tmchid);
  52. if ($admin_id > 0) {
  53. $this->mMchid2Infos[$mchid] = ['transfer_mchid' => $tmchid,
  54. 'admin_id' => $admin_id,
  55. 'lowestratio' => $lowestratio,
  56. 'card_types' => $card_types];
  57. }
  58. }
  59. }
  60. }
  61. }
  62. public function need_transfer($mchid,$card_type,&$lowestratio)
  63. {
  64. if(array_key_exists($mchid,$this->mMchid2Infos))
  65. {
  66. $transfer_cfg = $this->mMchid2Infos[$mchid];
  67. $lowestratio = $transfer_cfg['lowestratio'];
  68. $card_types = $transfer_cfg['card_types'];
  69. if (!in_array($card_type, $card_types)) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. public function transfer_info($mchid)
  76. {
  77. if(array_key_exists($mchid,$this->mMchid2Infos))
  78. {
  79. $transfer_cfg = $this->mMchid2Infos[$mchid];
  80. $transfer_mchid = $transfer_cfg['transfer_mchid'];
  81. $admin_id = $transfer_cfg['admin_id'];
  82. return [$transfer_mchid,$admin_id];
  83. }
  84. return [0,0];
  85. }
  86. public function transfers()
  87. {
  88. return $this->mMchid2Infos;
  89. }
  90. }