minutes.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. $this->_check_merchant_alarm_amount();
  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. 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.*,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. $ret = $this->_cron_common([8]);
  270. perfor_clear();
  271. }
  272. while($ret === true);
  273. sleep(1);
  274. }
  275. }
  276. /**
  277. * 执行通用任务
  278. */
  279. private function _cron_common($types)
  280. {
  281. Log::record(__FUNCTION__,Log::DEBUG);
  282. //查找待执行任务
  283. $model_cron = Model('cron');
  284. $cron = $model_cron->getCronList(['exetime'=> ['elt',time()],'type' => ['in',$types]]);
  285. if (!is_array($cron)) return false;
  286. $count = count($cron);
  287. if ($count <= 0) return false;
  288. Log::record("match cron count={$count}",Log::DEBUG);
  289. $cron_array = [];
  290. $cronid = [];
  291. $exeid = 1;
  292. foreach ($cron as $v)
  293. {
  294. $type = intval($v['type']);
  295. if($type == 8) {
  296. $cron_array[$v['type']][$exeid] = $v;
  297. $exeid++;
  298. }
  299. else {
  300. $cron_array[$v['type']][$v['exeid']] = $v;
  301. }
  302. }
  303. foreach ($cron_array as $k=>$v)
  304. {
  305. // 如果方法不存是,直接删除id
  306. if (!method_exists($this,'_cron_'.$k)) {
  307. $tmp = current($v);
  308. $cronid[] = $tmp['id'];
  309. continue;
  310. }
  311. $method = '_cron_'.$k;
  312. Log::record("crontab minutest:{$method}",Log::DEBUG);
  313. $result = call_user_func_array([$this,'_cron_'.$k], [$v]);
  314. if (is_array($result)){
  315. $cronid = array_merge($cronid,$result);
  316. }
  317. else {
  318. $method = '_cron_'.$k;
  319. Log::record("crontab minutest err:{$method}",Log::ERR);
  320. }
  321. }
  322. //删除执行完成的cron信息
  323. if (!empty($cronid) && is_array($cronid)){
  324. $model_cron->delCron(['id'=> ['in',$cronid]]);
  325. }
  326. return true;
  327. }
  328. //'任务类型 1商品上架 2根据商品id更新商品促销价格 3优惠套装过期 4推荐展位过期 5团购开始更新商品促销价格 6团购过期 7限时折扣过期',
  329. //1商品上架
  330. private function _cron_1($cron = array())
  331. {
  332. $condition = ['goods_commonid' => ['in',array_keys($cron)]];
  333. $update = Model('goods')->editProducesOnline($condition);
  334. if ($update){
  335. //返回执行成功的cronid
  336. $cronid = [];
  337. foreach ($cron as $v) {
  338. $cronid[] = $v['id'];
  339. }
  340. } else {
  341. return false;
  342. }
  343. return $cronid;
  344. }
  345. //2根据商品id更新商品促销价格
  346. private function _cron_2($cron = array())
  347. {
  348. $condition = ['goods_id' => ['in',array_keys($cron)]];
  349. $update = Model('goods')->editGoodsPromotionPrice($condition);
  350. if ($update){
  351. //返回执行成功的cronid
  352. $cronid = [];
  353. foreach ($cron as $v) {
  354. $cronid[] = $v['id'];
  355. }
  356. }else{
  357. return false;
  358. }
  359. return $cronid;
  360. }
  361. //3优惠套装过期
  362. private function _cron_3($cron = [])
  363. {
  364. $condition = ['store_id' => ['in', array_keys($cron)]];
  365. $update = Model('p_bundling')->editBundlingQuotaClose($condition);
  366. if ($update) {
  367. //返回执行成功的cronid
  368. $cronid = [];
  369. foreach ($cron as $v) {
  370. $cronid[] = $v['id'];
  371. }
  372. } else {
  373. return false;
  374. }
  375. return $cronid;
  376. }
  377. //4推荐展位过期
  378. private function _cron_4($cron = [])
  379. {
  380. $condition = array('store_id' => array('in', array_keys($cron)));
  381. $update = Model('p_booth')->editBoothClose($condition);
  382. if ($update) {
  383. //返回执行成功的cronid
  384. $cronid = array();
  385. foreach ($cron as $v) {
  386. $cronid[] = $v['id'];
  387. }
  388. } else {
  389. return false;
  390. }
  391. return $cronid;
  392. }
  393. //5团购开始更新商品促销价格
  394. private function _cron_5($cron = array())
  395. {
  396. $condition = [];
  397. $condition['goods_commonid'] = ['in', array_keys($cron)];
  398. $condition['start_time'] = ['lt', time()];
  399. $condition['end_time'] = ['gt', time()];
  400. $groupbuy = Model('groupbuy')->getGroupbuyList($condition);
  401. foreach ($groupbuy as $val) {
  402. Model('goods')->editGoods(['goods_promotion_price' => $val['groupbuy_price'], 'goods_promotion_type' => 1],
  403. ['goods_commonid' => $val['goods_commonid']]);
  404. }
  405. //返回执行成功的cronid
  406. $cronid = array();
  407. foreach ($cron as $v) {
  408. $cronid[] = $v['id'];
  409. }
  410. return $cronid;
  411. }
  412. /**
  413. * 抢购过期
  414. *
  415. * @param array $cron
  416. */
  417. private function _cron_6($cron = array())
  418. {
  419. $condition = ['goods_commonid' => ['in', array_keys($cron)]];
  420. //抢购活动过期
  421. $update = Model('groupbuy')->editExpireGroupbuy($condition);
  422. if ($update){
  423. //返回执行成功的cronid
  424. $cronid = [];
  425. foreach ($cron as $v) {
  426. $cronid[] = $v['id'];
  427. }
  428. }else{
  429. return false;
  430. }
  431. return $cronid;
  432. }
  433. /**
  434. * 限时折扣过期
  435. *
  436. * @param array $cron
  437. */
  438. private function _cron_7($cron = array())
  439. {
  440. $condition = array('xianshi_id' => array('in', array_keys($cron)));
  441. //限时折扣过期
  442. $update = Model('p_xianshi')->editExpireXianshi($condition);
  443. if ($update){
  444. //返回执行成功的cronid
  445. $cronid = array();
  446. foreach ($cron as $v) {
  447. $cronid[] = $v['id'];
  448. }
  449. }
  450. else{
  451. return false;
  452. }
  453. return $cronid;
  454. }
  455. private function _cron_8($cron = array())
  456. {
  457. $cronid = [];
  458. foreach ($cron as $v)
  459. {
  460. $cronid[] = intval($v['id']);
  461. $params = $v['params'];
  462. if(!empty($params))
  463. {
  464. $params = unserialize($params);
  465. if(is_array($params))
  466. {
  467. foreach ($params as $key => $value) {
  468. Log::record("delay queue: {$key}",Log::DEBUG);
  469. QueueClient::push($key,$value);
  470. }
  471. }
  472. }
  473. }
  474. return $cronid;
  475. }
  476. //查询超过5分钟的充值中订单
  477. public function _SendOrderQuery()
  478. {
  479. $model_refill_order = Model('refill_order');
  480. $condition['order_state'] = ORDER_STATE_SEND;
  481. $condition['refill_order.order_time'] = ['lt', (time() - 300)];
  482. $orders = $model_refill_order->getMerchantOrderList($condition, 1000, 'refill_order.order_id');
  483. if(!empty($orders)) {
  484. foreach ($orders as $order) {
  485. $order_id = $order['order_id'];
  486. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  487. }
  488. }
  489. }
  490. }