minutes.php 17 KB

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