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->map_cfg($config['third_providers'],'RefillPhone');
  114. $this->mProviderNames = array_unique($this->mProviderNames);
  115. $channels = $this->read_channel();
  116. foreach ($channels as $item)
  117. {
  118. $name = $item['name'];
  119. $regins = $item['regins'];
  120. if(array_key_exists($name,$this->mProviders)) {
  121. $this->mProviders[$name]->setOpened($item['opened']);
  122. $this->mProviders[$name]->setRegins($regins);
  123. }
  124. }
  125. }
  126. catch (Exception $ex)
  127. {
  128. Log::record(__FUNCTION__ ." " . $ex->getMessage(), Log::ERR);
  129. }
  130. }
  131. private function read_channel()
  132. {
  133. $refill_provider = Model('refill_provider');
  134. $items = $refill_provider->getProviderList(['provider_id' => ['gt' , 0]]);
  135. $result = [];
  136. foreach ($items as $item)
  137. {
  138. $name = $item['name'];
  139. $regins = [];
  140. $tmp = unserialize($item['provinces']);
  141. if(!empty($tmp))
  142. {
  143. foreach ($tmp as $card_type => $sregions)
  144. {
  145. $rs = explode(',',$sregions);
  146. if(!empty($rs)) {
  147. $regins[$card_type] = $rs;
  148. }
  149. }
  150. }
  151. $val = ['name' => $name,
  152. 'type' => intval($item['type']),
  153. 'opened' => intval($item['opened']) == 1,
  154. 'sort' => intval($item['sort']),
  155. 'regins' => $regins
  156. ];
  157. $result[$name] = $val;
  158. }
  159. return $result;
  160. }
  161. public function get_providers(int $mchid, int $spec, int $card_type, int $quality, $regin_no): array
  162. {
  163. $qnames = $this->mSpecTypes[$quality] ?? [];
  164. $key = "{$card_type}-{$spec}";
  165. Log::record("quality = {$quality} , key = {$key}",Log::DEBUG);
  166. if(array_key_exists($key,$qnames))
  167. {
  168. $names = $qnames[$key];
  169. $providers = [];
  170. foreach ($names as $name)
  171. {
  172. $provider = $this->mProviders[$name];
  173. $opened = $provider->opened();
  174. if($opened == false) continue;
  175. if($provider->match_regin($card_type,$regin_no)) {
  176. $providers[] = $provider;
  177. }
  178. }
  179. return $providers;
  180. }
  181. else {
  182. return [];
  183. }
  184. }
  185. private function debug()
  186. {
  187. //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
  188. foreach ($this->mSpecTypes as $quality => $items) {
  189. Log::record("quality = {$quality}",Log::DEBUG);
  190. foreach ($items as $types_spec => $names) {
  191. $snames = implode(',',$names);
  192. Log::record("types_spec = {$types_spec} providers={$snames}",Log::DEBUG);
  193. }
  194. }
  195. }
  196. public function provider(string $chname)
  197. {
  198. if(array_key_exists($chname,$this->mProviders)) {
  199. return $this->mProviders[$chname];
  200. }
  201. else {
  202. return null;
  203. }
  204. }
  205. public function goods()
  206. {
  207. //$this->mSpecTypes[$quality]["{$type}-{$spec}"][] = $name;
  208. $result = [];
  209. foreach ($this->mSpecTypes as $qMaps)
  210. {
  211. foreach ($qMaps as $type_spec => $names) {
  212. $data = explode('-',$type_spec);
  213. $type = intval($data[0]);
  214. $spec = intval($data[1]);
  215. $result[$type][] = $spec;
  216. }
  217. }
  218. $type_specs = [];
  219. foreach ($result as $type => $value) {
  220. $type_specs[$type] = array_unique($value);
  221. }
  222. return $type_specs;
  223. }
  224. public function getCaller($name)
  225. {
  226. try
  227. {
  228. $file = BASE_HELPER_RAPI_PATH . "/{$name}/RefillCallBack.php";
  229. if(!file_exists($file)){
  230. Log::record("provider callback api file={$file} not exist.",Log::DEBUG);
  231. return false;
  232. } else {
  233. require_once($file);
  234. Log::record("file={$file} load success.",Log::DEBUG);
  235. }
  236. $class_name = "refill\\{$name}\\RefillCallBack";
  237. if (class_exists($class_name, false)) {
  238. $caller = new $class_name();
  239. return $caller;
  240. } else {
  241. $error = "Base Error: class {$class_name} isn't exists!";
  242. Log::record($error, Log::ERR);
  243. return false;
  244. }
  245. }
  246. catch (Exception $ex) {
  247. Log::record($ex->getMessage(), Log::ERR);
  248. return false;
  249. }
  250. }
  251. }