|
@@ -457,4 +457,172 @@ class handler
|
|
|
{
|
|
|
return md5("refill_balance_stat_all-".serialize($condition));
|
|
|
}
|
|
|
+
|
|
|
+ public function order_search_export($cond)
|
|
|
+ {
|
|
|
+ $mod = Model('');
|
|
|
+
|
|
|
+ $order_reader = function ($cond, $total_stage, $stage_limit) use($mod)
|
|
|
+ {
|
|
|
+ for ($cur_stage = 1; $cur_stage <= $total_stage; $cur_stage ++)
|
|
|
+ {
|
|
|
+ $skip = ($cur_stage - 1) * $stage_limit;
|
|
|
+
|
|
|
+ $items = $mod->table('refill_order,vr_order')
|
|
|
+ ->field('refill_order.*,vr_order.order_state')
|
|
|
+ ->join('inner')
|
|
|
+ ->on('refill_order.order_id=vr_order.order_id')
|
|
|
+ ->where($cond)
|
|
|
+ ->order('refill_order.channel_name DESC')
|
|
|
+ ->limit("$skip,$stage_limit")
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ yield [$items,$total_stage,$cur_stage];
|
|
|
+ if(empty($items)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ $elapse_time = function($seconds)
|
|
|
+ {
|
|
|
+ $minutes = intval($seconds / 60);
|
|
|
+ $second = intval($seconds % 60);
|
|
|
+ if ($minutes >= 60) {
|
|
|
+ $minute = $minutes % 60;
|
|
|
+ $hours = intval($minutes / 60);
|
|
|
+ $result = "{$minute}m{$second}s";
|
|
|
+ } elseif ($minutes > 0) {
|
|
|
+ $result = "{$minutes}m{$second}s";
|
|
|
+ } else {
|
|
|
+ $result = "{$second}s";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($hours)) {
|
|
|
+ $result = "{$hours}h{$minute}m";
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ };
|
|
|
+
|
|
|
+ $column_values = ['机构名称','订单号', '充值卡号', '充值卡类型', '充值额度', '下单日期', '耗时', '订单状态', '商家单号', '渠道单号'];
|
|
|
+ $data_keys = ['mch_name', 'order_sn', 'card_no', 'card_type_text', 'refill_amount', 'order_time_text', 'diff_time_text', 'order_state_text', 'mch_order', 'ch_trade_no'];
|
|
|
+
|
|
|
+ $column_key = 'A';
|
|
|
+ for ($excel_index = 0; $excel_index < count($column_values); $excel_index++) {
|
|
|
+ $column_keys[] = $column_key;
|
|
|
+ $column_key++;
|
|
|
+ }
|
|
|
+
|
|
|
+ $objPHPExcel = new PHPExcel();
|
|
|
+ $objPHPExcel->setActiveSheetIndex(0);
|
|
|
+ $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
|
|
|
+ foreach ($column_keys as $key => $column_key) {
|
|
|
+ $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(15);
|
|
|
+ $cell_value = $column_key . 1;
|
|
|
+ $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信', mtopcard\ThirdRefillCard => '增值业务'];
|
|
|
+
|
|
|
+ $merchant_list = Model('')->table('merchant')->limit(1000)->select();
|
|
|
+ foreach ($merchant_list as $value) {
|
|
|
+ $merchants[$value['mchid']] = $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ $excel_writer = function ($order, $index) use ($card_type_texts, $column_keys, $objPHPExcel, $data_keys, $elapse_time, $merchants)
|
|
|
+ {
|
|
|
+ $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
|
|
|
+ $order['card_type_text'] = $card_type_texts[$order['card_type']];
|
|
|
+ $order['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
|
|
|
+
|
|
|
+ if ($order['notify_time'] > 0) {
|
|
|
+ $diff_time = $order['notify_time'] - $order['order_time'];
|
|
|
+ } else {
|
|
|
+ $cur_time = time();
|
|
|
+ $diff_time = $cur_time - $order['order_time'];
|
|
|
+ }
|
|
|
+ $order['diff_time_text'] = $elapse_time($diff_time);
|
|
|
+ $order['order_state_text'] = orderState($order);
|
|
|
+
|
|
|
+ foreach ($column_keys as $key => $column_key) {
|
|
|
+ $field = $column_key . ($index + 2);
|
|
|
+ $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ $start_time = time();
|
|
|
+ $percentor = function ($total_stage, $cur_stage) use ($start_time)
|
|
|
+ {
|
|
|
+ $task_id = $this->mTtaskId;
|
|
|
+ if($total_stage > $cur_stage) {
|
|
|
+ $total_used = time() - $start_time;
|
|
|
+ $remain = $total_used * ($total_stage - $cur_stage) / $cur_stage;
|
|
|
+ $remain = intval($remain);
|
|
|
+
|
|
|
+ $expected_time = date("H:i:s", (time() + $remain));
|
|
|
+ $stage = "导出进度:{$cur_stage}/{$total_stage}, 预计在{$expected_time}完成";
|
|
|
+ } else {
|
|
|
+ $stage = "已经完成";
|
|
|
+ }
|
|
|
+
|
|
|
+ $mod_task = Model('task');
|
|
|
+ $mod_task->where(['task_id' => $task_id])->update(['stage' => $stage]);
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ $stage = 0;
|
|
|
+ $execl_index = 0;
|
|
|
+ $total = $mod->table('refill_order,vr_order')
|
|
|
+ ->field('*')
|
|
|
+ ->where($cond)->join('inner')->on('refill_order.order_id=vr_order.order_id')
|
|
|
+ ->master(false)
|
|
|
+ ->count();
|
|
|
+
|
|
|
+ $stage_limit = 1000;
|
|
|
+ $total_stage = ceil($total / $stage_limit);
|
|
|
+ $reader = $order_reader($cond, $total_stage, $stage_limit);
|
|
|
+ foreach ($reader as $result)
|
|
|
+ {
|
|
|
+ [$items,$total_stage,$cur_stage] = $result;
|
|
|
+ foreach ($items as $order)
|
|
|
+ {
|
|
|
+ Log::record("handler write order index={$execl_index}",Log::DEBUG);
|
|
|
+
|
|
|
+ $excel_writer($order,$execl_index);
|
|
|
+ $execl_index += 1;
|
|
|
+ }
|
|
|
+ Log::record("handler total_stage={$total_stage} cur_stage={$cur_stage}",Log::DEBUG);
|
|
|
+ if($stage == 0) {
|
|
|
+ $stage = $cur_stage;
|
|
|
+ }
|
|
|
+ elseif($stage != $cur_stage) {
|
|
|
+ $percentor($total_stage,$stage);
|
|
|
+ $stage = $cur_stage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $path = BASE_ROOT_PATH . "/data/upload/task/";
|
|
|
+ if (!is_dir($path)) {
|
|
|
+ mkdir($path, 0755);
|
|
|
+ }
|
|
|
+ $filename = date('YmdHis', time()) . "订单导出.xlsx";
|
|
|
+
|
|
|
+ $file_path = "{$path}{$filename}";
|
|
|
+ $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
|
|
+ $objWriter->save($file_path);
|
|
|
+
|
|
|
+ $percentor($total_stage,$total_stage);
|
|
|
+
|
|
|
+ return [true, $filename];
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Log::record("handler {$e->getMessage()}",Log::ERR);
|
|
|
+ return [false, false];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function order_search_export_title($condition)
|
|
|
+ {
|
|
|
+ return md5("refill_order_export-".serialize($condition));
|
|
|
+ }
|
|
|
}
|