123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace task;
- use Exception;
- use mtopcard;
- use PHPExcel;
- use PHPExcel_IOFactory;
- class handler
- {
- public function refill_order_stat($condition)
- {
- try
- {
- $items = Model('')->table('refill_order,vr_order')
- ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
- ->join('inner')
- ->on('refill_order.order_id=vr_order.order_id')
- ->where($condition)
- ->group('order_state')
- ->select();
- $all = [];
- $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
- $sending = $success = $cancel = $data;
- foreach ($items as $item)
- {
- if ($item['order_state'] == ORDER_STATE_SEND) {
- $sending = $item;
- } elseif ($item['order_state'] == ORDER_STATE_SUCCESS) {
- $success = $item;
- } elseif ($item['order_state'] == ORDER_STATE_CANCEL) {
- $cancel = $item;
- }
- $all['order_count'] += $item['order_count'];
- $all['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
- $all['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
- $all['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
- }
- $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
- return [true,$result];
- }
- catch (Exception $ex)
- {
- return [false,false];
- }
- }
- public function refill_order_stat_title($condition)
- {
- return md5("refill_order_stat-".serialize($condition));
- }
- public function refill_order_export($condition)
- {
- $orders = Model('refill_order')->getAllOrders($condition);
- if (empty($orders)) {
- return [false, '统计数据为空'];
- }
- $merchants = [];
- $column_values = ['商户号', '商户名称', '商户订单号','平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额(下游)', '上游名称','上游订单号', '折扣金额(上游)'];
- $data_keys = ['mchid', 'mch_name', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'mch_amount', 'channel_name', 'ch_trade_no', 'channel_amount'];
- $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
- foreach ($merchant_list as $value) {
- $merchants[$value['mchid']] = $value;
- }
- $column_key = 'A';
- for($index=0;$index<count($column_values);$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 => '中国电信'];
- foreach ($orders as $k => $order) {
- if(!empty($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']) : '';
- $order['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
- $order['order_state_text'] = orderState($order);
- foreach ($column_keys as $key => $column_key) {
- $field = $column_key . ($k + 2);
- $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
- }
- }
- 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);
- return [true, $filename];
- } catch (Exception $e) {
- return [false, false];
- }
- }
- public function refill_order_export_title($condition)
- {
- return md5("refill_order_export-".serialize($condition));
- }
- }
|