ProviderManager.php 7.9 KB

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