minutes.php 17 KB

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