minutes.php 16 KB

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