minutes.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 = $old['data'];
  132. $new = serialize($abilitys);
  133. if($new != $old) {
  134. wcache("refill_able" , ['data' => $new] , 'merchant-');
  135. $publisher = new message\publisher();
  136. $publisher->modify_refill_merchant();
  137. }
  138. }
  139. /**
  140. * 未付款订单超期自动关闭
  141. */
  142. private function _order_timeout_cancel()
  143. {
  144. Log::record(__FUNCTION__,Log::DEBUG);
  145. //实物订单超期未支付系统自动关闭
  146. $_break = false;
  147. $model_order = Model('order');
  148. $logic_order = Logic('order');
  149. $condition = [];
  150. $condition['order_state'] = ORDER_STATE_NEW;
  151. $condition['add_time'] = ['lt',time() - ORDER_AUTO_CANCEL_DAY * 86400];
  152. //分批,每批处理100个订单,最多处理5W个订单
  153. for ($i = 0; $i < 500; $i++)
  154. {
  155. if ($_break) {
  156. break;
  157. }
  158. $order_list = $model_order->getOrderList($condition, '', '*', '', 100);
  159. if (empty($order_list)) break;
  160. foreach ($order_list as $order_info)
  161. {
  162. Log::record("1",Log::DEBUG);
  163. $result = $logic_order->changeOrderStateCancel($order_info,'system','系统','超期未支付系统自动关闭订单',true,false);
  164. Log::record("2",Log::DEBUG);
  165. if (!$result['state']) {
  166. $this->log('实物订单超期未支付关闭失败SN:'.$order_info['order_sn']); $_break = true; break;
  167. } else {
  168. Log::record("3",Log::DEBUG);
  169. account_helper::onPredeposit('order_cancel',$order_info['buyer_id'],$order_info['order_sn']);
  170. Log::record("4",Log::DEBUG);
  171. }
  172. }
  173. }
  174. //虚拟订单超期未支付系统自动关闭
  175. $_break = false;
  176. $model_vr_order = Model('vr_order');
  177. $logic_vr_order = Logic('vr_order');
  178. $condition = [];
  179. $condition['order_state'] = ORDER_STATE_NEW;
  180. $condition['add_time'] = ['lt',time() - VRORDER_AUTO_CANCEL_MINUTE * 60];
  181. //分批,每批处理100个订单,最多处理5W个订单
  182. for ($i = 0; $i < 500; $i++)
  183. {
  184. if ($_break) {
  185. break;
  186. }
  187. $order_list = $model_vr_order->getOrderList($condition, '', '*', '',100);
  188. if (empty($order_list)) break;
  189. foreach ($order_list as $order_info) {
  190. $result = $logic_vr_order->changeOrderStateCancel($order_info,'system','超期未支付系统自动关闭订单',false);
  191. }
  192. if (!$result['state']) {
  193. $this->log('虚拟订单超期未支付关闭失败SN:'.$order_info['order_sn']);
  194. $_break = true;
  195. }
  196. }
  197. }
  198. /**
  199. * 更新首页的商品价格信息
  200. */
  201. private function _web_index_update()
  202. {
  203. Model('web_config')->updateWebGoods();
  204. }
  205. /**
  206. * 发送邮件消息
  207. */
  208. private function _cron_mail_send()
  209. {
  210. //每次发送数量
  211. $_num = 50;
  212. $model_storemsgcron = Model('mail_cron');
  213. $cron_array = $model_storemsgcron->getMailCronList([], $_num);
  214. if (!empty($cron_array))
  215. {
  216. $email = new Email();
  217. $mail_array = [];
  218. foreach ($cron_array as $val)
  219. {
  220. $return = $email->send_sys_email($val['mail'],$val['subject'],$val['contnet']);
  221. if ($return) {
  222. // 记录需要删除的id
  223. $mail_array[] = $val['mail_id'];
  224. }
  225. }
  226. // 删除已发送的记录
  227. $model_storemsgcron->delMailCron(['mail_id' => ['in', $mail_array]]);
  228. }
  229. }
  230. /**
  231. * 执行通用任务
  232. */
  233. private function _cron_common()
  234. {
  235. Log::record(__FUNCTION__,Log::DEBUG);
  236. //查找待执行任务
  237. $model_cron = Model('cron');
  238. $cron = $model_cron->getCronList(['exetime'=> ['elt',time()]]);
  239. if (!is_array($cron)) return ;
  240. $cron_array = [];
  241. $cronid = [];
  242. $exeid = 1;
  243. foreach ($cron as $v)
  244. {
  245. $type = intval($v['type']);
  246. if($type == 8) {
  247. $cron_array[$v['type']][$exeid] = $v;
  248. $exeid++;
  249. }
  250. else {
  251. $cron_array[$v['type']][$v['exeid']] = $v;
  252. }
  253. }
  254. foreach ($cron_array as $k=>$v)
  255. {
  256. // 如果方法不存是,直接删除id
  257. if (!method_exists($this,'_cron_'.$k)) {
  258. $tmp = current($v);
  259. $cronid[] = $tmp['id'];
  260. continue;
  261. }
  262. $method = '_cron_'.$k;
  263. Log::record("crontab minutest err:{$method}",Log::DEBUG);
  264. $result = call_user_func_array([$this,'_cron_'.$k], [$v]);
  265. if (is_array($result)){
  266. $cronid = array_merge($cronid,$result);
  267. }
  268. else {
  269. $method = '_cron_'.$k;
  270. Log::record("crontab minutest err:{$method}",Log::ERR);
  271. }
  272. }
  273. //删除执行完成的cron信息
  274. if (!empty($cronid) && is_array($cronid)){
  275. $model_cron->delCron(['id'=> ['in',$cronid]]);
  276. }
  277. }
  278. //'任务类型 1商品上架 2根据商品id更新商品促销价格 3优惠套装过期 4推荐展位过期 5团购开始更新商品促销价格 6团购过期 7限时折扣过期',
  279. //1商品上架
  280. private function _cron_1($cron = array())
  281. {
  282. $condition = ['goods_commonid' => ['in',array_keys($cron)]];
  283. $update = Model('goods')->editProducesOnline($condition);
  284. if ($update){
  285. //返回执行成功的cronid
  286. $cronid = [];
  287. foreach ($cron as $v) {
  288. $cronid[] = $v['id'];
  289. }
  290. } else {
  291. return false;
  292. }
  293. return $cronid;
  294. }
  295. //2根据商品id更新商品促销价格
  296. private function _cron_2($cron = array())
  297. {
  298. $condition = ['goods_id' => ['in',array_keys($cron)]];
  299. $update = Model('goods')->editGoodsPromotionPrice($condition);
  300. if ($update){
  301. //返回执行成功的cronid
  302. $cronid = [];
  303. foreach ($cron as $v) {
  304. $cronid[] = $v['id'];
  305. }
  306. }else{
  307. return false;
  308. }
  309. return $cronid;
  310. }
  311. //3优惠套装过期
  312. private function _cron_3($cron = [])
  313. {
  314. $condition = ['store_id' => ['in', array_keys($cron)]];
  315. $update = Model('p_bundling')->editBundlingQuotaClose($condition);
  316. if ($update) {
  317. //返回执行成功的cronid
  318. $cronid = [];
  319. foreach ($cron as $v) {
  320. $cronid[] = $v['id'];
  321. }
  322. } else {
  323. return false;
  324. }
  325. return $cronid;
  326. }
  327. //4推荐展位过期
  328. private function _cron_4($cron = [])
  329. {
  330. $condition = array('store_id' => array('in', array_keys($cron)));
  331. $update = Model('p_booth')->editBoothClose($condition);
  332. if ($update) {
  333. //返回执行成功的cronid
  334. $cronid = array();
  335. foreach ($cron as $v) {
  336. $cronid[] = $v['id'];
  337. }
  338. } else {
  339. return false;
  340. }
  341. return $cronid;
  342. }
  343. //5团购开始更新商品促销价格
  344. private function _cron_5($cron = array())
  345. {
  346. $condition = [];
  347. $condition['goods_commonid'] = ['in', array_keys($cron)];
  348. $condition['start_time'] = ['lt', time()];
  349. $condition['end_time'] = ['gt', time()];
  350. $groupbuy = Model('groupbuy')->getGroupbuyList($condition);
  351. foreach ($groupbuy as $val) {
  352. Model('goods')->editGoods(['goods_promotion_price' => $val['groupbuy_price'], 'goods_promotion_type' => 1],
  353. ['goods_commonid' => $val['goods_commonid']]);
  354. }
  355. //返回执行成功的cronid
  356. $cronid = array();
  357. foreach ($cron as $v) {
  358. $cronid[] = $v['id'];
  359. }
  360. return $cronid;
  361. }
  362. /**
  363. * 抢购过期
  364. *
  365. * @param array $cron
  366. */
  367. private function _cron_6($cron = array())
  368. {
  369. $condition = ['goods_commonid' => ['in', array_keys($cron)]];
  370. //抢购活动过期
  371. $update = Model('groupbuy')->editExpireGroupbuy($condition);
  372. if ($update){
  373. //返回执行成功的cronid
  374. $cronid = [];
  375. foreach ($cron as $v) {
  376. $cronid[] = $v['id'];
  377. }
  378. }else{
  379. return false;
  380. }
  381. return $cronid;
  382. }
  383. /**
  384. * 限时折扣过期
  385. *
  386. * @param array $cron
  387. */
  388. private function _cron_7($cron = array())
  389. {
  390. $condition = array('xianshi_id' => array('in', array_keys($cron)));
  391. //限时折扣过期
  392. $update = Model('p_xianshi')->editExpireXianshi($condition);
  393. if ($update){
  394. //返回执行成功的cronid
  395. $cronid = array();
  396. foreach ($cron as $v) {
  397. $cronid[] = $v['id'];
  398. }
  399. }
  400. else{
  401. return false;
  402. }
  403. return $cronid;
  404. }
  405. private function _cron_8($cron = array())
  406. {
  407. $cronid = [];
  408. foreach ($cron as $v)
  409. {
  410. $cronid[] = intval($v['id']);
  411. $params = $v['params'];
  412. if(!empty($params))
  413. {
  414. $params = unserialize($params);
  415. if(is_array($params))
  416. {
  417. foreach ($params as $key => $value) {
  418. QueueClient::push($key,$value);
  419. }
  420. }
  421. }
  422. }
  423. return $cronid;
  424. }
  425. }