ProviderManager.php 9.0 KB

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