ProviderManager.php 8.5 KB

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