minutes.php 16 KB

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