minutes.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. /**
  3. * 任务计划 - 分钟执行的任务
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. require_once (BASE_ROOT_PATH . '/helper/message/publisher.php');
  11. require_once(BASE_HELPER_PATH . '/refill/util.php');
  12. require_once(BASE_HELPER_PATH . '/refill/divert_account.php');
  13. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  14. class minutesControl extends BaseCronControl
  15. {
  16. public function indexexOp()
  17. {
  18. $this->_cron_common([1,2,3,4,5,6,7]);
  19. }
  20. public function indexOp()
  21. {
  22. Log::record(__FUNCTION__ . " start",Log::DEBUG);
  23. //未付款订单超期自动关闭
  24. // $this->_order_timeout_cancel();
  25. $this->_cron_common([1,2,3,4,5,6,7]);
  26. $this->_fetch_order_unavaliable();
  27. $this->_check_merchant_alarm_amount();
  28. Log::record(__FUNCTION__ . " end",Log::DEBUG);
  29. // $this->_web_index_update();
  30. // $this->_check_merchant_alarm_amount();
  31. // $this->check_refill_order_limit();
  32. // $this->_cron_mail_send();
  33. // $this->_SendOrderQuery();
  34. }
  35. public function update_channel_balanceOp()
  36. {
  37. $balance_getter = function ($provider_id)
  38. {
  39. $provider_model = Model('refill_provider');
  40. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id, 'opened' => 1]);
  41. if (empty($provider_info)) {
  42. return false;
  43. }
  44. $channel_name = $provider_info['name'];
  45. if ($provider_info['type'] == 1) {
  46. $type = 'RefillOil';
  47. } elseif ($provider_info['type'] == 2 || $provider_info['type'] == 3) {
  48. $type = 'RefillPhone';
  49. } else {
  50. $type = 'RefillPhone';
  51. }
  52. Log::record("query balance channel_name:{$channel_name}");
  53. $provider = refill\util::getProvider($channel_name,$type);
  54. [$state, $balance] = $provider->balance();
  55. Log::record("query balance channel_name:{$channel_name}, err_msg:{$balance}");
  56. if(!$state) {
  57. return false;
  58. } else {
  59. return round(floatval($balance),4);
  60. }
  61. };
  62. $providers = function ()
  63. {
  64. $pids = [];
  65. $i = 0;
  66. while (true)
  67. {
  68. $start = $i * 1000;
  69. $items = Model()->table('refill_provider')->field('*')->order('provider_id desc')->limit("{$start},1000")->select();
  70. if(empty($items)) {
  71. return $pids;
  72. }
  73. $i++;
  74. foreach ($items as $item) {
  75. $pid = intval($item['provider_id']);
  76. $pids[] = $pid;
  77. }
  78. }
  79. };
  80. $mod_provider = Model('refill_provider');
  81. $pids = $providers();
  82. foreach ($pids as $pid)
  83. {
  84. try {
  85. $balance = $balance_getter($pid);
  86. if($balance !== false) {
  87. $mod_provider->editProvider(['balance' => $balance],['provider_id' => $pid]);
  88. }
  89. }
  90. catch (Exception $ex)
  91. {
  92. Log::record($ex->getMessage(),Log::ERR);
  93. }
  94. }
  95. }
  96. private function _fetch_order_unavaliable()
  97. {
  98. try {
  99. $mod_fetch_order = Model('fetch_order');
  100. $trans = new trans_wapper($mod_fetch_order,__METHOD__);
  101. $items = $mod_fetch_order->timeout_orders(900);
  102. foreach ($items as $item) {
  103. $fetch_id = $item['fetch_id'];
  104. $mod_fetch_order->unavaliable($fetch_id);
  105. }
  106. $trans->commit();
  107. foreach ($items as $item) {
  108. $order_sn = $item['order_sn'];
  109. $chname = $item['channel_name'];
  110. refill\util::push_notify($chname,['order_sn' => $order_sn,'state' => 'CANCEL']);
  111. }
  112. }
  113. catch (Exception $ex) {
  114. $trans->rollback();
  115. Log::record(__METHOD__ . " " . $ex->getMessage(),Log::ERR);
  116. }
  117. }
  118. private function _check_merchant_alarm_amount()
  119. {
  120. if(defined('COMPANY_NAME') && COMPANY_NAME === 'XYZ_COMPANY')
  121. {
  122. $mch_cache = rcache("merchant-notify" , 'refill-');
  123. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  124. $new_caches = [];
  125. $merchants = Model('merchant')->getMerchantList(['merchant.merchant_state'=>1],'','',true,'merchant.*,member.available_predeposit' ,"0,1000");
  126. foreach ($merchants as $merchant)
  127. {
  128. $mchid = $merchant['mchid'];
  129. $phones = empty($merchant['warning_phone']) ? [] : unserialize($merchant['warning_phone']);
  130. $available_pd = intval($merchant['available_predeposit']);
  131. $alarm_pd = intval($merchant['alarm_amount']);
  132. if(array_key_exists($mchid,$caches)) {
  133. $mch_cache = $caches[$mchid];
  134. }
  135. else {
  136. $mch_cache = ['last_time' => 0, 'send_count' => 0];
  137. }
  138. if($available_pd < $alarm_pd || $available_pd < 10000)
  139. {
  140. $counts = $mch_cache['send_count'];
  141. if(($mch_cache['last_time'] + 300 < time()) && $counts < 2) {
  142. $mch_cache = ['last_time' => time(), 'send_count' => $counts + 1];
  143. foreach ($phones as $phone) {
  144. if(!empty($phone)){
  145. QueueClient::push('sendSMS', ['mobile'=>$phone,
  146. 'type'=>'balance_warning','datas' => [date("m月d日H时") , $merchant['available_predeposit']]]);
  147. }
  148. }
  149. }
  150. }
  151. else {
  152. $mch_cache = ['last_time' => 0, 'send_count' => 0];
  153. }
  154. $new_caches[$mchid] = $mch_cache;
  155. }
  156. wcache("merchant-notify", ['data' => serialize($new_caches)], 'refill-');
  157. }
  158. }
  159. public function check_refill_order_limit()
  160. {
  161. $mch_cache = rcache("storge_limit" , 'merchant-');
  162. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  163. $reader = function ($mchid,$time)
  164. {
  165. $cond['mchid'] = $mchid;
  166. $cond['inner_status'] = 0;
  167. $cond['order_state'] = ORDER_STATE_SUCCESS;
  168. $cond['refill_order.order_time'] = ['egt', $time];
  169. $items = Model('')->table('refill_order,vr_order')->join('inner')
  170. ->on('refill_order.order_id=vr_order.order_id')
  171. ->field('refill_order.mchid,refill_order.card_type,refill_order.refill_amount,count(*) as num')
  172. ->group('refill_order.card_type,refill_order.refill_amount')
  173. ->where($cond)->select();
  174. return $items;
  175. };
  176. $merger = function ($limits,$reals,$mchid,$card_type)
  177. {
  178. $result = [];
  179. foreach ($limits as $amount => $limit)
  180. {
  181. $limit = intval($limit);
  182. if($limit === -1) {
  183. $allow = true;
  184. }
  185. elseif($limit === 0) {
  186. $allow = false;
  187. }
  188. else
  189. {
  190. $finded = false;
  191. foreach ($reals as $item)
  192. {
  193. $tmp = intval($item['refill_amount'] + 0.005);
  194. if($item['card_type'] == $card_type && $tmp == $amount) {
  195. $num = $item['num'];
  196. $allow = $limit > $num;
  197. $finded = true;
  198. break;
  199. }
  200. }
  201. if(!$finded) {
  202. $allow = true;
  203. }
  204. }
  205. $result["{$mchid}-{$card_type}-{$amount}"] = $allow;
  206. }
  207. return $result;
  208. };
  209. $type_map = ['petrochina' => 1,'sinopec' => 2];
  210. $abilitys = [];
  211. foreach ($caches as $cache)
  212. {
  213. $mchid = intval($cache['mchid']);
  214. $start = $cache['time'];
  215. $petros = $cache['petrochina'];
  216. $sinos = $cache['sinopec'];
  217. $items = $reader($mchid,$start);
  218. $prets = $merger($petros,$items,$mchid,$type_map['petrochina']);
  219. $srets = $merger($sinos,$items,$mchid,$type_map['sinopec']);
  220. foreach ($prets as $key => $val) {
  221. $abilitys[$key] = $val;
  222. }
  223. foreach ($srets as $key => $val) {
  224. $abilitys[$key] = $val;
  225. }
  226. }
  227. $old = rcache("refill_able",'merchant-');
  228. $old = empty($old) ? "" : $old['data'];
  229. ksort($abilitys);
  230. $new = serialize($abilitys);
  231. if($new != $old) {
  232. wcache("refill_able" , ['data' => $new] , 'merchant-');
  233. // $publisher = new message\publisher();
  234. // $publisher->modify_refill_merchant();
  235. }
  236. }
  237. /**
  238. * 未付款订单超期自动关闭
  239. */
  240. private function _order_timeout_cancel()
  241. {
  242. Log::record(__FUNCTION__,Log::DEBUG);
  243. //实物订单超期未支付系统自动关闭
  244. $_break = false;
  245. $model_order = Model('order');
  246. $logic_order = Logic('order');
  247. $condition = [];
  248. $condition['order_state'] = ORDER_STATE_NEW;
  249. $condition['add_time'] = ['lt',time() - ORDER_AUTO_CANCEL_DAY * 86400];
  250. //分批,每批处理100个订单,最多处理5W个订单
  251. for ($i = 0; $i < 500; $i++)
  252. {
  253. if ($_break) {
  254. break;
  255. }
  256. $order_list = $model_order->getOrderList($condition, '', '*', '', 100);
  257. if (empty($order_list)) break;
  258. foreach ($order_list as $order_info)
  259. {
  260. Log::record("1",Log::DEBUG);
  261. $result = $logic_order->changeOrderStateCancel($order_info,'system','系统','超期未支付系统自动关闭订单',true,false);
  262. Log::record("2",Log::DEBUG);
  263. if (!$result['state']) {
  264. $this->log('实物订单超期未支付关闭失败SN:'.$order_info['order_sn']); $_break = true; break;
  265. } else {
  266. Log::record("3",Log::DEBUG);
  267. account_helper::onPredeposit('order_cancel',$order_info['buyer_id'],$order_info['order_sn']);
  268. Log::record("4",Log::DEBUG);
  269. }
  270. }
  271. }
  272. //虚拟订单超期未支付系统自动关闭
  273. $_break = false;
  274. $model_vr_order = Model('vr_order');
  275. $logic_vr_order = Logic('vr_order');
  276. $condition = [];
  277. $condition['order_state'] = ORDER_STATE_NEW;
  278. $condition['add_time'] = ['lt',time() - VRORDER_AUTO_CANCEL_MINUTE * 60];
  279. //分批,每批处理100个订单,最多处理5W个订单
  280. for ($i = 0; $i < 500; $i++)
  281. {
  282. if ($_break) {
  283. break;
  284. }
  285. $order_list = $model_vr_order->getOrderList($condition, '', '*', '',100);
  286. if (empty($order_list)) break;
  287. foreach ($order_list as $order_info) {
  288. $result = $logic_vr_order->changeOrderStateCancel($order_info,'system','超期未支付系统自动关闭订单',false);
  289. }
  290. if (!$result['state']) {
  291. $this->log('虚拟订单超期未支付关闭失败SN:'.$order_info['order_sn']);
  292. $_break = true;
  293. }
  294. }
  295. }
  296. /**
  297. * 更新首页的商品价格信息
  298. */
  299. private function _web_index_update()
  300. {
  301. Model('web_config')->updateWebGoods();
  302. }
  303. /**
  304. * 发送邮件消息
  305. */
  306. private function _cron_mail_send()
  307. {
  308. //每次发送数量
  309. $_num = 50;
  310. $model_storemsgcron = Model('mail_cron');
  311. $cron_array = $model_storemsgcron->getMailCronList([], $_num);
  312. if (!empty($cron_array))
  313. {
  314. $email = new Email();
  315. $mail_array = [];
  316. foreach ($cron_array as $val)
  317. {
  318. $return = $email->send_sys_email($val['mail'],$val['subject'],$val['contnet']);
  319. if ($return) {
  320. // 记录需要删除的id
  321. $mail_array[] = $val['mail_id'];
  322. }
  323. }
  324. // 删除已发送的记录
  325. $model_storemsgcron->delMailCron(['mail_id' => ['in', $mail_array]]);
  326. }
  327. }
  328. public function second_queueOp()
  329. {
  330. while (true)
  331. {
  332. do{
  333. try {
  334. $ret = $this->_cron_common([8]);
  335. perfor_clear();
  336. } catch (Exception $ex) {
  337. Log::record($ex->getMessage(),Log::ERR);
  338. }
  339. }
  340. while($ret === true);
  341. sleep(1);
  342. }
  343. }
  344. public function account_editOp()
  345. {
  346. $stamp = 0;
  347. while (true)
  348. {
  349. try {
  350. $divertor = new refill\divert_account();
  351. $next = time();
  352. $divertor->update_account($stamp);
  353. $stamp = $next;
  354. } catch (Exception $ex) {
  355. Log::record($ex->getMessage(),Log::ERR);
  356. }
  357. sleep(1);
  358. }
  359. }
  360. /**
  361. * 执行通用任务
  362. */
  363. private function _cron_common($types)
  364. {
  365. Log::record(__FUNCTION__,Log::DEBUG);
  366. //查找待执行任务
  367. $model_cron = Model('cron');
  368. $cron = $model_cron->getCronList(['exetime'=> ['elt',time()],'type' => ['in',$types]]);
  369. if (!is_array($cron)) return false;
  370. $count = count($cron);
  371. if ($count <= 0) return false;
  372. Log::record("match cron count={$count}",Log::DEBUG);
  373. $cron_array = [];
  374. $cronid = [];
  375. $exeid = 1;
  376. foreach ($cron as $v)
  377. {
  378. $type = intval($v['type']);
  379. if($type == 8) {
  380. $cron_array[$v['type']][$exeid] = $v;
  381. $exeid++;
  382. }
  383. else {
  384. $cron_array[$v['type']][$v['exeid']] = $v;
  385. }
  386. }
  387. foreach ($cron_array as $k=>$v)
  388. {
  389. // 如果方法不存是,直接删除id
  390. if (!method_exists($this,'_cron_'.$k)) {
  391. $tmp = current($v);
  392. $cronid[] = $tmp['id'];
  393. continue;
  394. }
  395. $method = '_cron_'.$k;
  396. Log::record("crontab minutest:{$method}",Log::DEBUG);
  397. $result = call_user_func_array([$this,'_cron_'.$k], [$v]);
  398. if (is_array($result)){
  399. $cronid = array_merge($cronid,$result);
  400. }
  401. else {
  402. $method = '_cron_'.$k;
  403. Log::record("crontab minutest err:{$method}",Log::ERR);
  404. }
  405. }
  406. //删除执行完成的cron信息
  407. if (!empty($cronid) && is_array($cronid)){
  408. $model_cron->delCron(['id'=> ['in',$cronid]]);
  409. }
  410. return true;
  411. }
  412. //'任务类型 1商品上架 2根据商品id更新商品促销价格 3优惠套装过期 4推荐展位过期 5团购开始更新商品促销价格 6团购过期 7限时折扣过期',
  413. //1商品上架
  414. private function _cron_1($cron = array())
  415. {
  416. $condition = ['goods_commonid' => ['in',array_keys($cron)]];
  417. $update = Model('goods')->editProducesOnline($condition);
  418. if ($update){
  419. //返回执行成功的cronid
  420. $cronid = [];
  421. foreach ($cron as $v) {
  422. $cronid[] = $v['id'];
  423. }
  424. } else {
  425. return false;
  426. }
  427. return $cronid;
  428. }
  429. //2根据商品id更新商品促销价格
  430. private function _cron_2($cron = array())
  431. {
  432. $condition = ['goods_id' => ['in',array_keys($cron)]];
  433. $update = Model('goods')->editGoodsPromotionPrice($condition);
  434. if ($update){
  435. //返回执行成功的cronid
  436. $cronid = [];
  437. foreach ($cron as $v) {
  438. $cronid[] = $v['id'];
  439. }
  440. }else{
  441. return false;
  442. }
  443. return $cronid;
  444. }
  445. //3优惠套装过期
  446. private function _cron_3($cron = [])
  447. {
  448. $condition = ['store_id' => ['in', array_keys($cron)]];
  449. $update = Model('p_bundling')->editBundlingQuotaClose($condition);
  450. if ($update) {
  451. //返回执行成功的cronid
  452. $cronid = [];
  453. foreach ($cron as $v) {
  454. $cronid[] = $v['id'];
  455. }
  456. } else {
  457. return false;
  458. }
  459. return $cronid;
  460. }
  461. //4推荐展位过期
  462. private function _cron_4($cron = [])
  463. {
  464. $condition = array('store_id' => array('in', array_keys($cron)));
  465. $update = Model('p_booth')->editBoothClose($condition);
  466. if ($update) {
  467. //返回执行成功的cronid
  468. $cronid = array();
  469. foreach ($cron as $v) {
  470. $cronid[] = $v['id'];
  471. }
  472. } else {
  473. return false;
  474. }
  475. return $cronid;
  476. }
  477. //5团购开始更新商品促销价格
  478. private function _cron_5($cron = array())
  479. {
  480. $condition = [];
  481. $condition['goods_commonid'] = ['in', array_keys($cron)];
  482. $condition['start_time'] = ['lt', time()];
  483. $condition['end_time'] = ['gt', time()];
  484. $groupbuy = Model('groupbuy')->getGroupbuyList($condition);
  485. foreach ($groupbuy as $val) {
  486. Model('goods')->editGoods(['goods_promotion_price' => $val['groupbuy_price'], 'goods_promotion_type' => 1],
  487. ['goods_commonid' => $val['goods_commonid']]);
  488. }
  489. //返回执行成功的cronid
  490. $cronid = array();
  491. foreach ($cron as $v) {
  492. $cronid[] = $v['id'];
  493. }
  494. return $cronid;
  495. }
  496. /**
  497. * 抢购过期
  498. *
  499. * @param array $cron
  500. */
  501. private function _cron_6($cron = array())
  502. {
  503. $condition = ['goods_commonid' => ['in', array_keys($cron)]];
  504. //抢购活动过期
  505. $update = Model('groupbuy')->editExpireGroupbuy($condition);
  506. if ($update){
  507. //返回执行成功的cronid
  508. $cronid = [];
  509. foreach ($cron as $v) {
  510. $cronid[] = $v['id'];
  511. }
  512. }else{
  513. return false;
  514. }
  515. return $cronid;
  516. }
  517. /**
  518. * 限时折扣过期
  519. *
  520. * @param array $cron
  521. */
  522. private function _cron_7($cron = array())
  523. {
  524. $condition = array('xianshi_id' => array('in', array_keys($cron)));
  525. //限时折扣过期
  526. $update = Model('p_xianshi')->editExpireXianshi($condition);
  527. if ($update){
  528. //返回执行成功的cronid
  529. $cronid = array();
  530. foreach ($cron as $v) {
  531. $cronid[] = $v['id'];
  532. }
  533. }
  534. else{
  535. return false;
  536. }
  537. return $cronid;
  538. }
  539. private function _cron_8($cron = array())
  540. {
  541. $cronid = [];
  542. foreach ($cron as $v)
  543. {
  544. $cronid[] = intval($v['id']);
  545. $params = $v['params'];
  546. if(!empty($params))
  547. {
  548. $params = unserialize($params);
  549. if(is_array($params))
  550. {
  551. foreach ($params as $key => $value) {
  552. Log::record("delay queue: {$key}",Log::DEBUG);
  553. QueueClient::push($key,$value);
  554. }
  555. }
  556. }
  557. }
  558. return $cronid;
  559. }
  560. //查询超过5分钟的充值中订单
  561. public function _SendOrderQuery()
  562. {
  563. $model_refill_order = Model('refill_order');
  564. $condition['order_state'] = ORDER_STATE_SEND;
  565. $condition['refill_order.order_time'] = ['lt', (time() - 300)];
  566. $orders = $model_refill_order->getMerchantOrderList($condition, 1000, 'refill_order.order_id');
  567. if(!empty($orders)) {
  568. foreach ($orders as $order) {
  569. $order_id = $order['order_id'];
  570. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  571. }
  572. }
  573. }
  574. }