policy.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. namespace refill;
  3. use scope_trace;
  4. use Log;
  5. use rbridge\RBridgeFactory;
  6. use StatesHelper;
  7. use mtopcard;
  8. class policy extends ProviderManager implements IPolicy
  9. {
  10. protected $mChannelControl;
  11. protected $mQuality;
  12. protected $mPrices;
  13. protected $mAmountLockTurn;
  14. protected $mStorageLocker;
  15. protected $mGroupCtl;
  16. protected $mRatioCtl;
  17. protected $mGlobalInterceptor;
  18. protected $mMChannels;
  19. protected $mThirdHelper;
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->mChannelControl = new chctlex();
  24. $this->mQuality = new quality_ploy();
  25. $this->mPrices = new merchant_price();
  26. $this->mStorageLocker = new rstorage();
  27. $this->mGroupCtl = new rgroup_ctl();
  28. $this->mRatioCtl = new mratio_controlex();
  29. $this->mQuality->setRatioCtl($this->mRatioCtl);
  30. $this->mGlobalInterceptor = new interceptor();
  31. $this->mMChannels = new mchannel();
  32. $this->mThirdHelper = new third_helper();
  33. }
  34. public function load()
  35. {
  36. parent::load();
  37. $opened_names = $this->mOpenedProviderNames;
  38. sort($opened_names);
  39. $opened_merchants = $this->opened_merchants();
  40. $this->mChannelControl->load($opened_names);
  41. $this->mChannelControl->update_price($this);
  42. $this->mQuality->load();
  43. $this->mPrices->load($opened_merchants);
  44. $turn_name = 'oil_amount_lock_turn';
  45. $this->mAmountLockTurn = rkcache($turn_name);
  46. Log::record("AmountLockTurn = {$this->mAmountLockTurn}",Log::DEBUG);
  47. $this->mStorageLocker->load();
  48. $this->mGroupCtl->load($opened_names,$opened_merchants);
  49. $this->mRatioCtl->load();
  50. $this->mGlobalInterceptor->load();
  51. $this->mMChannels->load();
  52. $this->mThirdHelper->load();
  53. $this->init_mavg_price();
  54. }
  55. private function init_mavg_price()
  56. {
  57. $specs_merger = function ($qspecs)
  58. {
  59. $q_keys = [];
  60. foreach ($qspecs as $quality => $specs)
  61. {
  62. foreach ($specs as $item) {
  63. [$card_type, $spec] = $item;
  64. $q_keys[$quality][] = "{$card_type}-{$spec}";
  65. }
  66. }
  67. $sspecs = [];
  68. $first = true;
  69. foreach ($q_keys as $quality => $specs)
  70. {
  71. if ($first) {
  72. $sspecs = $specs;
  73. $first = false;
  74. } else {
  75. $sspecs = array_intersect($sspecs, $specs);
  76. }
  77. }
  78. $result = [];
  79. foreach ($sspecs as $item) {
  80. [$card_type, $spec] = explode('-',$item);
  81. $result[] = [intval($card_type), intval($spec)];
  82. }
  83. return $result;
  84. };
  85. $pQuality = $this->mQuality;
  86. $mchids = $pQuality->mechants_quality();
  87. foreach ($mchids as $mchid => $mixed_quality)
  88. {
  89. $qualities = $pQuality->qualities($mixed_quality);
  90. $specs = [];
  91. foreach ($qualities as $quality) {
  92. $item = $this->mPrices->merchant_specs($mchid, $quality);
  93. if (empty($item)) continue;
  94. $specs[$quality] = $item;
  95. }
  96. $specs = $specs_merger($specs);
  97. foreach ($specs as $item)
  98. {
  99. [$card_type, $spec] = $item;
  100. if (!in_array($card_type, [4, 5, 6])) continue;
  101. $prices = [];
  102. $sale_price = $this->mPrices->price($mchid,$card_type,$spec,$mixed_quality,'');
  103. $prices[$mixed_quality] = round($sale_price,4);
  104. foreach ($qualities as $quality) {
  105. $inprice = $this->calc_maxprice($mchid, $quality, $card_type, $spec);
  106. $prices[$quality] = round($inprice,4);
  107. }
  108. $this->mRatioCtl->setMixedPrice($mchid, $mixed_quality, $card_type, $spec, $prices);
  109. }
  110. }
  111. }
  112. protected function calc_avgprice($mchid,$quality,$card_type,$spec)
  113. {
  114. $providers = parent::get_providers($mchid,$spec,$card_type,$quality,-1);
  115. $names = [];
  116. foreach ($providers as $provider) {
  117. $names[] = $provider->name();
  118. }
  119. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $quality);
  120. if($hasGroup)
  121. {
  122. if(empty($channels))
  123. {
  124. if(!$can_others) {
  125. return false;
  126. }
  127. }
  128. else
  129. {
  130. $ret = array_intersect($names, $channels);
  131. if (empty($ret)) {
  132. return false;
  133. } else {
  134. $names = $ret;
  135. }
  136. }
  137. }
  138. $name_provider = [];
  139. foreach ($providers as $provider) {
  140. $name = $provider->name();
  141. $name_provider[$name] = $provider;
  142. }
  143. $total = 0.0;
  144. foreach ($names as $name) {
  145. $provider = $name_provider[$name];
  146. [$goods_id, $price] = $provider->goods($quality, $spec, $card_type, -1, []);
  147. $total += $price;
  148. }
  149. return $total / count($names);
  150. }
  151. protected function calc_maxprice($mchid,$quality,$card_type,$spec)
  152. {
  153. $providers = parent::get_providers($mchid,$spec,$card_type,$quality,-1);
  154. $names = [];
  155. foreach ($providers as $provider) {
  156. $names[] = $provider->name();
  157. }
  158. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $quality);
  159. if($hasGroup)
  160. {
  161. if(empty($channels))
  162. {
  163. if(!$can_others) {
  164. return false;
  165. }
  166. }
  167. else
  168. {
  169. $ret = array_intersect($names, $channels);
  170. if (empty($ret)) {
  171. return false;
  172. } else {
  173. $names = $ret;
  174. }
  175. }
  176. }
  177. $name_provider = [];
  178. foreach ($providers as $provider) {
  179. $name = $provider->name();
  180. $name_provider[$name] = $provider;
  181. }
  182. $max_price = 0.0;
  183. foreach ($names as $name) {
  184. $provider = $name_provider[$name];
  185. [$goods_id, $price] = $provider->goods($quality, $spec, $card_type, -1, []);
  186. if($price > $max_price) $max_price = $price;
  187. }
  188. return $max_price;
  189. }
  190. private function opened_merchants()
  191. {
  192. $mchids = [];
  193. $i = 0;
  194. while (true)
  195. {
  196. $start = $i * 1000;
  197. $items = Model()->table('merchant')->field('*')->where(['merchant_state' => 1])->order('mchid asc')->limit("{$start},1000")->select();
  198. if(empty($items)) {
  199. break;
  200. }
  201. $i++;
  202. foreach ($items as $item) {
  203. $mchids[] = intval($item['mchid']);
  204. }
  205. }
  206. sort($mchids);
  207. return $mchids;
  208. }
  209. public function find_providers(order $order): array
  210. {
  211. $mchid = $order->mchid();
  212. $spec = $order->spec();
  213. $card_type = $order->card_type();
  214. $org_quality = $order->org_quality();
  215. $cur_quality = $order->cur_quality();
  216. $pcode = $order->pcode();
  217. $regin_no = $order->region_no();
  218. $order_time = $order->order_time();
  219. $providers = parent::get_providers($mchid,$spec,$card_type,$cur_quality,$regin_no);
  220. if(empty($providers)) {
  221. return [$providers,false];
  222. }
  223. $names = [];
  224. foreach ($providers as $provider) {
  225. $names[] = $provider->name();
  226. }
  227. Log::record("ProviderManager::get_providers result=" . implode(',',$names),Log::DEBUG);
  228. //机构和通道,交叉设置过滤
  229. [$card_paper, $channels] = $this->mMChannels->channels($mchid, $card_type, $cur_quality);
  230. if ($card_paper === mtopcard\OilCardPaper || $card_paper === mtopcard\PhoneCardPaper) {
  231. sort($names);
  232. $names = array_intersect($names, $channels);
  233. }
  234. Log::record("mchannel::channels result=" . implode(',',$names),Log::DEBUG);
  235. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $cur_quality);
  236. Log::record("GroupControl mchid={$mchid} spec={$spec} quality={$cur_quality} card_type={$card_type} first result=" . implode(',',$channels),Log::DEBUG);
  237. if($hasGroup)
  238. {
  239. if(empty($channels))
  240. {
  241. if(!$can_others) {
  242. return [[],false];
  243. }
  244. }
  245. else
  246. {
  247. $ret = array_intersect($names, $channels);
  248. if (empty($ret)) {
  249. return [[], false];
  250. } else {
  251. $names = $ret;
  252. }
  253. }
  254. }
  255. Log::record("GroupControl second result=" . implode(',',$names),Log::DEBUG);
  256. if(PolicyUtil::mixed_quality($org_quality)) {
  257. $mixedQuality = $org_quality;
  258. }
  259. else {
  260. $mixedQuality = $cur_quality;
  261. }
  262. $price = $this->mPrices->price($mchid,$card_type,$spec,$mixedQuality,$pcode);
  263. if($price === false) {
  264. return [[],false];
  265. }
  266. $extra_price = $this->mPrices->extra_price($mchid,$card_type,$spec,$mixedQuality,$pcode);
  267. $extra_price = $extra_price == false ? 0.00 : $extra_price;
  268. //以当前通道质量为准
  269. $max_inprice = $this->mPrices->max_inprice($mchid,$card_type,$spec,$cur_quality,$pcode);
  270. Log::record("max_price={$max_inprice}",Log::DEBUG);
  271. global $config;
  272. $auto_find = $config['auto_find_channels'];
  273. $mobile_types = [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard];
  274. $qualities = [Quality::Normal, Quality::Quick, Quality::CardKey, Quality::ThirdShop];
  275. if ($auto_find && in_array($card_type, $mobile_types, true) && in_array($cur_quality, $qualities, true)) {
  276. $names = $this->mChannelControl->auto_match($names, $spec, $card_type, $cur_quality, $price - $extra_price, $max_inprice, time() - $order_time);
  277. Log::record("policy::find_providers ChannelControl auto_match quality={$cur_quality} spec={$spec} card_type={$card_type} result=" . implode(',', $names), Log::DEBUG);
  278. } else {
  279. $names = $this->mChannelControl->match($names, $spec, $card_type, $cur_quality, $max_inprice);
  280. Log::record("policy::find_providers ChannelControl match quality={$cur_quality} spec={$spec} card_type={$card_type} result=" . implode(',', $names), Log::DEBUG);
  281. }
  282. $name_provider = [];
  283. foreach ($providers as $provider) {
  284. $name = $provider->name();
  285. $name_provider[$name] = $provider;
  286. }
  287. $result = [];
  288. foreach ($names as $name)
  289. {
  290. if(array_key_exists($name,$name_provider)) {
  291. $result[] = $name_provider[$name];
  292. }
  293. }
  294. return [$result,false];
  295. }
  296. public function price($mchid,$spec,$card_type,$quality,$pcode)
  297. {
  298. return $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  299. }
  300. public function max_inprice($mchid, $spec, $card_type, $quality, $pcode)
  301. {
  302. return $this->mPrices->max_inprice($mchid,$card_type,$spec,$quality,$pcode);
  303. }
  304. public function channeles(int $mchid, int $spec, int $card_type, int $quality, $regin_no)
  305. {
  306. $providers = parent::get_providers($mchid, $spec, $card_type, $quality, $regin_no);
  307. return count($providers);
  308. }
  309. public function find_quality(order $order, bool $skip_pre = false): array
  310. {
  311. $trace = new scope_trace(__METHOD__);
  312. $mchid = $order->mchid();
  313. $spec = $order->spec();
  314. $card_type = $order->card_type();
  315. $org_quality = $order->org_quality();
  316. $cur_quality = $order->cur_quality();
  317. $commit_times = $order->commit_times();
  318. $elapse_secs = $order->elapse_secs();
  319. $pcode = $order->pcode();
  320. $regin_no = $order->region_no();
  321. if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  322. $caller = new times_caller($mchid,$spec,$card_type,-1,$this);
  323. } else {
  324. $caller = null;
  325. }
  326. [$org_quality, $qualities,$match] = $this->mQuality->find_quality($mchid, $card_type, $spec, $org_quality, $cur_quality, $commit_times, $elapse_secs, $caller);
  327. $order->set_match($match);
  328. if(empty($qualities)) {
  329. return [$org_quality,0];
  330. }
  331. $namer = function ($providers)
  332. {
  333. $result = [];
  334. foreach ($providers as $provider) {
  335. $result[] = $provider->name();
  336. }
  337. return $result;
  338. };
  339. Log::record("skip_pre = $skip_pre",Log::DEBUG);
  340. $start = false;
  341. foreach ($qualities as $quality)
  342. {
  343. if(!$start && $skip_pre)
  344. {
  345. if($quality == $cur_quality) {
  346. $start = true;
  347. continue;
  348. }
  349. }
  350. $price = $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  351. if($price === false) {
  352. Log::record("{$mchid} 没有协商 quality = {$quality} 价格",Log::DEBUG);
  353. continue;
  354. }
  355. $max_inprice = $this->mPrices->max_inprice($mchid, $card_type, $spec, $quality, $pcode);
  356. Log::record("quality = {$quality} max_price={$max_inprice}",Log::DEBUG);
  357. $providers = parent::get_providers($mchid, $spec, $card_type, $quality, $regin_no);
  358. if(empty($providers)) {
  359. continue;
  360. }
  361. $names = $namer($providers);
  362. //机构和通道,交叉设置过滤
  363. [$card_paper, $channels] = $this->mMChannels->channels($mchid, $card_type, $quality);
  364. if ($card_paper === mtopcard\OilCardPaper || $card_paper === mtopcard\PhoneCardPaper) {
  365. sort($names);
  366. $names = array_intersect($names, $channels);
  367. }
  368. $names = $this->mChannelControl->match($names, $spec, $card_type, $quality, $max_inprice);
  369. if (!empty($names)) {
  370. return [$org_quality, $quality];
  371. } else {
  372. Log::record("Policy::find_quality fail: mchid={$mchid} {$quality}-{$spec}-{$card_type}", Log::DEBUG);
  373. }
  374. }
  375. Log::record("Policy::find_quality fail: mchid={$mchid} {$spec}-{$card_type}", Log::DEBUG);
  376. return [$org_quality,0];
  377. }
  378. public function allow($mchid,$card_type,$amount,$quality) : bool
  379. {
  380. return $this->mStorageLocker->allow($mchid,$card_type,$amount);
  381. }
  382. private function allow_storge($mchid,$card_type,$amount,$quality)
  383. {
  384. $reader = function ()
  385. {
  386. $cache = rcache("refill_able",'merchant-');
  387. if(!empty($cache)) {
  388. $result = unserialize($cache['data']);
  389. }
  390. else {
  391. $result = [];
  392. }
  393. return $result;
  394. };
  395. if(defined('MOBILE_SERVER') && MOBILE_SERVER === true)
  396. {
  397. if(StatesHelper::fetch_state('merchant')) {
  398. $this->mLimits = $reader();
  399. }
  400. }
  401. else {
  402. $this->mLimits = $reader();
  403. }
  404. $key = "{$mchid}-{$card_type}-{$amount}";
  405. if(empty($this->mLimits)) {
  406. return true;
  407. }
  408. elseif(array_key_exists($key,$this->mLimits)) {
  409. return $this->mLimits[$key];
  410. }
  411. else {
  412. return true;
  413. }
  414. }
  415. public function notify($order_info, $refill_info) : bool
  416. {
  417. $order_state = $order_info['order_state'];
  418. if ($order_state == ORDER_STATE_CANCEL) {
  419. $state = "CANCEL";
  420. } else {
  421. $state = "SUCCESS";
  422. }
  423. $mchid = $refill_info['mchid'];
  424. $mch_info = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  425. [$params, $sign] = $this->body($state, $refill_info, $mch_info);
  426. $params['sign'] = $sign;
  427. $notify_url = $refill_info['notify_url'];
  428. //如果http请求内部,又发出回调自己的请求,在处理进程非动态扩容的情况下,容易造成阻塞.
  429. if ($this->is_url($notify_url)) {
  430. $resp = http_request($notify_url, $params, 'POST');
  431. } else {
  432. $resp = RBridgeFactory::instance()->notify($notify_url, $params);
  433. }
  434. return $resp == "SUCCESS" || $resp == 'FAILED';
  435. }
  436. private function body($state, $refill_info, $mch_info)
  437. {
  438. if($state == 'SUCCESS') {
  439. $err_msg = '';
  440. } else {
  441. $err_msg = 'fail';
  442. }
  443. $ctls = ['osn' => true, 'sms' => false];
  444. util::onEventCallback($refill_info, $mch_info, $ctls);
  445. if($ctls['osn']) {
  446. $osn = $refill_info['official_sn'] ?? "";
  447. } else {
  448. $osn = "";
  449. }
  450. $params = [
  451. "mchid" => $refill_info['mchid'],
  452. "order_sn" => $refill_info['mch_order'],
  453. "amount" => $refill_info['refill_amount'],
  454. "sale_price" => ncPriceFormat($refill_info['mch_amount']),
  455. "cardno" => $refill_info['card_no'],
  456. "trade_no" => $refill_info['order_sn'],
  457. "idcard" => $refill_info['idcard'] ?? "",
  458. "card_name" => $refill_info['card_name'] ?? "",
  459. 'official_sn' => $osn,
  460. 'message' => $err_msg,
  461. "state" => $state];
  462. if($ctls['sms'])
  463. {
  464. [$has_sms,$sms] = $this->sms($refill_info);
  465. if($has_sms) {
  466. $params['sms'] = $sms;
  467. }
  468. }
  469. $secure_key = $mch_info['secure_key'];
  470. $sign = $this->sign($params, $secure_key);
  471. return [$params, $sign];
  472. }
  473. private function sms($refill_info)
  474. {
  475. $official_sn = $refill_info['official_sn'] ?? "";
  476. $card_type = $refill_info['card_type'];
  477. $card_no = $refill_info['card_no'];
  478. $amount = intval($refill_info['refill_amount']);
  479. if(!empty($official_sn) && $card_type == mtopcard\SinopecCard && !empty($card_no))
  480. {
  481. $short_no = substr($card_no,-6);
  482. $ret = preg_match('/\d{4}(?P<month>\d{2})(?P<day>\d{2})(?P<hour>\d{2})(?P<min>\d{2})\d{4}/u', $official_sn, $matches);
  483. if($ret > 0) {
  484. $sms = "【中国石化】您尾号为{$short_no}的加油卡于{$matches['month']}月{$matches['day']}日 {$matches['hour']}时{$matches['min']}分充值成功,金额{$amount}元,订单号:{$official_sn}";
  485. return [true,$sms];
  486. }
  487. }
  488. return [false,''];
  489. }
  490. private function sign($params, $key)
  491. {
  492. ksort($params);
  493. $body = "";
  494. $i = 0;
  495. foreach ($params as $k => $v) {
  496. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  497. if ($i == 0) {
  498. $body .= "$k" . "=" . urlencode($v);
  499. } else {
  500. $body .= "&" . "$k" . "=" . urlencode($v);
  501. }
  502. $i++;
  503. }
  504. }
  505. $body .= "&key=$key";
  506. Log::record("notify_body=$body", Log::DEBUG);
  507. return md5($body);
  508. }
  509. private function check_empty($value)
  510. {
  511. if (!isset($value))
  512. return true;
  513. if ($value === null)
  514. return true;
  515. if (trim($value) === "")
  516. return true;
  517. return false;
  518. }
  519. private function is_url($url)
  520. {
  521. $checker = function ($haystack, $needle) {
  522. $length = strlen($needle);
  523. return (substr($haystack, 0, $length) === $needle);
  524. };
  525. return $checker($url, "http://") || $checker($url, "https://");
  526. }
  527. public function need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no) : bool
  528. {
  529. return $this->mRatioCtl->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no);
  530. }
  531. public function region_intercept($quality,$card_type,$region_no) : bool
  532. {
  533. return $this->mGlobalInterceptor->isIntercept($quality,$card_type,$region_no);
  534. }
  535. public function update_mchratios($gross,$detail,$types)
  536. {
  537. $this->mRatioCtl->update($gross,$detail,$types);
  538. }
  539. public function update_chctl($params)
  540. {
  541. $this->mChannelControl->update_chctl($params);
  542. }
  543. public function update_maxspeeds($speeds)
  544. {
  545. $this->mChannelControl->update_maxspeeds($speeds);
  546. }
  547. public function third_retry(order $order): array
  548. {
  549. return $this->mThirdHelper->third_retry($order);
  550. }
  551. public function third_mixed($mchid,$pcode): bool
  552. {
  553. return $this->mThirdHelper->mixed($mchid,$pcode);
  554. }
  555. }