123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace task;
- require_once(BASE_HELPER_PATH . '/stat_helper.php');
- require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
- use Exception;
- use mtopcard;
- use PHPExcel;
- use PHPExcel_IOFactory;
- use statistics\stat_refill;
- 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($cond)
- {
- $tmcond_gen = function ($time_type,$time_scope,$cur_start,$cur_end)
- {
- if($time_type == 'notify_time') {
- [$start, $end] = $time_scope['order_time'];
- $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
- [$start, $end] = $time_scope['add_time'];
- $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
- $cond['refill_order.notify_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
- } else {
- $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
- [$start, $end] = $time_scope['add_time'];
- $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
- }
- return $cond;
- };
- $normal_cond = $cond['normal'];
- $time_scope = $cond['time_scope'];
- $order_reader = function ($normal_cond,$time_scope) use ($tmcond_gen)
- {
- if(isset($time_scope['notify_time'])) {
- $time_type = 'notify_time';
- [$start,$end] = $time_scope['notify_time'];
- } else {
- $time_type = 'order_time';
- [$start,$end] = $time_scope['order_time'];
- }
- for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
- {
- if($cur_start + 3600 >= $end) {
- $cur_end = $end;
- } else {
- $cur_end = $cur_start + 3600;
- }
- $tmcond = $tmcond_gen($time_type,$time_scope,$cur_start,$cur_end);
- $cond = array_merge($normal_cond,$tmcond);
- $mod = Model();
- $len = 1000;
- $i = 0;
- while (true)
- {
- $start = $i * $len;
- $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.order_id asc')
- ->limit("{$start},{$len}")
- ->select();
- $i++;
- if(empty($items)) break;
- foreach ($items as $item) {
- yield $item;
- }
- }
- }
- };
- $merchants = [];
- $column_values = ['商户号', '商户订单号', '平台单号','面额', '充值卡号', '充值卡类型', '是否转网', '下单日期', '完成日期', '订单状态', '扣款金额'];
- $data_keys = ['mchid', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'is_transfer_text', 'order_time_text', 'notify_time_text','order_state_text', 'mch_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 => '中国电信'];
- $orders = $order_reader($normal_cond,$time_scope);
- $index = 0;
- foreach ($orders as $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);
- if($order['is_transfer'] == 1) {
- $order['is_transfer_text'] = '是';
- }else{
- $order['is_transfer_text'] = '否';
- }
- foreach ($column_keys as $key => $column_key) {
- $field = $column_key . ($index + 2);
- $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
- }
- $index += 1;
- }
- 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));
- }
- public function order_stat_reload($condition)
- {
- $refill = new stat_refill();
- $type = $condition['type'];
- $time_stamp = $condition['time_stamp'];
- $cid = $condition['cid'];
- $order_time_type = $condition['order_time_type'];
- if ($type == 'merchant') {
- $refill->merchant_stat($time_stamp, $cid, $order_time_type);
- } elseif ($type == 'provider') {
- $refill->provider_stat($time_stamp, $cid, $order_time_type);
- }
- return [true, 'success'];
- }
- public function order_stat_reload_title($condition)
- {
- return md5("order_stat_reload-".serialize($condition));
- }
- }
|