123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace refill;
- require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefill.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
- require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
- require_once(BASE_HELPER_PATH . '/refill/util.php');
- use Log;
- use mtopcard;
- use Exception;
- class ProviderManager
- {
- protected $mOpenedProviderNames;
- protected $mAllQMapPTS;
- protected $mProviders;
- protected $mSpecTypes;
- public function __construct()
- {
- $this->mOpenedProviderNames = [];
- $this->mAllQMapPTS = [];
- $this->mProviders = [];
- $this->mSpecTypes = [];
- }
- public function getQPTA()
- {
- return $this->mAllQMapPTS;
- }
- private function map_cfg($cfgs,$refill_type,$channels)
- {
- $card_types = function ($stypes)
- {
- $result = [];
- $types = explode(',',$stypes);
- foreach ($types as $stype) {
- $type = mtopcard\topcard_type($stype);
- $result[] = $type;
- }
- return $result;
- };
- $result = [];
- foreach ($cfgs as $item)
- {
- $name = $item['name'];
- if(!array_key_exists($name,$channels) || $channels[$name]['opened'] == false) {
- continue;
- }
- $cfg = $item['cfg'];
- $provider = $this->create_provider($name,$cfg,$refill_type);
- if($provider !== false) {
- $this->mProviders[$name] = $provider;
- $this->mOpenedProviderNames[] = $name;
- } else {
- continue;
- }
- $amounts = $cfg['amount'] ?? [];
- foreach ($amounts as $spec => $goods)
- {
- foreach ($goods as $gitem)
- {
- $quality = $gitem['quality'] ?? 1;
- $types = $card_types($gitem['card_type']);
- foreach ($types as $type) {
- $this->mAllQMapPTS[$quality]["{$name}-{$type}-{$spec}"] = ['goods_id' => $gitem['goods_id'],'price' => $gitem['price'],'provider' => $provider];
- $this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
- }
- }
- }
- }
- return $result;
- }
- private function create_provider($name,$cfg,$refill_type)
- {
- try
- {
- $file = BASE_HELPER_RAPI_PATH . "/{$name}/{$refill_type}.php";
- if (!file_exists($file)) {
- Log::record("provider api file={$file} not exist.", Log::DEBUG);
- return false;
- } else {
- require_once($file);
- Log::record("file={$file} load success.", Log::DEBUG);
- }
- $class = "refill\\{$name}\\{$refill_type}";
- if (class_exists($class, false)) {
- $provider = new $class($cfg);
- $provider->setOpened(false);
- return $provider;
- } else {
- $error = "Base Error: class {$class} isn't exists!";
- Log::record(__FUNCTION__ . " {$error}", Log::ERR);
- }
- }
- catch (Exception $ex) {
- Log::record($ex->getMessage(), Log::ERR);
- }
- return false;
- }
- public function load()
- {
- try
- {
- $file = BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php';
- if(!file_exists($file)){
- Log::record("refill.ini.php ={$file} not exist",Log::DEBUG);
- return false;
- } else {
- include($file);
- }
- $this->mOpenedProviderNames = [];
- $this->mAllQMapPTS = [];
- $this->mProviders = [];
- $this->mSpecTypes = [];
- $channels = $this->read_channel();
- global $config;
- $this->map_cfg($config['phone_providers'],'RefillPhone',$channels);
- $this->map_cfg($config['oil_providers'],'RefillOil',$channels);
- $this->map_cfg($config['third_providers'],'RefillPhone',$channels);
- $this->mOpenedProviderNames = array_unique($this->mOpenedProviderNames);
- foreach ($channels as $item)
- {
- $name = $item['name'];
- $regins = $item['regins'];
- if(array_key_exists($name,$this->mProviders)) {
- $this->mProviders[$name]->setOpened($item['opened']);
- $this->mProviders[$name]->setRegins($regins);
- }
- }
- }
- catch (Exception $ex)
- {
- Log::record(__FUNCTION__ ." " . $ex->getMessage(), Log::ERR);
- }
- }
- private function read_channel()
- {
- $refill_provider = Model('refill_provider');
- $items = $refill_provider->getProviderList(['provider_id' => ['gt' , 0]]);
- $result = [];
- foreach ($items as $item)
- {
- $name = $item['name'];
- $regins = [];
- $tmp = unserialize($item['provinces']);
- if(!empty($tmp))
- {
- foreach ($tmp as $card_type => $sregions)
- {
- $rs = explode(',',$sregions);
- if(!empty($rs)) {
- $regins[$card_type] = $rs;
- }
- }
- }
- $val = ['name' => $name,
- 'type' => intval($item['type']),
- 'opened' => intval($item['opened']) == 1,
- 'sort' => intval($item['sort']),
- 'regins' => $regins
- ];
- $result[$name] = $val;
- }
- return $result;
- }
- public function get_providers(int $mchid, int $spec, int $card_type, int $quality, $regin_no): array
- {
- $qnames = $this->mSpecTypes[$quality] ?? [];
- $key = "{$card_type}-{$spec}";
- Log::record("quality = {$quality} , key = {$key}",Log::DEBUG);
- if(array_key_exists($key,$qnames))
- {
- $names = $qnames[$key];
- $providers = [];
- foreach ($names as $name)
- {
- $provider = $this->mProviders[$name];
- $opened = $provider->opened();
- if($opened == false) continue;
- if($provider->match_regin($card_type,$regin_no)) {
- $providers[] = $provider;
- }
- }
- return $providers;
- }
- else {
- return [];
- }
- }
- private function debug()
- {
- //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
- foreach ($this->mSpecTypes as $quality => $items) {
- Log::record("quality = {$quality}",Log::DEBUG);
- foreach ($items as $types_spec => $names) {
- $snames = implode(',',$names);
- Log::record("types_spec = {$types_spec} providers={$snames}",Log::DEBUG);
- }
- }
- }
- public function provider(string $chname)
- {
- if(array_key_exists($chname,$this->mProviders)) {
- return $this->mProviders[$chname];
- }
- else {
- return null;
- }
- }
- public function goods()
- {
- //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
- $result = [];
- foreach ($this->mSpecTypes as $qMaps)
- {
- foreach ($qMaps as $type_spec => $names) {
- $data = explode('-',$type_spec);
- $type = intval($data[0]);
- $spec = intval($data[1]);
- $result[$type][] = $spec;
- }
- }
- $type_specs = [];
- foreach ($result as $type => $value) {
- $type_specs[$type] = array_unique($value);
- }
- return $type_specs;
- }
- public function getCaller($name)
- {
- try
- {
- $file = BASE_HELPER_RAPI_PATH . "/{$name}/RefillCallBack.php";
- if(!file_exists($file)){
- Log::record("provider callback api file={$file} not exist.",Log::DEBUG);
- return false;
- } else {
- require_once($file);
- Log::record("file={$file} load success.",Log::DEBUG);
- }
- $class_name = "refill\\{$name}\\RefillCallBack";
- if (class_exists($class_name, false)) {
- $caller = new $class_name();
- return $caller;
- } else {
- $error = "Base Error: class {$class_name} isn't exists!";
- Log::record($error, Log::ERR);
- return false;
- }
- }
- catch (Exception $ex) {
- Log::record($ex->getMessage(), Log::ERR);
- return false;
- }
- }
- }
|