minutes.php 19 KB

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