util.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  5. require_once(BASE_HELPER_PATH . '/queue/monitor.php');
  6. require_once(BASE_HELPER_PATH . '/refill/policy/rlock.php');
  7. use queue;
  8. use mtopcard;
  9. use Log;
  10. use Exception;
  11. use Cache;
  12. use QueueClient;
  13. use refill;
  14. use trans_wapper;
  15. class util
  16. {
  17. const ThirdRefillAmount = 100;
  18. static function make_mobile()
  19. {
  20. static $prefix = ["139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152",
  21. "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185",
  22. "176", "133", "153", "189", "180", "181", "177"];
  23. $pos = mt_rand(0, count($prefix) - 1);
  24. $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
  25. return $no;
  26. }
  27. public static function can_refill($card_no, $card_type)
  28. {
  29. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  30. $result = rcache('card_expired', '', "{$card_no}");
  31. if (empty($result)) {
  32. wcache("card_expired", [$card_no => time()], '');
  33. return [true, 0];
  34. } else {
  35. $latest = current($result);
  36. $cur = time();
  37. $success = ($cur - $latest) > 2;
  38. if ($success) {
  39. wcache("card_expired", [$card_no => time()], '');
  40. }
  41. return [$success, $latest + 2 - $cur];
  42. }
  43. } else {
  44. return [true, 0];
  45. }
  46. }
  47. public static function can_commit($card_no, $card_type)
  48. {
  49. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
  50. {
  51. $result = rcache('card_expired', '', "{$card_no}");
  52. if (empty($result)) {
  53. wcache("card_expired", [$card_no => time()], '');
  54. return [true, 0];
  55. }
  56. else
  57. {
  58. $latest = current($result);
  59. $cur = time();
  60. $lowest = 30;
  61. if ($cur > $latest && ($cur - $latest) >= $lowest) {
  62. wcache("card_expired", [$card_no => time()], '');
  63. return [true, 0];
  64. } else {
  65. wcache("card_expired", [$card_no => $latest + $lowest], '');
  66. return [false, $latest + $lowest - $cur];
  67. }
  68. }
  69. }
  70. else {
  71. return [true, 0];
  72. }
  73. }
  74. static function write_card($card_no, $card_type,$bind_phone)
  75. {
  76. if(empty($bind_phone)) {
  77. return false;
  78. }
  79. if ($card_type !== mtopcard\SinopecCard && $card_type !== mtopcard\PetroChinaCard) {
  80. return false;
  81. }
  82. $mobile_types = [mtopcard\ChinaMobileCard,mtopcard\ChinaUnicomCard,mtopcard\ChinaTelecomCard];
  83. $ctype = mtopcard\simple_card_type($bind_phone);
  84. if (!in_array($ctype,$mobile_types)) {
  85. return false;
  86. }
  87. $mod_topcard = Model('topcard');
  88. $ret = $mod_topcard->get_card($card_no);
  89. if (empty($ret)) {
  90. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  91. } else {
  92. $mod_topcard->edit($card_no,$bind_phone);
  93. }
  94. dcache($card_no, 'cardrefill-');
  95. return true;
  96. }
  97. static function read_card($card_no, $card_type = 0)
  98. {
  99. if (empty($card_no)) return false;
  100. $data = rcache($card_no, 'cardrefill-');
  101. if (empty($data)) {
  102. $mod_topcard = Model('topcard');
  103. $ret = $mod_topcard->get_card($card_no);
  104. if (empty($ret)) {
  105. if ($card_type === 0) {
  106. $card_type = mtopcard\card_type($card_no,$regin_no);
  107. }
  108. $bind_phone = util::make_mobile();
  109. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  110. $data['bind_phone'] = $bind_phone;
  111. $data['refill_time'] = time();
  112. $data['times'] = 0;
  113. $data['black_card'] = 0;
  114. wcache($card_no, $data, 'cardrefill-');
  115. } else {
  116. $val = $ret[0];
  117. $data['bind_phone'] = $val['bind_phone'];
  118. $data['black_card'] = $val['black_card'];
  119. $data['refill_time'] = time();
  120. $data['times'] = 0;
  121. }
  122. }
  123. //之前没加black_card处理,这个字段不存在.
  124. if (!array_key_exists('black_card', $data)) {
  125. $data['black_card'] = 0;
  126. }
  127. return $data;
  128. }
  129. static function inc_card($card_no, $card_info)
  130. {
  131. $card_info['times'] += 1;
  132. $card_info['refill_time'] = time();
  133. wcache($card_no, $card_info, 'cardrefill-');
  134. }
  135. public static function del_card($card_no)
  136. {
  137. dcache($card_no, 'cardrefill-');
  138. }
  139. public static function set_black($card_no)
  140. {
  141. if (empty($card_no)) return false;
  142. $card_info = util::read_card($card_no);
  143. if (!empty($card_info)) {
  144. $card_info['black_card'] = 1;
  145. $mod_topcard = Model('topcard');
  146. $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
  147. wcache($card_no, $card_info, 'cardrefill-');
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. private static function black_order($order_sn, $msg)
  154. {
  155. static $errMsgs = ["只能给主卡且卡状态正常的加油卡充值", "加油卡卡号错误或不支持"];
  156. if (empty($msg)) return false;
  157. if (in_array($msg, $errMsgs)) {
  158. $refill = Model('refill_order');
  159. $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
  160. if (empty($order)) return false;
  161. $card_no = $order['card_no'];
  162. return util::set_black($card_no);
  163. }
  164. }
  165. public static function black_from_log($file_name)
  166. {
  167. $fn = fopen($file_name, "r");
  168. if (empty($fn)) {
  169. Log::record("Open File {$file_name} error.", Log::ERR);
  170. return false;
  171. } else {
  172. Log::record("{$file_name} start woring", Log::DEBUG);
  173. }
  174. $errs = [];
  175. while (!feof($fn)) {
  176. $line = trim(fgets($fn));
  177. $ret = preg_match('/[\w\W]+"channelOrderNumber":"(?P<order_sn>[^"]+)"[\w\W]+"message":"(?P<message>[\x{4e00}-\x{9fa5}]+)"[\w\W]+"status":109/u', $line, $matches);
  178. if ($ret) {
  179. $order_sn = $matches['order_sn'];
  180. $message = $matches['message'];
  181. self::black_order($order_sn, $message);
  182. $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
  183. }
  184. }
  185. foreach ($errs as $msg => $count) {
  186. Log::record("msg:{$msg} count:{$count}", Log::DEBUG);
  187. }
  188. fclose($fn);
  189. return true;
  190. }
  191. public static function async_add($params, $period = 10)
  192. {
  193. try {
  194. QueueClient::async_push("AysncAddDispatcher", ['method' => 'add', 'params' => $params], $period);
  195. return true;
  196. } catch (Exception $ex) {
  197. return false;
  198. }
  199. }
  200. public static function async_notify($chname,$data, $period)
  201. {
  202. try {
  203. QueueClient::async_push("AysncAddDispatcher", ['method' => 'notify', 'params' => ['channel' => $chname, 'params' => $data]], $period);
  204. return true;
  205. } catch (Exception $ex) {
  206. return false;
  207. }
  208. }
  209. public static function push_add($params)
  210. {
  211. try
  212. {
  213. $ret = self::push_queue('add', $params);
  214. return $ret !== false;
  215. }
  216. catch (Exception $ex) {
  217. return false;
  218. }
  219. }
  220. public static function push_add_zero($params)
  221. {
  222. try
  223. {
  224. $ret = self::push_queue('add_zero', $params);
  225. return $ret !== false;
  226. }
  227. catch (Exception $ex) {
  228. return false;
  229. }
  230. }
  231. public static function push_addthird($params)
  232. {
  233. try
  234. {
  235. $ret = self::push_queue('addthird', $params);
  236. return $ret !== false;
  237. }
  238. catch (Exception $ex) {
  239. return false;
  240. }
  241. }
  242. public static function push_notify($chname, $params)
  243. {
  244. try
  245. {
  246. $ret = self::push_queue('notify', ['channel' => $chname, 'params' => $params]);
  247. return $ret !== false;
  248. }
  249. catch (Exception $ex) {
  250. return false;
  251. }
  252. }
  253. public static function push_notify_merchant($order_id, $manual)
  254. {
  255. try
  256. {
  257. $ret = self::push_queue('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
  258. return $ret !== false;
  259. }
  260. catch (Exception $ex) {
  261. return false;
  262. }
  263. }
  264. public static function push_query($order_id)
  265. {
  266. try
  267. {
  268. $ret = self::push_queue('query', ['order_id' => $order_id]);
  269. return $ret !== false;
  270. }
  271. catch (Exception $ex) {
  272. return false;
  273. }
  274. }
  275. public static function push_query_net($order_id)
  276. {
  277. try {
  278. $ret = self::push_queue('query_net', ['order_id' => $order_id]);
  279. return $ret !== false;
  280. }
  281. catch (Exception $ex) {
  282. return false;
  283. }
  284. }
  285. public static function manual_success($order_id)
  286. {
  287. try
  288. {
  289. $ret = self::push_queue('manual_success', ['order_id' => $order_id]);
  290. return $ret !== false;
  291. }
  292. catch (Exception $ex) {
  293. return false;
  294. }
  295. }
  296. public static function manual_cancel($order_id)
  297. {
  298. try
  299. {
  300. $ret = self::push_queue('manual_cancel', ['order_id' => $order_id]);
  301. return $ret !== false;
  302. }
  303. catch (Exception $ex) {
  304. return false;
  305. }
  306. }
  307. public static function push_queue($method, $value)
  308. {
  309. if (defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP) {
  310. $queue_name = 'QUEUE_DISPATCHER_CO';
  311. $ins = Cache::getInstance('cacheredis');
  312. return $ins->lpush($queue_name, serialize([$method => $value]));
  313. }
  314. else {
  315. return queue\DispatcherClient::instance()->push($method,$value);
  316. }
  317. }
  318. public static function dispatcher_queue_length()
  319. {
  320. $ins = Cache::getInstance('cacheredis');
  321. return $ins->lLen('QUEUE_DISPATCHER_CO');
  322. }
  323. public static function monitor_queue_length()
  324. {
  325. $ins = Cache::getInstance('cacheredis');
  326. return $ins->lLen('REFILL_MONITOR_QUEUE');
  327. }
  328. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  329. public static function monitor_submit($mchid, $spec, $card_type, $mch_amount, $time)
  330. {
  331. $time = intval($time);
  332. queue\MonitorClient::instance()->onSubmit($mchid,$time,$spec,$card_type,$mch_amount);
  333. }
  334. public static function monitor_callback($mchid,$spec,$card_type,$mch_amount,$channel_amount,$succ,$time)
  335. {
  336. queue\MonitorClient::instance()->onCallback($mchid,$time,$spec,$card_type,floatval($mch_amount),floatval($channel_amount),$succ);
  337. }
  338. public static function monitor_netchk($chname,$succ)
  339. {
  340. queue\MonitorClient::instance()->onNetCheck($chname, time(),$succ);
  341. }
  342. public static function monitor_commit($chname, $spec, $card_type, $channel_amount,$commit_time)
  343. {
  344. queue\MonitorClient::instance()->onCommit($chname, $commit_time, $spec, $card_type, $channel_amount);
  345. }
  346. public static function monitor_notify($chname, $spec, $card_type, $channel_amount, $period, $succ,$commit_time,$mch_amount)
  347. {
  348. if ($succ) {
  349. $mch_amount = floatval($mch_amount);
  350. } else {
  351. $mch_amount = 0;
  352. }
  353. queue\MonitorClient::instance()->onNotify($chname, $commit_time, $spec, $card_type, floatval($channel_amount), $period, $succ,$mch_amount);
  354. }
  355. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  356. public static function set_order_channels($mchid,$mchorder,$datas)
  357. {
  358. $ins = Cache::getInstance('cacheredis');
  359. $name = 'order_channels';
  360. $key = "{$mchid}-{$mchorder}";
  361. $ins->hset($name, '', [$key => serialize($datas)]);
  362. }
  363. public static function get_order_channels($mchid, $mchorder)
  364. {
  365. //old-name oil_exclude_channels
  366. $ins = Cache::getInstance('cacheredis');
  367. $name = 'order_channels';
  368. $key = "{$mchid}-{$mchorder}";
  369. $chnames = $ins->hget($name, '', $key);
  370. $chnames = unserialize($chnames);
  371. if(is_array($chnames)) {
  372. return $chnames;
  373. } else {
  374. return [];
  375. }
  376. }
  377. public static function del_order_channels($mchid, $mchorder)
  378. {
  379. $ins = Cache::getInstance('cacheredis');
  380. $name = 'order_channels';
  381. $key = "{$mchid}-{$mchorder}";
  382. $ins->hdel($name, '', $key);
  383. }
  384. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  385. public static function set_cancel_order($mchid,$mch_order)
  386. {
  387. $ins = Cache::getInstance('cacheredis');
  388. $name = 'order_cancel_hash';
  389. $key = "{$mchid}-{$mch_order}";
  390. $ins->hset($name, '', [$key=> 1]);
  391. }
  392. public static function query_cancel_order($mchid,$mch_order)
  393. {
  394. $ins = Cache::getInstance('cacheredis');
  395. $name = 'order_cancel_hash';
  396. $key = "{$mchid}-{$mch_order}";
  397. $value = $ins->hget($name,'',$key);
  398. return $value;
  399. }
  400. public static function del_cancel_order($mchid,$mch_order)
  401. {
  402. $ins = Cache::getInstance('cacheredis');
  403. $name = 'order_cancel_hash';
  404. $key = "{$mchid}-{$mch_order}";
  405. $ins->hdel($name, $key);
  406. }
  407. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  408. public static function set_next_order($mchid,$mch_order)
  409. {
  410. $ins = Cache::getInstance('cacheredis');
  411. $name = 'order_next_hash';
  412. $key = "{$mchid}-{$mch_order}";
  413. $ins->hset($name, '', [$key=> 1]);
  414. }
  415. public static function query_next_order($mchid,$mch_order)
  416. {
  417. $ins = Cache::getInstance('cacheredis');
  418. $name = 'order_next_hash';
  419. $key = "{$mchid}-{$mch_order}";
  420. $value = $ins->hget($name,'',$key);
  421. return $value;
  422. }
  423. public static function del_next_order($mchid,$mch_order)
  424. {
  425. $ins = Cache::getInstance('cacheredis');
  426. $name = 'order_success_hash';
  427. $key = "{$mchid}-{$mch_order}";
  428. $ins->hdel($name, $key);
  429. }
  430. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  431. public static function merchant_debt_stoped($mchid)
  432. {
  433. if($mchid > 0)
  434. {
  435. $ret = rcache('merchant-debt-judge', 'refill-',"{$mchid}");
  436. if(empty($ret)) {
  437. return false;
  438. }
  439. $stoped = intval($ret[$mchid]);
  440. return ($stoped === 1);
  441. }
  442. else {
  443. return false;
  444. }
  445. }
  446. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  447. public static function push_queue_order($mchid,$mch_order,$order_state)
  448. {
  449. if(empty($mch_order)) return;
  450. $ins = Cache::getInstance('cacheredis');
  451. $name = 'merchant_order_query';
  452. $key = "{$mchid}-{$mch_order}";
  453. $ins->hset($name, '', [$key => $order_state]);
  454. }
  455. public static function del_queue_order($mchid,$mch_order)
  456. {
  457. if(empty($mch_order)) return;
  458. $ins = Cache::getInstance('cacheredis');
  459. $name = 'merchant_order_query';
  460. $key = "{$mchid}-{$mch_order}";
  461. $ret = $ins->hdel($name, '', $key);
  462. }
  463. public static function pop_queue_order($mchid,$mch_order,$order_time = 0)
  464. {
  465. util::del_order_channels($mchid,$mch_order);
  466. Model('refill_order')->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_HANDLED]);
  467. $ins = Cache::getInstance('cacheredis');
  468. $name = 'merchant_order_query';
  469. $key = "{$mchid}-{$mch_order}";
  470. $ret = $ins->hdel($name, '', $key);
  471. }
  472. public static function query_queue_order($mchid,$mch_order)
  473. {
  474. $ins = Cache::getInstance('cacheredis');
  475. $name = 'merchant_order_query';
  476. $key = "{$mchid}-{$mch_order}";
  477. $value = $ins->hget($name,'',$key);
  478. return $value;
  479. }
  480. public static function need_check($net_errno)
  481. {
  482. if(empty($net_errno)) return false;
  483. [$type,$code] = explode('-',$net_errno);
  484. $code = intval($code);
  485. if($type == "CURL") {
  486. static $errors = [CURLE_GOT_NOTHING,CURLE_RECV_ERROR];
  487. return in_array($code,$errors);
  488. } elseif($type == "HTTP") {
  489. static $excludes = [404];
  490. return !in_array($code,$excludes);
  491. } else {
  492. return false;
  493. }
  494. }
  495. public static function order_errflag($net_errno) {
  496. return ($net_errno === 'ORDER_CREATE_FAIL');
  497. }
  498. public static function onOrderSuccess($refill_info,$order_info)
  499. {
  500. $data = store_member::instance()->get_member($order_info['store_id']);
  501. if(empty($data)) {
  502. Log::record("cannot find member when store_id={$order_info['store_id']}",Log::ERR);
  503. return false;
  504. }
  505. $data['order_sn'] = $refill_info['order_sn'];
  506. $data['amount'] = $refill_info['channel_amount'];
  507. $model_pd = Model('predeposit');
  508. $model_pd->changePd('order_pay',$data,true);
  509. return true;
  510. }
  511. public static function getProvider($name,$type = 'RefillPhone')
  512. {
  513. $file = BASE_HELPER_RAPI_PATH . "/$name/{$type}.php";
  514. if(!file_exists($file)){
  515. Log::record("provider api file={$file} not exist.",Log::DEBUG);
  516. return false;
  517. } else {
  518. require_once($file);
  519. Log::record("file={$file} load success.",Log::DEBUG);
  520. }
  521. $class_name = "refill\\{$name}\\{$type}";
  522. if (class_exists($class_name, false)) {
  523. $caller = new $class_name([]);
  524. return $caller;
  525. } else {
  526. $error = "Base Error: class {$class_name} isn't exists!";
  527. Log::record($error, Log::ERR);
  528. return false;
  529. }
  530. }
  531. public static function xmlToArray($xml)
  532. {
  533. $object = simplexml_load_string($xml);
  534. $val = json_decode(json_encode($object), true);
  535. $msg = json_encode($val);
  536. Log::record("xmlToArray result={$msg}", Log::DEBUG);
  537. return $val;
  538. }
  539. //for tester
  540. public static function send_normal($order_sn)
  541. {
  542. $status = mt_rand(1,100);
  543. if($status > 97) {
  544. $status = 1;
  545. } else {
  546. $status = 0;
  547. }
  548. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  549. go(function () use ($url, $status,$order_sn)
  550. {
  551. sleep(3);
  552. while (true)
  553. {
  554. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  555. if($resp == 'SUCCESS') {
  556. break;
  557. }
  558. }
  559. Log::record("resp = {$resp}",Log::DEBUG);
  560. });
  561. }
  562. public static function send_quick($order_sn)
  563. {
  564. $status = mt_rand(1,10);
  565. if($status > 3) {
  566. $status = 1;
  567. } else {
  568. $status = 0;
  569. }
  570. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  571. go(function () use ($url, $status,$order_sn)
  572. {
  573. sleep(3);
  574. while (true)
  575. {
  576. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  577. if($resp == 'SUCCESS') {
  578. break;
  579. }
  580. }
  581. Log::record("resp = {$resp}",Log::DEBUG);
  582. });
  583. }
  584. public static function retry_canceled_order($order_id, $skip)
  585. {
  586. $mod_order = Model('vr_order');
  587. $mod_refill = Model('refill_order');
  588. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  589. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  590. if(empty($refill_info) || empty($order_info)) {
  591. return [false,'无此订单或者订单已经重试中了...'];
  592. }
  593. $tran = new trans_wapper($mod_order,'notify change order state trans');
  594. try
  595. {
  596. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  597. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  598. $order_state = intval($order_info['order_state']);
  599. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  600. $tran->commit();
  601. return [false,'订单已经在重试'];
  602. }
  603. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  604. $tran->commit();
  605. $order = refill\order::from_db($refill_info,$order_info);
  606. $params = $order->queue_params();
  607. $params['order_time'] = time();
  608. if ($skip) {
  609. $mchid = $refill_info['mchid'];
  610. $mch_order = $refill_info['mch_order'];
  611. refill\util::set_next_order($mchid, $mch_order);
  612. }
  613. if(util::push_add($params)) {
  614. return [true,''];
  615. } else {
  616. return [false,'加入队列出错'];
  617. }
  618. }
  619. catch (Exception $ex) {
  620. $tran->rollback();
  621. Log::record($ex->getMessage(),Log::ERR);
  622. return [false,"{$ex->getMessage()}"];
  623. }
  624. }
  625. public static function transfer_success_order($order_id, $manual_recharge_amount)
  626. {
  627. $mod_order = Model('vr_order');
  628. $mod_refill = Model('refill_order');
  629. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  630. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  631. if(empty($refill_info) || empty($order_info)) {
  632. return [false,'无此订单'];
  633. }
  634. $tran = new trans_wapper($mod_order,'notify change order state trans');
  635. try
  636. {
  637. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  638. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  639. $order_state = intval($order_info['order_state']);
  640. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  641. $tran->commit();
  642. return [false,'订单已经在重试'];
  643. }
  644. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  645. $order = refill\order::from_db($refill_info,$order_info);
  646. $mchid = $order->mchid();
  647. [$success,$success_order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
  648. if(!$success) {
  649. $tran->rollback();
  650. return [false, $errmsg];
  651. }
  652. $mod_refill->edit($success_order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  653. $mod_refill->edit($order_id, ['is_retrying' => 0]);
  654. if($manual_recharge_amount > 0) {
  655. $mod_refill->edit($success_order_id, ['channel_amount' => $manual_recharge_amount]);
  656. }
  657. $tran->commit();
  658. return [true,''];
  659. }
  660. catch (Exception $ex) {
  661. $tran->rollback();
  662. Log::record($ex->getMessage(),Log::ERR);
  663. return [false,"{$ex->getMessage()}"];
  664. }
  665. }
  666. public static function vr_order_part()
  667. {
  668. $miner = function ($time) {
  669. return strtotime(date('Y-m-d',$time)) - 86400 * 15;
  670. };
  671. $maxer = function ($time) {
  672. return $time + 3600;
  673. };
  674. $now = time();
  675. return [['egt', $miner($now)], ['elt', $maxer($now)], 'and'];
  676. }
  677. public static function refill_order_part($order_time = 0)
  678. {
  679. $miner = function ($time) {
  680. return strtotime(date('Y-m-d',$time)) - 86400 * 15;
  681. };
  682. $maxer = function ($time) {
  683. return $time + 3600;
  684. };
  685. if($order_time == 0)
  686. {
  687. $now = time();
  688. return [['egt', $miner($now)], ['elt', $maxer($now)], 'and'];
  689. } else {
  690. return $order_time;
  691. }
  692. }
  693. private static function part_calc($time, $sub_period, $add_period)
  694. {
  695. //查询30天的订单
  696. $miner = function ($time) use($sub_period) {
  697. return strtotime(date('Y-m-d', $time)) - $sub_period;
  698. };
  699. $maxer = function ($time) use($add_period) {
  700. return $time + $add_period;
  701. };
  702. $namer = function ($time) {
  703. return 'p'.date('Ym', $time);
  704. };
  705. if (defined('DB_PARTIONED') && DB_PARTIONED)
  706. {
  707. if(is_string($time)) {
  708. $time = intval($time);
  709. }
  710. if($time == 0)
  711. {
  712. $now = time();
  713. $a = $namer($miner($now));
  714. $b = $namer($maxer($now));
  715. if($a != $b) {
  716. return [$a,$b];
  717. } else {
  718. return $a;
  719. }
  720. }
  721. else {
  722. return $namer($time);
  723. }
  724. }
  725. else {
  726. return '';
  727. }
  728. }
  729. public static function part_query($order_time = 0)
  730. {
  731. return self::part_calc($order_time, 86400 * 30, 3600);
  732. }
  733. public static function part_notify()
  734. {
  735. return self::part_calc(0, 86400 * 7, 3600);
  736. }
  737. public static function part_refill($order_time)
  738. {
  739. return self::part_calc($order_time, 86400 * 2, 3600);
  740. }
  741. public static function part_vr_order($add_time)
  742. {
  743. return self::part_calc($add_time, 86400 * 2, 3600);
  744. }
  745. public static function part_vr_order_time($order_time)
  746. {
  747. $namer = function ($time) {
  748. return 'p'.date('Ym', $time);
  749. };
  750. if (defined('DB_PARTIONED') && DB_PARTIONED)
  751. {
  752. if(is_string($order_time)) {
  753. $order_time = intval($order_time);
  754. }
  755. if ($order_time == 0) {
  756. return '';
  757. }
  758. else
  759. {
  760. $a = $namer($order_time);
  761. $b = $namer(time() + 3600);
  762. if ($a == $b) {
  763. return $a;
  764. } else {
  765. return [$a, $b];
  766. }
  767. }
  768. }
  769. else {
  770. return '';
  771. }
  772. }
  773. public static function part_vr_create()
  774. {
  775. return self::part_calc(0, 3600, 3600);
  776. }
  777. public static function calc_part($time = 0)
  778. {
  779. return self::part_calc($time, 86400 * 15, 3600);
  780. }
  781. }