minutes.php 14 KB

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