policy.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace refill;
  3. use Log;
  4. use mtopcard;
  5. class policy extends ProviderManager implements IPolicy
  6. {
  7. protected $mChannelControl;
  8. protected $mQuality;
  9. protected $mPrices;
  10. protected $mGroupCtl;
  11. protected $mRatioCtl;
  12. protected $mGlobalInterceptor;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->mChannelControl = new chctl();
  17. $this->mQuality = new quality_ploy();
  18. $this->mPrices = new merchant_price();
  19. $this->mGroupCtl = new rgroup_ctl();
  20. $this->mRatioCtl = new mratio_control();
  21. $this->mQuality->setRatioCtl($this->mRatioCtl);
  22. $this->mGlobalInterceptor = new interceptor();
  23. }
  24. public function load()
  25. {
  26. parent::load();
  27. $opened_names = $this->mOpenedProviderNames;
  28. sort($opened_names);
  29. $opened_merchants = $this->opened_merchants();
  30. $this->mChannelControl->load($opened_names);
  31. $this->mChannelControl->update_price($this);
  32. $this->mQuality->load();
  33. $this->mPrices->load($opened_merchants);
  34. $turn_name = 'oil_amount_lock_turn';
  35. $this->mAmountLockTurn = rkcache($turn_name);
  36. Log::record("AmountLockTurn = {$this->mAmountLockTurn}",Log::DEBUG);
  37. $this->mGroupCtl->load($opened_names,$opened_merchants);
  38. $this->mRatioCtl->load();
  39. $this->init_mavg_price();
  40. }
  41. private function init_mavg_price()
  42. {
  43. $specs_merger = function ($qspecs)
  44. {
  45. $q_keys = [];
  46. foreach ($qspecs as $quality => $specs)
  47. {
  48. foreach ($specs as $item)
  49. {
  50. [$card_type, $spec] = $item;
  51. $q_keys[$quality][] = "{$card_type}-{$spec}";
  52. }
  53. }
  54. $sspecs = [];
  55. $first = true;
  56. foreach ($q_keys as $quality => $specs)
  57. {
  58. if($first) {
  59. $sspecs = $specs;
  60. $first = false;
  61. }
  62. else {
  63. $sspecs = array_intersect($sspecs,$specs);
  64. }
  65. }
  66. $result = [];
  67. foreach ($sspecs as $item) {
  68. [$card_type, $spec] = explode('-',$item);
  69. $result[] = [intval($card_type), intval($spec)];
  70. }
  71. return $result;
  72. };
  73. $pQuality = $this->mQuality;
  74. $mchids = $pQuality->mechants_quality();
  75. foreach ($mchids as $mchid => $mixed_quality)
  76. {
  77. $qualities = $pQuality->qualities($mixed_quality);
  78. $specs = [];
  79. foreach ($qualities as $quality) {
  80. $item = $this->mPrices->merchant_specs($mchid, $quality);
  81. if (empty($item)) continue;
  82. $specs[$quality] = $item;
  83. }
  84. $specs = $specs_merger($specs);
  85. foreach ($specs as $item)
  86. {
  87. [$card_type, $spec] = $item;
  88. if (!in_array($card_type, [4, 5, 6])) continue;
  89. $prices = [];
  90. $sale_price = $this->mPrices->price($mchid,$card_type,$spec,$mixed_quality,'');
  91. $prices[$mixed_quality] = round($sale_price,4);
  92. foreach ($qualities as $quality) {
  93. $inprice = $this->calc_maxprice($mchid, $quality, $card_type, $spec);
  94. $prices[$quality] = round($inprice,4);
  95. }
  96. $this->mRatioCtl->setMixedPrice($mchid, $mixed_quality, $card_type, $spec, $prices);
  97. }
  98. }
  99. }
  100. protected function calc_avgprice($mchid,$quality,$card_type,$spec)
  101. {
  102. $providers = parent::get_providers($mchid,$spec,$card_type,$quality,-1);
  103. $names = [];
  104. foreach ($providers as $provider) {
  105. $names[] = $provider->name();
  106. }
  107. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $quality);
  108. if($hasGroup)
  109. {
  110. if(empty($channels))
  111. {
  112. if(!$can_others) {
  113. return false;
  114. }
  115. }
  116. else
  117. {
  118. $ret = array_intersect($names, $channels);
  119. if (empty($ret)) {
  120. return false;
  121. }
  122. else {
  123. $names = $ret;
  124. }
  125. }
  126. }
  127. $name_provider = [];
  128. foreach ($providers as $provider) {
  129. $name = $provider->name();
  130. $name_provider[$name] = $provider;
  131. }
  132. $total = 0.0;
  133. foreach ($names as $name) {
  134. $provider = $name_provider[$name];
  135. [$goods_id, $price] = $provider->goods($quality, $spec, $card_type, -1, []);
  136. $total += $price;
  137. }
  138. return $total / count($names);
  139. }
  140. protected function calc_maxprice($mchid,$quality,$card_type,$spec)
  141. {
  142. $providers = parent::get_providers($mchid,$spec,$card_type,$quality,-1);
  143. $names = [];
  144. foreach ($providers as $provider) {
  145. $names[] = $provider->name();
  146. }
  147. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $quality);
  148. if($hasGroup)
  149. {
  150. if(empty($channels))
  151. {
  152. if(!$can_others) {
  153. return false;
  154. }
  155. }
  156. else
  157. {
  158. $ret = array_intersect($names, $channels);
  159. if (empty($ret)) {
  160. return false;
  161. }
  162. else {
  163. $names = $ret;
  164. }
  165. }
  166. }
  167. $name_provider = [];
  168. foreach ($providers as $provider) {
  169. $name = $provider->name();
  170. $name_provider[$name] = $provider;
  171. }
  172. $max_price = 0.0;
  173. foreach ($names as $name) {
  174. $provider = $name_provider[$name];
  175. [$goods_id, $price] = $provider->goods($quality, $spec, $card_type, -1, []);
  176. if($price > $max_price) $max_price = $price;
  177. }
  178. return $max_price;
  179. }
  180. private function opened_merchants()
  181. {
  182. $mchids = [];
  183. $i = 0;
  184. while (true)
  185. {
  186. $start = $i * 1000;
  187. $items = Model()->table('merchant')->field('*')->where(['merchant_state' => 1])->order('mchid asc')->limit("{$start},1000")->select();
  188. if(empty($items)) {
  189. break;
  190. }
  191. $i++;
  192. foreach ($items as $item) {
  193. $mchids[] = intval($item['mchid']);
  194. }
  195. }
  196. sort($mchids);
  197. return $mchids;
  198. }
  199. public function find_providers(order $order): array
  200. {
  201. $mchid = $order->mchid();
  202. $spec = $order->spec();
  203. $card_type = $order->card_type();
  204. $org_quality = $order->org_quality();
  205. $cur_quality = $order->cur_quality();
  206. $pcode = $order->pcode();
  207. $regin_no = $order->region_no();
  208. $order_time = $order->order_time();
  209. $providers = parent::get_providers($mchid,$spec,$card_type,$cur_quality,$regin_no);
  210. if(empty($providers)) {
  211. return [$providers,false];
  212. }
  213. $names = [];
  214. foreach ($providers as $provider) {
  215. $names[] = $provider->name();
  216. }
  217. Log::record("ProviderManager::get_providers result=" . implode(',',$names),Log::DEBUG);
  218. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $cur_quality);
  219. Log::record("GroupControl mchid={$mchid} spec={$spec} quality={$cur_quality} card_type={$card_type} first result=" . implode(',',$channels),Log::DEBUG);
  220. if($hasGroup)
  221. {
  222. if(empty($channels))
  223. {
  224. if(!$can_others) {
  225. return [[],false];
  226. }
  227. }
  228. else
  229. {
  230. $ret = array_intersect($names, $channels);
  231. if (empty($ret)) {
  232. return [[],false];
  233. }
  234. else {
  235. $names = $ret;
  236. }
  237. }
  238. }
  239. Log::record("GroupControl second result=" . implode(',',$names),Log::DEBUG);
  240. if(PolicyUtil::mixed_quality($org_quality)) {
  241. $mixedQuality = $org_quality;
  242. }
  243. else {
  244. $mixedQuality = $cur_quality;
  245. }
  246. $price = $this->mPrices->price($mchid,$card_type,$spec,$mixedQuality,$pcode);
  247. if($price === false) {
  248. return [[],false];
  249. }
  250. $extra_price = $this->mPrices->extra_price($mchid,$card_type,$spec,$mixedQuality,$pcode);
  251. $extra_price = $extra_price == false ? 0.00 : $extra_price;
  252. //以当前通道质量为准
  253. $max_inprice = $this->mPrices->max_inprice($mchid,$card_type,$spec,$cur_quality,$pcode);
  254. global $config;
  255. $auto_find = $config['auto_find_channels'];
  256. $mobile_types = [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard];
  257. $qualities = [Quality::Normal];
  258. if ($auto_find && in_array($card_type, $mobile_types, true) && in_array($cur_quality, $qualities, true)) {
  259. $names = $this->mChannelControl->auto_match($names, $spec, $card_type, $cur_quality, $price - $extra_price, $max_inprice, time() - $order_time);
  260. Log::record("policy::find_providers ChannelControl auto_match quality={$cur_quality} spec={$spec} card_type={$card_type} result=" . implode(',', $names), Log::DEBUG);
  261. } else {
  262. $names = $this->mChannelControl->match($names, $spec, $card_type, $cur_quality, $max_inprice);
  263. Log::record("policy::find_providers ChannelControl match quality={$cur_quality} spec={$spec} card_type={$card_type} result=" . implode(',', $names), Log::DEBUG);
  264. }
  265. $name_provider = [];
  266. foreach ($providers as $provider) {
  267. $name = $provider->name();
  268. $name_provider[$name] = $provider;
  269. }
  270. $result = [];
  271. foreach ($names as $name)
  272. {
  273. if(array_key_exists($name,$name_provider)) {
  274. $result[] = $name_provider[$name];
  275. }
  276. }
  277. return [$result,false];
  278. }
  279. public function price($mchid,$spec,$card_type,$quality,$pcode)
  280. {
  281. return $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  282. }
  283. public function max_inprice($mchid, $spec, $card_type, $quality, $pcode)
  284. {
  285. return $this->mPrices->max_inprice($mchid,$card_type,$spec,$quality,$pcode);
  286. }
  287. public function channeles(int $mchid, int $spec, int $card_type, int $quality, $regin_no)
  288. {
  289. $providers = parent::get_providers($mchid, $spec, $card_type, $quality, $regin_no);
  290. return count($providers);
  291. }
  292. public function find_quality(order $order, bool $skip_pre = false): array
  293. {
  294. $mchid = $order->mchid();
  295. $spec = $order->spec();
  296. $card_type = $order->card_type();
  297. $org_quality = $order->org_quality();
  298. $cur_quality = $order->cur_quality();
  299. $commit_times = $order->commit_times();
  300. $elapse_secs = $order->elapse_secs();
  301. $pcode = $order->pcode();
  302. $regin_no = $order->region_no();
  303. if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  304. $caller = new times_caller($mchid,$spec,$card_type,-1,$this);
  305. } else {
  306. $caller = null;
  307. }
  308. [$org_quality, $qualities] = $this->mQuality->find_quality($mchid, $card_type, $spec, $org_quality, $cur_quality, $commit_times, $elapse_secs, $caller);
  309. if(empty($qualities)) {
  310. return [$org_quality,0];
  311. }
  312. $namer = function ($providers)
  313. {
  314. $result = [];
  315. foreach ($providers as $provider) {
  316. $result[] = $provider->name();
  317. }
  318. return $result;
  319. };
  320. $start = false;
  321. foreach ($qualities as $quality)
  322. {
  323. if(!$start && $skip_pre)
  324. {
  325. if($quality == $cur_quality) {
  326. $start = true;
  327. }
  328. continue;
  329. }
  330. $price = $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  331. if($price === false) {
  332. Log::record("{$mchid} 没有协商 quality = {$quality} 价格",Log::DEBUG);
  333. continue;
  334. }
  335. $max_inprice = $this->mPrices->max_inprice($mchid,$card_type,$spec,$cur_quality,$pcode);
  336. $providers = parent::get_providers($mchid, $spec, $card_type, $quality, $regin_no);
  337. if(empty($providers)) continue;
  338. $names = $namer($providers);
  339. $names = $this->mChannelControl->match($names, $spec, $card_type, $quality, $max_inprice);
  340. if (!empty($names)) {
  341. return [$org_quality, $quality];
  342. } else {
  343. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is fail", Log::DEBUG);
  344. }
  345. }
  346. return [$org_quality,0];
  347. }
  348. public function allow($mchid, $card_type, $amount, $quality): bool
  349. {
  350. return true;
  351. }
  352. public function notify($order_info, $refill_info) : bool
  353. {
  354. $order_state = $order_info['order_state'];
  355. if ($order_state == ORDER_STATE_CANCEL) {
  356. $state = 2;
  357. } else {
  358. $state = 1;
  359. }
  360. $mchid = $refill_info['mchid'];
  361. $mch_info = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  362. [$params, $sign] = $this->body($state, $refill_info, $mch_info);
  363. $params['sgn'] = $sign;
  364. $notify_url = $refill_info['notify_url'];
  365. $resp = http_request($notify_url, $params, 'GET');
  366. return $resp == "ok";
  367. }
  368. private function body($state, $refill_info, $mch_info)
  369. {
  370. $params = [
  371. "usr" => $refill_info['mchid'],
  372. "ord" => $refill_info['mch_order'],
  373. 'bz' => $refill_info['official_sn'] ?? "",
  374. "state" => $state];
  375. $secure_key = $mch_info['secure_key'];
  376. $card_type = $refill_info['card_type'];
  377. if($card_type == mtopcard\ThirdRefillCard)
  378. {
  379. $mod_third = Model('thrid_refill');
  380. $thrid_info = $mod_third->getThird($refill_info['order_id']);
  381. if (!empty($thrid_info))
  382. {
  383. $card_info = $thrid_info['card_info'];
  384. if (!empty($card_info)) {
  385. $encrypt = openssl_encrypt($card_info,'AES-128-CBC',$secure_key);
  386. if($encrypt != false) {
  387. $params['card_info'] = $encrypt;
  388. }
  389. }
  390. }
  391. }
  392. $sign = $this->sign($params, $secure_key);
  393. return [$params, $sign];
  394. }
  395. public function need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no) : bool
  396. {
  397. return $this->mRatioCtl->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no);
  398. }
  399. public function region_intercept($quality,$card_type,$region_no) : bool
  400. {
  401. return $this->mGlobalInterceptor->isIntercept($quality,$card_type,$region_no);
  402. }
  403. private function sign($params, $key)
  404. {
  405. $body = "{$params['ord']}{$params['state']}{$key}";
  406. Log::record("notify body={$body}",Log::DEBUG);
  407. return strtoupper(md5($body));
  408. }
  409. public function update_mchratios($gross, $detail, $types)
  410. {
  411. $this->mRatioCtl->update($gross,$detail);
  412. }
  413. }