minutes.php 14 KB

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