minutes.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. require_once(BASE_HELPER_PATH . '/refill/util.php');
  12. require_once(BASE_HELPER_PATH . '/refill/divert_account.php');
  13. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  14. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  15. require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
  16. require_once(BASE_HELPER_PATH . '/task/task_helper.php');
  17. class minutesControl extends BaseCronControl
  18. {
  19. public function indexexOp()
  20. {
  21. $this->_cron_common([1,2,3,4,5,6,7]);
  22. }
  23. public function indexOp()
  24. {
  25. Log::record(__FUNCTION__ . " start",Log::DEBUG);
  26. //未付款订单超期自动关闭
  27. // $this->_order_timeout_cancel();
  28. $this->_cron_common([1,2,3,4,5,6,7]);
  29. $this->_fetch_order_unavaliable();
  30. $this->_check_merchant_alarm_amount();
  31. Log::record(__FUNCTION__ . " end",Log::DEBUG);
  32. // $this->_web_index_update();
  33. // $this->_check_merchant_alarm_amount();
  34. // $this->check_refill_order_limit();
  35. // $this->_cron_mail_send();
  36. // $this->_SendOrderQuery();
  37. }
  38. //任务队列处理
  39. public function admin_taskOp()
  40. {
  41. Log::short_name('task');
  42. Log::record('start task',Log::DEBUG);
  43. $this->exec_task(false);
  44. }
  45. public function update_channel_balanceOp()
  46. {
  47. $balance_getter = function ($provider_id)
  48. {
  49. $provider_model = Model('refill_provider');
  50. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id, 'opened' => 1]);
  51. if (empty($provider_info)) {
  52. return false;
  53. }
  54. $channel_name = $provider_info['name'];
  55. if ($provider_info['type'] == 1) {
  56. $type = 'RefillOil';
  57. } elseif ($provider_info['type'] == 2 || $provider_info['type'] == 3) {
  58. $type = 'RefillPhone';
  59. } else {
  60. $type = 'RefillPhone';
  61. }
  62. Log::record("query balance channel_name:{$channel_name}",Log::DEBUG);
  63. $provider = refill\util::getProvider($channel_name,$type);
  64. if($provider == false) return false;
  65. [$state, $balance] = $provider->balance();
  66. Log::record("query balance channel_name:{$channel_name}, err_msg:{$balance}",Log::DEBUG);
  67. if(!$state) {
  68. return false;
  69. } else {
  70. return round(floatval($balance),4);
  71. }
  72. };
  73. $providers = function ()
  74. {
  75. $pids = [];
  76. $i = 0;
  77. while (true)
  78. {
  79. $start = $i * 1000;
  80. $items = Model()->table('refill_provider')->where(['opened' => 1])->field('*')->order('provider_id desc')->limit("{$start},1000")->select();
  81. if(empty($items)) {
  82. return $pids;
  83. }
  84. $i++;
  85. foreach ($items as $item) {
  86. $pid = intval($item['provider_id']);
  87. $pids[] = $pid;
  88. }
  89. }
  90. };
  91. $mod_provider = Model('refill_provider');
  92. $pids = $providers();
  93. foreach ($pids as $pid)
  94. {
  95. try {
  96. $balance = $balance_getter($pid);
  97. if($balance !== false) {
  98. $mod_provider->editProvider(['balance' => $balance],['provider_id' => $pid]);
  99. }
  100. }
  101. catch (Exception $ex)
  102. {
  103. Log::record($ex->getMessage(),Log::ERR);
  104. }
  105. }
  106. }
  107. private function _fetch_order_unavaliable()
  108. {
  109. try {
  110. $mod_fetch_order = Model('fetch_order');
  111. $trans = new trans_wapper($mod_fetch_order,__METHOD__);
  112. $items = $mod_fetch_order->timeout_orders(900);
  113. foreach ($items as $item) {
  114. $fetch_id = $item['fetch_id'];
  115. $mod_fetch_order->unavaliable($fetch_id);
  116. }
  117. $trans->commit();
  118. foreach ($items as $item) {
  119. $order_sn = $item['order_sn'];
  120. $chname = $item['channel_name'];
  121. refill\util::push_notify($chname,['order_sn' => $order_sn,'state' => 'CANCEL']);
  122. }
  123. }
  124. catch (Exception $ex) {
  125. $trans->rollback();
  126. Log::record(__METHOD__ . " " . $ex->getMessage(),Log::ERR);
  127. }
  128. }
  129. private function _check_merchant_alarm_amount()
  130. {
  131. if(defined('COMPANY_NAME') && COMPANY_NAME === 'XYZ_COMPANY')
  132. {
  133. $mch_cache = rcache("merchant-notify" , 'refill-');
  134. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  135. $new_caches = [];
  136. $merchants = Model('merchant')->getMerchantList(['merchant.merchant_state'=>1],'','',true,'merchant.*,member.available_predeposit' ,"0,1000");
  137. foreach ($merchants as $merchant)
  138. {
  139. $mchid = $merchant['mchid'];
  140. $phones = empty($merchant['warning_phone']) ? [] : unserialize($merchant['warning_phone']);
  141. $available_pd = intval($merchant['available_predeposit']);
  142. $alarm_pd = intval($merchant['alarm_amount']);
  143. if(array_key_exists($mchid,$caches)) {
  144. $mch_cache = $caches[$mchid];
  145. }
  146. else {
  147. $mch_cache = ['last_time' => 0, 'send_count' => 0];
  148. }
  149. if($available_pd < $alarm_pd || $available_pd < 10000)
  150. {
  151. $counts = $mch_cache['send_count'];
  152. if(($mch_cache['last_time'] + 300 < time()) && $counts < 2) {
  153. $mch_cache = ['last_time' => time(), 'send_count' => $counts + 1];
  154. foreach ($phones as $phone) {
  155. if(!empty($phone)){
  156. QueueClient::push('sendSMS', ['mobile'=>$phone,
  157. 'type'=>'balance_warning','datas' => [date("m月d日H时") , $merchant['available_predeposit']]]);
  158. }
  159. }
  160. }
  161. }
  162. else {
  163. $mch_cache = ['last_time' => 0, 'send_count' => 0];
  164. }
  165. $new_caches[$mchid] = $mch_cache;
  166. }
  167. wcache("merchant-notify", ['data' => serialize($new_caches)], 'refill-');
  168. }
  169. }
  170. public function check_refill_order_limit()
  171. {
  172. $mch_cache = rcache("storge_limit" , 'merchant-');
  173. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  174. $reader = function ($mchid,$time)
  175. {
  176. $cond['mchid'] = $mchid;
  177. $cond['inner_status'] = 0;
  178. $cond['order_state'] = ORDER_STATE_SUCCESS;
  179. $cond['refill_order.order_time'] = ['egt', $time];
  180. $items = Model('')->table('refill_order,vr_order')->join('inner')
  181. ->on('refill_order.order_id=vr_order.order_id')
  182. ->field('refill_order.mchid,refill_order.card_type,refill_order.refill_amount,count(*) as num')
  183. ->group('refill_order.card_type,refill_order.refill_amount')
  184. ->where($cond)->select();
  185. return $items;
  186. };
  187. $merger = function ($limits,$reals,$mchid,$card_type)
  188. {
  189. $result = [];
  190. foreach ($limits as $amount => $limit)
  191. {
  192. $limit = intval($limit);
  193. if($limit === -1) {
  194. $allow = true;
  195. }
  196. elseif($limit === 0) {
  197. $allow = false;
  198. }
  199. else
  200. {
  201. $finded = false;
  202. foreach ($reals as $item)
  203. {
  204. $tmp = intval($item['refill_amount'] + 0.005);
  205. if($item['card_type'] == $card_type && $tmp == $amount) {
  206. $num = $item['num'];
  207. $allow = $limit > $num;
  208. $finded = true;
  209. break;
  210. }
  211. }
  212. if(!$finded) {
  213. $allow = true;
  214. }
  215. }
  216. $result["{$mchid}-{$card_type}-{$amount}"] = $allow;
  217. }
  218. return $result;
  219. };
  220. $type_map = ['petrochina' => 1,'sinopec' => 2];
  221. $abilitys = [];
  222. foreach ($caches as $cache)
  223. {
  224. $mchid = intval($cache['mchid']);
  225. $start = $cache['time'];
  226. $petros = $cache['petrochina'];
  227. $sinos = $cache['sinopec'];
  228. $items = $reader($mchid,$start);
  229. $prets = $merger($petros,$items,$mchid,$type_map['petrochina']);
  230. $srets = $merger($sinos,$items,$mchid,$type_map['sinopec']);
  231. foreach ($prets as $key => $val) {
  232. $abilitys[$key] = $val;
  233. }
  234. foreach ($srets as $key => $val) {
  235. $abilitys[$key] = $val;
  236. }
  237. }
  238. $old = rcache("refill_able",'merchant-');
  239. $old = empty($old) ? "" : $old['data'];
  240. ksort($abilitys);
  241. $new = serialize($abilitys);
  242. if($new != $old) {
  243. wcache("refill_able" , ['data' => $new] , 'merchant-');
  244. // $publisher = new message\publisher();
  245. // $publisher->modify_refill_merchant();
  246. }
  247. }
  248. /**
  249. * 未付款订单超期自动关闭
  250. */
  251. private function _order_timeout_cancel()
  252. {
  253. Log::record(__FUNCTION__,Log::DEBUG);
  254. //实物订单超期未支付系统自动关闭
  255. $_break = false;
  256. $model_order = Model('order');
  257. $logic_order = Logic('order');
  258. $condition = [];
  259. $condition['order_state'] = ORDER_STATE_NEW;
  260. $condition['add_time'] = ['lt',time() - ORDER_AUTO_CANCEL_DAY * 86400];
  261. //分批,每批处理100个订单,最多处理5W个订单
  262. for ($i = 0; $i < 500; $i++)
  263. {
  264. if ($_break) {
  265. break;
  266. }
  267. $order_list = $model_order->getOrderList($condition, '', '*', '', 100);
  268. if (empty($order_list)) break;
  269. foreach ($order_list as $order_info)
  270. {
  271. Log::record("1",Log::DEBUG);
  272. $result = $logic_order->changeOrderStateCancel($order_info,'system','系统','超期未支付系统自动关闭订单',true,false);
  273. Log::record("2",Log::DEBUG);
  274. if (!$result['state']) {
  275. $this->log('实物订单超期未支付关闭失败SN:'.$order_info['order_sn']); $_break = true; break;
  276. } else {
  277. Log::record("3",Log::DEBUG);
  278. account_helper::onPredeposit('order_cancel',$order_info['buyer_id'],$order_info['order_sn']);
  279. Log::record("4",Log::DEBUG);
  280. }
  281. }
  282. }
  283. //虚拟订单超期未支付系统自动关闭
  284. $_break = false;
  285. $model_vr_order = Model('vr_order');
  286. $logic_vr_order = Logic('vr_order');
  287. $condition = [];
  288. $condition['order_state'] = ORDER_STATE_NEW;
  289. $condition['add_time'] = ['lt',time() - VRORDER_AUTO_CANCEL_MINUTE * 60];
  290. //分批,每批处理100个订单,最多处理5W个订单
  291. for ($i = 0; $i < 500; $i++)
  292. {
  293. if ($_break) {
  294. break;
  295. }
  296. $order_list = $model_vr_order->getOrderList($condition, '', '*', '',100);
  297. if (empty($order_list)) break;
  298. foreach ($order_list as $order_info) {
  299. $result = $logic_vr_order->changeOrderStateCancel($order_info,'system','超期未支付系统自动关闭订单',false);
  300. }
  301. if (!$result['state']) {
  302. $this->log('虚拟订单超期未支付关闭失败SN:'.$order_info['order_sn']);
  303. $_break = true;
  304. }
  305. }
  306. }
  307. /**
  308. * 更新首页的商品价格信息
  309. */
  310. private function _web_index_update()
  311. {
  312. Model('web_config')->updateWebGoods();
  313. }
  314. /**
  315. * 发送邮件消息
  316. */
  317. private function _cron_mail_send()
  318. {
  319. //每次发送数量
  320. $_num = 50;
  321. $model_storemsgcron = Model('mail_cron');
  322. $cron_array = $model_storemsgcron->getMailCronList([], $_num);
  323. if (!empty($cron_array))
  324. {
  325. $email = new Email();
  326. $mail_array = [];
  327. foreach ($cron_array as $val)
  328. {
  329. $return = $email->send_sys_email($val['mail'],$val['subject'],$val['contnet']);
  330. if ($return) {
  331. // 记录需要删除的id
  332. $mail_array[] = $val['mail_id'];
  333. }
  334. }
  335. // 删除已发送的记录
  336. $model_storemsgcron->delMailCron(['mail_id' => ['in', $mail_array]]);
  337. }
  338. }
  339. public function second_queueOp()
  340. {
  341. while (true)
  342. {
  343. do{
  344. try {
  345. $ret = $this->_cron_common([8]);
  346. perfor_clear();
  347. } catch (Exception $ex) {
  348. Log::record($ex->getMessage(),Log::ERR);
  349. }
  350. }
  351. while($ret === true);
  352. sleep(1);
  353. }
  354. }
  355. public function account_editOp()
  356. {
  357. $stamp = 0;
  358. while (true)
  359. {
  360. try {
  361. $divertor = new refill\divert_account();
  362. $next = time();
  363. $divertor->update_account($stamp);
  364. $stamp = $next;
  365. } catch (Exception $ex) {
  366. Log::record($ex->getMessage(),Log::ERR);
  367. }
  368. sleep(1);
  369. }
  370. }
  371. private function exec_task($merchant)
  372. {
  373. $task_manager = new task\manager();
  374. $task_id = 0;
  375. while (true)
  376. {
  377. try
  378. {
  379. $task = $task_manager->pop_task($task_id,$merchant);
  380. if (!empty($task)) {
  381. $task_manager->handle($task);
  382. $task_id = $task['task_id'];
  383. }
  384. }
  385. catch (Exception $ex) {
  386. Log::record($ex->getMessage(),Log::ERR);
  387. }
  388. sleep(1);
  389. }
  390. }
  391. //任务队列处理
  392. public function merchant_taskOp()
  393. {
  394. Log::short_name('merchant_task');
  395. Log::record('start task',Log::DEBUG);
  396. $this->exec_task(true);
  397. }
  398. //更新统计业务数据
  399. public function stat_utilOp()
  400. {
  401. Log::short_name('stat_util');
  402. while (true)
  403. {
  404. try {
  405. $this->_update_earlist_send();
  406. $this->_calc_arrears();
  407. } catch (Exception $ex) {
  408. Log::record($ex->getMessage(), Log::ERR);
  409. }
  410. sleep(1);
  411. }
  412. }
  413. private function _update_earlist_send()
  414. {
  415. //查找最早的充值中的订单时间
  416. // $update_earliest_ordertime = function () {
  417. // $mod = Model('refill_detail');
  418. // $time = $mod->getEarliestSendTime();
  419. // wcache('stat-info', ['earliest_order_time' => $time], 'refill-');
  420. // };
  421. // $update_earliest_ordertime();
  422. //按各个机构,更新最早的充值中的订单时间
  423. $update_earliest_ordertime_bymerchant = function () {
  424. $mod = Model('refill_detail');
  425. $mtimes = $mod->getEarliestSendTimeByMerchant();
  426. $val = json_encode($mtimes);
  427. wkcachex('stat-earliest-ordertime', $val, 'refill-');
  428. };
  429. $update_earliest_ordertime_bymerchant();
  430. }
  431. private function _calc_arrears()
  432. {
  433. $send_amount_reader = function()
  434. {
  435. $val = rkcachex('stat-merchant-sendamount', 'refill-');
  436. $val = json_decode($val,true);
  437. return $val;
  438. };
  439. $debts_detail_fun = function ($amounts)
  440. {
  441. $debts_detail = [];
  442. $mod = Model();
  443. $i = 0;
  444. while (true)
  445. {
  446. $start = $i * 1000;
  447. $items = $mod->table('merchant,member')->join('inner')->on('member.member_id=merchant.admin_id')
  448. ->field('mchid,credit_bonus,co_id,available_predeposit')->where(['merchant_state' => 1, 'co_id' => ['gt', 0]])->order('mchid asc')->limit("{$start},1000")->select();
  449. if(empty($items)) {
  450. break;
  451. }
  452. $i++;
  453. foreach ($items as $item)
  454. {
  455. $mchid = intval($item['mchid']);
  456. $co_id = intval($item['co_id']);
  457. $credit_bonus = round(floatval($item['credit_bonus']), 2);
  458. $available_predeposit = round(floatval($item['available_predeposit']), 2);
  459. if (array_key_exists($mchid, $amounts)) {
  460. $send_amounts = $amounts[$mchid]['send_amounts'];
  461. $lack_amounts = $amounts[$mchid]['lack_amounts'];
  462. } else {
  463. $send_amounts = 0;
  464. $lack_amounts = 0;
  465. }
  466. $available = $available_predeposit - $credit_bonus + $send_amounts - $lack_amounts;
  467. $debt = -$available;
  468. $debts_detail[$co_id][$mchid] = $debt;
  469. $val = ['debt' => $debt, 'send_amounts' => $send_amounts, 'lack_amounts' => $lack_amounts];
  470. wcache('merchant-debts-detail', [$mchid => serialize($val)], 'refill-');
  471. }
  472. }
  473. return $debts_detail;
  474. };
  475. $company_debt_fun = function ()
  476. {
  477. $co_debts = [];
  478. $mod = Model('refill_company');
  479. $i = 0;
  480. while (true)
  481. {
  482. $start = $i * 1000;
  483. $items = $mod->field('co_id,max_debt')->where(['opened' => 1, 'co_type' => 'merchant'])->order('co_id asc')->limit("{$start},1000")->select();
  484. if(empty($items)) {
  485. break;
  486. }
  487. $i++;
  488. foreach ($items as $item)
  489. {
  490. $co_id = intval($item['co_id']);
  491. $max_debt = round(floatval($item['max_debt']), 2);
  492. $co_debts[$co_id] = $max_debt;
  493. }
  494. }
  495. return $co_debts;
  496. };
  497. $calc_debts = function ($co_id,$detail)
  498. {
  499. if(!array_key_exists($co_id,$detail)) {
  500. return [[], 0];
  501. }
  502. $mchids=[];
  503. $sum_debts = 0;
  504. $co_details = $detail[$co_id];
  505. foreach ($co_details as $mchid => $debts) {
  506. $mchids[] = $mchid;
  507. $sum_debts += $debts;
  508. }
  509. return [$mchids,$sum_debts];
  510. };
  511. $judge_stop = function ($co_debts, $debts_detail) use ($calc_debts)
  512. {
  513. foreach ($co_debts as $co_id => $max_debts)
  514. {
  515. [$mchids, $sum_debts] = $calc_debts($co_id,$debts_detail);
  516. wcache('company-debts', [$co_id => $sum_debts], 'refill-');
  517. if(empty($mchids)) continue;
  518. if($max_debts < $sum_debts) {
  519. $fStop = 1;
  520. } else {
  521. $fStop = 0;
  522. }
  523. foreach ($mchids as $mchid) {
  524. wcache('merchant-debt-judge', [$mchid => $fStop], 'refill-');
  525. }
  526. }
  527. };
  528. $need_calc = function ($clac_time)
  529. {
  530. static $latest_time = 0;
  531. if($clac_time > $latest_time) {
  532. $latest_time = $clac_time;
  533. return true;
  534. } else {
  535. return false;
  536. }
  537. };
  538. $result = $send_amount_reader();
  539. if (!empty($result) && $need_calc(intval($result['time']))) {
  540. $mch_amounts = $result['send_amounts'];
  541. $debts_detail = $debts_detail_fun($mch_amounts);
  542. $co_debts = $company_debt_fun();
  543. $judge_stop($co_debts, $debts_detail);
  544. }
  545. }
  546. /**
  547. * 执行通用任务
  548. */
  549. private function _cron_common($types)
  550. {
  551. Log::record(__FUNCTION__,Log::DEBUG);
  552. //查找待执行任务
  553. $model_cron = Model('cron');
  554. $cron = $model_cron->getCronList(['exetime'=> ['elt',time()],'type' => ['in',$types]]);
  555. if (!is_array($cron)) return false;
  556. $count = count($cron);
  557. if ($count <= 0) return false;
  558. Log::record("match cron count={$count}",Log::DEBUG);
  559. $cron_array = [];
  560. $cronid = [];
  561. $exeid = 1;
  562. foreach ($cron as $v)
  563. {
  564. $type = intval($v['type']);
  565. if($type == 8) {
  566. $cron_array[$v['type']][$exeid] = $v;
  567. $exeid++;
  568. }
  569. else {
  570. $cron_array[$v['type']][$v['exeid']] = $v;
  571. }
  572. }
  573. foreach ($cron_array as $k=>$v)
  574. {
  575. // 如果方法不存是,直接删除id
  576. if (!method_exists($this,'_cron_'.$k)) {
  577. $tmp = current($v);
  578. $cronid[] = $tmp['id'];
  579. continue;
  580. }
  581. $method = '_cron_'.$k;
  582. Log::record("crontab minutest:{$method}",Log::DEBUG);
  583. $result = call_user_func_array([$this, '_cron_' . $k], [$v]);
  584. if (is_array($result)) {
  585. $cronid = array_merge($cronid, $result);
  586. } else {
  587. $method = '_cron_' . $k;
  588. Log::record("crontab minutest err:{$method}", Log::ERR);
  589. }
  590. }
  591. //删除执行完成的cron信息
  592. if (!empty($cronid) && is_array($cronid)) {
  593. $model_cron->delCron(['id' => ['in', $cronid]]);
  594. }
  595. return true;
  596. }
  597. //'任务类型 1商品上架 2根据商品id更新商品促销价格 3优惠套装过期 4推荐展位过期 5团购开始更新商品促销价格 6团购过期 7限时折扣过期',
  598. //1商品上架
  599. private function _cron_1($cron = array())
  600. {
  601. $condition = ['goods_commonid' => ['in',array_keys($cron)]];
  602. $update = Model('goods')->editProducesOnline($condition);
  603. if ($update)
  604. {
  605. //返回执行成功的cronid
  606. $cronid = [];
  607. foreach ($cron as $v) {
  608. $cronid[] = $v['id'];
  609. }
  610. }
  611. else {
  612. return false;
  613. }
  614. return $cronid;
  615. }
  616. //2根据商品id更新商品促销价格
  617. private function _cron_2($cron = array())
  618. {
  619. $condition = ['goods_id' => ['in',array_keys($cron)]];
  620. $update = Model('goods')->editGoodsPromotionPrice($condition);
  621. if ($update){
  622. //返回执行成功的cronid
  623. $cronid = [];
  624. foreach ($cron as $v) {
  625. $cronid[] = $v['id'];
  626. }
  627. }else{
  628. return false;
  629. }
  630. return $cronid;
  631. }
  632. //3优惠套装过期
  633. private function _cron_3($cron = [])
  634. {
  635. $condition = ['store_id' => ['in', array_keys($cron)]];
  636. $update = Model('p_bundling')->editBundlingQuotaClose($condition);
  637. if ($update) {
  638. //返回执行成功的cronid
  639. $cronid = [];
  640. foreach ($cron as $v) {
  641. $cronid[] = $v['id'];
  642. }
  643. } else {
  644. return false;
  645. }
  646. return $cronid;
  647. }
  648. //4推荐展位过期
  649. private function _cron_4($cron = [])
  650. {
  651. $condition = array('store_id' => array('in', array_keys($cron)));
  652. $update = Model('p_booth')->editBoothClose($condition);
  653. if ($update) {
  654. //返回执行成功的cronid
  655. $cronid = array();
  656. foreach ($cron as $v) {
  657. $cronid[] = $v['id'];
  658. }
  659. } else {
  660. return false;
  661. }
  662. return $cronid;
  663. }
  664. //5团购开始更新商品促销价格
  665. private function _cron_5($cron = array())
  666. {
  667. $condition = [];
  668. $condition['goods_commonid'] = ['in', array_keys($cron)];
  669. $condition['start_time'] = ['lt', time()];
  670. $condition['end_time'] = ['gt', time()];
  671. $groupbuy = Model('groupbuy')->getGroupbuyList($condition);
  672. foreach ($groupbuy as $val) {
  673. Model('goods')->editGoods(['goods_promotion_price' => $val['groupbuy_price'], 'goods_promotion_type' => 1],
  674. ['goods_commonid' => $val['goods_commonid']]);
  675. }
  676. //返回执行成功的cronid
  677. $cronid = array();
  678. foreach ($cron as $v) {
  679. $cronid[] = $v['id'];
  680. }
  681. return $cronid;
  682. }
  683. /**
  684. * 抢购过期
  685. *
  686. * @param array $cron
  687. */
  688. private function _cron_6($cron = array())
  689. {
  690. $condition = ['goods_commonid' => ['in', array_keys($cron)]];
  691. //抢购活动过期
  692. $update = Model('groupbuy')->editExpireGroupbuy($condition);
  693. if ($update){
  694. //返回执行成功的cronid
  695. $cronid = [];
  696. foreach ($cron as $v) {
  697. $cronid[] = $v['id'];
  698. }
  699. }else{
  700. return false;
  701. }
  702. return $cronid;
  703. }
  704. /**
  705. * 限时折扣过期
  706. *
  707. * @param array $cron
  708. */
  709. private function _cron_7($cron = array())
  710. {
  711. $condition = array('xianshi_id' => array('in', array_keys($cron)));
  712. //限时折扣过期
  713. $update = Model('p_xianshi')->editExpireXianshi($condition);
  714. if ($update){
  715. //返回执行成功的cronid
  716. $cronid = array();
  717. foreach ($cron as $v) {
  718. $cronid[] = $v['id'];
  719. }
  720. }
  721. else{
  722. return false;
  723. }
  724. return $cronid;
  725. }
  726. private function _cron_8($cron = array())
  727. {
  728. $cronid = [];
  729. foreach ($cron as $v)
  730. {
  731. $cronid[] = intval($v['id']);
  732. $params = $v['params'];
  733. if(!empty($params))
  734. {
  735. $params = unserialize($params);
  736. if(is_array($params))
  737. {
  738. foreach ($params as $key => $value) {
  739. Log::record("delay queue: {$key}",Log::DEBUG);
  740. QueueClient::push($key,$value);
  741. }
  742. }
  743. }
  744. }
  745. return $cronid;
  746. }
  747. //查询超过5分钟的充值中订单
  748. public function _SendOrderQuery()
  749. {
  750. $model_refill_order = Model('refill_order');
  751. $condition['order_state'] = ORDER_STATE_SEND;
  752. $condition['refill_order.order_time'] = ['lt', (time() - 300)];
  753. $orders = $model_refill_order->getMerchantOrderList($condition, 1000, 0,'refill_order.order_id');
  754. if(!empty($orders)) {
  755. foreach ($orders as $order) {
  756. $order_id = $order['order_id'];
  757. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  758. }
  759. }
  760. }
  761. //
  762. public function _refill_task(): bool
  763. {
  764. Log::record(__FUNCTION__,Log::DEBUG);
  765. $model_refill_task = Model('refill_task');
  766. $task = $model_refill_task->UnDispose('order_export');
  767. if (empty($task)) return false;
  768. $task_id = $task['task_id'];
  769. $task_condition = unserialize($task['task_condition']);
  770. if (empty($task_condition)) {
  771. Log::record("refill task condition unserialize err, task_id:{$task_id}");
  772. return false;
  773. }
  774. $type = $task['type'];
  775. $cid = $task['cid'];
  776. if ($type === 'merchant') {
  777. $condition['refill_order.mchid'] = $cid;
  778. } elseif ($type === 'provider') {
  779. $condition['vr_order.store_id'] = $cid;
  780. } else {
  781. Log::record("refill task type err, task_id:{$task_id}");
  782. return false;
  783. }
  784. $order_time_type = $task_condition['order_time_type'];
  785. $start_unixtime = $task_condition['start_unixtime'];
  786. $end_unixtime = $task_condition['end_unixtime'];
  787. $order_state = $task_condition['order_state'];
  788. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  789. $condition["refill_order.{$order_time_type}"] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  790. $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
  791. } elseif ($start_unixtime > 0) {
  792. $condition["refill_order.{$order_time_type}"] = ['egt', $start_unixtime];
  793. $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
  794. }
  795. if (in_array($order_state, ['0', '10', '20', '30', '40'], true)) {
  796. $condition['order_state'] = $order_state;
  797. }
  798. $model_refill_task->Dispose($task_id);
  799. [$state, $task_result] = $this->order_export($condition, $type, $task_id);
  800. if ($state) {
  801. $model_refill_task->DisposeFinish($task_id, $task_result);
  802. } else {
  803. $model_refill_task->DisposeErr($task_id, $task_result);
  804. }
  805. return true;
  806. }
  807. /**
  808. * @throws PHPExcel_Exception
  809. */
  810. private function order_export($condition, $type, $task_id)
  811. {
  812. $orders = Model('refill_order')->getAllOrders($condition);
  813. if(empty($orders)) {
  814. return [false, '统计数据为空'];
  815. }
  816. $orders = $this->orderFormat($orders);
  817. $objPHPExcel = new PHPExcel();
  818. if ($type === 'merchant') {
  819. $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
  820. $column_values = ['商户号', '客户订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
  821. $data_keys = ['mchid', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'mch_amount'];
  822. } elseif ($type === 'provider') {
  823. $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
  824. $column_values = ['上游订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
  825. $data_keys = ['ch_trade_no', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'channel_amount'];
  826. } else {
  827. return [false, '主体类型错误'];
  828. }
  829. $objPHPExcel->setActiveSheetIndex(0);
  830. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  831. foreach ($column_keys as $key => $column_key) {
  832. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(25);
  833. $cell_value = $column_key . 1;
  834. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  835. }
  836. foreach ($orders as $k => $order) {
  837. foreach ($column_keys as $key => $column_key) {
  838. $field = $column_key.($k+2);
  839. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]],
  840. PHPExcel_Cell_DataType::TYPE_STRING);
  841. }
  842. }
  843. try {
  844. $path = BASE_ROOT_PATH . "/data/upload/task/";
  845. if(!is_dir($path)){
  846. mkdir($path, 0755);
  847. }
  848. $filename = date('YmdHis',time())."-任务导出-任务ID:{$task_id}.xlsx";
  849. $file_path = $path . $filename;
  850. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  851. $objWriter->save($file_path);
  852. return [true, $filename];
  853. } catch (Exception $e) {
  854. return [false, $e->getMessage()];
  855. }
  856. }
  857. private function orderFormat($orders): array
  858. {
  859. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  860. $orderState = function ($order_info) {
  861. $is_retrying = $order_info['is_retrying'];
  862. switch ($order_info['order_state']) {
  863. case ORDER_STATE_CANCEL:
  864. if ($is_retrying) {
  865. $order_state = '重试中';
  866. } else {
  867. $order_state = '已取消';
  868. }
  869. break;
  870. case ORDER_STATE_NEW:
  871. $order_state = '待付款';
  872. break;
  873. case ORDER_STATE_PAY:
  874. $order_state = '待发货';
  875. break;
  876. case ORDER_STATE_SEND:
  877. $order_state = '待收货';
  878. break;
  879. case ORDER_STATE_SUCCESS:
  880. $order_state = '交易完成';
  881. break;
  882. default :
  883. $order_state = '未知状态';
  884. }
  885. return $order_state;
  886. };
  887. foreach($orders as $key => $order)
  888. {
  889. $orders[$key]['card_type_text'] = $card_type_texts[$order['card_type']];
  890. $orders[$key]['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  891. $orders[$key]['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  892. $orders[$key]['order_state_text'] = $orderState($order);
  893. }
  894. return $orders;
  895. }
  896. }