minutes.php 18 KB

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