minutes.php 21 KB

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