ProviderManager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $class = "refill\\{$name}\\{$refill_type}";
  72. if (class_exists($class, false)) {
  73. $provider = new $class($cfg);
  74. $provider->setOpened(false);
  75. return $provider;
  76. } else {
  77. $error = "Base Error: class {$class} isn't exists!";
  78. Log::record(__FUNCTION__ . " {$error}", Log::ERR);
  79. }
  80. }
  81. catch (Exception $ex) {
  82. Log::record($ex->getMessage(), Log::ERR);
  83. }
  84. return false;
  85. }
  86. public function load()
  87. {
  88. $this->mProviderNames = [];
  89. $this->mAllQMapPTS = [];
  90. $this->mProviders = [];
  91. $this->mSpecTypes = [];
  92. global $config;
  93. $this->map_cfg($config['phone_providers'],'RefillPhone');
  94. $this->map_cfg($config['oil_providers'],'RefillOil');
  95. $this->mProviderNames = array_unique($this->mProviderNames);
  96. $channels = $this->read_channel();
  97. foreach ($channels as $item)
  98. {
  99. $name = $item['name'];
  100. if(array_key_exists($name,$this->mProviders)) {
  101. $this->mProviders[$name]->setOpened($item['opened']);
  102. }
  103. }
  104. }
  105. private function read_channel()
  106. {
  107. $refill_provider = Model('refill_provider');
  108. $items = $refill_provider->getProviderList([]);
  109. $result = [];
  110. foreach ($items as $item) {
  111. $name = $item['name'];
  112. $val = ['name' => $name,
  113. 'type' => intval($item['type']),
  114. 'opened' => (intval($item['opened']) == 1) ? true : false,
  115. 'sort' => intval($item['sort'])];
  116. $result[$name] = $val;
  117. }
  118. return $result;
  119. }
  120. public function find_providers(int $spec, int $card_type,int $quality): array
  121. {
  122. $qnames = $this->mSpecTypes[$quality] ?? [];
  123. $key = "{$card_type}-{$spec}";
  124. if(array_key_exists($key,$qnames)) {
  125. $names = $qnames[$key];
  126. $providers = [];
  127. foreach ($names as $name)
  128. {
  129. $provider = $this->mProviders[$name];
  130. if($provider->opened()) {
  131. $providers[] = $provider;
  132. }
  133. }
  134. return $providers;
  135. }
  136. else {
  137. return [];
  138. }
  139. }
  140. public function provider(string $chname)
  141. {
  142. if(array_key_exists($chname,$this->mProviders)) {
  143. return $this->mProviders[$chname];
  144. }
  145. else {
  146. return null;
  147. }
  148. }
  149. public function combine_goods($configs,$type)
  150. {
  151. $mod_prov = Model('refill_provider');
  152. $provider_items = $mod_prov->getProviderList(['type' => $type]);
  153. foreach ($provider_items as $item) {
  154. $providers[$item['name']] = $item;
  155. }
  156. $result = [];
  157. foreach ($configs as $item)
  158. {
  159. if($providers[$item['name']]['opened'] != 1) {
  160. continue;
  161. }
  162. $cfg = $item['cfg'];
  163. $card_types = $cfg['card_type'] ?? [];
  164. $amounts = $cfg['amount'] ?? [];
  165. foreach ($card_types as $type) {
  166. if (array_key_exists($type, $result)) {
  167. $item = $result[$type];
  168. } else {
  169. $item = [];
  170. }
  171. foreach ($amounts as $amount => $val) {
  172. $item[] = $amount;
  173. }
  174. $item = array_unique($item);
  175. $result[$type] = $item;
  176. }
  177. }
  178. return $result;
  179. }
  180. public function goods()
  181. {
  182. //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
  183. $result = [];
  184. foreach ($this->mSpecTypes as $qMaps)
  185. {
  186. foreach ($qMaps as $type_spec => $names) {
  187. $data = explode('-',$type_spec);
  188. $type = intval($data[0]);
  189. $spec = intval($data[1]);
  190. $result[$type][] = $spec;
  191. }
  192. }
  193. $type_specs = [];
  194. foreach ($result as $type => $value) {
  195. $type_specs[$type] = array_unique($value);
  196. }
  197. return $type_specs;
  198. }
  199. public function getCaller($chname)
  200. {
  201. try
  202. {
  203. $class_name = "refill\\{$chname}\\RefillCallBack";
  204. if (class_exists($class_name, false)) {
  205. $caller = new $class_name();
  206. return $caller;
  207. } else {
  208. $error = "Base Error: class {$class_name} isn't exists!";
  209. Log::record($error, Log::ERR);
  210. return false;
  211. }
  212. }
  213. catch (Exception $ex) {
  214. Log::record($ex->getMessage(), Log::ERR);
  215. return false;
  216. }
  217. }
  218. }