minutes.php 28 KB

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