ProviderManager.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. $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 combine_goods($configs,$type)
  187. {
  188. $mod_prov = Model('refill_provider');
  189. $provider_items = $mod_prov->getProviderList(['type' => $type]);
  190. foreach ($provider_items as $item) {
  191. $providers[$item['name']] = $item;
  192. }
  193. $result = [];
  194. foreach ($configs as $item)
  195. {
  196. if($providers[$item['name']]['opened'] != 1) {
  197. continue;
  198. }
  199. $cfg = $item['cfg'];
  200. $card_types = $cfg['card_type'] ?? [];
  201. $amounts = $cfg['amount'] ?? [];
  202. foreach ($card_types as $type) {
  203. if (array_key_exists($type, $result)) {
  204. $item = $result[$type];
  205. } else {
  206. $item = [];
  207. }
  208. foreach ($amounts as $amount => $val) {
  209. $item[] = $amount;
  210. }
  211. $item = array_unique($item);
  212. $result[$type] = $item;
  213. }
  214. }
  215. return $result;
  216. }
  217. public function goods()
  218. {
  219. //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
  220. $result = [];
  221. foreach ($this->mSpecTypes as $qMaps)
  222. {
  223. foreach ($qMaps as $type_spec => $names) {
  224. $data = explode('-',$type_spec);
  225. $type = intval($data[0]);
  226. $spec = intval($data[1]);
  227. $result[$type][] = $spec;
  228. }
  229. }
  230. $type_specs = [];
  231. foreach ($result as $type => $value) {
  232. $type_specs[$type] = array_unique($value);
  233. }
  234. return $type_specs;
  235. }
  236. public function getCaller($name)
  237. {
  238. try
  239. {
  240. $file = BASE_HELPER_RAPI_PATH . "/{$name}/RefillCallBack.php";
  241. if(!file_exists($file)){
  242. Log::record("provider callback api file={$file} not exist.",Log::DEBUG);
  243. return false;
  244. } else {
  245. require_once($file);
  246. Log::record("file={$file} load success.",Log::DEBUG);
  247. }
  248. $class_name = "refill\\{$name}\\RefillCallBack";
  249. if (class_exists($class_name, false)) {
  250. $caller = new $class_name();
  251. return $caller;
  252. } else {
  253. $error = "Base Error: class {$class_name} isn't exists!";
  254. Log::record($error, Log::ERR);
  255. return false;
  256. }
  257. }
  258. catch (Exception $ex) {
  259. Log::record($ex->getMessage(), Log::ERR);
  260. return false;
  261. }
  262. }
  263. }