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_ex($cond) { $tmcond_gen = function ($cur_start,$cur_end) { $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and']; $end = $cur_end + 86400*2; $cond['vr_order.add_time'] = [['egt', $cur_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) { [$start, $end] = $time_scope['order_time']; for ($cur_start = $start; $cur_start < $end; $cur_start += 86400) { if ($cur_start + 86400 >= $end) { $cur_end = $end; } else { $cur_end = $cur_start + 86400; } $tmcond = $tmcond_gen($cur_start, $cur_end); $cond = array_merge($normal_cond, $tmcond); $mod = Model(); $items = $mod->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($cond) ->group('order_state') ->select(); if(empty($items)) continue; yield $items; } }; $summer = function ($items,&$result) { foreach ($items as $item) { $order_state = $item['order_state']; $result[$order_state]['order_count'] += $item['order_count']; $result[$order_state]['refill_amounts'] += ncPriceFormat($item['refill_amounts']); $result[$order_state]['channel_amounts'] += ncPriceFormat($item['channel_amounts']); $result[$order_state]['mch_amounts'] += ncPriceFormat($item['mch_amounts']); } }; $initor = function (&$records) { $order_states = [ORDER_STATE_CANCEL, ORDER_STATE_SEND, ORDER_STATE_SUCCESS]; foreach ($order_states as $state) { $val = ['order_count' => 0, 'refill_amounts' => 0, 'channel_amounts' => 0, 'mch_amounts' => 0]; $records[$state] = $val; } }; $all_summer = function ($records) { $result = ['order_count' => 0, 'refill_amounts' => 0, 'channel_amounts' => 0, 'mch_amounts' => 0]; foreach ($records as $item) { $result['order_count'] += $item['order_count']; $result['refill_amounts'] += ncPriceFormat($item['refill_amounts']); $result['channel_amounts'] += ncPriceFormat($item['channel_amounts']); $result['mch_amounts'] += ncPriceFormat($item['mch_amounts']); } return $result; }; try { $records = []; $initor($records); $stats = $order_reader($normal_cond, $time_scope); foreach ($stats as $items) { $summer($items,$records); } $all = $all_summer($records); $result = ['all' => $all, 'sending' => $records[ORDER_STATE_SEND], 'success' => $records[ORDER_STATE_SUCCESS], 'cancel' => $records[ORDER_STATE_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_stat_ex_title($condition) { return md5("refill_order_stat_ex-".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 = $cur_start - 86400*2; $end = $cur_end + 86400*2; $cond['refill_order.order_time'] = [['egt', $start], ['lt', $cur_end], 'and']; $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']; $end = $cur_end + 86400*2; $cond['vr_order.add_time'] = [['egt', $cur_start], ['elt', $end], 'and']; } return $cond; }; $normal_cond = $cond['normal']; $time_scope = $cond['time_scope']; $export_type = $cond['export_type']; $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,vr_order.store_name') ->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 = ['order_sn', 'refill_amount', 'card_no', 'card_type_text', 'is_transfer_text', 'order_time_text', 'notify_time_text','order_state_text', 'official_sn']; if($export_type === 'merchant') { $column_values = array_merge(['商户号', '商户订单号'], $column_values, ['扣款金额']); $data_keys = array_merge(['mchid', 'mch_order'], $data_keys, ['mch_amount']); }elseif ($export_type === 'provider') { $column_values = array_merge(['上游名称', '上游订单号'], $column_values, ['折扣金额']); $data_keys = array_merge(['store_name', 'ch_trade_no'], $data_keys, ['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;$indexsetActiveSheetIndex(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); } elseif ($type == 'system') { $refill->system_stat($time_stamp, $order_time_type, 'reload'); } return [true, 'success']; } public function order_stat_reload_title($condition) { return md5("order_stat_reload-".serialize($condition)); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public function order_stat_reload_some($condes) { foreach ($condes as $cond) { $this->order_stat_reload($cond); } return [true, 'success']; } public function order_stat_reload_some_title($condition) { return md5("order_stat_reload-some".serialize($condition)); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public function refill_balance_create($condition) { $type = $condition['type']; $cid = $condition['cid']; $start = $condition['start']; $end = $condition['end']; $time_type = $condition['time_type']; $parent_balance = $condition['parent_id']; $refill_balance = new refill_balance(); $refill_balance->add_balance($type, $cid, $start, $end, $time_type, $parent_balance); return [true, 'success']; } public function refill_balance_create_title($condition) { return md5("refill_balance_create-".serialize($condition)); } public function refill_balance_rebuild($condition) { $balance_id = $condition['balance_id']; $refill_balance = new refill_balance(); $refill_balance->rebuild_balance($balance_id); return [true, 'success']; } public function refill_balance_rebuild_title($condition) { return md5("refill_balance_rebuild-".serialize($condition)); } public function refill_balance_stat_all($condition) { $end = $condition['end']; $refill_balance = new refill_balance(); $refill_balance->stat_all($end); return [true, 'success']; } public function refill_balance_stat_all_title($condition) { return md5("refill_balance_stat_all-".serialize($condition)); } }