minutes.php 15 KB

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