ProviderManager.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. require_once(BASE_HELPER_PATH . '/refill/IRefill.php');
  5. require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
  6. require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
  7. require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
  8. require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
  9. require_once(BASE_HELPER_PATH . '/refill/util.php');
  10. use Log;
  11. use mtopcard;
  12. use Exception;
  13. class ProviderManager
  14. {
  15. protected $mProviderNames;
  16. protected $mAllQMapPTS;
  17. protected $mProviders;
  18. protected $mSpecTypes;
  19. public function __construct()
  20. {
  21. }
  22. public function getQPTA()
  23. {
  24. return $this->mAllQMapPTS;
  25. }
  26. private function map_cfg($cfgs,$refill_type)
  27. {
  28. $card_types = function ($stypes)
  29. {
  30. $result = [];
  31. $types = explode(',',$stypes);
  32. foreach ($types as $stype) {
  33. $type = mtopcard\topcard_type($stype);
  34. $result[] = $type;
  35. }
  36. return $result;
  37. };
  38. $result = [];
  39. foreach ($cfgs as $item)
  40. {
  41. $name = $item['name'];
  42. $cfg = $item['cfg'];
  43. $provider = $this->create_provider($name,$cfg,$refill_type);
  44. if($provider !== false) {
  45. $this->mProviders[$name] = $provider;
  46. $this->mProviderNames[] = $name;
  47. } else {
  48. continue;
  49. }
  50. $amounts = $cfg['amount'] ?? [];
  51. foreach ($amounts as $spec => $goods)
  52. {
  53. foreach ($goods as $gitem)
  54. {
  55. $quality = $gitem['quality'] ?? 1;
  56. $types = $card_types($gitem['card_type']);
  57. foreach ($types as $type) {
  58. $this->mAllQMapPTS[$quality]["{$name}-{$type}-{$spec}"] = ['goods_id' => $gitem['goods_id'],'price' => $gitem['price'],'provider' => $provider];
  59. $this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
  60. }
  61. }
  62. }
  63. }
  64. return $result;
  65. }
  66. private function create_provider($name,$cfg,$refill_type)
  67. {
  68. try
  69. {
  70. $file = BASE_HELPER_RAPI_PATH . "/{$name}/{$refill_type}.php";
  71. if(!file_exists($file)){
  72. Log::record("provider api file={$file} not exist.",Log::DEBUG);
  73. return false;
  74. } else {
  75. require_once($file);
  76. Log::record("file={$file} load success.",Log::DEBUG);
  77. }
  78. $class = "refill\\{$name}\\{$refill_type}";
  79. if (class_exists($class, false)) {
  80. $provider = new $class($cfg);
  81. $provider->setOpened(false);
  82. return $provider;
  83. } else {
  84. $error = "Base Error: class {$class} isn't exists!";
  85. Log::record(__FUNCTION__ . " {$error}", Log::ERR);
  86. }
  87. }
  88. catch (Exception $ex) {
  89. Log::record($ex->getMessage(), Log::ERR);
  90. }
  91. return false;
  92. }
  93. public function load()
  94. {
  95. try
  96. {
  97. $file = BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php';
  98. if(!file_exists($file)){
  99. Log::record("refill.ini.php ={$file} not exist",Log::DEBUG);
  100. return false;
  101. } else {
  102. include($file);
  103. }
  104. $this->mProviderNames = [];
  105. $this->mAllQMapPTS = [];
  106. $this->mProviders = [];
  107. $this->mSpecTypes = [];
  108. global $config;
  109. $this->map_cfg($config['phone_providers'],'RefillPhone');
  110. $this->map_cfg($config['oil_providers'],'RefillOil');
  111. $this->mProviderNames = array_unique($this->mProviderNames);
  112. $channels = $this->read_channel();
  113. foreach ($channels as $item)
  114. {
  115. $name = $item['name'];
  116. if(array_key_exists($name,$this->mProviders)) {
  117. $this->mProviders[$name]->setOpened($item['opened']);
  118. }
  119. }
  120. }
  121. catch (Exception $ex)
  122. {
  123. Log::record(__FUNCTION__ ." " . $ex->getMessage(), Log::ERR);
  124. }
  125. }
  126. private function read_channel()
  127. {
  128. $refill_provider = Model('refill_provider');
  129. $items = $refill_provider->getProviderList([]);
  130. $result = [];
  131. foreach ($items as $item) {
  132. $name = $item['name'];
  133. $val = ['name' => $name,
  134. 'type' => intval($item['type']),
  135. 'opened' => intval($item['opened']) == 1,
  136. 'sort' => intval($item['sort'])];
  137. $result[$name] = $val;
  138. }
  139. return $result;
  140. }
  141. public function find_providers(int $spec, int $card_type,int $quality): array
  142. {
  143. $qnames = $this->mSpecTypes[$quality] ?? [];
  144. // $this->debug();
  145. $key = "{$card_type}-{$spec}";
  146. Log::record("quality = {$quality} , key = {$key}",Log::DEBUG);
  147. if(array_key_exists($key,$qnames))
  148. {
  149. $names = $qnames[$key];
  150. $providers = [];
  151. foreach ($names as $name)
  152. {
  153. Log::record("name = {$name}",Log::DEBUG);
  154. $provider = $this->mProviders[$name];
  155. if($provider->opened()) {
  156. $providers[] = $provider;
  157. }
  158. }
  159. Log::record("providers count = " . count($providers),Log::DEBUG);
  160. return $providers;
  161. }
  162. else {
  163. return [];
  164. }
  165. }
  166. private function debug()
  167. {
  168. //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
  169. foreach ($this->mSpecTypes as $quality => $items) {
  170. Log::record("quality = {$quality}",Log::DEBUG);
  171. foreach ($items as $types_spec => $names) {
  172. $snames = implode(',',$names);
  173. Log::record("types_spec = {$types_spec} providers={$snames}",Log::DEBUG);
  174. }
  175. }
  176. }
  177. public function provider(string $chname)
  178. {
  179. if(array_key_exists($chname,$this->mProviders)) {
  180. return $this->mProviders[$chname];
  181. }
  182. else {
  183. return null;
  184. }
  185. }
  186. public function goods()
  187. {
  188. //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
  189. $result = [];
  190. foreach ($this->mSpecTypes as $qMaps)
  191. {
  192. foreach ($qMaps as $type_spec => $names) {
  193. $data = explode('-',$type_spec);
  194. $type = intval($data[0]);
  195. $spec = intval($data[1]);
  196. $result[$type][] = $spec;
  197. }
  198. }
  199. $type_specs = [];
  200. foreach ($result as $type => $value) {
  201. $type_specs[$type] = array_unique($value);
  202. }
  203. return $type_specs;
  204. }
  205. public function getCaller($name)
  206. {
  207. try
  208. {
  209. $file = BASE_HELPER_RAPI_PATH . "/{$name}/RefillCallBack.php";
  210. if(!file_exists($file)){
  211. Log::record("provider callback api file={$file} not exist.",Log::DEBUG);
  212. return false;
  213. } else {
  214. require_once($file);
  215. Log::record("file={$file} load success.",Log::DEBUG);
  216. }
  217. $class_name = "refill\\{$name}\\RefillCallBack";
  218. if (class_exists($class_name, false)) {
  219. $caller = new $class_name();
  220. return $caller;
  221. } else {
  222. $error = "Base Error: class {$class_name} isn't exists!";
  223. Log::record($error, Log::ERR);
  224. return false;
  225. }
  226. }
  227. catch (Exception $ex) {
  228. Log::record($ex->getMessage(), Log::ERR);
  229. return false;
  230. }
  231. }
  232. }