ProviderManager.php 8.8 KB

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