ProviderManager.php 6.8 KB

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