123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace refill;
- class transfer
- {
- private $mMchid2Infos;
- public function __construct()
- {
- $this->load();
- }
- private static $stInstance = null;
- public static function instance()
- {
- if (self::$stInstance == null) {
- self::$stInstance = new transfer();
- }
- return self::$stInstance;
- }
- public function load()
- {
- $mch_checker = function ($mchid) {
- $item = Model()->table('merchant')->field('mchid,admin_id')->where(['mchid' => $mchid])->find();
- return intval($item['admin_id']);
- };
- $this->mMchid2Infos = [];
- $i = 0;
- while (true)
- {
- $start = $i * 1000;
- $items = Model()->table('merchant')->field('mchid,transfer_cfg')->order('mchid asc')->limit("{$start},1000")->select();
- if(empty($items)) {
- return;
- }
- $i++;
- foreach ($items as $item)
- {
- $transfer_cfg = $item['transfer_cfg'];
- if(empty($transfer_cfg)) {
- continue;
- }
- $transfer_cfg = unserialize($transfer_cfg);
- if($transfer_cfg === false) {
- continue;
- }
- $mchid = intval($item['mchid']);
- $opened = intval($transfer_cfg['transfer_opened']);
- $tmchid = intval($transfer_cfg['transfer_mchid']);
- $lowestratio = round($transfer_cfg['transfer_lowestratio'], 3);
- $card_types = $transfer_cfg['card_types'] ?? [];
- if ($opened == 1 and $tmchid > 0 and $tmchid != $mchid and $lowestratio > 0.001)
- {
- $admin_id = $mch_checker($tmchid);
- if ($admin_id > 0) {
- $this->mMchid2Infos[$mchid] = ['transfer_mchid' => $tmchid,
- 'admin_id' => $admin_id,
- 'lowestratio' => $lowestratio,
- 'card_types' => $card_types];
- }
- }
- }
- }
- }
- public function need_transfer($mchid,$card_type,&$lowestratio)
- {
- if(array_key_exists($mchid,$this->mMchid2Infos))
- {
- $transfer_cfg = $this->mMchid2Infos[$mchid];
- $lowestratio = $transfer_cfg['lowestratio'];
- $card_types = $transfer_cfg['card_types'];
- if (!in_array($card_type, $card_types)) {
- return true;
- }
- }
- return false;
- }
- public function transfer_info($mchid)
- {
- if(array_key_exists($mchid,$this->mMchid2Infos))
- {
- $transfer_cfg = $this->mMchid2Infos[$mchid];
- $transfer_mchid = $transfer_cfg['transfer_mchid'];
- $admin_id = $transfer_cfg['admin_id'];
- return [$transfer_mchid,$admin_id];
- }
- return [0,0];
- }
- public function transfers()
- {
- return $this->mMchid2Infos;
- }
- }
|