minutes.php 28 KB

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