123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- <?php
- namespace task;
- require_once(BASE_HELPER_PATH . '/stat_helper.php');
- require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
- require_once(BASE_HELPER_PATH . '/statistics/refill_balance.php');
- use Exception;
- use message\pubevent;
- use mtopcard;
- use PHPExcel;
- use PHPExcel_IOFactory;
- use statistics\refill_balance;
- use statistics\stat_refill;
- use Log;
- class handler
- {
- private $mTtaskId = 0;
- public function set_task_id($task_id)
- {
- $this->mTtaskId = $task_id;
- }
- 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_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'];
- }
- $total_stage = ceil(($end - $start) / 3600);
- $cur_stage = 1;
- 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();
- $order_id = 0;
- while (true)
- {
- $cond['refill_order.order_id'] = ['gt', $order_id];
- $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("0,1000")
- ->select();
- if(empty($items)) {
- yield [$items,$total_stage,$cur_stage];
- break;
- }
- else {
- $last_item = end($items);
- $order_id = intval($last_item['order_id']);
- Log::record("handler order_id={$order_id}",Log::DEBUG);
- yield [$items,$total_stage,$cur_stage];
- }
- }
- $cur_stage++;
- }
- };
- $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($execl_index=0;$execl_index<count($column_values);$execl_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 => '中国电信'];
- $execl_writer = function ($order,$index) use ($card_type_texts, $column_keys,$objPHPExcel,$data_keys)
- {
- 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]]);
- }
- };
- $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;
- $reader = $order_reader($normal_cond,$time_scope);
- foreach ($reader as $result)
- {
- [$items,$total_stage,$cur_stage] = $result;
- foreach ($items as $order)
- {
- Log::record("handler write order index={$execl_index}",Log::DEBUG);
- $execl_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 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));
- }
- }
|